Skip to content

Commit b67e9f3

Browse files
committed
test
Signed-off-by: Mengxin Liu <liumengxinfly@gmail.com>
1 parent f6c95e8 commit b67e9f3

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

pkg/controller/vpc.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2525

2626
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
27+
"github.com/kubeovn/kube-ovn/pkg/ovs"
2728
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
2829
"github.com/kubeovn/kube-ovn/pkg/util"
2930
)
@@ -289,7 +290,23 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error {
289290
return err
290291
}
291292

292-
learnFromARPRequest := true
293+
learnFromARPRequest := vpc.Spec.EnableExternal
294+
if !learnFromARPRequest {
295+
for _, subnetName := range vpc.Status.Subnets {
296+
subnet, err := c.subnetsLister.Get(subnetName)
297+
if err != nil {
298+
if k8serrors.IsNotFound(err) {
299+
continue
300+
}
301+
klog.Errorf("failed to get subnet %s for vpc %s: %v", subnetName, key, err)
302+
return err
303+
}
304+
if subnet.Spec.Vlan != "" && subnet.Spec.U2OInterconnection {
305+
learnFromARPRequest = true
306+
break
307+
}
308+
}
309+
}
293310

294311
if err = c.createVpcRouter(key, learnFromARPRequest); err != nil {
295312
klog.Errorf("failed to create vpc router for vpc %s: %v", key, err)
@@ -481,6 +498,11 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error {
481498
}
482499

483500
// handle policy route
501+
policyExternalIDs := map[string]string{
502+
ovs.ExternalIDVendor: util.CniTypeName,
503+
ovs.ExternalIDVpcPolicyRoute: "true",
504+
}
505+
484506
var (
485507
policyRouteExisted, policyRouteNeedDel, policyRouteNeedAdd []*kubeovnv1.PolicyRoute
486508
policyRouteLogical []*ovnnb.LogicalRouterPolicy
@@ -493,33 +515,30 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error {
493515
policyRouteNeedDel, policyRouteNeedAdd = diffPolicyRouteWithExisted(policyRouteExisted, vpc.Spec.PolicyRoutes)
494516
} else {
495517
if vpc.Spec.PolicyRoutes == nil {
496-
// do not clean default vpc policy routes
497-
if err = c.OVNNbClient.ClearLogicalRouterPolicy(vpc.Name); err != nil {
498-
klog.Errorf("clean all vpc %s policy route failed, %v", vpc.Name, err)
518+
// only clean vpc spec managed policy routes, not those created by subnet or egress gateway controllers
519+
if err = c.OVNNbClient.DeleteLogicalRouterPolicies(vpc.Name, -1, policyExternalIDs); err != nil {
520+
klog.Errorf("clean vpc %s spec policy routes failed, %v", vpc.Name, err)
499521
return err
500522
}
501523
} else {
502-
policyRouteLogical, err = c.OVNNbClient.ListLogicalRouterPolicies(vpc.Name, -1, nil, true)
524+
policyRouteLogical, err = c.OVNNbClient.ListLogicalRouterPolicies(vpc.Name, -1, policyExternalIDs, false)
503525
if err != nil {
504526
klog.Errorf("failed to get vpc %s policy route list, %v", vpc.Name, err)
505527
return err
506528
}
507-
// diff vpc policy route
508529
policyRouteNeedDel, policyRouteNeedAdd = diffPolicyRouteWithLogical(policyRouteLogical, vpc.Spec.PolicyRoutes)
509530
}
510531
}
511-
// delete policies non-exist
512532
for _, item := range policyRouteNeedDel {
513533
klog.Infof("delete policy route for router: %s, priority: %d, match %s", vpc.Name, item.Priority, item.Match)
514534
if err = c.OVNNbClient.DeleteLogicalRouterPolicy(vpc.Name, item.Priority, item.Match); err != nil {
515535
klog.Errorf("del vpc %s policy route failed, %v", vpc.Name, err)
516536
return err
517537
}
518538
}
519-
// add new policies
520539
for _, item := range policyRouteNeedAdd {
521-
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)
522-
if err = c.OVNNbClient.AddLogicalRouterPolicy(vpc.Name, item.Priority, item.Match, string(item.Action), []string{item.NextHopIP}, nil, externalIDs); err != nil {
540+
klog.Infof("add policy route for router: %s, match %s, action %s, nexthop %s, externalID %v", vpc.Name, item.Match, string(item.Action), item.NextHopIP, policyExternalIDs)
541+
if err = c.OVNNbClient.AddLogicalRouterPolicy(vpc.Name, item.Priority, item.Match, string(item.Action), []string{item.NextHopIP}, nil, policyExternalIDs); err != nil {
523542
klog.Errorf("add policy route to vpc %s failed, %v", vpc.Name, err)
524543
return err
525544
}
@@ -580,7 +599,7 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error {
580599
if subnet.Spec.Vpc == key {
581600
// Accelerate subnet update when vpc config is updated.
582601
// In case VPC not set namespaces, subnet will backoff and may take long time to back to ready.
583-
if subnet.Status.IsNotReady() || subnet.Spec.U2OInterconnection {
602+
if subnet.Status.IsNotReady() {
584603
c.addOrUpdateSubnetQueue.Add(subnet.Name)
585604
}
586605
if vpc.Name != util.DefaultVpc && vpc.Spec.EnableBfd && subnet.Spec.EnableEcmp {

pkg/controller/vpc_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"k8s.io/utils/keymutex"
1212

1313
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
14+
"github.com/kubeovn/kube-ovn/pkg/ovs"
1415
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
1516
"github.com/kubeovn/kube-ovn/pkg/util"
1617
)
@@ -108,7 +109,8 @@ func Test_handleAddOrUpdateVpc_staticRoutes(t *testing.T) {
108109
externalIDs,
109110
"10.0.0.1",
110111
).Return(nil)
111-
mockOvnClient.EXPECT().ClearLogicalRouterPolicy(vpcName).Return(nil)
112+
policyExternalIDs := map[string]string{ovs.ExternalIDVendor: util.CniTypeName, ovs.ExternalIDVpcPolicyRoute: "true"}
113+
mockOvnClient.EXPECT().DeleteLogicalRouterPolicies(vpcName, -1, policyExternalIDs).Return(nil)
112114
mockOvnClient.EXPECT().ListLogicalSwitch(gomock.Any(), gomock.Any()).Return([]ovnnb.LogicalSwitch{}, nil).AnyTimes()
113115
mockOvnClient.EXPECT().ListLogicalRouter(gomock.Any(), gomock.Any()).Return([]ovnnb.LogicalRouter{}, nil).AnyTimes()
114116
mockOvnClient.EXPECT().DeleteLogicalRouterPort(fmt.Sprintf("bfd@%s", vpcName)).Return(nil)
@@ -168,7 +170,8 @@ func Test_handleAddOrUpdateVpc_staticRoutes(t *testing.T) {
168170
Nat: []string{},
169171
}, nil)
170172
mockOvnClient.EXPECT().DeleteLogicalRouterStaticRoute(vpcName, gomock.Any(), gomock.Any(), "10.0.0.0/24", "1.2.3.4").Return(nil)
171-
mockOvnClient.EXPECT().ClearLogicalRouterPolicy(vpcName).Return(nil)
173+
policyExternalIDs := map[string]string{ovs.ExternalIDVendor: util.CniTypeName, ovs.ExternalIDVpcPolicyRoute: "true"}
174+
mockOvnClient.EXPECT().DeleteLogicalRouterPolicies(vpcName, -1, policyExternalIDs).Return(nil)
172175
mockOvnClient.EXPECT().ListLogicalSwitch(gomock.Any(), gomock.Any()).Return([]ovnnb.LogicalSwitch{}, nil).AnyTimes()
173176
mockOvnClient.EXPECT().ListLogicalRouter(gomock.Any(), gomock.Any()).Return([]ovnnb.LogicalRouter{}, nil).AnyTimes()
174177
mockOvnClient.EXPECT().DeleteLogicalRouterPort(fmt.Sprintf("bfd@%s", vpcName)).Return(nil)
@@ -222,7 +225,8 @@ func Test_handleAddOrUpdateVpc_staticRoutes(t *testing.T) {
222225
}, nil)
223226
mockOvnClient.EXPECT().DeleteLogicalRouterStaticRoute(vpcName, gomock.Any(), gomock.Any(), "10.0.0.0/24", "1.2.3.4").Return(nil)
224227
mockOvnClient.EXPECT().DeleteLogicalRouterStaticRoute(vpcName, gomock.Any(), gomock.Any(), "192.168.0.0/24", "10.0.0.1").Return(nil)
225-
mockOvnClient.EXPECT().ClearLogicalRouterPolicy(vpcName).Return(nil)
228+
policyExternalIDs := map[string]string{ovs.ExternalIDVendor: util.CniTypeName, ovs.ExternalIDVpcPolicyRoute: "true"}
229+
mockOvnClient.EXPECT().DeleteLogicalRouterPolicies(vpcName, -1, policyExternalIDs).Return(nil)
226230
mockOvnClient.EXPECT().ListLogicalSwitch(gomock.Any(), gomock.Any()).Return([]ovnnb.LogicalSwitch{}, nil).AnyTimes()
227231
mockOvnClient.EXPECT().ListLogicalRouter(gomock.Any(), gomock.Any()).Return([]ovnnb.LogicalRouter{}, nil).AnyTimes()
228232
mockOvnClient.EXPECT().DeleteLogicalRouterPort(fmt.Sprintf("bfd@%s", vpcName)).Return(nil)
@@ -285,7 +289,8 @@ func Test_handleAddOrUpdateVpc_staticRoutes(t *testing.T) {
285289
externalIDs,
286290
"10.0.0.1",
287291
).Return(nil)
288-
mockOvnClient.EXPECT().ClearLogicalRouterPolicy(vpcName).Return(nil)
292+
policyExternalIDs := map[string]string{ovs.ExternalIDVendor: util.CniTypeName, ovs.ExternalIDVpcPolicyRoute: "true"}
293+
mockOvnClient.EXPECT().DeleteLogicalRouterPolicies(vpcName, -1, policyExternalIDs).Return(nil)
289294
mockOvnClient.EXPECT().ListLogicalSwitch(gomock.Any(), gomock.Any()).Return([]ovnnb.LogicalSwitch{}, nil).AnyTimes()
290295
mockOvnClient.EXPECT().ListLogicalRouter(gomock.Any(), gomock.Any()).Return([]ovnnb.LogicalRouter{}, nil).AnyTimes()
291296
mockOvnClient.EXPECT().DeleteLogicalRouterPort(fmt.Sprintf("bfd@%s", vpcName)).Return(nil)

pkg/ovs/ovn.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const (
5252

5353
ExternalIDVendor = "vendor"
5454
ExternalIDVpcEgressGateway = "vpc-egress-gateway"
55+
ExternalIDVpcPolicyRoute = "vpc-policy-route"
5556
)
5657

5758
// NewLegacyClient init a legacy ovn client

0 commit comments

Comments
 (0)