diff --git a/go.mod b/go.mod index 3b95426bcbd..cdb8659bd9c 100644 --- a/go.mod +++ b/go.mod @@ -35,7 +35,6 @@ require ( github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.2 github.com/vishvananda/netlink v1.2.1-beta.2 - golang.org/x/exp v0.0.0-20230321023759-10a507213a29 golang.org/x/sys v0.8.0 golang.org/x/time v0.3.0 google.golang.org/grpc v1.55.0 diff --git a/go.sum b/go.sum index fd2a1d74966..5a4c8730ec6 100644 --- a/go.sum +++ b/go.sum @@ -1533,8 +1533,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= -golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug= -golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/mocks/pkg/ovs/interface.go b/mocks/pkg/ovs/interface.go index ceca5924979..53aca1f8f9f 100644 --- a/mocks/pkg/ovs/interface.go +++ b/mocks/pkg/ovs/interface.go @@ -352,6 +352,21 @@ func (mr *MockLogicalRouterPortMockRecorder) GetLogicalRouterPort(lrpName, ignor return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLogicalRouterPort", reflect.TypeOf((*MockLogicalRouterPort)(nil).GetLogicalRouterPort), lrpName, ignoreNotFound) } +// GetLogicalRouterPortByUUID mocks base method. +func (m *MockLogicalRouterPort) GetLogicalRouterPortByUUID(uuid string) (*ovnnb.LogicalRouterPort, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetLogicalRouterPortByUUID", uuid) + ret0, _ := ret[0].(*ovnnb.LogicalRouterPort) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetLogicalRouterPortByUUID indicates an expected call of GetLogicalRouterPortByUUID. +func (mr *MockLogicalRouterPortMockRecorder) GetLogicalRouterPortByUUID(uuid interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLogicalRouterPortByUUID", reflect.TypeOf((*MockLogicalRouterPort)(nil).GetLogicalRouterPortByUUID), uuid) +} + // ListLogicalRouterPorts mocks base method. func (m *MockLogicalRouterPort) ListLogicalRouterPorts(externalIDs map[string]string, filter func(*ovnnb.LogicalRouterPort) bool) ([]ovnnb.LogicalRouterPort, error) { m.ctrl.T.Helper() @@ -1421,34 +1436,34 @@ func (mr *MockLogicalRouterStaticRouteMockRecorder) DeleteLogicalRouterStaticRou return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteLogicalRouterStaticRoute", reflect.TypeOf((*MockLogicalRouterStaticRoute)(nil).DeleteLogicalRouterStaticRoute), lrName, policy, prefix, nextHop, routeType) } -// GetLogicalRouterRouteByOpts mocks base method. -func (m *MockLogicalRouterStaticRoute) GetLogicalRouterRouteByOpts(key, value string) ([]ovnnb.LogicalRouterStaticRoute, error) { +// ListLogicalRouterStaticRoutes mocks base method. +func (m *MockLogicalRouterStaticRoute) ListLogicalRouterStaticRoutes(lrName string, externalIDs map[string]string) ([]*ovnnb.LogicalRouterStaticRoute, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetLogicalRouterRouteByOpts", key, value) - ret0, _ := ret[0].([]ovnnb.LogicalRouterStaticRoute) + ret := m.ctrl.Call(m, "ListLogicalRouterStaticRoutes", lrName, externalIDs) + ret0, _ := ret[0].([]*ovnnb.LogicalRouterStaticRoute) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetLogicalRouterRouteByOpts indicates an expected call of GetLogicalRouterRouteByOpts. -func (mr *MockLogicalRouterStaticRouteMockRecorder) GetLogicalRouterRouteByOpts(key, value interface{}) *gomock.Call { +// ListLogicalRouterStaticRoutes indicates an expected call of ListLogicalRouterStaticRoutes. +func (mr *MockLogicalRouterStaticRouteMockRecorder) ListLogicalRouterStaticRoutes(lrName, externalIDs interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLogicalRouterRouteByOpts", reflect.TypeOf((*MockLogicalRouterStaticRoute)(nil).GetLogicalRouterRouteByOpts), key, value) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLogicalRouterStaticRoutes", reflect.TypeOf((*MockLogicalRouterStaticRoute)(nil).ListLogicalRouterStaticRoutes), lrName, externalIDs) } -// ListLogicalRouterStaticRoutes mocks base method. -func (m *MockLogicalRouterStaticRoute) ListLogicalRouterStaticRoutes(externalIDs map[string]string) ([]ovnnb.LogicalRouterStaticRoute, error) { +// ListLogicalRouterStaticRoutesByOption mocks base method. +func (m *MockLogicalRouterStaticRoute) ListLogicalRouterStaticRoutesByOption(lrName, key, value string) ([]*ovnnb.LogicalRouterStaticRoute, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListLogicalRouterStaticRoutes", externalIDs) - ret0, _ := ret[0].([]ovnnb.LogicalRouterStaticRoute) + ret := m.ctrl.Call(m, "ListLogicalRouterStaticRoutesByOption", lrName, key, value) + ret0, _ := ret[0].([]*ovnnb.LogicalRouterStaticRoute) ret1, _ := ret[1].(error) return ret0, ret1 } -// ListLogicalRouterStaticRoutes indicates an expected call of ListLogicalRouterStaticRoutes. -func (mr *MockLogicalRouterStaticRouteMockRecorder) ListLogicalRouterStaticRoutes(externalIDs interface{}) *gomock.Call { +// ListLogicalRouterStaticRoutesByOption indicates an expected call of ListLogicalRouterStaticRoutesByOption. +func (mr *MockLogicalRouterStaticRouteMockRecorder) ListLogicalRouterStaticRoutesByOption(lrName, key, value interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLogicalRouterStaticRoutes", reflect.TypeOf((*MockLogicalRouterStaticRoute)(nil).ListLogicalRouterStaticRoutes), externalIDs) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLogicalRouterStaticRoutesByOption", reflect.TypeOf((*MockLogicalRouterStaticRoute)(nil).ListLogicalRouterStaticRoutesByOption), lrName, key, value) } // LogicalRouterStaticRouteExists mocks base method. @@ -1545,6 +1560,20 @@ func (mr *MockLogicalRouterPolicyMockRecorder) DeleteLogicalRouterPolicy(lrName, return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteLogicalRouterPolicy", reflect.TypeOf((*MockLogicalRouterPolicy)(nil).DeleteLogicalRouterPolicy), lrName, priority, match) } +// DeleteLogicalRouterPolicyByNexthop mocks base method. +func (m *MockLogicalRouterPolicy) DeleteLogicalRouterPolicyByNexthop(lrName string, priority int, nexthop string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteLogicalRouterPolicyByNexthop", lrName, priority, nexthop) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteLogicalRouterPolicyByNexthop indicates an expected call of DeleteLogicalRouterPolicyByNexthop. +func (mr *MockLogicalRouterPolicyMockRecorder) DeleteLogicalRouterPolicyByNexthop(lrName, priority, nexthop interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteLogicalRouterPolicyByNexthop", reflect.TypeOf((*MockLogicalRouterPolicy)(nil).DeleteLogicalRouterPolicyByNexthop), lrName, priority, nexthop) +} + // DeleteLogicalRouterPolicyByUUID mocks base method. func (m *MockLogicalRouterPolicy) DeleteLogicalRouterPolicyByUUID(lrName, uuid string) error { m.ctrl.T.Helper() @@ -1575,18 +1604,18 @@ func (mr *MockLogicalRouterPolicyMockRecorder) GetLogicalRouterPolicy(lrName, pr } // ListLogicalRouterPolicies mocks base method. -func (m *MockLogicalRouterPolicy) ListLogicalRouterPolicies(priority int, externalIDs map[string]string) ([]ovnnb.LogicalRouterPolicy, error) { +func (m *MockLogicalRouterPolicy) ListLogicalRouterPolicies(lrName string, priority int, externalIDs map[string]string) ([]*ovnnb.LogicalRouterPolicy, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListLogicalRouterPolicies", priority, externalIDs) - ret0, _ := ret[0].([]ovnnb.LogicalRouterPolicy) + ret := m.ctrl.Call(m, "ListLogicalRouterPolicies", lrName, priority, externalIDs) + ret0, _ := ret[0].([]*ovnnb.LogicalRouterPolicy) ret1, _ := ret[1].(error) return ret0, ret1 } // ListLogicalRouterPolicies indicates an expected call of ListLogicalRouterPolicies. -func (mr *MockLogicalRouterPolicyMockRecorder) ListLogicalRouterPolicies(priority, externalIDs interface{}) *gomock.Call { +func (mr *MockLogicalRouterPolicyMockRecorder) ListLogicalRouterPolicies(lrName, priority, externalIDs interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLogicalRouterPolicies", reflect.TypeOf((*MockLogicalRouterPolicy)(nil).ListLogicalRouterPolicies), priority, externalIDs) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLogicalRouterPolicies", reflect.TypeOf((*MockLogicalRouterPolicy)(nil).ListLogicalRouterPolicies), lrName, priority, externalIDs) } // MockNAT is a mock of NAT interface. @@ -1641,18 +1670,18 @@ func (mr *MockNATMockRecorder) DeleteNats(lrName, natType, logicalIP interface{} } // ListNats mocks base method. -func (m *MockNAT) ListNats(natType, logicalIP string, externalIDs map[string]string) ([]ovnnb.NAT, error) { +func (m *MockNAT) ListNats(lrName, natType, logicalIP string, externalIDs map[string]string) ([]*ovnnb.NAT, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListNats", natType, logicalIP, externalIDs) - ret0, _ := ret[0].([]ovnnb.NAT) + ret := m.ctrl.Call(m, "ListNats", lrName, natType, logicalIP, externalIDs) + ret0, _ := ret[0].([]*ovnnb.NAT) ret1, _ := ret[1].(error) return ret0, ret1 } // ListNats indicates an expected call of ListNats. -func (mr *MockNATMockRecorder) ListNats(natType, logicalIP, externalIDs interface{}) *gomock.Call { +func (mr *MockNATMockRecorder) ListNats(lrName, natType, logicalIP, externalIDs interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListNats", reflect.TypeOf((*MockNAT)(nil).ListNats), natType, logicalIP, externalIDs) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListNats", reflect.TypeOf((*MockNAT)(nil).ListNats), lrName, natType, logicalIP, externalIDs) } // NatExists mocks base method. @@ -2307,6 +2336,20 @@ func (mr *MockOvnClientMockRecorder) DeleteLogicalRouterPolicy(lrName, priority, return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteLogicalRouterPolicy", reflect.TypeOf((*MockOvnClient)(nil).DeleteLogicalRouterPolicy), lrName, priority, match) } +// DeleteLogicalRouterPolicyByNexthop mocks base method. +func (m *MockOvnClient) DeleteLogicalRouterPolicyByNexthop(lrName string, priority int, nexthop string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteLogicalRouterPolicyByNexthop", lrName, priority, nexthop) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteLogicalRouterPolicyByNexthop indicates an expected call of DeleteLogicalRouterPolicyByNexthop. +func (mr *MockOvnClientMockRecorder) DeleteLogicalRouterPolicyByNexthop(lrName, priority, nexthop interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteLogicalRouterPolicyByNexthop", reflect.TypeOf((*MockOvnClient)(nil).DeleteLogicalRouterPolicyByNexthop), lrName, priority, nexthop) +} + // DeleteLogicalRouterPolicyByUUID mocks base method. func (m *MockOvnClient) DeleteLogicalRouterPolicyByUUID(lrName, uuid string) error { m.ctrl.T.Helper() @@ -2535,19 +2578,19 @@ func (mr *MockOvnClientMockRecorder) GetLogicalRouterPort(lrpName, ignoreNotFoun return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLogicalRouterPort", reflect.TypeOf((*MockOvnClient)(nil).GetLogicalRouterPort), lrpName, ignoreNotFound) } -// GetLogicalRouterRouteByOpts mocks base method. -func (m *MockOvnClient) GetLogicalRouterRouteByOpts(key, value string) ([]ovnnb.LogicalRouterStaticRoute, error) { +// GetLogicalRouterPortByUUID mocks base method. +func (m *MockOvnClient) GetLogicalRouterPortByUUID(uuid string) (*ovnnb.LogicalRouterPort, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetLogicalRouterRouteByOpts", key, value) - ret0, _ := ret[0].([]ovnnb.LogicalRouterStaticRoute) + ret := m.ctrl.Call(m, "GetLogicalRouterPortByUUID", uuid) + ret0, _ := ret[0].(*ovnnb.LogicalRouterPort) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetLogicalRouterRouteByOpts indicates an expected call of GetLogicalRouterRouteByOpts. -func (mr *MockOvnClientMockRecorder) GetLogicalRouterRouteByOpts(key, value interface{}) *gomock.Call { +// GetLogicalRouterPortByUUID indicates an expected call of GetLogicalRouterPortByUUID. +func (mr *MockOvnClientMockRecorder) GetLogicalRouterPortByUUID(uuid interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLogicalRouterRouteByOpts", reflect.TypeOf((*MockOvnClient)(nil).GetLogicalRouterRouteByOpts), key, value) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLogicalRouterPortByUUID", reflect.TypeOf((*MockOvnClient)(nil).GetLogicalRouterPortByUUID), uuid) } // GetLogicalSwitchPort mocks base method. @@ -2656,18 +2699,18 @@ func (mr *MockOvnClientMockRecorder) ListLogicalRouter(needVendorFilter, filter } // ListLogicalRouterPolicies mocks base method. -func (m *MockOvnClient) ListLogicalRouterPolicies(priority int, externalIDs map[string]string) ([]ovnnb.LogicalRouterPolicy, error) { +func (m *MockOvnClient) ListLogicalRouterPolicies(lrName string, priority int, externalIDs map[string]string) ([]*ovnnb.LogicalRouterPolicy, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListLogicalRouterPolicies", priority, externalIDs) - ret0, _ := ret[0].([]ovnnb.LogicalRouterPolicy) + ret := m.ctrl.Call(m, "ListLogicalRouterPolicies", lrName, priority, externalIDs) + ret0, _ := ret[0].([]*ovnnb.LogicalRouterPolicy) ret1, _ := ret[1].(error) return ret0, ret1 } // ListLogicalRouterPolicies indicates an expected call of ListLogicalRouterPolicies. -func (mr *MockOvnClientMockRecorder) ListLogicalRouterPolicies(priority, externalIDs interface{}) *gomock.Call { +func (mr *MockOvnClientMockRecorder) ListLogicalRouterPolicies(lrName, priority, externalIDs interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLogicalRouterPolicies", reflect.TypeOf((*MockOvnClient)(nil).ListLogicalRouterPolicies), priority, externalIDs) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLogicalRouterPolicies", reflect.TypeOf((*MockOvnClient)(nil).ListLogicalRouterPolicies), lrName, priority, externalIDs) } // ListLogicalRouterPorts mocks base method. @@ -2686,18 +2729,33 @@ func (mr *MockOvnClientMockRecorder) ListLogicalRouterPorts(externalIDs, filter } // ListLogicalRouterStaticRoutes mocks base method. -func (m *MockOvnClient) ListLogicalRouterStaticRoutes(externalIDs map[string]string) ([]ovnnb.LogicalRouterStaticRoute, error) { +func (m *MockOvnClient) ListLogicalRouterStaticRoutes(lrName string, externalIDs map[string]string) ([]*ovnnb.LogicalRouterStaticRoute, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListLogicalRouterStaticRoutes", externalIDs) - ret0, _ := ret[0].([]ovnnb.LogicalRouterStaticRoute) + ret := m.ctrl.Call(m, "ListLogicalRouterStaticRoutes", lrName, externalIDs) + ret0, _ := ret[0].([]*ovnnb.LogicalRouterStaticRoute) ret1, _ := ret[1].(error) return ret0, ret1 } // ListLogicalRouterStaticRoutes indicates an expected call of ListLogicalRouterStaticRoutes. -func (mr *MockOvnClientMockRecorder) ListLogicalRouterStaticRoutes(externalIDs interface{}) *gomock.Call { +func (mr *MockOvnClientMockRecorder) ListLogicalRouterStaticRoutes(lrName, externalIDs interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLogicalRouterStaticRoutes", reflect.TypeOf((*MockOvnClient)(nil).ListLogicalRouterStaticRoutes), lrName, externalIDs) +} + +// ListLogicalRouterStaticRoutesByOption mocks base method. +func (m *MockOvnClient) ListLogicalRouterStaticRoutesByOption(lrName, key, value string) ([]*ovnnb.LogicalRouterStaticRoute, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListLogicalRouterStaticRoutesByOption", lrName, key, value) + ret0, _ := ret[0].([]*ovnnb.LogicalRouterStaticRoute) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListLogicalRouterStaticRoutesByOption indicates an expected call of ListLogicalRouterStaticRoutesByOption. +func (mr *MockOvnClientMockRecorder) ListLogicalRouterStaticRoutesByOption(lrName, key, value interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLogicalRouterStaticRoutes", reflect.TypeOf((*MockOvnClient)(nil).ListLogicalRouterStaticRoutes), externalIDs) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLogicalRouterStaticRoutesByOption", reflect.TypeOf((*MockOvnClient)(nil).ListLogicalRouterStaticRoutesByOption), lrName, key, value) } // ListLogicalSwitch mocks base method. @@ -2746,18 +2804,18 @@ func (mr *MockOvnClientMockRecorder) ListLogicalSwitchPortsWithLegacyExternalIDs } // ListNats mocks base method. -func (m *MockOvnClient) ListNats(natType, logicalIP string, externalIDs map[string]string) ([]ovnnb.NAT, error) { +func (m *MockOvnClient) ListNats(lrName, natType, logicalIP string, externalIDs map[string]string) ([]*ovnnb.NAT, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListNats", natType, logicalIP, externalIDs) - ret0, _ := ret[0].([]ovnnb.NAT) + ret := m.ctrl.Call(m, "ListNats", lrName, natType, logicalIP, externalIDs) + ret0, _ := ret[0].([]*ovnnb.NAT) ret1, _ := ret[1].(error) return ret0, ret1 } // ListNats indicates an expected call of ListNats. -func (mr *MockOvnClientMockRecorder) ListNats(natType, logicalIP, externalIDs interface{}) *gomock.Call { +func (mr *MockOvnClientMockRecorder) ListNats(lrName, natType, logicalIP, externalIDs interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListNats", reflect.TypeOf((*MockOvnClient)(nil).ListNats), natType, logicalIP, externalIDs) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListNats", reflect.TypeOf((*MockOvnClient)(nil).ListNats), lrName, natType, logicalIP, externalIDs) } // ListNormalLogicalSwitchPorts mocks base method. diff --git a/pkg/apis/kubeovn/v1/types.go b/pkg/apis/kubeovn/v1/types.go index cbb616d45f5..ea2b2dce36d 100644 --- a/pkg/apis/kubeovn/v1/types.go +++ b/pkg/apis/kubeovn/v1/types.go @@ -413,7 +413,7 @@ var ( ) type PolicyRoute struct { - Priority int32 `json:"priority,omitempty"` + Priority int `json:"priority,omitempty"` Match string `json:"match,omitempty"` Action PolicyRouteAction `json:"action,omitempty"` // NextHopIP is an optional parameter. It needs to be provided only when 'action' is 'reroute'. diff --git a/pkg/controller/external_vpc.go b/pkg/controller/external_vpc.go index 04b1988dddc..b5bc07471db 100644 --- a/pkg/controller/external_vpc.go +++ b/pkg/controller/external_vpc.go @@ -2,13 +2,13 @@ package controller import ( "context" - "fmt" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/klog/v2" v1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1" + "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb" "github.com/kubeovn/kube-ovn/pkg/util" ) @@ -83,7 +83,9 @@ func (c *Controller) syncExternalVpc() { func (c *Controller) getRouterStatus() (logicalRouters map[string]util.LogicalRouter, err error) { logicalRouters = make(map[string]util.LogicalRouter) - externalOvnRouters, err := c.ovnLegacyClient.CustomFindEntity("logical_router", []string{"name", "port"}, fmt.Sprintf("external_ids{!=}vendor=%s", util.CniTypeName)) + externalOvnRouters, err := c.ovnClient.ListLogicalRouter(false, func(lr *ovnnb.LogicalRouter) bool { + return len(lr.ExternalIDs) == 0 || lr.ExternalIDs["vendor"] != util.CniTypeName + }) if err != nil { klog.Errorf("failed to list external logical router, %v", err) return logicalRouters, err @@ -93,30 +95,27 @@ func (c *Controller) getRouterStatus() (logicalRouters map[string]util.LogicalRo return logicalRouters, nil } - for _, aExternalRouter := range externalOvnRouters { - var aLogicalRouter util.LogicalRouter - aLogicalRouter.Name = aExternalRouter["name"][0] - var ports []util.Port - for _, portUUId := range aExternalRouter["port"] { - portName, err := c.ovnLegacyClient.GetEntityInfo("logical_router_port", portUUId, []string{"name"}) + for _, externalLR := range externalOvnRouters { + lr := util.LogicalRouter{ + Name: externalLR.Name, + Ports: make([]util.Port, 0, len(externalLR.Ports)), + } + for _, uuid := range externalLR.Ports { + lrp, err := c.ovnClient.GetLogicalRouterPortByUUID(uuid) if err != nil { - klog.Info("get port error") + klog.Warningf("failed to get LRP by UUID %s: %v", uuid, err) continue } - aPort := util.Port{ - Name: portName["name"], - Subnet: "", - } - ports = append(ports, aPort) + lr.Ports = append(lr.Ports, util.Port{Name: lrp.Name}) } - aLogicalRouter.Ports = ports - logicalRouters[aLogicalRouter.Name] = aLogicalRouter + logicalRouters[lr.Name] = lr } - UUID := "_uuid" for routerName, logicalRouter := range logicalRouters { tmpRouter := logicalRouter for _, port := range logicalRouter.Ports { - peerPorts, err := c.ovnLegacyClient.CustomFindEntity("logical_switch_port", []string{UUID}, fmt.Sprintf("options:router-port=%s", port.Name)) + peerPorts, err := c.ovnClient.ListLogicalSwitchPorts(false, nil, func(lsp *ovnnb.LogicalSwitchPort) bool { + return len(lsp.Options) != 0 && lsp.Options["router-port"] == port.Name + }) if err != nil || len(peerPorts) > 1 { klog.Errorf("failed to list peer port of %s, %v", port, err) continue @@ -124,13 +123,16 @@ func (c *Controller) getRouterStatus() (logicalRouters map[string]util.LogicalRo if len(peerPorts) == 0 { continue } - switches, err := c.ovnLegacyClient.CustomFindEntity("logical_switch", []string{"name"}, fmt.Sprintf("ports{>=}%s", peerPorts[0][UUID][0])) + lsp := peerPorts[0] + switches, err := c.ovnClient.ListLogicalSwitch(false, func(ls *ovnnb.LogicalSwitch) bool { + return util.ContainsString(ls.Ports, lsp.UUID) + }) if err != nil || len(switches) > 1 { - klog.Errorf("failed to list peer switch of %s, %v", peerPorts, err) + klog.Errorf("failed to get logical switch of LSP %s: %v", lsp.Name, err) continue } var aLogicalSwitch util.LogicalSwitch - aLogicalSwitch.Name = switches[0]["name"][0] + aLogicalSwitch.Name = switches[0].Name tmpRouter.LogicalSwitches = append(tmpRouter.LogicalSwitches, aLogicalSwitch) } logicalRouters[routerName] = tmpRouter diff --git a/pkg/controller/init.go b/pkg/controller/init.go index 26cb764887b..3f3d54c8d36 100644 --- a/pkg/controller/init.go +++ b/pkg/controller/init.go @@ -744,15 +744,16 @@ func (c *Controller) initSyncCrdVlans() error { } func (c *Controller) migrateNodeRoute(af int, node, ip, nexthop string) error { - // migrate from old version static route to policy route match := fmt.Sprintf("ip%d.dst == %s", af, ip) - consistent, err := c.ovnLegacyClient.CheckPolicyRouteNexthopConsistent(match, nexthop, util.NodeRouterPolicyPriority) - if err != nil { - return err + externalIDs := map[string]string{ + "vendor": util.CniTypeName, + "node": node, } - if consistent { - klog.V(3).Infof("node policy route migrated") - return nil + klog.V(3).Infof("add policy route for router: %s, priority: %d, match %s, action %s, nexthop %s, extrenalID %v", + c.config.ClusterRouter, util.NodeRouterPolicyPriority, match, "reroute", nexthop, externalIDs) + if err := c.ovnClient.AddLogicalRouterPolicy(c.config.ClusterRouter, util.NodeRouterPolicyPriority, match, "reroute", []string{nexthop}, externalIDs); err != nil { + klog.Errorf("failed to add logical router policy for node %s: %v", node, err) + return err } routeTables, err := c.ovnLegacyClient.GetRouteTables(c.config.ClusterRouter) @@ -769,8 +770,8 @@ func (c *Controller) migrateNodeRoute(af int, node, ip, nexthop string) error { asName := nodeUnderlayAddressSetName(node, af) obsoleteMatch := fmt.Sprintf("ip%d.dst == %s && ip%d.src != $%s", af, ip, af, asName) - klog.Infof("delete policy route for router: %s, priority: %d, match %s", c.config.ClusterRouter, util.NodeRouterPolicyPriority, obsoleteMatch) - if err := c.ovnLegacyClient.DeletePolicyRoute(c.config.ClusterRouter, util.NodeRouterPolicyPriority, obsoleteMatch); err != nil { + klog.V(3).Infof("delete policy route for router: %s, priority: %d, match %s", c.config.ClusterRouter, util.NodeRouterPolicyPriority, obsoleteMatch) + if err := c.ovnClient.DeleteLogicalRouterPolicy(c.config.ClusterRouter, util.NodeRouterPolicyPriority, obsoleteMatch); err != nil { klog.Errorf("failed to delete obsolete logical router policy for node %s: %v", node, err) return err } @@ -780,17 +781,6 @@ func (c *Controller) migrateNodeRoute(af int, node, ip, nexthop string) error { return err } - externalIDs := map[string]string{ - "vendor": util.CniTypeName, - "node": node, - } - klog.Infof("add policy route for router: %s, priority: %d, match %s, action %s, nexthop %s, extrenalID %v", - c.config.ClusterRouter, util.NodeRouterPolicyPriority, match, "reroute", nexthop, externalIDs) - if err := c.ovnLegacyClient.AddPolicyRoute(c.config.ClusterRouter, util.NodeRouterPolicyPriority, match, "reroute", nexthop, externalIDs); err != nil { - klog.Errorf("failed to add logical router policy for node %s: %v", node, err) - return err - } - return nil } diff --git a/pkg/controller/node.go b/pkg/controller/node.go index 71e69c6e9bc..7f838b03a1c 100644 --- a/pkg/controller/node.go +++ b/pkg/controller/node.go @@ -11,6 +11,7 @@ import ( "time" goping "github.com/oilbeater/go-ping" + "github.com/scylladb/go-set/strset" v1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -277,12 +278,12 @@ func (c *Controller) handleAddNode(key string) error { "address-family": strconv.Itoa(af), } klog.Infof("add policy route for router: %s, match %s, action %s, nexthop %s, externalID %v", c.config.ClusterRouter, match, "reroute", ip, externalIDs) - if err = c.ovnLegacyClient.AddPolicyRoute(c.config.ClusterRouter, util.NodeRouterPolicyPriority, match, "reroute", ip, externalIDs); err != nil { + if err = c.ovnClient.AddLogicalRouterPolicy(c.config.ClusterRouter, util.NodeRouterPolicyPriority, match, "reroute", []string{ip}, externalIDs); err != nil { klog.Errorf("failed to add logical router policy for node %s: %v", node.Name, err) return err } - if err = c.deletePolicyRouteForLocalDnsCacheOnNode(portName, node.Name, af); err != nil { + if err = c.deletePolicyRouteForLocalDnsCacheOnNode(node.Name, af); err != nil { return err } @@ -478,7 +479,7 @@ func (c *Controller) handleDeleteNode(key string) error { afs := []int{4, 6} for _, af := range afs { - if err := c.deletePolicyRouteForLocalDnsCacheOnNode(portName, key, af); err != nil { + if err := c.deletePolicyRouteForLocalDnsCacheOnNode(key, af); err != nil { return err } } @@ -500,7 +501,7 @@ func (c *Controller) handleDeleteNode(key string) error { if addr.Ip == "" { continue } - if err := c.ovnLegacyClient.DeletePolicyRouteByNexthop(c.config.ClusterRouter, util.NodeRouterPolicyPriority, addr.Ip); err != nil { + if err := c.ovnClient.DeleteLogicalRouterPolicyByNexthop(c.config.ClusterRouter, util.NodeRouterPolicyPriority, addr.Ip); err != nil { klog.Errorf("failed to delete router policy for node %s: %v", key, err) return err } @@ -815,10 +816,10 @@ func (c *Controller) checkGatewayReady() error { if !success { if exist { klog.Warningf("failed to ping ovn0 %s or node %s is not ready, delete ecmp policy route for node", ip, node.Name) - nextHops = util.RemoveString(nextHops, ip) + nextHops.Remove(ip) delete(nameIpMap, node.Name) klog.Infof("update policy route for centralized subnet %s, nextHops %s", subnet.Name, nextHops) - if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, cidrBlock, nextHops, nameIpMap); err != nil { + if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, cidrBlock, nextHops.List(), nameIpMap); err != nil { klog.Errorf("failed to delete ecmp policy route for subnet %s on node %s, %v", subnet.Name, node.Name, err) return err } @@ -826,13 +827,13 @@ func (c *Controller) checkGatewayReady() error { } else { klog.V(3).Infof("succeed to ping gw %s", ip) if !exist { - nextHops = append(nextHops, ip) + nextHops.Add(ip) if nameIpMap == nil { nameIpMap = make(map[string]string, 1) } nameIpMap[node.Name] = ip klog.Infof("update policy route for centralized subnet %s, nextHops %s", subnet.Name, nextHops) - if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, cidrBlock, nextHops, nameIpMap); err != nil { + if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, cidrBlock, nextHops.List(), nameIpMap); err != nil { klog.Errorf("failed to add ecmp policy route for subnet %s on node %s, %v", subnet.Name, node.Name, err) return err } @@ -841,10 +842,10 @@ func (c *Controller) checkGatewayReady() error { } else { if exist { klog.Infof("subnet %s gatewayNode does not contains node %v, delete policy route for node ip %s", subnet.Name, node.Name, ip) - nextHops = util.RemoveString(nextHops, ip) + nextHops.Remove(ip) delete(nameIpMap, node.Name) klog.Infof("update policy route for centralized subnet %s, nextHops %s", subnet.Name, nextHops) - if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, cidrBlock, nextHops, nameIpMap); err != nil { + if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, cidrBlock, nextHops.List(), nameIpMap); err != nil { klog.Errorf("failed to delete ecmp policy route for subnet %s on node %s, %v", subnet.Name, node.Name, err) return err } @@ -1083,22 +1084,24 @@ func (c *Controller) addNodeGwStaticRoute() error { return nil } -func (c *Controller) getPolicyRouteParas(cidr string, priority int32) ([]string, map[string]string, error) { - // TODO:// support get policy route by vpc +func (c *Controller) getPolicyRouteParas(cidr string, priority int) (*strset.Set, map[string]string, error) { ipSuffix := "ip4" if util.CheckProtocol(cidr) == kubeovnv1.ProtocolIPv6 { ipSuffix = "ip6" } match := fmt.Sprintf("%s.src == %s", ipSuffix, cidr) - nextHops, nameIpMap, err := c.ovnLegacyClient.GetPolicyRouteParas(priority, match) + policy, err := c.ovnClient.GetLogicalRouterPolicy(c.config.ClusterRouter, priority, match, true) if err != nil { - klog.Errorf("failed to get policy route paras, %v", err) - return nextHops, nameIpMap, err + klog.Errorf("failed to get logical router policy: %v", err) + return nil, nil, err + } + if policy == nil { + return nil, nil, err } - return nextHops, nameIpMap, nil + return strset.New(policy.Nexthops...), policy.ExternalIDs, nil } -func (c *Controller) checkPolicyRouteExistForNode(nodeName, cidr, nexthop string, priority int32) (bool, error) { +func (c *Controller) checkPolicyRouteExistForNode(nodeName, cidr, nexthop string, priority int) (bool, error) { _, nameIpMap, err := c.getPolicyRouteParas(cidr, priority) if err != nil { klog.Errorf("failed to get policy route paras, %v", err) @@ -1151,10 +1154,10 @@ func (c *Controller) deletePolicyRouteForNode(nodeName string) error { } if exist { - nextHops = util.RemoveString(nextHops, nameIpMap[nodeName]) + nextHops.Remove(nameIpMap[nodeName]) delete(nameIpMap, nodeName) - if len(nextHops) == 0 { + if nextHops.Size() == 0 { klog.Infof("delete policy route for centralized subnet %s, nextHops %s", subnet.Name, nextHops) if err := c.deletePolicyRouteForCentralizedSubnet(subnet); err != nil { klog.Errorf("failed to delete policy route for centralized subnet %s, %v", subnet.Name, err) @@ -1162,7 +1165,7 @@ func (c *Controller) deletePolicyRouteForNode(nodeName string) error { } } else { klog.Infof("update policy route for centralized subnet %s, nextHops %s", subnet.Name, nextHops) - if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, cidrBlock, nextHops, nameIpMap); err != nil { + if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, cidrBlock, nextHops.List(), nameIpMap); err != nil { klog.Errorf("failed to update policy route for subnet %s on node %s, %v", subnet.Name, nodeName, err) return err } @@ -1217,13 +1220,13 @@ func (c *Controller) addPolicyRouteForCentralizedSubnetOnNode(nodeName, nodeIP s klog.Errorf("get ecmp policy route paras for subnet %v, error %v", subnet.Name, err) continue } - nextHops = append(nextHops, nextHop) + nextHops.Add(nextHop) if nameIpMap == nil { nameIpMap = make(map[string]string, 1) } nameIpMap[nodeName] = nextHop klog.Infof("update policy route for centralized subnet %s, nextHops %s", subnet.Name, nextHops) - if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, cidrBlock, nextHops, nameIpMap); err != nil { + if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, cidrBlock, nextHops.List(), nameIpMap); err != nil { klog.Errorf("failed to update policy route for subnet %s on node %s, %v", subnet.Name, nodeName, err) return err } @@ -1254,36 +1257,33 @@ func (c *Controller) addPolicyRouteForLocalDnsCacheOnNode(nodePortName, nodeIP, pgAs := strings.Replace(fmt.Sprintf("%s_ip%d", nodePortName, af), "-", ".", -1) match := fmt.Sprintf("ip%d.src == $%s && ip%d.dst == %s", af, pgAs, af, c.config.NodeLocalDnsIP) klog.Infof("add node local dns cache policy route for router: %s, match %s, action %s, nexthop %s, externalID %v", c.config.ClusterRouter, match, "reroute", nodeIP, externalIDs) - if err := c.ovnLegacyClient.AddPolicyRoute(c.config.ClusterRouter, util.NodeLocalDnsPolicyPriority, match, "reroute", nodeIP, externalIDs); err != nil { + if err := c.ovnClient.AddLogicalRouterPolicy(c.config.ClusterRouter, util.NodeLocalDnsPolicyPriority, match, "reroute", []string{nodeIP}, externalIDs); err != nil { klog.Errorf("failed to add logical router policy for node %s: %v", nodeName, err) return err } return nil } -func (c *Controller) deletePolicyRouteForLocalDnsCacheOnNode(nodePortName, nodeName string, af int) error { - results, err := c.ovnLegacyClient.CustomFindEntity("Logical_Router_Policy", []string{"_uuid", "match", "priority"}, - fmt.Sprintf("external_ids:vendor=\"%s\"", util.CniTypeName), - fmt.Sprintf("external_ids:node=\"%s\"", nodeName), - fmt.Sprintf("external_ids:address-family=\"%s\"", strconv.Itoa(af)), - "external_ids:isLocalDnsCache=\"true\"", - ) +func (c *Controller) deletePolicyRouteForLocalDnsCacheOnNode(nodeName string, af int) error { + policies, err := c.ovnClient.ListLogicalRouterPolicies(c.config.ClusterRouter, -1, map[string]string{ + "vendor": util.CniTypeName, + "node": nodeName, + "address-family": strconv.Itoa(af), + "isLocalDnsCache": "true", + }) if err != nil { - klog.Errorf("customFindEntity failed, %v", err) + klog.Errorf("failed to list logical router policies: %v", err) return err } - - if len(results) == 0 { + if len(policies) == 0 { return nil } - var uuids []string - for _, result := range results { - uuids = append(uuids, result["_uuid"][0]) - klog.Infof("delete node local dns cache policy route for router %s with match %s ", c.config.ClusterRouter, result["match"]) + for _, policy := range policies { + klog.Infof("delete node local dns cache policy route for router %s with match %s", c.config.ClusterRouter, policy.Match) - if err := c.ovnLegacyClient.DeletePolicyRouteByUUID(c.config.ClusterRouter, uuids); err != nil { - klog.Errorf("failed to delete policy route for node local dns in router %s with match %s : %v", c.config.ClusterRouter, result["match"], err) + if err := c.ovnClient.DeleteLogicalRouterPolicyByUUID(c.config.ClusterRouter, policy.UUID); err != nil { + klog.Errorf("failed to delete policy route for node local dns in router %s with match %s: %v", c.config.ClusterRouter, policy.Match, err) return err } } diff --git a/pkg/controller/ovn-ic.go b/pkg/controller/ovn-ic.go index 3fe22b53e4a..935d087e70b 100644 --- a/pkg/controller/ovn-ic.go +++ b/pkg/controller/ovn-ic.go @@ -459,7 +459,7 @@ func (c *Controller) syncOneRouteToPolicy(key, value string) { klog.Errorf("logical router does not exist %v at %v", err, time.Now()) return } - lrRouteList, err := c.ovnClient.GetLogicalRouterRouteByOpts(key, value) + lrRouteList, err := c.ovnClient.ListLogicalRouterStaticRoutesByOption(lr.Name, key, value) if err != nil { klog.Errorf("failed to list lr ovn-ic route %v", err) return @@ -468,14 +468,14 @@ func (c *Controller) syncOneRouteToPolicy(key, value string) { klog.V(5).Info("lr ovn-ic route does not exist") err := c.ovnClient.DeleteLogicalRouterPolicies(lr.Name, util.OvnICPolicyPriority, map[string]string{key: value}) if err != nil { - klog.Errorf("delete ovn-ic lr policy", err) + klog.Errorf("failed to delete ovn-ic lr policy: %v", err) return } return } policyMap := map[string]string{} - lrPolicyList, err := c.ovnClient.ListLogicalRouterPolicies(util.OvnICPolicyPriority, map[string]string{key: value}) + lrPolicyList, err := c.ovnClient.ListLogicalRouterPolicies(lr.Name, util.OvnICPolicyPriority, map[string]string{key: value}) if err != nil { klog.Errorf("failed to list ovn-ic lr policy ", err) return diff --git a/pkg/controller/security_group.go b/pkg/controller/security_group.go index f2e75b5dbb5..ce15fa38470 100644 --- a/pkg/controller/security_group.go +++ b/pkg/controller/security_group.go @@ -422,7 +422,7 @@ func (c *Controller) syncSgLogicalPort(key string) error { return err } - results, err := c.ovnLegacyClient.CustomFindEntity("logical_switch_port", []string{"_uuid", "name", "port_security"}, fmt.Sprintf("external_ids:associated_sg_%s=true", key)) + results, err := c.ovnClient.ListLogicalSwitchPorts(false, map[string]string{"external_ids:associated_sg_" + key: "true"}, nil) if err != nil { klog.Errorf("failed to find logical port, %v", err) return err @@ -431,18 +431,23 @@ func (c *Controller) syncSgLogicalPort(key string) error { return nil } - var v4s, v6s []string - var ports []string - for _, ret := range results { - if len(ret["port_security"]) < 2 { + var ports, v4s, v6s []string + for _, lsp := range results { + if len(lsp.PortSecurity) == 0 { continue } - ports = append(ports, ret["name"][0]) - for _, address := range ret["port_security"][1:] { - if strings.Contains(address, ":") { - v6s = append(v6s, address) - } else { - v4s = append(v4s, address) + ports = append(ports, lsp.Name) + for _, ps := range lsp.PortSecurity { + fields := strings.Fields(ps) + if len(fields) < 2 { + continue + } + for _, address := range fields[1:] { + if strings.Contains(address, ":") { + v6s = append(v6s, address) + } else { + v4s = append(v4s, address) + } } } } diff --git a/pkg/controller/subnet.go b/pkg/controller/subnet.go index ddf56ba708e..7a185caf281 100644 --- a/pkg/controller/subnet.go +++ b/pkg/controller/subnet.go @@ -1556,43 +1556,27 @@ func (c *Controller) reconcileOvnDefaultVpcRoute(subnet *kubeovnv1.Subnet) error } v4Cidr, v6Cidr := util.SplitStringIP(subnet.Spec.CIDRBlock) if nodeV4Ips != nil && v4Cidr != "" { - sort.Strings(nodeV4Ips) - exist, err := c.ovnLegacyClient.VpcHasPolicyRoute(c.config.ClusterRouter, nodeV4Ips, util.GatewayRouterPolicyPriority) - if err != nil { - klog.Errorf("failed to check if vpc %s has v4 ecmp policy route for centralized subnet %s, %v", c.config.ClusterRouter, subnet.Name, err) + klog.V(3).Infof("delete old distributed policy route for subnet %s", subnet.Name) + if err := c.deletePolicyRouteByGatewayType(subnet, kubeovnv1.GWDistributedType, false); err != nil { + klog.Errorf("failed to delete policy route for overlay subnet %s, %v", subnet.Name, err) return err } - if !exist { - klog.Infof("delete old distributed policy route for subnet %s", subnet.Name) - if err := c.deletePolicyRouteByGatewayType(subnet, kubeovnv1.GWDistributedType, false); err != nil { - klog.Errorf("failed to delete policy route for overlay subnet %s, %v", subnet.Name, err) - return err - } - klog.Infof("subnet %s configure ecmp policy route, nexthops %v", subnet.Name, nodeV4Ips) - if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, v4Cidr, nodeV4Ips, nameV4IpMap); err != nil { - klog.Errorf("failed to add v4 ecmp policy route for centralized subnet %s: %v", subnet.Name, err) - return err - } + klog.V(3).Infof("subnet %s configure ecmp policy route, nexthops %v", subnet.Name, nodeV4Ips) + if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, v4Cidr, nodeV4Ips, nameV4IpMap); err != nil { + klog.Errorf("failed to add v4 ecmp policy route for centralized subnet %s: %v", subnet.Name, err) + return err } } if nodeV6Ips != nil && v6Cidr != "" { - sort.Strings(nodeV6Ips) - exist, err := c.ovnLegacyClient.VpcHasPolicyRoute(c.config.ClusterRouter, nodeV6Ips, util.GatewayRouterPolicyPriority) - if err != nil { - klog.Errorf("failed to check if vpc %s has v6 ecmp policy route for centralized subnet %s, %v", c.config.ClusterRouter, subnet.Name, err) + klog.V(3).Infof("delete old distributed policy route for subnet %s", subnet.Name) + if err := c.deletePolicyRouteByGatewayType(subnet, kubeovnv1.GWDistributedType, false); err != nil { + klog.Errorf("failed to delete policy route for overlay subnet %s, %v", subnet.Name, err) return err } - if !exist { - klog.Infof("delete old distributed policy route for subnet %s", subnet.Name) - if err := c.deletePolicyRouteByGatewayType(subnet, kubeovnv1.GWDistributedType, false); err != nil { - klog.Errorf("failed to delete policy route for overlay subnet %s, %v", subnet.Name, err) - return err - } - klog.Infof("subnet %s configure ecmp policy route, nexthops %v", subnet.Name, nodeV6Ips) - if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, v6Cidr, nodeV6Ips, nameV6IpMap); err != nil { - klog.Errorf("failed to add v6 ecmp policy route for centralized subnet %s: %v", subnet.Name, err) - return err - } + klog.V(3).Infof("subnet %s configure ecmp policy route, nexthops %v", subnet.Name, nodeV6Ips) + if err = c.updatePolicyRouteForCentralizedSubnet(subnet.Name, v6Cidr, nodeV6Ips, nameV6IpMap); err != nil { + klog.Errorf("failed to add v6 ecmp policy route for centralized subnet %s: %v", subnet.Name, err) + return err } } } else { @@ -2103,19 +2087,12 @@ func (c *Controller) addCommonRoutesForSubnet(subnet *kubeovnv1.Subnet) error { af = 6 } match := fmt.Sprintf("ip%d.dst == %s", af, cidr) - exist, err := c.ovnLegacyClient.PolicyRouteExists(util.SubnetRouterPolicyPriority, match) - if err != nil { + externalIDs := map[string]string{"vendor": util.CniTypeName, "subnet": subnet.Name} + klog.V(3).Infof("add policy route for router: %s, match %s, action %s, extrenalID %v", c.config.ClusterRouter, match, "allow", externalIDs) + if err := c.ovnClient.AddLogicalRouterPolicy(c.config.ClusterRouter, util.SubnetRouterPolicyPriority, match, "allow", nil, externalIDs); err != nil { + klog.Errorf("failed to add logical router policy for CIDR %s of subnet %s: %v", cidr, subnet.Name, err) return err } - if !exist { - externalIDs := map[string]string{"vendor": util.CniTypeName, "subnet": subnet.Name} - klog.Infof("add policy route for router: %s, match %s, action %s, nexthop %s, extrenalID %v", c.config.ClusterRouter, match, "allow", "", externalIDs) - if err = c.ovnLegacyClient.AddPolicyRoute(c.config.ClusterRouter, util.SubnetRouterPolicyPriority, match, "allow", "", externalIDs); err != nil { - klog.Errorf("failed to add logical router policy for CIDR %s of subnet %s: %v", cidr, subnet.Name, err) - return err - } - } - } return nil } @@ -2151,7 +2128,6 @@ func (c *Controller) updatePolicyRouteForCentralizedSubnet(subnetName, cidr stri // there's no way to update policy route when gatewayNode changed for subnet, so delete and readd policy route // The delete operation is processed in AddPolicyRoute if the policy route is inconsistent, so no need delete here - nextHopIp := strings.Join(nextHops, ",") externalIDs := map[string]string{ "vendor": util.CniTypeName, "subnet": subnetName, @@ -2161,8 +2137,8 @@ func (c *Controller) updatePolicyRouteForCentralizedSubnet(subnetName, cidr stri for node, ip := range nameIpMap { externalIDs[node] = ip } - klog.Infof("add ecmp policy route for router: %s, match %s, action %s, nexthop %s, extrenalID %s", c.config.ClusterRouter, match, "allow", nextHopIp, externalIDs) - if err := c.ovnLegacyClient.AddPolicyRoute(c.config.ClusterRouter, util.GatewayRouterPolicyPriority, match, "reroute", nextHopIp, externalIDs); err != nil { + klog.Infof("add ecmp policy route for router: %s, match %s, action %s, nexthops %v, extrenalID %s", c.config.ClusterRouter, match, "allow", nextHops, externalIDs) + if err := c.ovnClient.AddLogicalRouterPolicy(c.config.ClusterRouter, util.GatewayRouterPolicyPriority, match, "reroute", nextHops, externalIDs); err != nil { klog.Errorf("failed to add policy route for centralized subnet %s: %v", subnetName, err) return err } @@ -2202,7 +2178,7 @@ func (c *Controller) deletePolicyRouteForCentralizedSubnet(subnet *kubeovnv1.Sub } match := fmt.Sprintf("%s.src == %s", ipSuffix, cidr) klog.Infof("delete policy route for router: %s, priority: %d, match %s", c.config.ClusterRouter, util.GatewayRouterPolicyPriority, match) - if err := c.ovnLegacyClient.DeletePolicyRoute(c.config.ClusterRouter, util.GatewayRouterPolicyPriority, match); err != nil { + if err := c.ovnClient.DeleteLogicalRouterPolicy(c.config.ClusterRouter, util.GatewayRouterPolicyPriority, match); err != nil { klog.Errorf("failed to delete policy route for centralized subnet %s: %v", subnet.Name, err) return err } @@ -2230,21 +2206,13 @@ func (c *Controller) addPolicyRouteForDistributedSubnet(subnet *kubeovnv1.Subnet pgAs := fmt.Sprintf("%s_%s", pgName, ipSuffix) match := fmt.Sprintf("%s.src == $%s", ipSuffix, pgAs) - exist, err := c.ovnLegacyClient.PolicyRouteExists(util.GatewayRouterPolicyPriority, match) - if err != nil { - return err - } - if exist { - continue - } - externalIDs := map[string]string{ "vendor": util.CniTypeName, "subnet": subnet.Name, "node": nodeName, } - klog.Infof("add policy route for router: %s, match %s, action %s, nexthop %s, extrenalID %v", c.config.ClusterRouter, match, "allow", "", externalIDs) - if err = c.ovnLegacyClient.AddPolicyRoute(c.config.ClusterRouter, util.GatewayRouterPolicyPriority, match, "reroute", nodeIP, externalIDs); err != nil { + klog.V(3).Infof("add policy route for router: %s, match %s, action %s, extrenalID %v", c.config.ClusterRouter, match, "allow", externalIDs) + if err := c.ovnClient.AddLogicalRouterPolicy(c.config.ClusterRouter, util.GatewayRouterPolicyPriority, match, "reroute", []string{nodeIP}, externalIDs); err != nil { klog.Errorf("failed to add logical router policy for port-group address-set %s: %v", pgAs, err) return err } @@ -2262,7 +2230,7 @@ func (c *Controller) deletePolicyRouteForDistributedSubnet(subnet *kubeovnv1.Sub pgAs := fmt.Sprintf("%s_%s", pgName, ipSuffix) match := fmt.Sprintf("%s.src == $%s", ipSuffix, pgAs) klog.Infof("delete policy route for router: %s, priority: %d, match %s", c.config.ClusterRouter, util.GatewayRouterPolicyPriority, match) - if err := c.ovnLegacyClient.DeletePolicyRoute(c.config.ClusterRouter, util.GatewayRouterPolicyPriority, match); err != nil { + if err := c.ovnClient.DeleteLogicalRouterPolicy(c.config.ClusterRouter, util.GatewayRouterPolicyPriority, match); err != nil { klog.Errorf("failed to delete policy route for subnet %s: %v", subnet.Name, err) return err } @@ -2286,7 +2254,7 @@ func (c *Controller) deletePolicyRouteByGatewayType(subnet *kubeovnv1.Subnet, ga } match := fmt.Sprintf("ip%d.dst == %s", af, cidr) klog.Infof("delete policy route for router: %s, priority: %d, match %s", c.config.ClusterRouter, util.SubnetRouterPolicyPriority, match) - if err := c.ovnLegacyClient.DeletePolicyRoute(c.config.ClusterRouter, util.SubnetRouterPolicyPriority, match); err != nil { + if err := c.ovnClient.DeleteLogicalRouterPolicy(c.config.ClusterRouter, util.SubnetRouterPolicyPriority, match); err != nil { klog.Errorf("failed to delete logical router policy for CIDR %s of subnet %s: %v", cidr, subnet.Name, err) return err } @@ -2418,19 +2386,19 @@ func (c *Controller) addPolicyRouteForU2OInterconn(subnet *kubeovnv1.Subnet) err policy3: underlay pod first access u2o interconnection lrp and then reoute to physical gw */ klog.Infof("add u2o interconnection policy for router: %s, match %s, action %s", subnet.Spec.Vpc, match1, "allow") - if err := c.ovnLegacyClient.AddPolicyRoute(subnet.Spec.Vpc, util.SubnetRouterPolicyPriority, match1, "allow", "", externalIDs); err != nil { + if err := c.ovnClient.AddLogicalRouterPolicy(subnet.Spec.Vpc, util.SubnetRouterPolicyPriority, match1, "allow", nil, externalIDs); err != nil { klog.Errorf("failed to add u2o interconnection policy1 for subnet %s %v", subnet.Name, err) return err } klog.Infof("add u2o interconnection policy for router: %s, match %s, action %s", subnet.Spec.Vpc, match2, "allow") - if err := c.ovnLegacyClient.AddPolicyRoute(subnet.Spec.Vpc, util.SubnetRouterPolicyPriority, match2, "allow", "", externalIDs); err != nil { + if err := c.ovnClient.AddLogicalRouterPolicy(subnet.Spec.Vpc, util.SubnetRouterPolicyPriority, match2, "allow", nil, externalIDs); err != nil { klog.Errorf("failed to add u2o interconnection policy2 for subnet %s %v", subnet.Name, err) return err } klog.Infof("add u2o interconnection policy for router: %s, match %s, action %s, nexthop %s", subnet.Spec.Vpc, match3, "reroute", nextHop) - if err := c.ovnLegacyClient.AddPolicyRoute(subnet.Spec.Vpc, util.GatewayRouterPolicyPriority, match3, "reroute", nextHop, externalIDs); err != nil { + if err := c.ovnClient.AddLogicalRouterPolicy(subnet.Spec.Vpc, util.GatewayRouterPolicyPriority, match3, "reroute", []string{nextHop}, externalIDs); err != nil { klog.Errorf("failed to add u2o interconnection policy3 for subnet %s %v", subnet.Name, err) return err } @@ -2439,29 +2407,25 @@ func (c *Controller) addPolicyRouteForU2OInterconn(subnet *kubeovnv1.Subnet) err } func (c *Controller) deletePolicyRouteForU2OInterconn(subnet *kubeovnv1.Subnet) error { - - results, err := c.ovnLegacyClient.CustomFindEntity("Logical_Router_Policy", []string{"_uuid", "match", "priority"}, - "external_ids:isU2ORoutePolicy=\"true\"", - fmt.Sprintf("external_ids:vendor=\"%s\"", util.CniTypeName), - fmt.Sprintf("external_ids:subnet=\"%s\"", subnet.Name)) + policies, err := c.ovnClient.ListLogicalRouterPolicies(subnet.Spec.Vpc, -1, map[string]string{ + "isU2ORoutePolicy": "true", + "vendor": util.CniTypeName, + "subnet": subnet.Name, + }) if err != nil { - klog.Errorf("customFindEntity failed, %v", err) + klog.Errorf("failed to list logical router policies: %v", err) return err } - - if len(results) == 0 { + if len(policies) == 0 { return nil } - var uuids []string - for _, result := range results { - uuids = append(uuids, result["_uuid"][0]) - klog.Infof("delete u2o interconnection policy for router %s with match %s priority %s ", subnet.Spec.Vpc, result["match"], result["priority"]) - } - - if err := c.ovnLegacyClient.DeletePolicyRouteByUUID(subnet.Spec.Vpc, uuids); err != nil { - klog.Errorf("failed to delete u2o interconnection policy for subnet %s: %v", subnet.Name, err) - return err + for _, policy := range policies { + klog.Infof("delete u2o interconnection policy for router %s with match %s priority %d", subnet.Spec.Vpc, policy.Match, policy.Priority) + if err = c.ovnClient.DeleteLogicalRouterPolicyByUUID(subnet.Spec.Vpc, policy.UUID); err != nil { + klog.Errorf("failed to delete u2o interconnection policy for subnet %s: %v", subnet.Name, err) + return err + } } u2oExcludeIp4Ag := strings.Replace(fmt.Sprintf(util.U2OExcludeIPAg, subnet.Name, "ip4"), "-", ".", -1) diff --git a/pkg/controller/vpc.go b/pkg/controller/vpc.go index c1a7cb9f1e5..820ad16071b 100644 --- a/pkg/controller/vpc.go +++ b/pkg/controller/vpc.go @@ -18,6 +18,7 @@ import ( kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1" "github.com/kubeovn/kube-ovn/pkg/ovs" + "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb" "github.com/kubeovn/kube-ovn/pkg/util" ) @@ -387,7 +388,7 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error { if vpc.Name != c.config.ClusterRouter && vpc.Spec.PolicyRoutes == nil { // do not clean default vpc policy routes - if err = c.ovnLegacyClient.CleanPolicyRoute(vpc.Name); err != nil { + if err = c.ovnClient.ClearLogicalRouterPolicy(vpc.Name); err != nil { klog.Errorf("clean all vpc %s policy route failed, %v", vpc.Name, err) return err } @@ -395,19 +396,19 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error { if vpc.Spec.PolicyRoutes != nil { // diff update vpc policy route - existPolicyRoute, err := c.ovnLegacyClient.GetPolicyRouteList(vpc.Name) + policyList, err := c.ovnClient.ListLogicalRouterPolicies(vpc.Name, -1, nil) if err != nil { klog.Errorf("failed to get vpc %s policy route list, %v", vpc.Name, err) return err } - policyRouteNeedDel, policyRouteNeedAdd, err := diffPolicyRoute(existPolicyRoute, vpc.Spec.PolicyRoutes) + policyRouteNeedDel, policyRouteNeedAdd, err := diffPolicyRoute(policyList, vpc.Spec.PolicyRoutes) if err != nil { klog.Errorf("failed to diff vpc %s policy route, %v", vpc.Name, err) return err } for _, item := range policyRouteNeedDel { klog.Infof("delete policy route for router: %s, priority: %d, match %s", vpc.Name, item.Priority, item.Match) - if err = c.ovnLegacyClient.DeletePolicyRoute(vpc.Name, item.Priority, item.Match); err != nil { + if err = c.ovnClient.DeleteLogicalRouterPolicy(vpc.Name, item.Priority, item.Match); err != nil { klog.Errorf("del vpc %s policy route failed, %v", vpc.Name, err) return err } @@ -415,7 +416,7 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error { for _, item := range policyRouteNeedAdd { externalIDs := map[string]string{"vendor": util.CniTypeName} klog.Infof("add policy route for router: %s, match %s, action %s, nexthop %s, externalID %v", c.config.ClusterRouter, item.Match, string(item.Action), item.NextHopIP, externalIDs) - if err = c.ovnLegacyClient.AddPolicyRoute(vpc.Name, item.Priority, item.Match, string(item.Action), item.NextHopIP, externalIDs); err != nil { + if err = c.ovnClient.AddLogicalRouterPolicy(vpc.Name, item.Priority, item.Match, string(item.Action), []string{item.NextHopIP}, externalIDs); err != nil { klog.Errorf("add policy route to vpc %s failed, %v", vpc.Name, err) return err } @@ -515,14 +516,13 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error { return nil } -func diffPolicyRoute(exist []*ovs.PolicyRoute, target []*kubeovnv1.PolicyRoute) (routeNeedDel []*kubeovnv1.PolicyRoute, routeNeedAdd []*kubeovnv1.PolicyRoute, err error) { +func diffPolicyRoute(exist []*ovnnb.LogicalRouterPolicy, target []*kubeovnv1.PolicyRoute) (routeNeedDel, routeNeedAdd []*kubeovnv1.PolicyRoute, err error) { existV1 := make([]*kubeovnv1.PolicyRoute, 0, len(exist)) for _, item := range exist { existV1 = append(existV1, &kubeovnv1.PolicyRoute{ - Priority: item.Priority, - Match: item.Match, - Action: kubeovnv1.PolicyRouteAction(item.Action), - NextHopIP: item.NextHopIP, + Priority: item.Priority, + Match: item.Match, + Action: kubeovnv1.PolicyRouteAction(item.Action), }) } diff --git a/pkg/ovs/interface.go b/pkg/ovs/interface.go index 4f4616d6630..16ec6708f9f 100644 --- a/pkg/ovs/interface.go +++ b/pkg/ovs/interface.go @@ -36,6 +36,7 @@ type LogicalRouterPort interface { DeleteLogicalRouterPort(lrpName string) error DeleteLogicalRouterPorts(externalIDs map[string]string, filter func(lrp *ovnnb.LogicalRouterPort) bool) error GetLogicalRouterPort(lrpName string, ignoreNotFound bool) (*ovnnb.LogicalRouterPort, error) + GetLogicalRouterPortByUUID(uuid string) (*ovnnb.LogicalRouterPort, error) ListLogicalRouterPorts(externalIDs map[string]string, filter func(lrp *ovnnb.LogicalRouterPort) bool) ([]ovnnb.LogicalRouterPort, error) LogicalRouterPortExists(lrpName string) (bool, error) } @@ -45,7 +46,7 @@ type LogicalSwitch interface { CreateBareLogicalSwitch(lsName string) error LogicalSwitchUpdateLoadBalancers(lsName string, op ovsdb.Mutator, lbNames ...string) error DeleteLogicalSwitch(lsName string) error - ListLogicalSwitch(needVendorFilter bool, filter func(lr *ovnnb.LogicalSwitch) bool) ([]ovnnb.LogicalSwitch, error) + ListLogicalSwitch(needVendorFilter bool, filter func(ls *ovnnb.LogicalSwitch) bool) ([]ovnnb.LogicalSwitch, error) LogicalSwitchExists(lsName string) (bool, error) } @@ -117,8 +118,8 @@ type LogicalRouterStaticRoute interface { AddLogicalRouterStaticRoute(lrName, policy, cidrBlock, nextHops, routeType string) error ClearLogicalRouterStaticRoute(lrName string) error DeleteLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, routeType string) error - GetLogicalRouterRouteByOpts(key, value string) ([]ovnnb.LogicalRouterStaticRoute, error) - ListLogicalRouterStaticRoutes(externalIDs map[string]string) ([]ovnnb.LogicalRouterStaticRoute, error) + ListLogicalRouterStaticRoutesByOption(lrName, key, value string) ([]*ovnnb.LogicalRouterStaticRoute, error) + ListLogicalRouterStaticRoutes(lrName string, externalIDs map[string]string) ([]*ovnnb.LogicalRouterStaticRoute, error) LogicalRouterStaticRouteExists(lrName, policy, prefix, nextHop, routeType string) (bool, error) } @@ -127,8 +128,9 @@ type LogicalRouterPolicy interface { DeleteLogicalRouterPolicy(lrName string, priority int, match string) error DeleteLogicalRouterPolicies(lrName string, priority int, externalIDs map[string]string) error DeleteLogicalRouterPolicyByUUID(lrName string, uuid string) error + DeleteLogicalRouterPolicyByNexthop(lrName string, priority int, nexthop string) error ClearLogicalRouterPolicy(lrName string) error - ListLogicalRouterPolicies(priority int, externalIDs map[string]string) ([]ovnnb.LogicalRouterPolicy, error) + ListLogicalRouterPolicies(lrName string, priority int, externalIDs map[string]string) ([]*ovnnb.LogicalRouterPolicy, error) GetLogicalRouterPolicy(lrName string, priority int, match string, ignoreNotFound bool) (*ovnnb.LogicalRouterPolicy, error) } @@ -138,7 +140,7 @@ type NAT interface { DeleteNats(lrName, natType, logicalIP string) error DeleteNat(lrName, natType, externalIP, logicalIP string) error NatExists(lrName, natType, externalIP, logicalIP string) (bool, error) - ListNats(natType, logicalIP string, externalIDs map[string]string) ([]ovnnb.NAT, error) + ListNats(lrName, natType, logicalIP string, externalIDs map[string]string) ([]*ovnnb.NAT, error) } type DHCPOptions interface { diff --git a/pkg/ovs/ovn-nb-logical_router_policy.go b/pkg/ovs/ovn-nb-logical_router_policy.go index b4c15b4d6f9..8c8c3fbc2e2 100644 --- a/pkg/ovs/ovn-nb-logical_router_policy.go +++ b/pkg/ovs/ovn-nb-logical_router_policy.go @@ -6,29 +6,42 @@ import ( "github.com/ovn-org/libovsdb/model" "github.com/ovn-org/libovsdb/ovsdb" + "github.com/scylladb/go-set/strset" ovsclient "github.com/kubeovn/kube-ovn/pkg/ovsdb/client" "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb" + "github.com/kubeovn/kube-ovn/pkg/util" ) // AddLogicalRouterPolicy add a policy route to logical router func (c *ovnClient) AddLogicalRouterPolicy(lrName string, priority int, match, action string, nextHops []string, externalIDs map[string]string) error { - exists, err := c.LogicalRouterPolicyExists(lrName, priority, match) + fnFilter := func(policy *ovnnb.LogicalRouterPolicy) bool { + return policy.Priority == priority && policy.Match == match + } + policyList, err := c.listLogicalRouterPoliciesByFilter(lrName, fnFilter) if err != nil { - return err + return fmt.Errorf("get policy priority %d match %s in logical router %s: %v", priority, match, lrName, err) } - if exists { - return nil + var found bool + duplicate := make([]string, 0, len(policyList)) + for _, policy := range policyList { + if found || policy.Action != action || (policy.Action == ovnnb.LogicalRouterPolicyActionReroute && !strset.New(nextHops...).IsEqual(strset.New(policy.Nexthops...))) { + duplicate = append(duplicate, policy.UUID) + } else { + found = true + } } - - policy, err := c.newLogicalRouterPolicy(lrName, priority, match, action, nextHops, externalIDs) - if err != nil { - return fmt.Errorf("new policy for logical router %s: %v", lrName, err) + for _, uuid := range duplicate { + if err = c.DeleteLogicalRouterPolicyByUUID(lrName, uuid); err != nil { + return err + } } - - if err := c.CreateLogicalRouterPolicies(lrName, policy); err != nil { - return fmt.Errorf("add policy to logical router %s: %v", lrName, err) + if len(duplicate) == len(policyList) { + policy := c.newLogicalRouterPolicy(priority, match, action, nextHops, externalIDs) + if err := c.CreateLogicalRouterPolicies(lrName, policy); err != nil { + return fmt.Errorf("add policy to logical router %s: %v", lrName, err) + } } return nil @@ -70,24 +83,6 @@ func (c *ovnClient) CreateLogicalRouterPolicies(lrName string, policies ...*ovnn return nil } -func (c *ovnClient) CreateBareLogicalRouterPolicy(lrName string, priority int, match, action string, nextHops []string) error { - policy, err := c.newLogicalRouterPolicy(lrName, priority, match, action, nextHops, nil) - if err != nil { - return fmt.Errorf("new logical router policy: %v", err) - } - - op, err := c.ovnNbClient.Create(policy) - if err != nil { - return fmt.Errorf("generate operations for creating logical router policy: %v", err) - } - - if err = c.Transact("lr-policy-create", op); err != nil { - return fmt.Errorf("create logical router policy: %v", err) - } - - return nil -} - // DeleteLogicalRouterPolicy delete policy from logical router func (c *ovnClient) DeleteLogicalRouterPolicy(lrName string, priority int, match string) error { policy, err := c.GetLogicalRouterPolicy(lrName, priority, match, true) @@ -109,14 +104,8 @@ func (c *ovnClient) DeleteLogicalRouterPolicy(lrName string, priority int, match // DeleteLogicalRouterPolicy delete some policies from logical router once func (c *ovnClient) DeleteLogicalRouterPolicies(lrName string, priority int, externalIDs map[string]string) error { - if externalIDs == nil { - externalIDs = make(map[string]string) - } - - externalIDs[logicalRouterKey] = lrName - // remove policies from logical router - policies, err := c.ListLogicalRouterPolicies(priority, externalIDs) + policies, err := c.ListLogicalRouterPolicies(lrName, priority, externalIDs) if err != nil { return err } @@ -129,50 +118,42 @@ func (c *ovnClient) DeleteLogicalRouterPolicies(lrName string, priority int, ext policiesUUIDs = append(policiesUUIDs, policy.UUID) } - policiesRemoveOp, err := c.LogicalRouterUpdatePolicyOp(lrName, policiesUUIDs, ovsdb.MutateOperationDelete) + ops, err := c.LogicalRouterUpdatePolicyOp(lrName, policiesUUIDs, ovsdb.MutateOperationDelete) if err != nil { return fmt.Errorf("generate operations for removing policy %v from logical router %s: %v", policiesUUIDs, lrName, err) } - - // delete policies - delPoliciesOp, err := c.WhereCache(policyFilter(priority, externalIDs)).Delete() - if err != nil { - return fmt.Errorf("generate operation for deleting nats: %v", err) - } - - ops := make([]ovsdb.Operation, 0, len(policiesRemoveOp)+len(delPoliciesOp)) - ops = append(ops, policiesRemoveOp...) - - ops = append(ops, delPoliciesOp...) - if err = c.Transact("lr-policies-del", ops); err != nil { return fmt.Errorf("delete logical router policy %v from logical router %s: %v", policiesUUIDs, lrName, err) } return nil } -func (c *ovnClient) DeleteLogicalRouterPolicyByUUID(lrName string, uuid string) error { +func (c *ovnClient) DeleteLogicalRouterPolicyByUUID(lrName, uuid string) error { // remove policy from logical router - policyRemoveOp, err := c.LogicalRouterUpdatePolicyOp(lrName, []string{uuid}, ovsdb.MutateOperationDelete) + ops, err := c.LogicalRouterUpdatePolicyOp(lrName, []string{uuid}, ovsdb.MutateOperationDelete) if err != nil { return fmt.Errorf("generate operations for removing policy '%s' from logical router %s: %v", uuid, lrName, err) } + if err = c.Transact("lr-policy-del", ops); err != nil { + return fmt.Errorf("delete logical router policy '%s' from logical router %s: %v", uuid, lrName, err) + } + return nil +} - // delete policy - deleteOps, err := c.ovnNbClient.Where(&ovnnb.LogicalRouterPolicy{ - UUID: uuid, - }).Delete() +func (c *ovnClient) DeleteLogicalRouterPolicyByNexthop(lrName string, priority int, nexthop string) error { + policyList, err := c.listLogicalRouterPoliciesByFilter(lrName, func(route *ovnnb.LogicalRouterPolicy) bool { + if route.Priority != priority { + return false + } + return (route.Nexthop != nil && *route.Nexthop == nexthop) || util.ContainsString(route.Nexthops, nexthop) + }) if err != nil { - return fmt.Errorf("failed to generate delete operations for router policy %s: %v", uuid, err) + return err } - - ops := make([]ovsdb.Operation, 0, len(policyRemoveOp)+len(deleteOps)) - ops = append(ops, policyRemoveOp...) - - ops = append(ops, deleteOps...) - - if err = c.Transact("lr-policy-del", ops); err != nil { - return fmt.Errorf("delete logical router policy '%s' from logical router %s: %v", uuid, lrName, err) + for _, policy := range policyList { + if err = c.DeleteLogicalRouterPolicyByUUID(lrName, policy.UUID); err != nil { + return err + } } return nil } @@ -186,23 +167,10 @@ func (c *ovnClient) ClearLogicalRouterPolicy(lrName string) error { // clear logical router policy lr.Policies = nil - policyClearOp, err := c.UpdateLogicalRouterOp(lr, &lr.Policies) + ops, err := c.UpdateLogicalRouterOp(lr, &lr.Policies) if err != nil { return fmt.Errorf("generate operations for clear logical router %s policy: %v", lrName, err) } - - // delete logical router policy - policyDelOp, err := c.WhereCache(func(policy *ovnnb.LogicalRouterPolicy) bool { - return len(policy.ExternalIDs) != 0 && policy.ExternalIDs[logicalRouterKey] == lrName - }).Delete() - if err != nil { - return fmt.Errorf("generate operations for deleting logical router %s policy: %v", lrName, err) - } - - ops := make([]ovsdb.Operation, 0, len(policyClearOp)+len(policyDelOp)) - ops = append(ops, policyClearOp...) - ops = append(ops, policyDelOp...) - if err = c.Transact("lr-policy-clear", ops); err != nil { return fmt.Errorf("clear logical router %s policy: %v", lrName, err) } @@ -218,13 +186,11 @@ func (c *ovnClient) GetLogicalRouterPolicy(lrName string, priority int, match st return nil, fmt.Errorf("the logical router name is required") } - ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) - defer cancel() - - policyList := make([]ovnnb.LogicalRouterPolicy, 0) - if err := c.ovnNbClient.WhereCache(func(policy *ovnnb.LogicalRouterPolicy) bool { - return len(policy.ExternalIDs) != 0 && policy.ExternalIDs[logicalRouterKey] == lrName && policy.Priority == priority && policy.Match == match - }).List(ctx, &policyList); err != nil { + fnFilter := func(policy *ovnnb.LogicalRouterPolicy) bool { + return policy.Priority == priority && policy.Match == match + } + policyList, err := c.listLogicalRouterPoliciesByFilter(lrName, fnFilter) + if err != nil { return nil, fmt.Errorf("get policy priority %d match %s in logical router %s: %v", priority, match, lrName, err) } @@ -240,64 +206,44 @@ func (c *ovnClient) GetLogicalRouterPolicy(lrName string, priority int, match st return nil, fmt.Errorf("more than one policy with same priority %d match %s in logical router %s", priority, match, lrName) } - return &policyList[0], nil + return policyList[0], nil } -// ListLogicalRouterPolicies list route policy which match the given externalIDs -func (c *ovnClient) ListLogicalRouterPolicies(priority int, externalIDs map[string]string) ([]ovnnb.LogicalRouterPolicy, error) { +// GetLogicalRouterPolicyByUUID get logical router policy by UUID +func (c *ovnClient) GetLogicalRouterPolicyByUUID(uuid string) (*ovnnb.LogicalRouterPolicy, error) { ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) defer cancel() - policyList := make([]ovnnb.LogicalRouterPolicy, 0) - - if err := c.WhereCache(policyFilter(priority, externalIDs)).List(ctx, &policyList); err != nil { - return nil, fmt.Errorf("list logical router policies: %v", err) + policy := &ovnnb.LogicalRouterPolicy{UUID: uuid} + if err := c.Get(ctx, policy); err != nil { + return nil, fmt.Errorf("get logical router policy by UUID %s: %v", uuid, err) } - return policyList, nil -} - -func (c *ovnClient) LogicalRouterPolicyExists(lrName string, priority int, match string) (bool, error) { - policy, err := c.GetLogicalRouterPolicy(lrName, priority, match, true) - return policy != nil, err + return policy, nil } -// newLogicalRouterPolicy return logical router policy with basic information -func (c *ovnClient) newLogicalRouterPolicy(lrName string, priority int, match, action string, nextHops []string, externalIDs map[string]string) (*ovnnb.LogicalRouterPolicy, error) { - if len(lrName) == 0 { - return nil, fmt.Errorf("the logical router name is required") - } - - if priority == 0 || len(match) == 0 || len(action) == 0 { - return nil, fmt.Errorf("logical router policy 'priority %d' and 'match %s' and 'action %s' is required", priority, match, action) - } - - exists, err := c.LogicalRouterPolicyExists(lrName, priority, match) - if err != nil { - return nil, fmt.Errorf("get logical router %s policy: %v", lrName, err) - } - - // found, ignore - if exists { - return nil, nil +func (c *ovnClient) GetLogicalRouterPoliciesByExtID(lrName, key, value string) ([]*ovnnb.LogicalRouterPolicy, error) { + fnFilter := func(policy *ovnnb.LogicalRouterPolicy) bool { + return len(policy.ExternalIDs) != 0 && policy.ExternalIDs[key] == value } + return c.listLogicalRouterPoliciesByFilter(lrName, fnFilter) +} - policy := &ovnnb.LogicalRouterPolicy{ - UUID: ovsclient.NamedUUID(), - Priority: priority, - Match: match, - Action: action, - Nexthops: nextHops, - ExternalIDs: map[string]string{ - logicalRouterKey: lrName, - }, - } +// ListLogicalRouterPolicies list route policy which match the given externalIDs +func (c *ovnClient) ListLogicalRouterPolicies(lrName string, priority int, externalIDs map[string]string) ([]*ovnnb.LogicalRouterPolicy, error) { + return c.listLogicalRouterPoliciesByFilter(lrName, policyFilter(priority, externalIDs)) +} - for k, v := range externalIDs { - policy.ExternalIDs[k] = v +// newLogicalRouterPolicy return logical router policy with basic information +func (c *ovnClient) newLogicalRouterPolicy(priority int, match, action string, nextHops []string, externalIDs map[string]string) *ovnnb.LogicalRouterPolicy { + return &ovnnb.LogicalRouterPolicy{ + UUID: ovsclient.NamedUUID(), + Priority: priority, + Match: match, + Action: action, + Nexthops: nextHops, + ExternalIDs: externalIDs, } - - return policy, nil } // policyFilter filter policies which match the given externalIDs @@ -323,7 +269,7 @@ func policyFilter(priority int, externalIDs map[string]string) func(policy *ovnn } } - if priority != -1 && priority != policy.Priority { + if priority >= 0 && priority != policy.Priority { return false } @@ -340,18 +286,28 @@ func (c *ovnClient) DeleteRouterPolicy(lr *ovnnb.LogicalRouter, uuid string) err if err != nil { return fmt.Errorf("failed to generate delete operations for router %s: %v", uuid, err) } - - lrPolicy := &ovnnb.LogicalRouterPolicy{ - UUID: uuid, + if err = c.Transact("lr-policy-delete", ops); err != nil { + return fmt.Errorf("failed to delete route policy %s: %v", uuid, err) } - deleteOps, err := c.ovnNbClient.Where(lrPolicy).Delete() + return nil +} + +func (c *ovnClient) listLogicalRouterPoliciesByFilter(lrName string, filter func(route *ovnnb.LogicalRouterPolicy) bool) ([]*ovnnb.LogicalRouterPolicy, error) { + lr, err := c.GetLogicalRouter(lrName, false) if err != nil { - return fmt.Errorf("failed to generate delete operations for router policy %s: %v", uuid, err) + return nil, err } - ops = append(ops, deleteOps...) - if err = c.Transact("lr-policy-delete", ops); err != nil { - return fmt.Errorf("failed to delete route policy %s: %v", uuid, err) + policyList := make([]*ovnnb.LogicalRouterPolicy, 0, len(lr.Policies)) + for _, uuid := range lr.Policies { + policy, err := c.GetLogicalRouterPolicyByUUID(uuid) + if err != nil { + return nil, err + } + if filter == nil || filter(policy) { + policyList = append(policyList, policy) + } } - return nil + + return policyList, nil } diff --git a/pkg/ovs/ovn-nb-logical_router_policy_test.go b/pkg/ovs/ovn-nb-logical_router_policy_test.go index cec1fbf96c1..eab2603f1cd 100644 --- a/pkg/ovs/ovn-nb-logical_router_policy_test.go +++ b/pkg/ovs/ovn-nb-logical_router_policy_test.go @@ -11,23 +11,15 @@ import ( "github.com/kubeovn/kube-ovn/pkg/util" ) -func newLogicalRouterPolicy(lrName string, priority int, match, action string, nextHops []string, externalIDs map[string]string) *ovnnb.LogicalRouterPolicy { - policy := &ovnnb.LogicalRouterPolicy{ - UUID: ovsclient.NamedUUID(), - Priority: priority, - Match: match, - Action: action, - Nexthops: nextHops, - ExternalIDs: map[string]string{ - logicalRouterKey: lrName, - }, +func newLogicalRouterPolicy(priority int, match, action string, nextHops []string, externalIDs map[string]string) *ovnnb.LogicalRouterPolicy { + return &ovnnb.LogicalRouterPolicy{ + UUID: ovsclient.NamedUUID(), + Priority: priority, + Match: match, + Action: action, + Nexthops: nextHops, + ExternalIDs: externalIDs, } - - for k, v := range externalIDs { - policy.ExternalIDs[k] = v - } - - return policy } func (suite *OvnClientTestSuite) testAddLogicalRouterPolicy() { @@ -76,8 +68,7 @@ func (suite *OvnClientTestSuite) testCreateLogicalRouterPolicies() { t.Run("add policies to logical router", func(t *testing.T) { for i := 0; i < 3; i++ { match := fmt.Sprintf("%s && tcp.dst == %d", matchPrefix, basePort+i) - policy, err := ovnClient.newLogicalRouterPolicy(lrName, priority, match, action, nil, nil) - require.NoError(t, err) + policy := ovnClient.newLogicalRouterPolicy(priority, match, action, nil, nil) policies = append(policies, policy) } @@ -166,7 +157,7 @@ func (suite *OvnClientTestSuite) testDeleteLogicalRouterPolicies() { require.NoError(t, err) require.Len(t, lr.Policies, 3) - policies, err := ovnClient.ListLogicalRouterPolicies(-1, externalIDs) + policies, err := ovnClient.ListLogicalRouterPolicies(lrName, -1, externalIDs) require.NoError(t, err) require.Len(t, policies, 3) } @@ -181,7 +172,7 @@ func (suite *OvnClientTestSuite) testDeleteLogicalRouterPolicies() { require.NoError(t, err) require.Empty(t, lr.Policies) - policies, err := ovnClient.ListLogicalRouterPolicies(-1, externalIDs) + policies, err := ovnClient.ListLogicalRouterPolicies(lrName, -1, externalIDs) require.NoError(t, err) require.Empty(t, policies) }) @@ -197,7 +188,7 @@ func (suite *OvnClientTestSuite) testDeleteLogicalRouterPolicies() { require.Len(t, lr.Policies, 2) // no basePriority policy - policies, err := ovnClient.ListLogicalRouterPolicies(-1, externalIDs) + policies, err := ovnClient.ListLogicalRouterPolicies(lrName, -1, externalIDs) require.NoError(t, err) require.Len(t, policies, 2) }) @@ -256,7 +247,10 @@ func (suite *OvnClientTestSuite) testGetLogicalRouterPolicy() { priority := 11000 match := "ip4.src == $ovn.default.lm2_ip4" - err := ovnClient.CreateBareLogicalRouterPolicy(lrName, priority, match, ovnnb.LogicalRouterPolicyActionAllow, nil) + err := ovnClient.CreateLogicalRouter(lrName) + require.NoError(t, err) + + err = ovnClient.AddLogicalRouterPolicy(lrName, priority, match, ovnnb.LogicalRouterPolicyActionAllow, nil, nil) require.NoError(t, err) t.Run("priority and match are same", func(t *testing.T) { @@ -285,11 +279,11 @@ func (suite *OvnClientTestSuite) testGetLogicalRouterPolicy() { require.NoError(t, err) }) - t.Run("no acl belongs to parent exist", func(t *testing.T) { + t.Run("no policy belongs to parent exist", func(t *testing.T) { t.Parallel() _, err := ovnClient.GetLogicalRouterPolicy(lrName+"_1", priority, match, false) - require.ErrorContains(t, err, "not found policy") + require.ErrorContains(t, err, "not found logical router") }) } @@ -304,19 +298,18 @@ func (suite *OvnClientTestSuite) test_newLogicalRouterPolicy() { nextHops := []string{"100.64.0.2"} action := ovnnb.LogicalRouterPolicyActionAllow + err := ovnClient.CreateLogicalRouter(lrName) + require.NoError(t, err) + expect := &ovnnb.LogicalRouterPolicy{ - Priority: priority, - Match: match, - Action: action, - Nexthops: nextHops, - ExternalIDs: map[string]string{ - logicalRouterKey: lrName, - "key": "value", - }, + Priority: priority, + Match: match, + Action: action, + Nexthops: nextHops, + ExternalIDs: map[string]string{"key": "value"}, } - policy, err := ovnClient.newLogicalRouterPolicy(lrName, priority, match, action, nextHops, map[string]string{"key": "value"}) - require.NoError(t, err) + policy := ovnClient.newLogicalRouterPolicy(priority, match, action, nextHops, map[string]string{"key": "value"}) expect.UUID = policy.UUID require.Equal(t, expect, policy) } @@ -325,7 +318,6 @@ func (suite *OvnClientTestSuite) test_policyFilter() { t := suite.T() t.Parallel() - lrName := "test-filter-policy-lr" basePriority := 10000 match := "ip4.src == $ovn.default.lm2_ip4" nextHops := []string{"100.64.0.2"} @@ -335,15 +327,14 @@ func (suite *OvnClientTestSuite) test_policyFilter() { // create three polices for i := 0; i < 3; i++ { priority := basePriority + i - policy := newLogicalRouterPolicy(lrName, priority, match, action, nextHops, nil) + policy := newLogicalRouterPolicy(priority, match, action, nextHops, map[string]string{"k1": "v1"}) policies = append(policies, policy) } - // create two polices with other logical router key + // create two polices with different external-ids for i := 0; i < 2; i++ { priority := basePriority + i - policy := newLogicalRouterPolicy(lrName, priority, match, action, nextHops, nil) - policy.ExternalIDs[logicalRouterKey] = lrName + "-test" + policy := newLogicalRouterPolicy(priority, match, action, nextHops, map[string]string{"k1": "v2"}) policies = append(policies, policy) } @@ -355,40 +346,39 @@ func (suite *OvnClientTestSuite) test_policyFilter() { count++ } } - require.Equal(t, count, 5) + require.Equal(t, 5, count) }) t.Run("include all policies with external ids", func(t *testing.T) { - filterFunc := policyFilter(-1, map[string]string{logicalRouterKey: lrName}) + filterFunc := policyFilter(-1, map[string]string{"k1": "v1"}) count := 0 for _, policy := range policies { if filterFunc(policy) { count++ } } - require.Equal(t, count, 3) + require.Equal(t, 3, count) }) t.Run("include all policies with same priority", func(t *testing.T) { - filterFunc := policyFilter(10000, map[string]string{logicalRouterKey: lrName}) + filterFunc := policyFilter(10000, map[string]string{"k1": "v1"}) count := 0 for _, policy := range policies { if filterFunc(policy) { count++ } } - require.Equal(t, count, 1) + require.Equal(t, 1, count) }) t.Run("result should exclude policies when externalIDs's length is not equal", func(t *testing.T) { t.Parallel() - policy := newLogicalRouterPolicy(lrName, basePriority+10, match, action, nextHops, nil) + policy := newLogicalRouterPolicy(basePriority+10, match, action, nextHops, map[string]string{"k1": "v1"}) filterFunc := policyFilter(-1, map[string]string{ - logicalRouterKey: lrName, - "key": "value", + "k1": "v1", + "key": "value", }) - require.False(t, filterFunc(policy)) }) } diff --git a/pkg/ovs/ovn-nb-logical_router_port.go b/pkg/ovs/ovn-nb-logical_router_port.go index c61cf1e3eb2..862c5691cc3 100644 --- a/pkg/ovs/ovn-nb-logical_router_port.go +++ b/pkg/ovs/ovn-nb-logical_router_port.go @@ -167,7 +167,7 @@ func (c *ovnClient) DeleteLogicalRouterPort(lrpName string) error { return nil } -// GetLogicalRouterPort get logical router port by name, +// GetLogicalRouterPort get logical router port by name func (c *ovnClient) GetLogicalRouterPort(lrpName string, ignoreNotFound bool) (*ovnnb.LogicalRouterPort, error) { ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) defer cancel() @@ -184,6 +184,19 @@ func (c *ovnClient) GetLogicalRouterPort(lrpName string, ignoreNotFound bool) (* return lrp, nil } +// GetLogicalRouterPortByUUID get logical router port by UUID +func (c *ovnClient) GetLogicalRouterPortByUUID(uuid string) (*ovnnb.LogicalRouterPort, error) { + ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) + defer cancel() + + lrp := &ovnnb.LogicalRouterPort{UUID: uuid} + if err := c.Get(ctx, lrp); err != nil { + return nil, fmt.Errorf("get logical router port by UUID %s: %v", uuid, err) + } + + return lrp, nil +} + // ListLogicalRouterPorts list logical router ports func (c *ovnClient) ListLogicalRouterPorts(externalIDs map[string]string, filter func(lrp *ovnnb.LogicalRouterPort) bool) ([]ovnnb.LogicalRouterPort, error) { ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) @@ -269,22 +282,7 @@ func (c *ovnClient) DeleteLogicalRouterPortOp(lrpName string) ([]ovsdb.Operation // remove logical router port from logical router lrName := lrp.ExternalIDs[logicalRouterKey] - lrpRemoveOp, err := c.LogicalRouterUpdatePortOp(lrName, lrp.UUID, ovsdb.MutateOperationDelete) - if err != nil { - return nil, err - } - - // delete logical router port - lrpDelOp, err := c.Where(lrp).Delete() - if err != nil { - return nil, err - } - - ops := make([]ovsdb.Operation, 0, len(lrpRemoveOp)+len(lrpDelOp)) - ops = append(ops, lrpRemoveOp...) - ops = append(ops, lrpDelOp...) - - return ops, nil + return c.LogicalRouterUpdatePortOp(lrName, lrp.UUID, ovsdb.MutateOperationDelete) } // LogicalRouterPortOp create operations about logical router port @@ -325,7 +323,7 @@ func logicalRouterPortFilter(externalIDs map[string]string, filter func(lrp *ovn if len(lrp.ExternalIDs) != 0 { for k, v := range externalIDs { - // if only key exist but not value in externalIDs, we should include this lsp, + // if only key exist but not value in externalIDs, we should include this lrp, // it's equal to shell command `ovn-nbctl --columns=xx find logical_router_port external_ids:key!=\"\"` if len(v) == 0 { if len(lrp.ExternalIDs[k]) == 0 { diff --git a/pkg/ovs/ovn-nb-logical_router_port_test.go b/pkg/ovs/ovn-nb-logical_router_port_test.go index 93673719cc9..09397f687b8 100644 --- a/pkg/ovs/ovn-nb-logical_router_port_test.go +++ b/pkg/ovs/ovn-nb-logical_router_port_test.go @@ -284,12 +284,6 @@ func (suite *OvnClientTestSuite) testDeleteLogicalRouterPorts() { require.NoError(t, err) - for i := 0; i < 3; i++ { - lrpName := fmt.Sprintf("%s-%d", prefix, i) - _, err := ovnClient.GetLogicalRouterPort(lrpName, false) - require.ErrorContains(t, err, "object not found") - } - lr, err = ovnClient.GetLogicalRouter(lrName, false) require.NoError(t, err) require.Empty(t, lr.Ports) @@ -320,9 +314,6 @@ func (suite *OvnClientTestSuite) testDeleteLogicalRouterPort() { err = ovnClient.DeleteLogicalRouterPort(lrpName) require.NoError(t, err) - _, err = ovnClient.GetLogicalRouterPort(lrpName, false) - require.ErrorContains(t, err, "object not found") - lr, err = ovnClient.GetLogicalRouter(lrName, false) require.NoError(t, err) require.NotContains(t, lr.Ports, lrp.UUID) @@ -434,7 +425,7 @@ func (suite *OvnClientTestSuite) testDeleteLogicalRouterPortOp() { ops, err := ovnClient.DeleteLogicalRouterPortOp(lrpName) require.NoError(t, err) - require.Len(t, ops, 2) + require.Len(t, ops, 1) require.Equal(t, []ovsdb.Mutation{ @@ -450,21 +441,6 @@ func (suite *OvnClientTestSuite) testDeleteLogicalRouterPortOp() { }, }, }, ops[0].Mutations) - - require.Equal(t, - ovsdb.Operation{ - Op: "delete", - Table: "Logical_Router_Port", - Where: []ovsdb.Condition{ - { - Column: "_uuid", - Function: "==", - Value: ovsdb.UUID{ - GoUUID: lrp.UUID, - }, - }, - }, - }, ops[1]) } func (suite *OvnClientTestSuite) testLogicalRouterPortOp() { diff --git a/pkg/ovs/ovn-nb-logical_router_route.go b/pkg/ovs/ovn-nb-logical_router_route.go index dc9058cfc9f..8e1290f7fc3 100644 --- a/pkg/ovs/ovn-nb-logical_router_route.go +++ b/pkg/ovs/ovn-nb-logical_router_route.go @@ -5,7 +5,6 @@ import ( "fmt" "strings" - "github.com/ovn-org/libovsdb/client" "github.com/ovn-org/libovsdb/model" "github.com/ovn-org/libovsdb/ovsdb" @@ -14,60 +13,11 @@ import ( "github.com/kubeovn/kube-ovn/pkg/util" ) -func (c *ovnClient) GetLogicalRouterRouteByOpts(key, value string) ([]ovnnb.LogicalRouterStaticRoute, error) { - ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) - defer cancel() - - api, err := c.ovnNbClient.WherePredict(ctx, func(r *ovnnb.LogicalRouterStaticRoute) bool { - return r.Options[key] == value - }) - if err != nil { - return nil, err +func (c *ovnClient) ListLogicalRouterStaticRoutesByOption(lrName, key, value string) ([]*ovnnb.LogicalRouterStaticRoute, error) { + fnFilter := func(route *ovnnb.LogicalRouterStaticRoute) bool { + return len(route.Options) != 0 && route.Options[key] == value } - - var lrRouteList []ovnnb.LogicalRouterStaticRoute - if err = api.List(ctx, &lrRouteList); err != nil && err != client.ErrNotFound { - return nil, err - } - - return lrRouteList, nil -} - -func (c *ovnClient) GetLogicalRouterPoliciesByExtID(key, value string) ([]ovnnb.LogicalRouterPolicy, error) { - ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) - defer cancel() - - api, err := c.ovnNbClient.WherePredict(ctx, func(p *ovnnb.LogicalRouterPolicy) bool { - return p.ExternalIDs[key] == value - }) - if err != nil { - return nil, err - } - - var lrPolicyList []ovnnb.LogicalRouterPolicy - if err = api.List(ctx, &lrPolicyList); err != nil && err != client.ErrNotFound { - return nil, err - } - - return lrPolicyList, nil -} - -func (c *ovnClient) CreateBareLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, routeType string) error { - route, err := c.newLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, routeType) - if err != nil { - return err - } - - op, err := c.ovnNbClient.Create(route) - if err != nil { - return fmt.Errorf("generate operations for creating logical router static route: %v", err) - } - - if err = c.Transact("lr-route-add", op); err != nil { - return fmt.Errorf("create logical router static route: %v", err) - } - - return nil + return c.listLogicalRouterStaticRoutesByFilter(lrName, fnFilter) } // CreateLogicalRouterStaticRoutes create several logical router static route once @@ -192,21 +142,10 @@ func (c *ovnClient) DeleteLogicalRouterStaticRoute(lrName, policy, prefix, nextH } // remove static route from logical router - routeRemoveOp, err := c.LogicalRouterUpdateStaticRouteOp(lrName, []string{route.UUID}, ovsdb.MutateOperationDelete) + ops, err := c.LogicalRouterUpdateStaticRouteOp(lrName, []string{route.UUID}, ovsdb.MutateOperationDelete) if err != nil { return fmt.Errorf("generate operations for removing static %s from logical router %s: %v", route.UUID, lrName, err) } - - // delete static route - routeDelOp, err := c.Where(route).Delete() - if err != nil { - return fmt.Errorf("generate operations for deleting static route %s: %v", route.UUID, err) - } - - ops := make([]ovsdb.Operation, 0, len(routeRemoveOp)+len(routeDelOp)) - ops = append(ops, routeRemoveOp...) - ops = append(ops, routeDelOp...) - if err = c.Transact("lr-route-del", ops); err != nil { return fmt.Errorf("delete static route %s", route.UUID) } @@ -223,23 +162,10 @@ func (c *ovnClient) ClearLogicalRouterStaticRoute(lrName string) error { // clear static route lr.StaticRoutes = nil - routeClearOp, err := c.UpdateLogicalRouterOp(lr, &lr.StaticRoutes) + ops, err := c.UpdateLogicalRouterOp(lr, &lr.StaticRoutes) if err != nil { return fmt.Errorf("generate operations for clear logical router %s static route: %v", lrName, err) } - - // delete static route - routeDelOp, err := c.WhereCache(func(route *ovnnb.LogicalRouterStaticRoute) bool { - return len(route.ExternalIDs) != 0 && route.ExternalIDs[logicalRouterKey] == lrName - }).Delete() - if err != nil { - return fmt.Errorf("generate operations for deleting logical router %s static routes: %v", lrName, err) - } - - ops := make([]ovsdb.Operation, 0, len(routeClearOp)+len(routeDelOp)) - ops = append(ops, routeClearOp...) - ops = append(ops, routeDelOp...) - if err = c.Transact("lr-route-clear", ops); err != nil { return fmt.Errorf("clear logical router %s static routes: %v", lrName, err) } @@ -247,6 +173,19 @@ func (c *ovnClient) ClearLogicalRouterStaticRoute(lrName string) error { return nil } +// GetLogicalRouterStaticRouteByUUID get logical router static route by UUID +func (c *ovnClient) GetLogicalRouterStaticRouteByUUID(uuid string) (*ovnnb.LogicalRouterStaticRoute, error) { + ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) + defer cancel() + + route := &ovnnb.LogicalRouterStaticRoute{UUID: uuid} + if err := c.Get(ctx, route); err != nil { + return nil, fmt.Errorf("get logical router static route by UUID %s: %v", uuid, err) + } + + return route, nil +} + // GetLogicalRouterStaticRoute get logical router static route by some attribute, // a static route is uniquely identified by router(lrName), policy and prefix when route is not ecmp // a static route is uniquely identified by router(lrName), policy, prefix and nextHop when route is ecmp @@ -256,15 +195,7 @@ func (c *ovnClient) GetLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, return nil, fmt.Errorf("the logical router name is required") } - ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) - defer cancel() - - routeList := make([]ovnnb.LogicalRouterStaticRoute, 0) - if err := c.ovnNbClient.WhereCache(func(route *ovnnb.LogicalRouterStaticRoute) bool { - if len(route.ExternalIDs) == 0 || route.ExternalIDs[logicalRouterKey] != lrName { - return false - } - + fnFilter := func(route *ovnnb.LogicalRouterStaticRoute) bool { // ecmp route if routeType == util.EcmpRouteType { return route.Policy != nil && *route.Policy == policy && route.IPPrefix == prefix && route.Nexthop == nextHop @@ -272,8 +203,9 @@ func (c *ovnClient) GetLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, // normal route return route.Policy != nil && *route.Policy == policy && route.IPPrefix == prefix - - }).List(ctx, &routeList); err != nil { + } + routeList, err := c.listLogicalRouterStaticRoutesByFilter(lrName, fnFilter) + if err != nil { return nil, fmt.Errorf("get logical router %s static route 'policy %s prefix %s nextHop %s': %v", lrName, policy, prefix, nextHop, err) } @@ -282,7 +214,6 @@ func (c *ovnClient) GetLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, if ignoreNotFound { return nil, nil } - return nil, fmt.Errorf("not found logical router %s static route 'policy %s prefix %s nextHop %s'", lrName, policy, prefix, nextHop) } @@ -290,17 +221,12 @@ func (c *ovnClient) GetLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, return nil, fmt.Errorf("more than one static route 'policy %s prefix %s nextHop %s' in logical router %s", policy, prefix, nextHop, lrName) } - return &routeList[0], nil + return routeList[0], nil } // ListLogicalRouterStaticRoutes list route which match the given externalIDs -func (c *ovnClient) ListLogicalRouterStaticRoutes(externalIDs map[string]string) ([]ovnnb.LogicalRouterStaticRoute, error) { - ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) - defer cancel() - - routeList := make([]ovnnb.LogicalRouterStaticRoute, 0) - - if err := c.WhereCache(func(route *ovnnb.LogicalRouterStaticRoute) bool { +func (c *ovnClient) ListLogicalRouterStaticRoutes(lrName string, externalIDs map[string]string) ([]*ovnnb.LogicalRouterStaticRoute, error) { + fnFilter := func(route *ovnnb.LogicalRouterStaticRoute) bool { if len(route.ExternalIDs) < len(externalIDs) { return false } @@ -322,11 +248,9 @@ func (c *ovnClient) ListLogicalRouterStaticRoutes(externalIDs map[string]string) } return true - }).List(ctx, &routeList); err != nil { - return nil, fmt.Errorf("list logical router static routes: %v", err) } - return routeList, nil + return c.listLogicalRouterStaticRoutesByFilter(lrName, fnFilter) } func (c *ovnClient) LogicalRouterStaticRouteExists(lrName, policy, prefix, nextHop, routeType string) (bool, error) { @@ -359,9 +283,6 @@ func (c *ovnClient) newLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, Policy: &policy, IPPrefix: prefix, Nexthop: nextHop, - ExternalIDs: map[string]string{ - logicalRouterKey: lrName, - }, } for _, option := range options { @@ -370,3 +291,23 @@ func (c *ovnClient) newLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, return route, nil } + +func (c *ovnClient) listLogicalRouterStaticRoutesByFilter(lrName string, filter func(route *ovnnb.LogicalRouterStaticRoute) bool) ([]*ovnnb.LogicalRouterStaticRoute, error) { + lr, err := c.GetLogicalRouter(lrName, false) + if err != nil { + return nil, err + } + + routeList := make([]*ovnnb.LogicalRouterStaticRoute, 0, len(lr.Policies)) + for _, uuid := range lr.StaticRoutes { + route, err := c.GetLogicalRouterStaticRouteByUUID(uuid) + if err != nil { + return nil, err + } + if filter == nil || filter(route) { + routeList = append(routeList, route) + } + } + + return routeList, nil +} diff --git a/pkg/ovs/ovn-nb-logical_router_route_test.go b/pkg/ovs/ovn-nb-logical_router_route_test.go index bdc767dbf34..ca9b127bb86 100644 --- a/pkg/ovs/ovn-nb-logical_router_route_test.go +++ b/pkg/ovs/ovn-nb-logical_router_route_test.go @@ -262,6 +262,9 @@ func (suite *OvnClientTestSuite) testGetLogicalRouterStaticRoute() { ovnClient := suite.ovnClient lrName := "test_get_route_lr" + err := ovnClient.CreateLogicalRouter(lrName) + require.NoError(t, err) + t.Run("normal route", func(t *testing.T) { t.Parallel() policy := ovnnb.LogicalRouterStaticRoutePolicyDstIP @@ -269,7 +272,7 @@ func (suite *OvnClientTestSuite) testGetLogicalRouterStaticRoute() { nextHop := "192.168.30.1" routeType := util.NormalRouteType - err := ovnClient.CreateBareLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, routeType) + err := ovnClient.AddLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, routeType) require.NoError(t, err) t.Run("found route", func(t *testing.T) { @@ -300,7 +303,7 @@ func (suite *OvnClientTestSuite) testGetLogicalRouterStaticRoute() { nextHop := "192.168.40.1" routeType := util.EcmpRouteType - err := ovnClient.CreateBareLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, routeType) + err := ovnClient.AddLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, routeType) require.NoError(t, err) t.Run("found route", func(t *testing.T) { @@ -340,7 +343,7 @@ func (suite *OvnClientTestSuite) testListLogicalRouterStaticRoutes() { require.NoError(t, err) t.Run("include same router routes", func(t *testing.T) { - out, err := ovnClient.ListLogicalRouterStaticRoutes(map[string]string{logicalRouterKey: lrName}) + out, err := ovnClient.ListLogicalRouterStaticRoutes(lrName, nil) require.NoError(t, err) require.Len(t, out, 3) }) @@ -357,13 +360,13 @@ func (suite *OvnClientTestSuite) test_newLogicalRouterStaticRoute() { nextHop := "192.168.30.1" routeType := util.NormalRouteType + err := ovnClient.CreateLogicalRouter(lrName) + require.NoError(t, err) + expect := &ovnnb.LogicalRouterStaticRoute{ Policy: &policy, IPPrefix: prefix, Nexthop: nextHop, - ExternalIDs: map[string]string{ - logicalRouterKey: lrName, - }, } route, err := ovnClient.newLogicalRouterStaticRoute(lrName, policy, prefix, nextHop, routeType) diff --git a/pkg/ovs/ovn-nb-logical_switch_port.go b/pkg/ovs/ovn-nb-logical_switch_port.go index 20d34037448..00686199e1c 100644 --- a/pkg/ovs/ovn-nb-logical_switch_port.go +++ b/pkg/ovs/ovn-nb-logical_switch_port.go @@ -570,21 +570,10 @@ func (c *ovnClient) DeleteLogicalSwitchPortOp(lspName string) ([]ovsdb.Operation // remove logical switch port from logical switch lsName := lsp.ExternalIDs[logicalSwitchKey] - lspRemoveOp, err := c.LogicalSwitchUpdatePortOp(lsName, lsp.UUID, ovsdb.MutateOperationDelete) + ops, err := c.LogicalSwitchUpdatePortOp(lsName, lsp.UUID, ovsdb.MutateOperationDelete) if err != nil { return nil, fmt.Errorf("generate operations for removing port %s from logical switch %s: %v", lspName, lsName, err) } - - // delete logical switch port - lspDelOp, err := c.Where(lsp).Delete() - if err != nil { - return nil, fmt.Errorf("generate operations for deleting logical switch port %s: %v", lspName, err) - } - - ops := make([]ovsdb.Operation, 0, len(lspRemoveOp)+len(lspDelOp)) - ops = append(ops, lspRemoveOp...) - ops = append(ops, lspDelOp...) - return ops, nil } diff --git a/pkg/ovs/ovn-nb-logical_switch_port_test.go b/pkg/ovs/ovn-nb-logical_switch_port_test.go index 0487db978db..c60ed3fc41e 100644 --- a/pkg/ovs/ovn-nb-logical_switch_port_test.go +++ b/pkg/ovs/ovn-nb-logical_switch_port_test.go @@ -1107,9 +1107,6 @@ func (suite *OvnClientTestSuite) testDeleteLogicalSwitchPort() { err = ovnClient.DeleteLogicalSwitchPort(lspName) require.NoError(t, err) - _, err = ovnClient.GetLogicalSwitchPort(lspName, false) - require.ErrorContains(t, err, "object not found") - ls, err = ovnClient.GetLogicalSwitch(lsName, false) require.NoError(t, err) require.NotContains(t, ls.Ports, lsp.UUID) @@ -1222,7 +1219,7 @@ func (suite *OvnClientTestSuite) testDeleteLogicalSwitchPortOp() { ops, err := ovnClient.DeleteLogicalSwitchPortOp(lspName) require.NoError(t, err) - require.Len(t, ops, 2) + require.Len(t, ops, 1) require.Equal(t, []ovsdb.Mutation{ { @@ -1237,21 +1234,6 @@ func (suite *OvnClientTestSuite) testDeleteLogicalSwitchPortOp() { }, }, }, ops[0].Mutations) - - require.Equal(t, - ovsdb.Operation{ - Op: "delete", - Table: "Logical_Switch_Port", - Where: []ovsdb.Condition{ - { - Column: "_uuid", - Function: "==", - Value: ovsdb.UUID{ - GoUUID: lsp.UUID, - }, - }, - }, - }, ops[1]) } func (suite *OvnClientTestSuite) testlogicalSwitchPortFilter() { diff --git a/pkg/ovs/ovn-nb-logical_switch_test.go b/pkg/ovs/ovn-nb-logical_switch_test.go index a5dc9fe925b..b9ec6322c88 100644 --- a/pkg/ovs/ovn-nb-logical_switch_test.go +++ b/pkg/ovs/ovn-nb-logical_switch_test.go @@ -66,12 +66,6 @@ func (suite *OvnClientTestSuite) testCreateLogicalSwitch() { t.Run("remove router type port when needRouter is false", func(t *testing.T) { err = ovnClient.CreateLogicalSwitch(lsName, lrName, "192.168.2.0/24,fd00::c0a8:9900/120", "192.168.2.1,fd00::c0a8:9901", false, false) require.NoError(t, err) - - _, err = ovnClient.GetLogicalSwitchPort(lspName, false) - require.ErrorContains(t, err, "object not found") - - _, err = ovnClient.GetLogicalRouterPort(lrpName, false) - require.ErrorContains(t, err, "object not found") }) t.Run("should no err when router type port doest't exist", func(t *testing.T) { diff --git a/pkg/ovs/ovn-nb-nat.go b/pkg/ovs/ovn-nb-nat.go index 594924062de..547d9749d50 100644 --- a/pkg/ovs/ovn-nb-nat.go +++ b/pkg/ovs/ovn-nb-nat.go @@ -12,22 +12,13 @@ import ( "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb" ) -func (c *ovnClient) CreateBareNat(lrName, natType, externalIP, logicalIP string) error { +func (c *ovnClient) AddNat(lrName, natType, externalIP, logicalIP string) error { nat, err := c.newNat(lrName, natType, externalIP, logicalIP) if err != nil { return err } - op, err := c.ovnNbClient.Create(nat) - if err != nil { - return fmt.Errorf("generate operations for creating nat: %v", err) - } - - if err = c.Transact("lr-nat-add", op); err != nil { - return fmt.Errorf("create nat: %v", err) - } - - return nil + return c.CreateNats(lrName, nat) } // CreateNats create several logical router nat rule once @@ -158,10 +149,8 @@ func (c *ovnClient) UpdateNat(nat *ovnnb.NAT, fields ...interface{}) error { // DeleteNat delete several nat rule once func (c *ovnClient) DeleteNats(lrName, natType, logicalIP string) error { - externalIDs := map[string]string{logicalRouterKey: lrName} - /* delete nats from logical router */ - nats, err := c.ListNats(natType, logicalIP, externalIDs) + nats, err := c.ListNats(lrName, natType, logicalIP, nil) if err != nil { return fmt.Errorf("list logical router %s nats 'type %s logical ip %s': %v", lrName, natType, logicalIP, err) } @@ -171,21 +160,10 @@ func (c *ovnClient) DeleteNats(lrName, natType, logicalIP string) error { natsUUIDs = append(natsUUIDs, nat.UUID) } - removeNatOp, err := c.LogicalRouterUpdateNatOp(lrName, natsUUIDs, ovsdb.MutateOperationDelete) + ops, err := c.LogicalRouterUpdateNatOp(lrName, natsUUIDs, ovsdb.MutateOperationDelete) if err != nil { return fmt.Errorf("generate operations for deleting nats from logical router %s: %v", lrName, err) } - - // delete nats - delNatsOp, err := c.WhereCache(natFilter(natType, logicalIP, externalIDs)).Delete() - if err != nil { - return fmt.Errorf("generate operation for deleting nats: %v", err) - } - - ops := make([]ovsdb.Operation, 0, len(removeNatOp)+len(delNatsOp)) - ops = append(ops, removeNatOp...) - ops = append(ops, delNatsOp...) - if err = c.Transact("nats-del", ops); err != nil { return fmt.Errorf("del nats from logical router %s: %v", lrName, err) } @@ -201,21 +179,10 @@ func (c *ovnClient) DeleteNat(lrName, natType, externalIP, logicalIP string) err } // remove nat from logical router - removeNatOp, err := c.LogicalRouterUpdateNatOp(lrName, []string{nat.UUID}, ovsdb.MutateOperationDelete) + ops, err := c.LogicalRouterUpdateNatOp(lrName, []string{nat.UUID}, ovsdb.MutateOperationDelete) if err != nil { return fmt.Errorf("generate operations for deleting nat from logical router %s: %v", lrName, err) } - - // delete nat - delNatsOp, err := c.Where(nat).Delete() - if err != nil { - return fmt.Errorf("generate operation for deleting nat: %v", err) - } - - ops := make([]ovsdb.Operation, 0, len(removeNatOp)+len(delNatsOp)) - ops = append(ops, removeNatOp...) - ops = append(ops, delNatsOp...) - if err = c.Transact("lr-nat-del", ops); err != nil { return fmt.Errorf("del nat from logical router %s: %v", lrName, err) } @@ -223,6 +190,19 @@ func (c *ovnClient) DeleteNat(lrName, natType, externalIP, logicalIP string) err return nil } +// GetNATByUUID get NAT by UUID +func (c *ovnClient) GetNATByUUID(uuid string) (*ovnnb.NAT, error) { + ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) + defer cancel() + + nat := &ovnnb.NAT{UUID: uuid} + if err := c.Get(ctx, nat); err != nil { + return nil, fmt.Errorf("get NAT by UUID %s: %v", uuid, err) + } + + return nat, nil +} + // GetNat get nat by some attribute, // a nat rule is uniquely identified by router(lrName), type(natType) and logical_ip when snat // a nat rule is uniquely identified by router(lrName), type(natType) and external_ip when dnat_and_snat @@ -232,25 +212,18 @@ func (c *ovnClient) GetNat(lrName, natType, externalIP, logicalIP string, ignore return nil, fmt.Errorf("the logical router name is required") } - ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) - defer cancel() - if natType == ovnnb.NATTypeDNAT { return nil, fmt.Errorf("does not support dnat for now") } - natList := make([]ovnnb.NAT, 0) - if err := c.ovnNbClient.WhereCache(func(nat *ovnnb.NAT) bool { - if len(nat.ExternalIDs) == 0 || nat.ExternalIDs[logicalRouterKey] != lrName { - return false - } - + fnFilter := func(nat *ovnnb.NAT) bool { if natType == ovnnb.NATTypeSNAT { return nat.Type == natType && nat.LogicalIP == logicalIP } - return nat.Type == natType && nat.ExternalIP == externalIP - }).List(ctx, &natList); err != nil { + } + natList, err := c.listLogicalRouterNatByFilter(lrName, fnFilter) + if err != nil { return nil, fmt.Errorf("get logical router %s nat 'type %s external ip %s logical ip %s': %v", lrName, natType, externalIP, logicalIP, err) } @@ -259,7 +232,6 @@ func (c *ovnClient) GetNat(lrName, natType, externalIP, logicalIP string, ignore if ignoreNotFound { return nil, nil } - return nil, fmt.Errorf("not found logical router %s nat 'type %s external ip %s logical ip %s'", lrName, natType, externalIP, logicalIP) } @@ -267,21 +239,12 @@ func (c *ovnClient) GetNat(lrName, natType, externalIP, logicalIP string, ignore return nil, fmt.Errorf("more than one nat 'type %s external ip %s logical ip %s' in logical router %s", natType, externalIP, logicalIP, lrName) } - return &natList[0], nil + return natList[0], nil } // ListNats list acls which match the given externalIDs -func (c *ovnClient) ListNats(natType, logicalIP string, externalIDs map[string]string) ([]ovnnb.NAT, error) { - ctx, cancel := context.WithTimeout(context.Background(), c.Timeout) - defer cancel() - - natList := make([]ovnnb.NAT, 0) - - if err := c.WhereCache(natFilter(natType, logicalIP, externalIDs)).List(ctx, &natList); err != nil { - return nil, fmt.Errorf("list acls: %v", err) - } - - return natList, nil +func (c *ovnClient) ListNats(lrName, natType, logicalIP string, externalIDs map[string]string) ([]*ovnnb.NAT, error) { + return c.listLogicalRouterNatByFilter(lrName, natFilter(natType, logicalIP, externalIDs)) } func (c *ovnClient) NatExists(lrName, natType, externalIP, logicalIP string) (bool, error) { @@ -318,9 +281,6 @@ func (c *ovnClient) newNat(lrName, natType, externalIP, logicalIP string, option Type: natType, ExternalIP: externalIP, LogicalIP: logicalIP, - ExternalIDs: map[string]string{ - logicalRouterKey: lrName, - }, } for _, option := range options { @@ -367,3 +327,23 @@ func natFilter(natType, logicalIP string, externalIDs map[string]string) func(na return true } } + +func (c *ovnClient) listLogicalRouterNatByFilter(lrName string, filter func(route *ovnnb.NAT) bool) ([]*ovnnb.NAT, error) { + lr, err := c.GetLogicalRouter(lrName, false) + if err != nil { + return nil, err + } + + natList := make([]*ovnnb.NAT, 0, len(lr.Nat)) + for _, uuid := range lr.Nat { + nat, err := c.GetNATByUUID(uuid) + if err != nil { + return nil, err + } + if filter == nil || filter(nat) { + natList = append(natList, nat) + } + } + + return natList, nil +} diff --git a/pkg/ovs/ovn-nb-nat_test.go b/pkg/ovs/ovn-nb-nat_test.go index 9e03905698c..0a0cd0b11e9 100644 --- a/pkg/ovs/ovn-nb-nat_test.go +++ b/pkg/ovs/ovn-nb-nat_test.go @@ -10,15 +10,12 @@ import ( "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb" ) -func newNat(lrName, natType, externalIP, logicalIP string, options ...func(nat *ovnnb.NAT)) *ovnnb.NAT { +func newNat(natType, externalIP, logicalIP string, options ...func(nat *ovnnb.NAT)) *ovnnb.NAT { nat := &ovnnb.NAT{ UUID: ovsclient.NamedUUID(), Type: natType, ExternalIP: externalIP, LogicalIP: logicalIP, - ExternalIDs: map[string]string{ - logicalRouterKey: lrName, - }, } for _, option := range options { @@ -381,13 +378,16 @@ func (suite *OvnClientTestSuite) testGetNat() { ovnClient := suite.ovnClient lrName := "test_get_nat_lr" + err := ovnClient.CreateLogicalRouter(lrName) + require.NoError(t, err) + t.Run("snat", func(t *testing.T) { t.Parallel() natType := "snat" externalIP := "192.168.30.254" logicalIP := "10.250.0.4" - err := ovnClient.CreateBareNat(lrName, natType, externalIP, logicalIP) + err := ovnClient.AddNat(lrName, natType, externalIP, logicalIP) require.NoError(t, err) t.Run("found nat", func(t *testing.T) { @@ -412,7 +412,7 @@ func (suite *OvnClientTestSuite) testGetNat() { externalIP := "192.168.30.254" logicalIP := "10.250.0.4" - err := ovnClient.CreateBareNat(lrName, natType, externalIP, logicalIP) + err := ovnClient.AddNat(lrName, natType, externalIP, logicalIP) require.NoError(t, err) t.Run("found nat", func(t *testing.T) { @@ -437,6 +437,9 @@ func (suite *OvnClientTestSuite) test_newNat() { externalIP := "192.168.30.254" logicalIP := "10.250.0.4" + err := ovnClient.CreateLogicalRouter(lrName) + require.NoError(t, err) + t.Run("new snat rule", func(t *testing.T) { t.Parallel() @@ -444,9 +447,6 @@ func (suite *OvnClientTestSuite) test_newNat() { Type: natType, ExternalIP: externalIP, LogicalIP: logicalIP, - ExternalIDs: map[string]string{ - logicalRouterKey: lrName, - }, } nat, err := ovnClient.newNat(lrName, natType, externalIP, logicalIP) @@ -463,12 +463,9 @@ func (suite *OvnClientTestSuite) test_newNat() { natType := "dnat_and_snat" expect := &ovnnb.NAT{ - Type: natType, - ExternalIP: externalIP, - LogicalIP: logicalIP, - ExternalIDs: map[string]string{ - logicalRouterKey: lrName, - }, + Type: natType, + ExternalIP: externalIP, + LogicalIP: logicalIP, LogicalPort: &lspName, ExternalMAC: &externalMac, } @@ -489,29 +486,29 @@ func (suite *OvnClientTestSuite) test_natFilter() { t := suite.T() t.Parallel() - lrName := "test-filter-nat-lr" externalIPs := []string{"192.168.30.254", "192.168.30.253"} logicalIPs := []string{"10.250.0.4", "10.250.0.5"} nats := make([]*ovnnb.NAT, 0) // create two snat rule for _, logicalIP := range logicalIPs { - nat := newNat(lrName, "snat", externalIPs[0], logicalIP) + nat := newNat("snat", externalIPs[0], logicalIP) + nat.ExternalIDs = map[string]string{"k1": "v1"} nats = append(nats, nat) } // create two dnat_and_snat rule for _, externalIP := range externalIPs { - nat := newNat(lrName, "dnat_and_snat", externalIP, logicalIPs[0]) + nat := newNat("dnat_and_snat", externalIP, logicalIPs[0]) + nat.ExternalIDs = map[string]string{"k1": "v1"} nats = append(nats, nat) } - // create three snat rule with other acl parent key + // create three snat rule with different external-ids for i := 0; i < 3; i++ { - nat := newNat(lrName, "snat", externalIPs[0], logicalIPs[0]) - nat.ExternalIDs[logicalRouterKey] = lrName + "-test" + nat := newNat("snat", externalIPs[0], logicalIPs[0]) + nat.ExternalIDs = map[string]string{"k1": "v2"} nats = append(nats, nat) - } t.Run("include all nat", func(t *testing.T) { @@ -526,7 +523,7 @@ func (suite *OvnClientTestSuite) test_natFilter() { }) t.Run("include all nat with external ids", func(t *testing.T) { - filterFunc := natFilter("", "", map[string]string{logicalRouterKey: lrName}) + filterFunc := natFilter("", "", map[string]string{"k1": "v1"}) count := 0 for _, nat := range nats { if filterFunc(nat) { @@ -548,7 +545,7 @@ func (suite *OvnClientTestSuite) test_natFilter() { }) t.Run("include snat with external ids", func(t *testing.T) { - filterFunc := natFilter("snat", "", map[string]string{logicalRouterKey: lrName}) + filterFunc := natFilter("snat", "", map[string]string{"k1": "v1"}) count := 0 for _, nat := range nats { if filterFunc(nat) { @@ -570,7 +567,7 @@ func (suite *OvnClientTestSuite) test_natFilter() { }) t.Run("include dnat_and_snat with external ids", func(t *testing.T) { - filterFunc := natFilter("dnat_and_snat", "", map[string]string{logicalRouterKey: lrName}) + filterFunc := natFilter("dnat_and_snat", "", map[string]string{"k1": "v1"}) count := 0 for _, nat := range nats { if filterFunc(nat) { @@ -581,7 +578,7 @@ func (suite *OvnClientTestSuite) test_natFilter() { }) t.Run("include all nat with same logical ip", func(t *testing.T) { - filterFunc := natFilter("", logicalIPs[0], map[string]string{logicalRouterKey: lrName}) + filterFunc := natFilter("", logicalIPs[0], map[string]string{"k1": "v1"}) count := 0 for _, nat := range nats { if filterFunc(nat) { @@ -592,7 +589,7 @@ func (suite *OvnClientTestSuite) test_natFilter() { }) t.Run("include snat with same logical ip", func(t *testing.T) { - filterFunc := natFilter("snat", logicalIPs[0], map[string]string{logicalRouterKey: lrName}) + filterFunc := natFilter("snat", logicalIPs[0], map[string]string{"k1": "v1"}) count := 0 for _, nat := range nats { if filterFunc(nat) { @@ -603,7 +600,7 @@ func (suite *OvnClientTestSuite) test_natFilter() { }) t.Run("include dnat_and_snat with same logical ip", func(t *testing.T) { - filterFunc := natFilter("dnat_and_snat", logicalIPs[0], map[string]string{logicalRouterKey: lrName}) + filterFunc := natFilter("dnat_and_snat", logicalIPs[0], map[string]string{"k1": "v1"}) count := 0 for _, nat := range nats { if filterFunc(nat) { @@ -616,10 +613,10 @@ func (suite *OvnClientTestSuite) test_natFilter() { t.Run("result should exclude nat when externalIDs's length is not equal", func(t *testing.T) { t.Parallel() - nat := newNat(lrName, "snat", externalIPs[0], logicalIPs[0]) + nat := newNat("snat", externalIPs[0], logicalIPs[0]) filterFunc := natFilter("", "", map[string]string{ - logicalRouterKey: lrName, - "key": "value", + "k1": "v1", + "key": "value", }) require.False(t, filterFunc(nat)) diff --git a/pkg/ovs/ovn-nb_test.go b/pkg/ovs/ovn-nb_test.go index 63ab9e0fb26..4bfd03d60c0 100644 --- a/pkg/ovs/ovn-nb_test.go +++ b/pkg/ovs/ovn-nb_test.go @@ -125,14 +125,6 @@ func (suite *OvnClientTestSuite) testRemoveRouterPort() { err = ovnClient.RemoveLogicalPatchPort(lspName, lrpName) require.NoError(t, err) - - /* validate logical switch port*/ - _, err = ovnClient.GetLogicalSwitchPort(lspName, false) - require.ErrorContains(t, err, "object not found") - - /* validate logical router port*/ - _, err = ovnClient.GetLogicalRouterPort(lrpName, false) - require.ErrorContains(t, err, "object not found") }) t.Run("should no err normal del router type port repeatedly", func(t *testing.T) { @@ -148,7 +140,6 @@ func (suite *OvnClientTestSuite) testDeleteLogicalGatewaySwitch() { ovnClient := suite.ovnClient lsName := "test-del-gw-ls" lrName := "test-del-gw-lr" - lrpName := fmt.Sprintf("%s-%s", lrName, lsName) err := ovnClient.CreateLogicalRouter(lrName) require.NoError(t, err) @@ -164,9 +155,6 @@ func (suite *OvnClientTestSuite) testDeleteLogicalGatewaySwitch() { _, err = ovnClient.GetLogicalSwitch(lsName, false) require.ErrorContains(t, err, "not found logical switch") - - _, err = ovnClient.GetLogicalRouterPort(lrpName, false) - require.ErrorContains(t, err, "object not found") } func (suite *OvnClientTestSuite) testDeleteSecurityGroup() { diff --git a/pkg/ovs/ovn-nbctl-legacy.go b/pkg/ovs/ovn-nbctl-legacy.go index 8ea5194a572..2033b9097ca 100644 --- a/pkg/ovs/ovn-nbctl-legacy.go +++ b/pkg/ovs/ovn-nbctl-legacy.go @@ -2,17 +2,13 @@ package ovs import ( "bytes" - "errors" "fmt" "os" "os/exec" "regexp" - "sort" - "strconv" "strings" "time" - "golang.org/x/exp/slices" "k8s.io/klog/v2" kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1" @@ -98,33 +94,6 @@ func (c LegacyClient) CustomFindEntity(entity string, attris []string, args ...s return result, nil } -func (c LegacyClient) GetEntityInfo(entity string, index string, attris []string) (result map[string]string, err error) { - var attrstr strings.Builder - for _, e := range attris { - attrstr.WriteString(e) - attrstr.WriteString(" ") - } - cmd := []string{"get", entity, index, strings.TrimSpace(attrstr.String())} - output, err := c.ovnNbCommand(cmd...) - if err != nil { - klog.Errorf("failed to get attributes from %s %s: %v", entity, index, err) - return nil, err - } - result = make(map[string]string) - if output == "" { - return result, nil - } - lines := strings.Split(output, "\n") - if len(lines) != len(attris) { - klog.Errorf("failed to get attributes from %s %s %s", entity, index, attris) - return nil, errors.New("length abnormal") - } - for i, l := range lines { - result[attris[i]] = l - } - return result, nil -} - type StaticRoute struct { Policy string CIDR string @@ -214,162 +183,6 @@ func (c LegacyClient) AddStaticRoute(policy, cidr, nextHop, ecmp, bfdId, router return nil } -// AddPolicyRoute add a policy route rule in ovn -func (c LegacyClient) AddPolicyRoute(router string, priority int32, match, action, nextHop string, externalIDs map[string]string) error { - consistent, err := c.CheckPolicyRouteNexthopConsistent(match, nextHop, priority) - if err != nil { - return err - } - if consistent { - return nil - } - - klog.Infof("remove inconsistent policy route from router %s: match %s", router, match) - if err := c.DeletePolicyRoute(router, priority, match); err != nil { - klog.Errorf("failed to delete policy route: %v", err) - return err - } - - // lr-policy-add ROUTER PRIORITY MATCH ACTION [NEXTHOP] - args := []string{MayExist, "lr-policy-add", router, strconv.Itoa(int(priority)), match, action} - if nextHop != "" { - args = append(args, nextHop) - } - klog.Infof("add policy route for router %s: priority %d, match %s, nextHop %s", router, priority, match, nextHop) - if _, err := c.ovnNbCommand(args...); err != nil { - return err - } - - if len(externalIDs) == 0 { - return nil - } - - result, err := c.CustomFindEntity("logical_router_policy", []string{"_uuid"}, fmt.Sprintf("priority=%d", priority), fmt.Sprintf(`match="%s"`, match)) - if err != nil { - klog.Errorf("failed to get logical router policy UUID: %v", err) - return err - } - for _, policy := range result { - args := make([]string, 0, len(externalIDs)+3) - args = append(args, "set", "logical_router_policy", policy["_uuid"][0]) - for k, v := range externalIDs { - args = append(args, fmt.Sprintf("external-ids:%s=%v", k, v)) - } - if _, err = c.ovnNbCommand(args...); err != nil { - return fmt.Errorf("failed to set external ids of logical router policy %s: %v", policy["_uuid"][0], err) - } - } - - return nil -} - -// DeletePolicyRoute delete a policy route rule in ovn -func (c LegacyClient) DeletePolicyRoute(router string, priority int32, match string) error { - exist, err := c.IsPolicyRouteExist(router, priority, match) - if err != nil { - return err - } - if !exist { - return nil - } - var args = []string{"lr-policy-del", router} - // lr-policy-del ROUTER [PRIORITY [MATCH]] - if priority > 0 { - args = append(args, strconv.Itoa(int(priority))) - if match != "" { - args = append(args, match) - } - } - klog.Infof("remove policy route from router %s: match %s", router, match) - _, err = c.ovnNbCommand(args...) - return err -} - -func (c LegacyClient) CleanPolicyRoute(router string) error { - // lr-policy-del ROUTER - klog.Infof("clean all policy route for route %s", router) - var args = []string{"lr-policy-del", router} - _, err := c.ovnNbCommand(args...) - return err -} - -func (c LegacyClient) IsPolicyRouteExist(router string, priority int32, match string) (bool, error) { - existPolicyRoute, err := c.GetPolicyRouteList(router) - if err != nil { - return false, err - } - for _, rule := range existPolicyRoute { - if rule.Priority != priority { - continue - } - if match == "" || rule.Match == match { - return true, nil - } - } - return false, nil -} - -func (c LegacyClient) DeletePolicyRouteByNexthop(router string, priority int32, nexthop string) error { - args := []string{ - "--no-heading", "--data=bare", "--columns=match", "find", "Logical_Router_Policy", - fmt.Sprintf("priority=%d", priority), - fmt.Sprintf(`nexthops{=}%s`, strings.ReplaceAll(nexthop, ":", `\:`)), - } - output, err := c.ovnNbCommand(args...) - if err != nil { - klog.Errorf("failed to list router policy by nexthop %s: %v", nexthop, err) - return err - } - if output == "" { - return nil - } - klog.Infof("delete policy route for router: %s, priority: %d, match %s", router, priority, output) - return c.DeletePolicyRoute(router, priority, output) -} - -type PolicyRoute struct { - Priority int32 - Match string - Action string - NextHopIP string -} - -func (c LegacyClient) GetPolicyRouteList(router string) (routeList []*PolicyRoute, err error) { - output, err := c.ovnNbCommand("lr-policy-list", router) - if err != nil { - klog.Errorf("failed to list logical router policy route: %v", err) - return nil, err - } - return parseLrPolicyRouteListOutput(output) -} - -var policyRouteRegexp = regexp.MustCompile(`^\s*(\d+)\s+(.*)\b\s+(allow|drop|reroute)\s*(.*)?$`) - -func parseLrPolicyRouteListOutput(output string) (routeList []*PolicyRoute, err error) { - lines := strings.Split(output, "\n") - routeList = make([]*PolicyRoute, 0, len(lines)) - for _, l := range lines { - if len(l) == 0 { - continue - } - sm := policyRouteRegexp.FindStringSubmatch(l) - if len(sm) != 5 { - continue - } - priority, err := strconv.ParseInt(sm[1], 10, 32) - if err != nil { - return nil, fmt.Errorf("found unexpected policy priority %s, please check", sm[1]) - } - routeList = append(routeList, &PolicyRoute{ - Priority: int32(priority), - Match: sm[2], - Action: sm[3], - NextHopIP: sm[4], - }) - } - return routeList, nil -} - func (c LegacyClient) GetStaticRouteList(router string) (routeList []*StaticRoute, err error) { output, err := c.ovnNbCommand("lr-route-list", router) if err != nil { @@ -732,126 +545,6 @@ func CheckAlive() error { return nil } -func (c *LegacyClient) VpcHasPolicyRoute(vpc string, nextHops []string, priority int32) (bool, error) { - // get all policies by vpc - outPolicies, err := c.ovnNbCommand("--data=bare", "--no-heading", - "--columns=policies", "find", "Logical_Router", fmt.Sprintf("name=%s", vpc)) - if err != nil { - klog.Errorf("failed to find Logical_Router_Policy %s: %v, %q", vpc, err, outPolicies) - return false, err - } - if outPolicies == "" { - klog.V(3).Infof("vpc %s has no policy routes", vpc) - return false, nil - } - - strRoutes := strings.Split(outPolicies, "\n")[0] - strPriority := fmt.Sprint(priority) - routes := strings.Fields(strRoutes) - // check if policie already exist - for _, r := range routes { - outPriorityNexthops, err := c.ovnNbCommand("--data=bare", "--no-heading", "--format=csv", "--columns=priority,nexthops", "list", "Logical_Router_Policy", r) - if err != nil { - klog.Errorf("failed to show Logical_Router_Policy %s: %v, %q", r, err, outPriorityNexthops) - return false, err - } - if outPriorityNexthops == "" { - return false, nil - } - priorityNexthops := strings.Split(outPriorityNexthops, "\n")[0] - result := strings.Split(priorityNexthops, ",") - if len(result) == 2 { - routePriority := result[0] - strNodeIPs := result[1] - nodeIPs := strings.Fields(strNodeIPs) - sort.Strings(nodeIPs) - if routePriority == strPriority && slices.Equal(nextHops, nodeIPs) { - // make sure priority, nexthops is just the same - return true, nil - } - } - } - return false, nil -} - -func (c *LegacyClient) PolicyRouteExists(priority int32, match string) (bool, error) { - results, err := c.CustomFindEntity("Logical_Router_Policy", []string{"_uuid"}, fmt.Sprintf("priority=%d", priority), fmt.Sprintf("match=\"%s\"", match)) - if err != nil { - klog.Errorf("customFindEntity failed, %v", err) - return false, err - } - if len(results) == 0 { - return false, nil - } - return true, nil -} - -func (c *LegacyClient) DeletePolicyRouteByUUID(router string, uuids []string) error { - if len(uuids) == 0 { - return nil - } - for _, uuid := range uuids { - var args []string - args = append(args, []string{"lr-policy-del", router, uuid}...) - if _, err := c.ovnNbCommand(args...); err != nil { - klog.Errorf("failed to delete router %s policy route: %v", router, err) - return err - } - } - return nil -} - -func (c *LegacyClient) GetPolicyRouteParas(priority int32, match string) ([]string, map[string]string, error) { - result, err := c.CustomFindEntity("Logical_Router_Policy", []string{"nexthops", "external_ids"}, fmt.Sprintf("priority=%d", priority), fmt.Sprintf(`match="%s"`, match)) - if err != nil { - klog.Errorf("customFindEntity failed, %v", err) - return nil, nil, err - } - if len(result) == 0 { - return nil, nil, nil - } - - nameIpMap := make(map[string]string, len(result[0]["external_ids"])) - for _, l := range result[0]["external_ids"] { - if len(strings.TrimSpace(l)) == 0 { - continue - } - parts := strings.Split(strings.TrimSpace(l), "=") - if len(parts) != 2 { - continue - } - name := strings.TrimSpace(parts[0]) - ip := strings.TrimSpace(parts[1]) - nameIpMap[name] = ip - } - - return result[0]["nexthops"], nameIpMap, nil -} - -func (c LegacyClient) CheckPolicyRouteNexthopConsistent(match, nexthop string, priority int32) (bool, error) { - exist, err := c.PolicyRouteExists(priority, match) - if err != nil { - return false, err - } - if !exist { - return false, nil - } - - dbNextHops, _, err := c.GetPolicyRouteParas(priority, match) - if err != nil { - klog.Errorf("failed to get policy route paras, %v", err) - return false, err - } - cfgNextHops := strings.Split(nexthop, ",") - - sort.Strings(dbNextHops) - sort.Strings(cfgNextHops) - if slices.Equal(dbNextHops, cfgNextHops) { - return true, nil - } - return false, nil -} - type dhcpOptions struct { UUID string CIDR string diff --git a/pkg/ovs/ovn-nbctl-legacy_test.go b/pkg/ovs/ovn-nbctl-legacy_test.go index c0b4780b463..fcb542263fe 100644 --- a/pkg/ovs/ovn-nbctl-legacy_test.go +++ b/pkg/ovs/ovn-nbctl-legacy_test.go @@ -65,16 +65,3 @@ Route Table
: ast.Equal(7, len(routeList)) ast.Equal(routeList[0].RouteTable, util.MainRouteTable) } - -func Test_parseLrPolicyRouteListOutput(t *testing.T) { - t.SkipNow() - ast := assert.New(t) - output := ` - 10 ip4.src == 1.1.0.0/24 reroute 198.19.0.4 - 10 ip4.src == 1.1.0.0/24 || ip4.src == 1.1.4.0/24 reroute 198.19.0.4 - 10 ip4.src == 1.1.0.0/24 || ip4.src == 1.1.4.0/24 || Iip4.src ==1.1.5.0/24 reroute 198.19.0.4 - 10 ip4.src == 1.1.1.0/24 drop` - routeList, err := parseLrPolicyRouteListOutput(output) - ast.Nil(err) - ast.Equal(6, len(routeList)) -}