Skip to content

Commit a8be198

Browse files
zhaocongqizbb88888
authored andcommitted
fix security groups changed when vm is shut down (#4976)
* fix security groups changed when vm is closed * add unit test for UnionStringSlice Signed-off-by: zhaocongqi <1229896069@qq.com> --------- Signed-off-by: zhaocongqi <1229896069@qq.com>
1 parent d42faa0 commit a8be198

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

pkg/controller/pod.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,19 @@ func (c *Controller) reconcileAllocateSubnets(cachedPod, pod *v1.Pod, needAlloca
694694
DHCPv6OptionsUUID: subnet.Status.DHCPv6OptionsUUID,
695695
}
696696

697+
var oldSgList []string
698+
if vmKey != "" {
699+
existingLsp, err := c.OVNNbClient.GetLogicalSwitchPort(portName, true)
700+
if err != nil {
701+
klog.Errorf("failed to get logical switch port %s: %v", portName, err)
702+
return nil, err
703+
}
704+
if existingLsp != nil {
705+
oldSgList, _ = c.getPortSg(existingLsp)
706+
}
707+
}
708+
697709
securityGroupAnnotation := pod.Annotations[fmt.Sprintf(util.SecurityGroupAnnotationTemplate, podNet.ProviderName)]
698-
securityGroups := strings.ReplaceAll(securityGroupAnnotation, " ", "")
699710
if err := c.OVNNbClient.CreateLogicalSwitchPort(subnet.Name, portName, ipStr, mac, podName, pod.Namespace,
700711
portSecurity, securityGroupAnnotation, vips, podNet.Subnet.Spec.EnableDHCP, dhcpOptions, subnet.Spec.Vpc); err != nil {
701712
c.recorder.Eventf(pod, v1.EventTypeWarning, "CreateOVNPortFailed", err.Error())
@@ -729,8 +740,10 @@ func (c *Controller) reconcileAllocateSubnets(cachedPod, pod *v1.Pod, needAlloca
729740
}
730741
}
731742

732-
if securityGroupAnnotation != "" {
733-
sgNames := strings.Split(securityGroups, ",")
743+
if securityGroupAnnotation != "" || oldSgList != nil {
744+
securityGroups := strings.ReplaceAll(securityGroupAnnotation, " ", "")
745+
newSgList := strings.Split(securityGroups, ",")
746+
sgNames := util.UnionStringSlice(oldSgList, newSgList)
734747
for _, sgName := range sgNames {
735748
if sgName != "" {
736749
c.syncSgPortsQueue.Add(sgName)

pkg/util/slice.go

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package util
22

3+
import "k8s.io/utils/set"
4+
35
func DiffStringSlice(slice1, slice2 []string) []string {
46
var diff []string
57

@@ -27,6 +29,14 @@ func DiffStringSlice(slice1, slice2 []string) []string {
2729
return diff
2830
}
2931

32+
func UnionStringSlice(slices ...[]string) []string {
33+
union := set.New[string]()
34+
for _, s := range slices {
35+
union.Insert(s...)
36+
}
37+
return union.UnsortedList()
38+
}
39+
3040
// IsStringsOverlap check if two string slices are overlapped
3141
func IsStringsOverlap(a, b []string) bool {
3242
for _, sa := range a {

pkg/util/slice_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,54 @@ func TestDiffStringSlice(t *testing.T) {
3737
}
3838
}
3939

40+
func TestUnionStringSlice(t *testing.T) {
41+
t.Parallel()
42+
tests := []struct {
43+
desc string
44+
slice1 []string
45+
slice2 []string
46+
want []string
47+
}{
48+
{
49+
desc: "both slices nil",
50+
slice1: nil,
51+
slice2: nil,
52+
want: []string{},
53+
},
54+
{
55+
desc: "first slice nil",
56+
slice1: nil,
57+
slice2: []string{"a", "b", "c"},
58+
want: []string{"a", "b", "c"},
59+
},
60+
{
61+
desc: "second slice nil",
62+
slice1: []string{"x", "y", "z"},
63+
slice2: nil,
64+
want: []string{"x", "y", "z"},
65+
},
66+
{
67+
desc: "duplicate elements",
68+
slice1: []string{"a", "b", "a", "c"},
69+
slice2: []string{"b", "c", "c", "d"},
70+
want: []string{"a", "b", "c", "d"},
71+
},
72+
{
73+
desc: "empty slices",
74+
slice1: []string{},
75+
slice2: []string{},
76+
want: []string{},
77+
},
78+
}
79+
80+
for _, tt := range tests {
81+
t.Run(tt.desc, func(t *testing.T) {
82+
result := UnionStringSlice(tt.slice1, tt.slice2)
83+
require.ElementsMatch(t, tt.want, result)
84+
})
85+
}
86+
}
87+
4088
func TestIsStringsOverlap(t *testing.T) {
4189
tests := []struct {
4290
name string

0 commit comments

Comments
 (0)