From 9392dedf0856562ff2844fc4f7b02051ab47a6cb Mon Sep 17 00:00:00 2001 From: bobz965 Date: Thu, 17 Aug 2023 11:34:21 +0800 Subject: [PATCH] remove unused Signed-off-by: bobz965 --- mocks/pkg/ovs/interface.go | 16 ++++++++-------- pkg/controller/controller.go | 6 +++--- pkg/controller/subnet.go | 2 +- pkg/ovs/interface.go | 2 +- pkg/ovs/ovn-nb-acl.go | 4 ++-- pkg/ovs/ovn-nb-acl_test.go | 9 +++++---- pkg/ovs/ovn-nb-suite_test.go | 7 +++---- pkg/ovs/ovn.go | 31 +++++++------------------------ pkg/ovsdb/client/client.go | 5 ++--- 9 files changed, 32 insertions(+), 50 deletions(-) diff --git a/mocks/pkg/ovs/interface.go b/mocks/pkg/ovs/interface.go index 55a32c99dff..3b2a9eed704 100644 --- a/mocks/pkg/ovs/interface.go +++ b/mocks/pkg/ovs/interface.go @@ -1282,17 +1282,17 @@ func (mr *MockACLMockRecorder) SetAclLog(pgName, protocol, logEnable, isIngress } // SetLogicalSwitchPrivate mocks base method. -func (m *MockACL) SetLogicalSwitchPrivate(lsName, cidrBlock string, allowSubnets []string) error { +func (m *MockACL) SetLogicalSwitchPrivate(lsName, cidrBlock, nodeSwitchCIDR string, allowSubnets []string) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetLogicalSwitchPrivate", lsName, cidrBlock, allowSubnets) + ret := m.ctrl.Call(m, "SetLogicalSwitchPrivate", lsName, cidrBlock, nodeSwitchCIDR, allowSubnets) ret0, _ := ret[0].(error) return ret0 } // SetLogicalSwitchPrivate indicates an expected call of SetLogicalSwitchPrivate. -func (mr *MockACLMockRecorder) SetLogicalSwitchPrivate(lsName, cidrBlock, allowSubnets interface{}) *gomock.Call { +func (mr *MockACLMockRecorder) SetLogicalSwitchPrivate(lsName, cidrBlock, nodeSwitchCIDR, allowSubnets interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogicalSwitchPrivate", reflect.TypeOf((*MockACL)(nil).SetLogicalSwitchPrivate), lsName, cidrBlock, allowSubnets) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogicalSwitchPrivate", reflect.TypeOf((*MockACL)(nil).SetLogicalSwitchPrivate), lsName, cidrBlock, nodeSwitchCIDR, allowSubnets) } // UpdateEgressAclOps mocks base method. @@ -3443,17 +3443,17 @@ func (mr *MockNbClientMockRecorder) SetLogicalSwitchPortsSecurityGroup(sgName, o } // SetLogicalSwitchPrivate mocks base method. -func (m *MockNbClient) SetLogicalSwitchPrivate(lsName, cidrBlock string, allowSubnets []string) error { +func (m *MockNbClient) SetLogicalSwitchPrivate(lsName, cidrBlock, nodeSwitchCIDR string, allowSubnets []string) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetLogicalSwitchPrivate", lsName, cidrBlock, allowSubnets) + ret := m.ctrl.Call(m, "SetLogicalSwitchPrivate", lsName, cidrBlock, nodeSwitchCIDR, allowSubnets) ret0, _ := ret[0].(error) return ret0 } // SetLogicalSwitchPrivate indicates an expected call of SetLogicalSwitchPrivate. -func (mr *MockNbClientMockRecorder) SetLogicalSwitchPrivate(lsName, cidrBlock, allowSubnets interface{}) *gomock.Call { +func (mr *MockNbClientMockRecorder) SetLogicalSwitchPrivate(lsName, cidrBlock, nodeSwitchCIDR, allowSubnets interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogicalSwitchPrivate", reflect.TypeOf((*MockNbClient)(nil).SetLogicalSwitchPrivate), lsName, cidrBlock, allowSubnets) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogicalSwitchPrivate", reflect.TypeOf((*MockNbClient)(nil).SetLogicalSwitchPrivate), lsName, cidrBlock, nodeSwitchCIDR, allowSubnets) } // SetLsDnatModDlDst mocks base method. diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index f5373c9175c..de3332d6be7 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -312,7 +312,7 @@ func Run(ctx context.Context, config *Configuration) { vpcs: &sync.Map{}, podSubnetMap: &sync.Map{}, deletingPodObjMap: &sync.Map{}, - ovnLegacyClient: ovs.NewLegacyClient(config.OvnTimeout, config.OvnSbAddr, config.ClusterRouter, config.ClusterTcpLoadBalancer, config.ClusterUdpLoadBalancer, config.ClusterTcpSessionLoadBalancer, config.ClusterUdpSessionLoadBalancer, config.NodeSwitch, config.NodeSwitchCIDR), + ovnLegacyClient: ovs.NewLegacyClient(config.OvnTimeout), ipam: ovnipam.NewIPAM(), namedPort: NewNamedPort(), @@ -488,10 +488,10 @@ func Run(ctx context.Context, config *Configuration) { } var err error - if controller.ovnNbClient, err = ovs.NewOvnNbClient(config.OvnNbAddr, config.OvnTimeout, config.NodeSwitchCIDR); err != nil { + if controller.ovnNbClient, err = ovs.NewOvnNbClient(config.OvnNbAddr, config.OvnTimeout); err != nil { util.LogFatalAndExit(err, "failed to create ovn nb client") } - if controller.ovnSbClient, err = ovs.NewOvnSbClient(config.OvnSbAddr, config.OvnTimeout, config.NodeSwitchCIDR); err != nil { + if controller.ovnSbClient, err = ovs.NewOvnSbClient(config.OvnSbAddr, config.OvnTimeout); err != nil { util.LogFatalAndExit(err, "failed to create ovn sb client") } if config.EnableLb { diff --git a/pkg/controller/subnet.go b/pkg/controller/subnet.go index ad79cc78bd8..f02fa6cf684 100644 --- a/pkg/controller/subnet.go +++ b/pkg/controller/subnet.go @@ -817,7 +817,7 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error { } if subnet.Spec.Private { - if err := c.ovnNbClient.SetLogicalSwitchPrivate(subnet.Name, subnet.Spec.CIDRBlock, subnet.Spec.AllowSubnets); err != nil { + if err := c.ovnNbClient.SetLogicalSwitchPrivate(subnet.Name, subnet.Spec.CIDRBlock, c.config.NodeSwitchCIDR, subnet.Spec.AllowSubnets); err != nil { c.patchSubnetStatus(subnet, "SetPrivateLogicalSwitchFailed", err.Error()) return err } diff --git a/pkg/ovs/interface.go b/pkg/ovs/interface.go index 406f24bab4e..0d8e28f4401 100644 --- a/pkg/ovs/interface.go +++ b/pkg/ovs/interface.go @@ -109,7 +109,7 @@ type ACL interface { UpdateSgAcl(sg *kubeovnv1.SecurityGroup, direction string) error UpdateLogicalSwitchAcl(lsName string, subnetAcls []kubeovnv1.Acl) error SetAclLog(pgName, protocol string, logEnable, isIngress bool) error - SetLogicalSwitchPrivate(lsName, cidrBlock string, allowSubnets []string) error + SetLogicalSwitchPrivate(lsName, cidrBlock, nodeSwitchCIDR string, allowSubnets []string) error DeleteAcls(parentName, parentType string, direction string, externalIDs map[string]string) error DeleteAclsOps(parentName, parentType string, direction string, externalIDs map[string]string) ([]ovsdb.Operation, error) } diff --git a/pkg/ovs/ovn-nb-acl.go b/pkg/ovs/ovn-nb-acl.go index abe01ee4d2a..dc66cc90260 100644 --- a/pkg/ovs/ovn-nb-acl.go +++ b/pkg/ovs/ovn-nb-acl.go @@ -450,7 +450,7 @@ func (c *ovnNbClient) UpdateAcl(acl *ovnnb.ACL, fields ...interface{}) error { } // SetLogicalSwitchPrivate will drop all ingress traffic except allow subnets, same subnet and node subnet -func (c *ovnNbClient) SetLogicalSwitchPrivate(lsName, cidrBlock string, allowSubnets []string) error { +func (c *ovnNbClient) SetLogicalSwitchPrivate(lsName, cidrBlock, nodeSwitchCIDR string, allowSubnets []string) error { // clear acls if err := c.DeleteAcls(lsName, logicalSwitchKey, "", nil); err != nil { return fmt.Errorf("clear logical switch %s acls: %v", lsName, err) @@ -476,7 +476,7 @@ func (c *ovnNbClient) SetLogicalSwitchPrivate(lsName, cidrBlock string, allowSub acls = append(acls, defaultDropAcl) nodeSubnetAclFunc := func(protocol, ipSuffix string) error { - for _, nodeCidr := range strings.Split(c.NodeSwitchCIDR, ",") { + for _, nodeCidr := range strings.Split(nodeSwitchCIDR, ",") { // skip different address family if protocol != util.CheckProtocol(nodeCidr) { continue diff --git a/pkg/ovs/ovn-nb-acl_test.go b/pkg/ovs/ovn-nb-acl_test.go index c6155714160..069e43e4bc7 100644 --- a/pkg/ovs/ovn-nb-acl_test.go +++ b/pkg/ovs/ovn-nb-acl_test.go @@ -741,6 +741,7 @@ func (suite *OvnClientTestSuite) testSetLogicalSwitchPrivate() { ovnClient := suite.ovnClient + nodeSwitchCidrBlock := "100.64.0.0/16,fd00:100:64::/112" cidrBlock := "10.244.0.0/16,fc00::af4:0/112" allowSubnets := []string{ "10.230.0.0/16", @@ -757,7 +758,7 @@ func (suite *OvnClientTestSuite) testSetLogicalSwitchPrivate() { err := ovnClient.CreateBareLogicalSwitch(lsName) require.NoError(t, err) - err = ovnClient.SetLogicalSwitchPrivate(lsName, cidrBlock, allowSubnets) + err = ovnClient.SetLogicalSwitchPrivate(lsName, cidrBlock, nodeSwitchCidrBlock, allowSubnets) require.NoError(t, err) ls, err := ovnClient.GetLogicalSwitch(lsName, false) @@ -804,7 +805,7 @@ func (suite *OvnClientTestSuite) testSetLogicalSwitchPrivate() { } // node subnet acl - for _, cidr := range strings.Split(ovnClient.NodeSwitchCIDR, ",") { + for _, cidr := range strings.Split(nodeSwitchCidrBlock, ",") { protocol := util.CheckProtocol(cidr) match := fmt.Sprintf(`ip4.src == %s`, cidr) @@ -826,7 +827,7 @@ func (suite *OvnClientTestSuite) testSetLogicalSwitchPrivate() { require.NoError(t, err) cidrBlock := "10.244.0.0/16" - err = ovnClient.SetLogicalSwitchPrivate(lsName, cidrBlock, allowSubnets) + err = ovnClient.SetLogicalSwitchPrivate(lsName, cidrBlock, nodeSwitchCidrBlock, allowSubnets) require.NoError(t, err) ls, err := ovnClient.GetLogicalSwitch(lsName, false) @@ -873,7 +874,7 @@ func (suite *OvnClientTestSuite) testSetLogicalSwitchPrivate() { } // node subnet acl - for _, cidr := range strings.Split(ovnClient.NodeSwitchCIDR, ",") { + for _, cidr := range strings.Split(nodeSwitchCidrBlock, ",") { protocol := util.CheckProtocol(cidr) match := fmt.Sprintf(`ip4.src == %s`, cidr) diff --git a/pkg/ovs/ovn-nb-suite_test.go b/pkg/ovs/ovn-nb-suite_test.go index c9953374e49..16824262397 100644 --- a/pkg/ovs/ovn-nb-suite_test.go +++ b/pkg/ovs/ovn-nb-suite_test.go @@ -39,7 +39,7 @@ func (suite *OvnClientTestSuite) SetupSuite() { endpoint := fmt.Sprintf("unix:%s", sock) require.FileExists(suite.T(), sock) - ovnClient, err := newOvnNbClient(suite.T(), endpoint, 10, "100.64.0.0/16,fd00:100:64::/64") + ovnClient, err := newOvnNbClient(suite.T(), endpoint, 10) require.NoError(suite.T(), err) suite.ovnClient = ovnClient @@ -649,7 +649,7 @@ func (suite *OvnClientTestSuite) Test_GetEntityInfo() { func Test_scratch(t *testing.T) { t.SkipNow() endpoint := "tcp:[172.20.149.35]:6641" - ovnClient, err := newOvnNbClient(t, endpoint, 10, "") + ovnClient, err := newOvnNbClient(t, endpoint, 10) require.NoError(t, err) err = ovnClient.DeleteAcls("test_pg", portGroupKey, ovnnb.ACLDirectionToLport, nil) @@ -692,7 +692,7 @@ func newOVSDBServer(t *testing.T, dbModel model.ClientDBModel, schema ovsdb.Data return server, tmpfile } -func newOvnNbClient(t *testing.T, ovnNbAddr string, ovnNbTimeout int, nodeSwitchCIDR string) (*ovnNbClient, error) { +func newOvnNbClient(t *testing.T, ovnNbAddr string, ovnNbTimeout int) (*ovnNbClient, error) { nbClient, err := newNbClient(ovnNbAddr, ovnNbTimeout) require.NoError(t, err) @@ -701,7 +701,6 @@ func newOvnNbClient(t *testing.T, ovnNbAddr string, ovnNbTimeout int, nodeSwitch Client: nbClient, Timeout: time.Duration(ovnNbTimeout) * time.Second, }, - NodeSwitchCIDR: nodeSwitchCIDR, }, nil } diff --git a/pkg/ovs/ovn.go b/pkg/ovs/ovn.go index 8b38b02e88d..84afd459a1c 100644 --- a/pkg/ovs/ovn.go +++ b/pkg/ovs/ovn.go @@ -17,23 +17,14 @@ import ( // LegacyClient is the legacy ovn client type LegacyClient struct { - OvnTimeout int - OvnICNbAddress string - OvnICSbAddress string - ClusterRouter string - ClusterTcpLoadBalancer string - ClusterUdpLoadBalancer string - ClusterTcpSessionLoadBalancer string - ClusterUdpSessionLoadBalancer string - NodeSwitch string - NodeSwitchCIDR string - Version string + OvnTimeout int + OvnICNbAddress string + OvnICSbAddress string } type ovnNbClient struct { ovsDbClient - ClusterRouter string - NodeSwitchCIDR string + ClusterRouter string } type ovnSbClient struct { @@ -56,16 +47,9 @@ const ( ) // NewLegacyClient init a legacy ovn client -func NewLegacyClient(timeout int, ovnSbAddr, clusterRouter, clusterTcpLoadBalancer, clusterUdpLoadBalancer, clusterTcpSessionLoadBalancer, clusterUdpSessionLoadBalancer, nodeSwitch, nodeSwitchCIDR string) *LegacyClient { +func NewLegacyClient(timeout int) *LegacyClient { return &LegacyClient{ - OvnTimeout: timeout, - ClusterRouter: clusterRouter, - ClusterTcpLoadBalancer: clusterTcpLoadBalancer, - ClusterUdpLoadBalancer: clusterUdpLoadBalancer, - ClusterTcpSessionLoadBalancer: clusterTcpSessionLoadBalancer, - ClusterUdpSessionLoadBalancer: clusterUdpSessionLoadBalancer, - NodeSwitch: nodeSwitch, - NodeSwitchCIDR: nodeSwitchCIDR, + OvnTimeout: timeout, } } @@ -104,12 +88,11 @@ func NewOvnNbClient(ovnNbAddr string, ovnNbTimeout int) (*ovnNbClient, error) { Client: nbClient, Timeout: time.Duration(ovnNbTimeout) * time.Second, }, - NodeSwitchCIDR: nodeSwitchCIDR, } return c, nil } -func NewOvnSbClient(ovnSbAddr string, ovnSbTimeout int, nodeSwitchCIDR string) (*ovnSbClient, error) { +func NewOvnSbClient(ovnSbAddr string, ovnSbTimeout int) (*ovnSbClient, error) { dbModel, err := ovnsb.FullDatabaseModel() if err != nil { klog.Error(err) diff --git a/pkg/ovsdb/client/client.go b/pkg/ovsdb/client/client.go index 6b771aac8f1..38371fa204c 100644 --- a/pkg/ovsdb/client/client.go +++ b/pkg/ovsdb/client/client.go @@ -58,8 +58,6 @@ func NewOvsDbClient(db, addr string, dbModel model.ClientDBModel, monitors []cli } options = append(options, client.WithEndpoint(ep)) } - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(len(endpoints)+1)*timeout) - defer cancel() if ssl { cert, err := tls.LoadX509KeyPair("/var/run/tls/cert", "/var/run/tls/key") if err != nil { @@ -81,12 +79,13 @@ func NewOvsDbClient(db, addr string, dbModel model.ClientDBModel, monitors []cli } options = append(options, client.WithTLSConfig(tlsConfig)) } - c, err := client.NewOVSDBClient(dbModel, options...) if err != nil { klog.Error(err) return nil, err } + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(len(endpoints)+1)*timeout) + defer cancel() if err = c.Connect(ctx); err != nil { klog.Errorf("failed to connect to OVN NB server %s: %v", addr, err) return nil, err