-
Notifications
You must be signed in to change notification settings - Fork 512
fix: sgs need to sync when neccessary #6297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,15 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/scylladb/go-set/strset" | ||
| "github.com/stretchr/testify/require" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| 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" | ||
| ) | ||
|
|
@@ -49,3 +53,137 @@ func Test_logicalRouterPortFilter(t *testing.T) { | |
| } | ||
| } | ||
| } | ||
|
|
||
| func Test_gcSecurityGroup(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| denyAllPg := ovs.GetSgPortGroupName(util.DenyAllSecurityGroup) | ||
| defaultPg := ovs.GetSgPortGroupName(util.DefaultSecurityGroupName) | ||
|
|
||
| t.Run("EnableSecurityGroup=true preserves deny_all port group", func(t *testing.T) { | ||
| t.Parallel() | ||
| fc := newFakeController(t) | ||
| ctrl := fc.fakeController | ||
| ctrl.config.EnableSecurityGroup = true | ||
|
|
||
| // Create a SecurityGroup CR so its port group is not orphaned | ||
| sg := &kubeovnv1.SecurityGroup{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "existing-sg"}, | ||
| } | ||
| _, err := ctrl.config.KubeOvnClient.KubeovnV1().SecurityGroups().Create( | ||
| context.Background(), sg, metav1.CreateOptions{}) | ||
| require.NoError(t, err) | ||
|
|
||
| existingSgPg := ovs.GetSgPortGroupName("existing-sg") | ||
| orphanedPg := ovs.GetSgPortGroupName("orphaned-sg") | ||
|
|
||
| portGroups := []ovnnb.PortGroup{ | ||
| {Name: denyAllPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": util.DenyAllSecurityGroup}}, | ||
| {Name: defaultPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": util.DefaultSecurityGroupName}}, | ||
| {Name: existingSgPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": "existing-sg"}}, | ||
| {Name: orphanedPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": "orphaned-sg"}}, | ||
| {Name: "np-pg", ExternalIDs: map[string]string{"vendor": util.CniTypeName, networkPolicyKey: "some-np"}}, | ||
| } | ||
|
|
||
| fc.mockOvnClient.EXPECT(). | ||
| ListPortGroups(map[string]string{"vendor": util.CniTypeName}). | ||
| Return(portGroups, nil) | ||
| // Only the orphaned port group should be deleted; deny_all, default, existing-sg, and np-pg are preserved | ||
| fc.mockOvnClient.EXPECT(). | ||
| DeletePortGroup(orphanedPg). | ||
| Return(nil) | ||
|
|
||
| err = ctrl.gcSecurityGroup() | ||
| require.NoError(t, err) | ||
| }) | ||
|
|
||
| t.Run("EnableSecurityGroup=false garbage-collects deny_all port group", func(t *testing.T) { | ||
| t.Parallel() | ||
| fc := newFakeController(t) | ||
| ctrl := fc.fakeController | ||
| ctrl.config.EnableSecurityGroup = false | ||
|
|
||
| // Create a SecurityGroup CR | ||
| sg := &kubeovnv1.SecurityGroup{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "existing-sg"}, | ||
| } | ||
| _, err := ctrl.config.KubeOvnClient.KubeovnV1().SecurityGroups().Create( | ||
| context.Background(), sg, metav1.CreateOptions{}) | ||
| require.NoError(t, err) | ||
|
|
||
| existingSgPg := ovs.GetSgPortGroupName("existing-sg") | ||
| orphanedPg := ovs.GetSgPortGroupName("orphaned-sg") | ||
|
|
||
| portGroups := []ovnnb.PortGroup{ | ||
| // deny_all should now be GC'd because EnableSecurityGroup=false | ||
| {Name: denyAllPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": util.DenyAllSecurityGroup}}, | ||
| {Name: defaultPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": util.DefaultSecurityGroupName}}, | ||
| {Name: existingSgPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": "existing-sg"}}, | ||
| {Name: orphanedPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": "orphaned-sg"}}, | ||
| {Name: "np-pg", ExternalIDs: map[string]string{"vendor": util.CniTypeName, networkPolicyKey: "some-np"}}, | ||
| } | ||
|
|
||
| fc.mockOvnClient.EXPECT(). | ||
| ListPortGroups(map[string]string{"vendor": util.CniTypeName}). | ||
| Return(portGroups, nil) | ||
| // Both deny_all and orphaned port groups should be deleted | ||
| // deny_all is no longer preserved because EnableSecurityGroup=false | ||
| fc.mockOvnClient.EXPECT(). | ||
| DeletePortGroup(denyAllPg, orphanedPg). | ||
| Return(nil) | ||
|
Comment on lines
+131
to
+133
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mock expectation for Consider making the expectation order-insensitive. One way to do this with |
||
|
|
||
| err = ctrl.gcSecurityGroup() | ||
| require.NoError(t, err) | ||
| }) | ||
|
|
||
| t.Run("no orphaned port groups results in no deletion", func(t *testing.T) { | ||
| t.Parallel() | ||
| fc := newFakeController(t) | ||
| ctrl := fc.fakeController | ||
| ctrl.config.EnableSecurityGroup = true | ||
|
|
||
| sg := &kubeovnv1.SecurityGroup{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "existing-sg"}, | ||
| } | ||
| _, err := ctrl.config.KubeOvnClient.KubeovnV1().SecurityGroups().Create( | ||
| context.Background(), sg, metav1.CreateOptions{}) | ||
| require.NoError(t, err) | ||
|
|
||
| existingSgPg := ovs.GetSgPortGroupName("existing-sg") | ||
|
|
||
| portGroups := []ovnnb.PortGroup{ | ||
| {Name: denyAllPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": util.DenyAllSecurityGroup}}, | ||
| {Name: defaultPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": util.DefaultSecurityGroupName}}, | ||
| {Name: existingSgPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": "existing-sg"}}, | ||
| } | ||
|
|
||
| fc.mockOvnClient.EXPECT(). | ||
| ListPortGroups(map[string]string{"vendor": util.CniTypeName}). | ||
| Return(portGroups, nil) | ||
| // DeletePortGroup should NOT be called | ||
|
|
||
| err = ctrl.gcSecurityGroup() | ||
| require.NoError(t, err) | ||
| }) | ||
|
|
||
| t.Run("network policy port groups are always preserved", func(t *testing.T) { | ||
| t.Parallel() | ||
| fc := newFakeController(t) | ||
| ctrl := fc.fakeController | ||
| ctrl.config.EnableSecurityGroup = false | ||
|
|
||
| portGroups := []ovnnb.PortGroup{ | ||
| {Name: "np-pg-1", ExternalIDs: map[string]string{"vendor": util.CniTypeName, networkPolicyKey: "policy-a"}}, | ||
| {Name: "np-pg-2", ExternalIDs: map[string]string{"vendor": util.CniTypeName, networkPolicyKey: "policy-b"}}, | ||
| {Name: defaultPg, ExternalIDs: map[string]string{"vendor": util.CniTypeName, "sg": util.DefaultSecurityGroupName}}, | ||
| } | ||
|
|
||
| fc.mockOvnClient.EXPECT(). | ||
| ListPortGroups(map[string]string{"vendor": util.CniTypeName}). | ||
| Return(portGroups, nil) | ||
| // No port groups should be deleted | ||
|
|
||
| err := ctrl.gcSecurityGroup() | ||
| require.NoError(t, err) | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.