Skip to content

Commit 622defd

Browse files
authored
Merge pull request #1854 from elastx/compare-cidr-fix
🐛Make sure that allowedCidrs lists are compared correctly to avoid patching LB listener when not needed
2 parents ca15733 + 9e356ad commit 622defd

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

pkg/cloud/services/loadbalancer/loadbalancer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package loadbalancer
1919
import (
2020
"errors"
2121
"fmt"
22-
"reflect"
22+
"slices"
2323
"time"
2424

2525
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners"
@@ -275,11 +275,11 @@ func (s *Service) getOrUpdateAllowedCIDRS(openStackCluster *infrav1.OpenStackClu
275275
// Validate CIDRs and convert any given IP into a CIDR.
276276
allowedCIDRs = validateIPs(openStackCluster, allowedCIDRs)
277277

278-
// Remove duplicates.
279-
allowedCIDRs = capostrings.Unique(allowedCIDRs)
280-
listener.AllowedCIDRs = capostrings.Unique(listener.AllowedCIDRs)
278+
// Sort and remove duplicates
279+
allowedCIDRs = capostrings.Canonicalize(allowedCIDRs)
280+
listener.AllowedCIDRs = capostrings.Canonicalize(listener.AllowedCIDRs)
281281

282-
if !reflect.DeepEqual(allowedCIDRs, listener.AllowedCIDRs) {
282+
if !slices.Equal(allowedCIDRs, listener.AllowedCIDRs) {
283283
s.scope.Logger().Info("CIDRs do not match, updating listener", "expectedCIDRs", allowedCIDRs, "currentCIDRs", listener.AllowedCIDRs)
284284
listenerUpdateOpts := listeners.UpdateOpts{
285285
AllowedCIDRs: &allowedCIDRs,

pkg/utils/strings/strings.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ limitations under the License.
1616

1717
package strings
1818

19-
func Unique(s []string) []string {
20-
inResult := make(map[string]bool)
21-
var result []string
22-
for _, str := range s {
23-
if _, ok := inResult[str]; !ok {
24-
inResult[str] = true
25-
result = append(result, str)
26-
}
27-
}
28-
return result
19+
import (
20+
"cmp"
21+
"slices"
22+
)
23+
24+
func Canonicalize[S ~[]E, E cmp.Ordered](s S) S {
25+
slices.Sort(s)
26+
return slices.Compact(s)
2927
}

pkg/utils/strings/strings_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package strings
18+
19+
import (
20+
"slices"
21+
"testing"
22+
)
23+
24+
func TestCanonicalize(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
value []string
28+
want []string
29+
}{
30+
{
31+
name: "Empty list",
32+
value: []string{},
33+
want: []string{},
34+
},
35+
{
36+
name: "Identity",
37+
value: []string{"a", "b", "c"},
38+
want: []string{"a", "b", "c"},
39+
},
40+
{
41+
name: "Out of order",
42+
value: []string{"c", "b", "a"},
43+
want: []string{"a", "b", "c"},
44+
},
45+
{
46+
name: "Duplicate elements",
47+
value: []string{"c", "b", "a", "c"},
48+
want: []string{"a", "b", "c"},
49+
},
50+
}
51+
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
got := Canonicalize(tt.value)
55+
if !slices.Equal(got, tt.want) {
56+
t.Errorf("CompareLists() = %v, want %v", got, tt.want)
57+
}
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)