From 1a01a389cb12bd2209ec41a70f6bc3998b6fd4b9 Mon Sep 17 00:00:00 2001 From: zhangzujian Date: Mon, 22 Apr 2024 05:26:34 +0000 Subject: [PATCH] wip Signed-off-by: zhangzujian --- dist/images/Dockerfile.base | 8 +-- go.mod | 6 +- go.sum | 8 +-- mocks/pkg/ovs/interface.go | 108 ++++------------------------------ pkg/controller/controller.go | 16 ----- pkg/controller/init.go | 19 +++++- pkg/controller/subnet.go | 40 ++++++------- pkg/ovs/interface.go | 5 +- pkg/ovs/ovn-nb-suite_test.go | 4 -- pkg/ovs/ovn-nb_global.go | 97 +++++++----------------------- pkg/ovs/ovn-nb_global_test.go | 28 +-------- 11 files changed, 84 insertions(+), 255 deletions(-) diff --git a/dist/images/Dockerfile.base b/dist/images/Dockerfile.base index 37c8b17be22..2cc91584acc 100644 --- a/dist/images/Dockerfile.base +++ b/dist/images/Dockerfile.base @@ -36,7 +36,7 @@ RUN cd /usr/src/ && git clone -b branch-22.12 --depth=1 https://github.com/ovn-o # change hash type from dp_hash to hash with field src_ip curl -s https://github.com/kubeovn/ovn/commit/4ad8763f707ff4088ae61396c7931e8735f71f22.patch | git apply && \ # set ether dst addr for dnat on logical switch - curl -s https://github.com/kubeovn/ovn/commit/44875725ad6ce3cb38e4d471d540fe69ed204bff.patch | git apply && \ + # curl -s https://github.com/kubeovn/ovn/commit/44875725ad6ce3cb38e4d471d540fe69ed204bff.patch | git apply && \ # modify src route priority curl -s https://github.com/kubeovn/ovn/commit/da1388ece89b27012d081c31310fd577b036b071.patch | git apply && \ # fix reaching resubmit limit in underlay @@ -46,11 +46,11 @@ RUN cd /usr/src/ && git clone -b branch-22.12 --depth=1 https://github.com/ovn-o # fix lr-lb dnat with multiple distributed gateway ports curl -s https://github.com/kubeovn/ovn/commit/80f37c2debbf9f5230403691f791d11cc2b2e277.patch | git apply && \ # lflow: do not send direct traffic between lports to conntrack - curl -s https://github.com/kubeovn/ovn/commit/54cbe0d1ba2051e640dd3e53498f373362547691.patch | git apply && \ + # curl -s https://github.com/kubeovn/ovn/commit/54cbe0d1ba2051e640dd3e53498f373362547691.patch | git apply && \ # northd: add nb option version_compatibility - curl -s https://github.com/kubeovn/ovn/commit/06f5a7c684a6030036e2663eecf934b37c3e666e.patch | git apply && \ + curl -s https://github.com/kubeovn/ovn/commit/88a665d796b1007e963a05c5e15e3faad219e189.patch | git apply && \ # northd: skip conntrack when access node local dns ip - curl -s https://github.com/kubeovn/ovn/commit/1ea964886da774506962d6bf23f8f894d93a10eb.patch | git apply + # curl -s https://github.com/kubeovn/ovn/commit/1ea964886da774506962d6bf23f8f894d93a10eb.patch | git apply RUN apt install -y build-essential fakeroot \ autoconf automake bzip2 debhelper-compat dh-exec dh-python dh-sequence-python3 dh-sequence-sphinxdoc \ diff --git a/go.mod b/go.mod index c84315b158d..99b5fa1afd7 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.22 toolchain go1.22.2 require ( - github.com/Microsoft/go-winio v0.6.1 - github.com/Microsoft/hcsshim v0.12.2 + github.com/Microsoft/go-winio v0.6.2 + github.com/Microsoft/hcsshim v0.12.3 github.com/alauda/felix v3.6.6-0.20201207121355-187332daf314+incompatible github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e github.com/cenkalti/backoff/v4 v4.3.0 @@ -38,6 +38,7 @@ require ( github.com/stretchr/testify v1.9.0 github.com/vishvananda/netlink v1.2.1-beta.2 go.uber.org/mock v0.4.0 + golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f golang.org/x/mod v0.17.0 golang.org/x/sys v0.19.0 golang.org/x/time v0.5.0 @@ -237,7 +238,6 @@ require ( go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.27.0 // indirect golang.org/x/crypto v0.22.0 // indirect - golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect golang.org/x/net v0.24.0 // indirect golang.org/x/oauth2 v0.19.0 // indirect golang.org/x/sync v0.7.0 // indirect diff --git a/go.sum b/go.sum index fed0f905b88..ccce1e70e23 100644 --- a/go.sum +++ b/go.sum @@ -635,10 +635,10 @@ github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0= github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= -github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= -github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= -github.com/Microsoft/hcsshim v0.12.2 h1:AcXy+yfRvrx20g9v7qYaJv5Rh+8GaHOS6b8G6Wx/nKs= -github.com/Microsoft/hcsshim v0.12.2/go.mod h1:RZV12pcHCXQ42XnlQ3pz6FZfmrC1C+R4gaOHhRNML1g= +github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= +github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= +github.com/Microsoft/hcsshim v0.12.3 h1:LS9NXqXhMoqNCplK1ApmVSfB4UnVLRDWRapB6EIlxE0= +github.com/Microsoft/hcsshim v0.12.3/go.mod h1:Iyl1WVpZzr+UkzjekHZbV8o5Z9ZkxNGx6CtY2Qg/JVQ= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I= github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c= diff --git a/mocks/pkg/ovs/interface.go b/mocks/pkg/ovs/interface.go index 23a133dfb45..d4d4b8b813d 100644 --- a/mocks/pkg/ovs/interface.go +++ b/mocks/pkg/ovs/interface.go @@ -88,60 +88,18 @@ func (mr *MockNBGlobalMockRecorder) SetICAutoRoute(enable, blackList any) *gomoc return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetICAutoRoute", reflect.TypeOf((*MockNBGlobal)(nil).SetICAutoRoute), enable, blackList) } -// SetLsCtSkipDstLportIPs mocks base method. -func (m *MockNBGlobal) SetLsCtSkipDstLportIPs(enabled bool) error { +// SetNBGlobalOptions mocks base method. +func (m *MockNBGlobal) SetNBGlobalOptions(options map[string]string) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetLsCtSkipDstLportIPs", enabled) + ret := m.ctrl.Call(m, "SetNBGlobalOptions", options) ret0, _ := ret[0].(error) return ret0 } -// SetLsCtSkipDstLportIPs indicates an expected call of SetLsCtSkipDstLportIPs. -func (mr *MockNBGlobalMockRecorder) SetLsCtSkipDstLportIPs(enabled any) *gomock.Call { +// SetNBGlobalOptions indicates an expected call of SetNBGlobalOptions. +func (mr *MockNBGlobalMockRecorder) SetNBGlobalOptions(options any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLsCtSkipDstLportIPs", reflect.TypeOf((*MockNBGlobal)(nil).SetLsCtSkipDstLportIPs), enabled) -} - -// SetNodeLocalDNSIP mocks base method. -func (m *MockNBGlobal) SetNodeLocalDNSIP(nodeLocalDNSIP string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetNodeLocalDNSIP", nodeLocalDNSIP) - ret0, _ := ret[0].(error) - return ret0 -} - -// SetNodeLocalDNSIP indicates an expected call of SetNodeLocalDNSIP. -func (mr *MockNBGlobalMockRecorder) SetNodeLocalDNSIP(nodeLocalDNSIP any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetNodeLocalDNSIP", reflect.TypeOf((*MockNBGlobal)(nil).SetNodeLocalDNSIP), nodeLocalDNSIP) -} - -// SetLsDnatModDlDst mocks base method. -func (m *MockNBGlobal) SetLsDnatModDlDst(enabled bool) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetLsDnatModDlDst", enabled) - ret0, _ := ret[0].(error) - return ret0 -} - -// SetLsDnatModDlDst indicates an expected call of SetLsDnatModDlDst. -func (mr *MockNBGlobalMockRecorder) SetLsDnatModDlDst(enabled any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLsDnatModDlDst", reflect.TypeOf((*MockNBGlobal)(nil).SetLsDnatModDlDst), enabled) -} - -// SetUseCtInvMatch mocks base method. -func (m *MockNBGlobal) SetUseCtInvMatch() error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetUseCtInvMatch") - ret0, _ := ret[0].(error) - return ret0 -} - -// SetUseCtInvMatch indicates an expected call of SetUseCtInvMatch. -func (mr *MockNBGlobalMockRecorder) SetUseCtInvMatch() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetUseCtInvMatch", reflect.TypeOf((*MockNBGlobal)(nil).SetUseCtInvMatch)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetNBGlobalOptions", reflect.TypeOf((*MockNBGlobal)(nil).SetNBGlobalOptions), options) } // UpdateNbGlobal mocks base method. @@ -4275,60 +4233,18 @@ func (mr *MockNbClientMockRecorder) SetLogicalSwitchPrivate(lsName, cidrBlock, n return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogicalSwitchPrivate", reflect.TypeOf((*MockNbClient)(nil).SetLogicalSwitchPrivate), lsName, cidrBlock, nodeSwitchCIDR, allowSubnets) } -// SetLsCtSkipDstLportIPs mocks base method. -func (m *MockNbClient) SetLsCtSkipDstLportIPs(enabled bool) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetLsCtSkipDstLportIPs", enabled) - ret0, _ := ret[0].(error) - return ret0 -} - -// SetLsCtSkipDstLportIPs indicates an expected call of SetLsCtSkipDstLportIPs. -func (mr *MockNbClientMockRecorder) SetLsCtSkipDstLportIPs(enabled any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLsCtSkipDstLportIPs", reflect.TypeOf((*MockNbClient)(nil).SetLsCtSkipDstLportIPs), enabled) -} - -// SetNodeLocalDNSIP mocks base method. -func (m *MockNbClient) SetNodeLocalDNSIP(nodeLocalDNSIP string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetNodeLocalDNSIP", nodeLocalDNSIP) - ret0, _ := ret[0].(error) - return ret0 -} - -// SetNodeLocalDNSIP indicates an expected call of SetNodeLocalDNSIP. -func (mr *MockNbClientMockRecorder) SetNodeLocalDNSIP(nodeLocalDNSIP any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetNodeLocalDNSIP", reflect.TypeOf((*MockNbClient)(nil).SetNodeLocalDNSIP), nodeLocalDNSIP) -} - -// SetLsDnatModDlDst mocks base method. -func (m *MockNbClient) SetLsDnatModDlDst(enabled bool) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetLsDnatModDlDst", enabled) - ret0, _ := ret[0].(error) - return ret0 -} - -// SetLsDnatModDlDst indicates an expected call of SetLsDnatModDlDst. -func (mr *MockNbClientMockRecorder) SetLsDnatModDlDst(enabled any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLsDnatModDlDst", reflect.TypeOf((*MockNbClient)(nil).SetLsDnatModDlDst), enabled) -} - -// SetUseCtInvMatch mocks base method. -func (m *MockNbClient) SetUseCtInvMatch() error { +// SetNBGlobalOptions mocks base method. +func (m *MockNbClient) SetNBGlobalOptions(options map[string]string) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetUseCtInvMatch") + ret := m.ctrl.Call(m, "SetNBGlobalOptions", options) ret0, _ := ret[0].(error) return ret0 } -// SetUseCtInvMatch indicates an expected call of SetUseCtInvMatch. -func (mr *MockNbClientMockRecorder) SetUseCtInvMatch() *gomock.Call { +// SetNBGlobalOptions indicates an expected call of SetNBGlobalOptions. +func (mr *MockNbClientMockRecorder) SetNBGlobalOptions(options any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetUseCtInvMatch", reflect.TypeOf((*MockNbClient)(nil).SetUseCtInvMatch)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetNBGlobalOptions", reflect.TypeOf((*MockNbClient)(nil).SetNBGlobalOptions), options) } // SetVirtualLogicalSwitchPortVirtualParents mocks base method. diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index d999d0fc7ca..9cfcc2ca0bf 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -740,22 +740,6 @@ func Run(ctx context.Context, config *Configuration) { func (c *Controller) Run(ctx context.Context) { // The init process can only be placed here if the init process do really affect the normal process of controller, such as Nodes/Pods/Subnets... // Otherwise, the init process should be placed after all workers have already started working - if err := c.OVNNbClient.SetLsDnatModDlDst(c.config.LsDnatModDlDst); err != nil { - util.LogFatalAndExit(err, "failed to set NB_Global option ls_dnat_mod_dl_dst") - } - - if err := c.OVNNbClient.SetUseCtInvMatch(); err != nil { - util.LogFatalAndExit(err, "failed to set NB_Global option use_ct_inv_match to false") - } - - if err := c.OVNNbClient.SetLsCtSkipDstLportIPs(c.config.LsCtSkipDstLportIPs); err != nil { - util.LogFatalAndExit(err, "failed to set NB_Global option ls_ct_skip_dst_lport_ips") - } - - if err := c.OVNNbClient.SetNodeLocalDNSIP(c.config.NodeLocalDNSIP); err != nil { - util.LogFatalAndExit(err, "failed to set NB_Global option node_local_dns_ip") - } - if err := c.InitOVN(); err != nil { util.LogFatalAndExit(err, "failed to initialize ovn resources") } diff --git a/pkg/controller/init.go b/pkg/controller/init.go index 71bc4f7d6d1..150e14a5467 100644 --- a/pkg/controller/init.go +++ b/pkg/controller/init.go @@ -29,6 +29,10 @@ import ( func (c *Controller) InitOVN() error { var err error + if err = c.initOVNNBGlobal(); err != nil { + util.LogFatalAndExit(err, "failed to initialize OVN NB_Global") + } + if err = c.initClusterRouter(); err != nil { klog.Errorf("init cluster router failed: %v", err) return err @@ -198,7 +202,18 @@ func (c *Controller) initNodeSwitch() error { return nil } -// InitClusterRouter init cluster router to connect different logical switches +func (c *Controller) initOVNNBGlobal() error { + options := map[string]string{ + "use_ct_inv_match": "false", + "node_switch": "", + } + if c.config.EnableLb { + options["node_switch"] = c.config.NodeSwitch + } + return c.OVNNbClient.SetNBGlobalOptions(options) +} + +// initClusterRouter init cluster router to connect different logical switches func (c *Controller) initClusterRouter() error { return c.OVNNbClient.CreateLogicalRouter(c.config.ClusterRouter) } @@ -230,7 +245,7 @@ func (c *Controller) initLB(name, protocol string, sessionAffinity bool) error { return nil } -// InitLoadBalancer init the default tcp and udp cluster loadbalancer +// initLoadBalancer init the default tcp and udp cluster loadbalancer func (c *Controller) initLoadBalancer() error { vpcs, err := c.vpcsLister.List(labels.Everything()) if err != nil { diff --git a/pkg/controller/subnet.go b/pkg/controller/subnet.go index f56bf9e3648..faa7ed7a785 100644 --- a/pkg/controller/subnet.go +++ b/pkg/controller/subnet.go @@ -831,26 +831,26 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error { return err } - if c.config.EnableLb && subnet.Name != c.config.NodeSwitch { - lbs := []string{ - vpc.Status.TCPLoadBalancer, - vpc.Status.TCPSessionLoadBalancer, - vpc.Status.UDPLoadBalancer, - vpc.Status.UDPSessionLoadBalancer, - vpc.Status.SctpLoadBalancer, - vpc.Status.SctpSessionLoadBalancer, - } - if subnet.Spec.EnableLb != nil && *subnet.Spec.EnableLb { - if err := c.OVNNbClient.LogicalSwitchUpdateLoadBalancers(subnet.Name, ovsdb.MutateOperationInsert, lbs...); err != nil { - c.patchSubnetStatus(subnet, "AddLbToLogicalSwitchFailed", err.Error()) - klog.Error(err) - return err - } - } else { - if err := c.OVNNbClient.LogicalSwitchUpdateLoadBalancers(subnet.Name, ovsdb.MutateOperationDelete, lbs...); err != nil { - klog.Errorf("remove load-balancer from subnet %s failed: %v", subnet.Name, err) - return err - } + lbs := []string{ + vpc.Status.TCPLoadBalancer, + vpc.Status.TCPSessionLoadBalancer, + vpc.Status.UDPLoadBalancer, + vpc.Status.UDPSessionLoadBalancer, + vpc.Status.SctpLoadBalancer, + vpc.Status.SctpSessionLoadBalancer, + } + // TODO: consider subnet.Spec.U2OInterconnection in the default vpc + if c.config.EnableLb && ((subnet.Name == c.config.NodeSwitch || subnet.Spec.Vpc != c.config.ClusterRouter) || + (subnet.Spec.Vlan != "" && subnet.Spec.EnableLb != nil && *subnet.Spec.EnableLb)) { + if err := c.OVNNbClient.LogicalSwitchUpdateLoadBalancers(subnet.Name, ovsdb.MutateOperationInsert, lbs...); err != nil { + c.patchSubnetStatus(subnet, "AddLbToLogicalSwitchFailed", err.Error()) + klog.Error(err) + return err + } + } else { + if err := c.OVNNbClient.LogicalSwitchUpdateLoadBalancers(subnet.Name, ovsdb.MutateOperationDelete, lbs...); err != nil { + klog.Errorf("remove load-balancer from subnet %s failed: %v", subnet.Name, err) + return err } } diff --git a/pkg/ovs/interface.go b/pkg/ovs/interface.go index 091e9e884e1..38906172006 100644 --- a/pkg/ovs/interface.go +++ b/pkg/ovs/interface.go @@ -14,11 +14,8 @@ import ( type NBGlobal interface { UpdateNbGlobal(nbGlobal *ovnnb.NBGlobal, fields ...interface{}) error SetAzName(azName string) error - SetUseCtInvMatch() error SetICAutoRoute(enable bool, blackList []string) error - SetLsDnatModDlDst(enabled bool) error - SetLsCtSkipDstLportIPs(enabled bool) error - SetNodeLocalDNSIP(nodeLocalDNSIP string) error + SetNBGlobalOptions(options map[string]string) error GetNbGlobal() (*ovnnb.NBGlobal, error) } diff --git a/pkg/ovs/ovn-nb-suite_test.go b/pkg/ovs/ovn-nb-suite_test.go index 409e092afcb..80445e31649 100644 --- a/pkg/ovs/ovn-nb-suite_test.go +++ b/pkg/ovs/ovn-nb-suite_test.go @@ -72,10 +72,6 @@ func (suite *OvnClientTestSuite) Test_SetUseCtInvMatch() { suite.testSetUseCtInvMatch() } -func (suite *OvnClientTestSuite) Test_SetLBCIDR() { - suite.testSetLBCIDR() -} - /* logical_switch unit test */ func (suite *OvnClientTestSuite) Test_CreateLogicalSwitch() { suite.testCreateLogicalSwitch() diff --git a/pkg/ovs/ovn-nb_global.go b/pkg/ovs/ovn-nb_global.go index 38a41db5771..353bf4f1b80 100644 --- a/pkg/ovs/ovn-nb_global.go +++ b/pkg/ovs/ovn-nb_global.go @@ -3,9 +3,9 @@ package ovs import ( "context" "fmt" - "reflect" "strings" + "golang.org/x/exp/maps" "k8s.io/klog/v2" "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb" @@ -88,96 +88,43 @@ func (c *OVNNbClient) SetAzName(azName string) error { return nil } -func (c *OVNNbClient) SetNbGlobalOptions(key string, value interface{}) error { - nbGlobal, err := c.GetNbGlobal() - if err != nil { - return fmt.Errorf("failed to get nb global: %v", err) - } - - v := fmt.Sprintf("%v", value) - if len(nbGlobal.Options) != 0 && nbGlobal.Options[key] == v { - return nil - } - - options := make(map[string]string, len(nbGlobal.Options)+1) - for k, v := range nbGlobal.Options { - options[k] = v - } - nbGlobal.Options[key] = v - if err := c.UpdateNbGlobal(nbGlobal, &nbGlobal.Options); err != nil { - return fmt.Errorf("failed to set nb global option %s to %v: %v", key, value, err) - } - - return nil -} - -func (c *OVNNbClient) SetUseCtInvMatch() error { - return c.SetNbGlobalOptions("use_ct_inv_match", false) -} - func (c *OVNNbClient) SetICAutoRoute(enable bool, blackList []string) error { - nbGlobal, err := c.GetNbGlobal() - if err != nil { - return fmt.Errorf("get nb global: %v", err) - } - - options := make(map[string]string, len(nbGlobal.Options)+3) - for k, v := range nbGlobal.Options { - options[k] = v + options := map[string]string{ + "ic-route-adv": "", + "ic-route-learn": "", + "ic-route-blacklist": "", } if enable { options["ic-route-adv"] = "true" options["ic-route-learn"] = "true" options["ic-route-blacklist"] = strings.Join(blackList, ",") - } else { - delete(options, "ic-route-adv") - delete(options, "ic-route-learn") - delete(options, "ic-route-blacklist") - } - if reflect.DeepEqual(options, nbGlobal.Options) { - nbGlobal.Options = options - return nil - } - - nbGlobal.Options = options - if err := c.UpdateNbGlobal(nbGlobal, &nbGlobal.Options); err != nil { - return fmt.Errorf("enable ovn-ic auto route, %v", err) } - return nil -} - -func (c *OVNNbClient) SetLBCIDR(serviceCIDR string) error { - return c.SetNbGlobalOptions("svc_ipv4_cidr", serviceCIDR) -} -func (c *OVNNbClient) SetLsDnatModDlDst(enabled bool) error { - return c.SetNbGlobalOptions("ls_dnat_mod_dl_dst", enabled) + return c.SetNBGlobalOptions(options) } -func (c *OVNNbClient) SetLsCtSkipDstLportIPs(enabled bool) error { - return c.SetNbGlobalOptions("ls_ct_skip_dst_lport_ips", enabled) -} - -func (c *OVNNbClient) SetNodeLocalDNSIP(nodeLocalDNSIP string) error { - if nodeLocalDNSIP != "" { - return c.SetNbGlobalOptions("node_local_dns_ip", nodeLocalDNSIP) - } - +func (c *OVNNbClient) SetNBGlobalOptions(options map[string]string) error { nbGlobal, err := c.GetNbGlobal() if err != nil { - return fmt.Errorf("get nb global: %v", err) + return fmt.Errorf("failed to get nb global: %v", err) } - options := make(map[string]string, len(nbGlobal.Options)) - for k, v := range nbGlobal.Options { - options[k] = v + newOptions := maps.Clone(nbGlobal.Options) + if newOptions == nil { + newOptions = make(map[string]string, len(options)) + } + for k, v := range options { + if v == "" { + delete(nbGlobal.Options, k) + } else { + nbGlobal.Options[k] = v + } } - delete(options, "node_local_dns_ip") - - nbGlobal.Options = options - if err := c.UpdateNbGlobal(nbGlobal, &nbGlobal.Options); err != nil { - return fmt.Errorf("remove option node_local_dns_ip failed , %v", err) + if !maps.Equal(nbGlobal.Options, newOptions) { + if err = c.UpdateNbGlobal(nbGlobal, &nbGlobal.Options); err != nil { + return fmt.Errorf("failed to update nb global options: %v", err) + } } return nil diff --git a/pkg/ovs/ovn-nb_global_test.go b/pkg/ovs/ovn-nb_global_test.go index a66dd684faf..0068c28e7bc 100644 --- a/pkg/ovs/ovn-nb_global_test.go +++ b/pkg/ovs/ovn-nb_global_test.go @@ -187,36 +187,10 @@ func (suite *OvnClientTestSuite) testSetUseCtInvMatch() { err := ovnClient.CreateNbGlobal(nbGlobal) require.NoError(t, err) - err = ovnClient.SetUseCtInvMatch() + err = ovnClient.SetNBGlobalOptions(map[string]string{"use_ct_inv_match": "false"}) require.NoError(t, err) out, err := ovnClient.GetNbGlobal() require.NoError(t, err) require.Equal(t, "false", out.Options["use_ct_inv_match"]) } - -func (suite *OvnClientTestSuite) testSetLBCIDR() { - t := suite.T() - - ovnClient := suite.ovnClient - serviceCIDR := "10.96.0.0/12" - - t.Cleanup(func() { - err := ovnClient.DeleteNbGlobal() - require.NoError(t, err) - - _, err = ovnClient.GetNbGlobal() - require.ErrorContains(t, err, "not found nb_global") - }) - - nbGlobal := mockNBGlobal() - err := ovnClient.CreateNbGlobal(nbGlobal) - require.NoError(t, err) - - err = ovnClient.SetLBCIDR(serviceCIDR) - require.NoError(t, err) - - out, err := ovnClient.GetNbGlobal() - require.NoError(t, err) - require.Equal(t, serviceCIDR, out.Options["svc_ipv4_cidr"]) -}