Skip to content

Commit

Permalink
moving the address_set to its own package
Browse files Browse the repository at this point in the history
in an effort to break up the large the ovn package move address_set
this will make mocking the address_set interface for testing much
easier

Signed-off-by: Jacob Tanenbaum <jtanenba@redhat.com>
  • Loading branch information
JacobTanenbaum committed Dec 16, 2020
1 parent 0ac9775 commit d6f2261
Show file tree
Hide file tree
Showing 22 changed files with 239 additions and 235 deletions.
@@ -1,4 +1,4 @@
package ovn
package addressset

import (
"fmt"
Expand Down Expand Up @@ -42,10 +42,8 @@ type AddressSetFactory interface {

// AddressSet is an interface for address set objects
type AddressSet interface {
// GetIPv4HashName returns the hashed name for v4 address set
GetIPv4HashName() string
// GetIPv6HashName returns the hashed name for v4 address set
GetIPv6HashName() string
// GetASHashName returns the hashed name for ipv6 and ipv4 addressSets
GetASHashNames() (string, string)
// GetName returns the descriptive name of the address set
GetName() string
// AddIPs adds the array of IPs to the address set
Expand Down Expand Up @@ -136,12 +134,16 @@ func (asf *ovnAddressSetFactory) DestroyAddressSetInBackingStore(name string) er
if err != nil {
return err
}
err = destroyAddressSet(getIPv4ASName(name))
ip4ASName, ip6ASName := MakeAddressSetName(name)
err = destroyAddressSet(ip4ASName)
if err != nil {
return err
}
err = destroyAddressSet(getIPv6ASName(name))
return err
err = destroyAddressSet(ip6ASName)
if err != nil {
return err
}
return nil
}

func destroyAddressSet(name string) error {
Expand Down Expand Up @@ -173,7 +175,7 @@ var _ AddressSet = &ovnAddressSets{}

// hash the provided input to make it a valid ovnAddressSet name.
func hashedAddressSet(s string) string {
return hashForOVN(s)
return util.HashForOVN(s)
}

func asDetail(as *ovnAddressSet) string {
Expand All @@ -195,14 +197,15 @@ func newOvnAddressSets(name string, ips []net.IP) (*ovnAddressSets, error) {
v4IPs = append(v4IPs, ip)
}
}
ip4ASName, ip6ASName := MakeAddressSetName(name)
if config.IPv4Mode {
v4set, err = newOvnAddressSet(getIPv4ASName(name), v4IPs)
v4set, err = newOvnAddressSet(ip4ASName, v4IPs)
if err != nil {
return nil, err
}
}
if config.IPv6Mode {
v6set, err = newOvnAddressSet(getIPv6ASName(name), v6IPs)
v6set, err = newOvnAddressSet(ip6ASName, v6IPs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -259,18 +262,16 @@ func newOvnAddressSet(name string, ips []net.IP) (*ovnAddressSet, error) {
return as, nil
}

func (as *ovnAddressSets) GetIPv4HashName() string {
func (as *ovnAddressSets) GetASHashNames() (string, string) {
var ipv4AS string
var ipv6AS string
if as.ipv4 != nil {
return as.ipv4.hashName
ipv4AS = as.ipv4.hashName
}
return ""
}

func (as *ovnAddressSets) GetIPv6HashName() string {
if as.ipv6 != nil {
return as.ipv6.hashName
ipv6AS = as.ipv6.hashName
}
return ""
return ipv4AS, ipv6AS
}

func (as *ovnAddressSets) GetName() string {
Expand Down Expand Up @@ -455,18 +456,11 @@ func (as *ovnAddressSet) destroy() error {
return nil
}

func getIPv4ASName(name string) string {
return name + ipv4AddressSetSuffix
}

func getIPv6ASName(name string) string {
return name + ipv6AddressSetSuffix
}

func getIPv4ASHashedName(name string) string {
return hashedAddressSet(name + ipv4AddressSetSuffix)
func MakeAddressSetName(name string) (string, string) {
return name + ipv4AddressSetSuffix, name + ipv6AddressSetSuffix
}

func getIPv6ASHashedName(name string) string {
return hashedAddressSet(name + ipv6AddressSetSuffix)
func MakeAddressSetHashNames(name string) (string, string) {
ipv4AddressSetName, ipv6AddressSetName := MakeAddressSetName(name)
return hashedAddressSet(ipv4AddressSetName), hashedAddressSet(ipv6AddressSetName)
}
13 changes: 13 additions & 0 deletions go-controller/pkg/ovn/address_set/address_set_suit_test.go
@@ -0,0 +1,13 @@
package addressset

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestClusterNode(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Address Set Operations Suite")
}
@@ -1,4 +1,4 @@
package ovn
package addressset

import (
"fmt"
Expand All @@ -20,6 +20,11 @@ type testAddressSetName struct {
suffix2 string
}

const (
fakeUUID = "8a86f6d8-7972-4253-b0bd-ddbef66e9303"
fakeUUIDv6 = "8a86f6d8-7972-4253-b0bd-ddbef66e9304"
)

func (asn *testAddressSetName) makeName() string {
return fmt.Sprintf("%s.%s.%s", asn.namespace, asn.suffix1, asn.suffix2)
}
Expand Down
@@ -1,4 +1,4 @@
package ovn
package addressset

import (
"net"
Expand All @@ -13,23 +13,23 @@ import (
. "github.com/onsi/gomega"
)

func newFakeAddressSetFactory() *fakeAddressSetFactory {
return &fakeAddressSetFactory{
func NewFakeAddressSetFactory() *FakeAddressSetFactory {
return &FakeAddressSetFactory{
sets: make(map[string]*fakeAddressSet),
}
}

type fakeAddressSetFactory struct {
type FakeAddressSetFactory struct {
sync.RWMutex
// maps address set name to object
sets map[string]*fakeAddressSet
}

// fakeFactory implements the AddressSetFactory interface
var _ AddressSetFactory = &fakeAddressSetFactory{}
var _ AddressSetFactory = &FakeAddressSetFactory{}

// NewAddressSet returns a new address set object
func (f *fakeAddressSetFactory) NewAddressSet(name string, ips []net.IP) (AddressSet, error) {
func (f *FakeAddressSetFactory) NewAddressSet(name string, ips []net.IP) (AddressSet, error) {
f.Lock()
defer f.Unlock()
_, ok := f.sets[name]
Expand All @@ -38,16 +38,17 @@ func (f *fakeAddressSetFactory) NewAddressSet(name string, ips []net.IP) (Addres
if err != nil {
return nil, err
}
ip4ASName, ip6ASName := MakeAddressSetName(name)
if set.ipv4 != nil {
f.sets[getIPv4ASName(name)] = set.ipv4
f.sets[ip4ASName] = set.ipv4
}
if set.ipv6 != nil {
f.sets[getIPv6ASName(name)] = set.ipv6
f.sets[ip6ASName] = set.ipv6
}
return set, nil
}

func (f *fakeAddressSetFactory) ForEachAddressSet(iteratorFn AddressSetIterFunc) error {
func (f *FakeAddressSetFactory) ForEachAddressSet(iteratorFn AddressSetIterFunc) error {
asNames := sets.String{}
for _, set := range f.sets {
asName := truncateSuffixFromAddressSet(set.getName())
Expand All @@ -66,21 +67,22 @@ func (f *fakeAddressSetFactory) ForEachAddressSet(iteratorFn AddressSetIterFunc)
return nil
}

func (f *fakeAddressSetFactory) DestroyAddressSetInBackingStore(name string) error {
func (f *FakeAddressSetFactory) DestroyAddressSetInBackingStore(name string) error {
if _, ok := f.sets[name]; ok {
f.removeAddressSet(name)
return nil
}
ip4ASName, ip6ASName := MakeAddressSetName(name)
if config.IPv4Mode {
f.removeAddressSet(getIPv4ASName(name))
f.removeAddressSet(ip4ASName)
}
if config.IPv6Mode {
f.removeAddressSet(getIPv6ASName(name))
f.removeAddressSet(ip6ASName)
}
return nil
}

func (f *fakeAddressSetFactory) getAddressSet(name string) *fakeAddressSet {
func (f *FakeAddressSetFactory) getAddressSet(name string) *fakeAddressSet {
f.RLock()
defer f.RUnlock()
if as, ok := f.sets[name]; ok {
Expand All @@ -91,46 +93,64 @@ func (f *fakeAddressSetFactory) getAddressSet(name string) *fakeAddressSet {
}

// removeAddressSet removes the address set from the factory
func (f *fakeAddressSetFactory) removeAddressSet(name string) {
func (f *FakeAddressSetFactory) removeAddressSet(name string) {
f.Lock()
defer f.Unlock()
delete(f.sets, name)
}

// ExpectNoAddressSet ensures the named address set does not exist
func (f *fakeAddressSetFactory) ExpectNoAddressSet(name string) {
func (f *FakeAddressSetFactory) ExpectNoAddressSet(name string) {
_, ok := f.sets[name]
Expect(ok).To(BeFalse())
}

// ExpectAddressSetWithIPs ensures the named address set exists with the given set of IPs
func (f *fakeAddressSetFactory) ExpectAddressSetWithIPs(name string, ips []string) {
as := f.getAddressSet(name)
Expect(as).NotTo(BeNil())
defer as.Unlock()
for _, expectedIP := range ips {
Expect(as.ips).To(HaveKey(expectedIP))
func (f *FakeAddressSetFactory) ExpectAddressSetWithIPs(name string, ips []string) {
var lenAddressSet int
name4, name6 := MakeAddressSetName(name)
as4 := f.getAddressSet(name4)
if as4 != nil {
defer as4.Unlock()
lenAddressSet = lenAddressSet + len(as4.ips)
}
as6 := f.getAddressSet(name6)
if as6 != nil {
defer as6.Unlock()
lenAddressSet = lenAddressSet + len(as6.ips)
}

for _, ip := range ips {
if utilnet.IsIPv6(net.ParseIP(ip)) {
Expect(as6).NotTo(BeNil())
Expect(as6.ips).To(HaveKey(ip))
} else {
Expect(as4).NotTo(BeNil())
Expect(as4.ips).To(HaveKey(ip))
}
}
Expect(as.ips).To(HaveLen(len(ips)))

Expect(lenAddressSet).To(Equal(len(ips)))
}

// ExpectEmptyAddressSet ensures the named address set exists with no IPs
func (f *fakeAddressSetFactory) ExpectEmptyAddressSet(name string) {
func (f *FakeAddressSetFactory) ExpectEmptyAddressSet(name string) {
f.ExpectAddressSetWithIPs(name, nil)
}

// EventuallyExpectEmptyAddressSet ensures the named address set eventually exists with no IPs
func (f *fakeAddressSetFactory) EventuallyExpectEmptyAddressSet(name string) {
func (f *FakeAddressSetFactory) EventuallyExpectEmptyAddressSet(name string) {
name4, _ := MakeAddressSetName(name)
Eventually(func() bool {
as := f.getAddressSet(name)
as := f.getAddressSet(name4)
Expect(as).NotTo(BeNil())
defer as.Unlock()
return len(as.ips) == 0
}).Should(BeTrue())
}

// EventuallyExpectNoAddressSet ensures the named address set eventually does not exist
func (f *fakeAddressSetFactory) EventuallyExpectNoAddressSet(name string) {
func (f *FakeAddressSetFactory) EventuallyExpectNoAddressSet(name string) {
Eventually(func() bool {
f.RLock()
defer f.RUnlock()
Expand Down Expand Up @@ -171,11 +191,12 @@ func newFakeAddressSets(name string, ips []net.IP, removeFn removeFunc) (*fakeAd
v4Ips = append(v4Ips, ip)
}
}
ip4ASName, ip6ASName := MakeAddressSetName(name)
if config.IPv4Mode {
v4set = newFakeAddressSet(getIPv4ASName(name), v4Ips, removeFn)
v4set = newFakeAddressSet(ip4ASName, v4Ips, removeFn)
}
if config.IPv6Mode {
v6set = newFakeAddressSet(getIPv6ASName(name), v6Ips, removeFn)
v6set = newFakeAddressSet(ip6ASName, v6Ips, removeFn)
}
return &fakeAddressSets{name: name, ipv4: v4set, ipv6: v6set}, nil
}
Expand All @@ -193,18 +214,16 @@ func newFakeAddressSet(name string, ips []net.IP, removeFn removeFunc) *fakeAddr
return as
}

func (as *fakeAddressSets) GetIPv4HashName() string {
func (as *fakeAddressSets) GetASHashNames() (string, string) {
var ipv4AS string
var ipv6AS string
if as.ipv4 != nil {
return as.ipv4.getHashName()
ipv4AS = as.ipv4.getHashName()
}
return ""
}

func (as *fakeAddressSets) GetIPv6HashName() string {
if as.ipv6 != nil {
return as.ipv6.getHashName()
ipv6AS = as.ipv6.getHashName()
}
return ""
return ipv4AS, ipv6AS
}

func (as *fakeAddressSets) GetName() string {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d6f2261

Please sign in to comment.