|
4 | 4 | "fmt"
|
5 | 5 | "sort"
|
6 | 6 | "strings"
|
| 7 | + |
| 8 | + kerrs "k8s.io/apimachinery/pkg/util/errors" |
7 | 9 | )
|
8 | 10 |
|
9 | 11 | // ovsFake implements a fake ovs.Interface for testing purposes
|
@@ -248,6 +250,52 @@ func (tx *ovsFakeTx) EndTransaction() error {
|
248 | 250 | return err
|
249 | 251 | }
|
250 | 252 |
|
| 253 | +func getFlowRule(bundleFlow string) (string, bool, error) { |
| 254 | + var parts []string |
| 255 | + isAdd := false |
| 256 | + addPrefix := "flow add" |
| 257 | + delPrefix := "flow delete" |
| 258 | + |
| 259 | + if strings.Contains(bundleFlow, addPrefix) { |
| 260 | + parts = strings.Split(bundleFlow, addPrefix) |
| 261 | + isAdd = true |
| 262 | + } else if strings.Contains(bundleFlow, delPrefix) { |
| 263 | + parts = strings.Split(bundleFlow, delPrefix) |
| 264 | + } else { |
| 265 | + return "", isAdd, fmt.Errorf("invalid bundle flow %q", bundleFlow) |
| 266 | + } |
| 267 | + |
| 268 | + if len(parts) != 2 { |
| 269 | + return "", isAdd, fmt.Errorf("invalid bundle flow %q", bundleFlow) |
| 270 | + } |
| 271 | + return strings.TrimLeft(parts[1], " "), isAdd, nil |
| 272 | +} |
| 273 | + |
| 274 | +func (fake *ovsFake) Bundle(bundleFlows []string) error { |
| 275 | + errs := []error{} |
| 276 | + tx := fake.NewTransaction() |
| 277 | + |
| 278 | + for _, bflow := range bundleFlows { |
| 279 | + flow, isAdd, err := getFlowRule(bflow) |
| 280 | + if err != nil { |
| 281 | + errs = append(errs, err) |
| 282 | + continue |
| 283 | + } |
| 284 | + |
| 285 | + if isAdd { |
| 286 | + tx.AddFlow(flow) |
| 287 | + } else { |
| 288 | + tx.DeleteFlows(flow) |
| 289 | + } |
| 290 | + } |
| 291 | + |
| 292 | + if err := tx.EndTransaction(); err != nil { |
| 293 | + errs = append(errs, err) |
| 294 | + } |
| 295 | + |
| 296 | + return kerrs.NewAggregate(errs) |
| 297 | +} |
| 298 | + |
251 | 299 | func (fake *ovsFake) DumpFlows(flow string, args ...interface{}) ([]string, error) {
|
252 | 300 | if err := fake.ensureExists(); err != nil {
|
253 | 301 | return nil, err
|
|
0 commit comments