func syncAddressSets(ovsdbClient ovsdb.Client, labels []db.Label) { ovsdbAddresses, err := ovsdbClient.ListAddressSets(lSwitch) if err != nil { log.WithError(err).Error("Failed to list address sets") return } var expAddressSets []ovsdb.AddressSet for _, l := range labels { if l.Label == stitch.PublicInternetLabel { continue } expAddressSets = append(expAddressSets, ovsdb.AddressSet{ Name: addressSetName(l.Label), Addresses: unique(append(l.ContainerIPs, l.IP)), }, ) } ovsdbKey := func(intf interface{}) interface{} { addrSet := intf.(ovsdb.AddressSet) // OVSDB returns the addresses in a non-deterministic order, so we // sort them. sort.Strings(addrSet.Addresses) return addressSetKey{ name: addrSet.Name, addresses: strings.Join(addrSet.Addresses, " "), } } _, toCreate, toDelete := join.HashJoin(addressSlice(expAddressSets), addressSlice(ovsdbAddresses), ovsdbKey, ovsdbKey) for _, intf := range toDelete { addr := intf.(ovsdb.AddressSet) if err := ovsdbClient.DeleteAddressSet(lSwitch, addr.Name); err != nil { log.WithError(err).Warn("Error deleting address set") } } for _, intf := range toCreate { addr := intf.(ovsdb.AddressSet) if err := ovsdbClient.CreateAddressSet( lSwitch, addr.Name, addr.Addresses); err != nil { log.WithError(err).Warn("Error adding address set") } } }
func checkAddressSet(t *testing.T, client ovsdb.Client, labels []db.Label, exp []ovsdb.AddressSet) { syncAddressSets(client, labels) actual, _ := client.ListAddressSets(lSwitch) ovsdbKey := func(intf interface{}) interface{} { addrSet := intf.(ovsdb.AddressSet) // OVSDB returns the addresses in a non-deterministic order, so we // sort them. sort.Strings(addrSet.Addresses) return addressSetKey{ name: addrSet.Name, addresses: strings.Join(addrSet.Addresses, " "), } } if _, lefts, rights := join.HashJoin(addressSlice(actual), addressSlice(exp), ovsdbKey, ovsdbKey); len(lefts) != 0 || len(rights) != 0 { t.Errorf("Wrong address sets: expected %v, got %v.", exp, actual) } }