Skip to content

Commit

Permalink
use slices.Equal for subnet comparison
Browse files Browse the repository at this point in the history
Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>
  • Loading branch information
mikkeloscar committed Apr 17, 2024
1 parent 7bf903b commit c0857e1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/zalando/skipper v0.21.36
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
gopkg.in/alecthomas/kingpin.v2 v2.2.6
k8s.io/api v0.27.12
k8s.io/apimachinery v0.27.12
Expand Down Expand Up @@ -70,7 +71,6 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.18.0 // indirect
Expand Down
27 changes: 4 additions & 23 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/zalando-incubator/kube-ingress-aws-controller/certs"
"github.com/zalando-incubator/kube-ingress-aws-controller/kubernetes"
"github.com/zalando-incubator/kube-ingress-aws-controller/problem"
"golang.org/x/exp/slices"
)

type loadBalancer struct {
Expand Down Expand Up @@ -455,8 +456,10 @@ func matchIngressesToLoadBalancers(
// Ignore NLBs with a wrong set of subnets
if lb.loadBalancerType == aws.LoadBalancerTypeNetwork {
subnets := subnetsByScheme(lb.scheme)
sort.Strings(subnets)
sort.Strings(lb.stack.Subnets)

if !equalSlices[string](lb.stack.Subnets, subnets) {
if !slices.Equal[[]string](lb.stack.Subnets, subnets) {
continue
}
}
Expand Down Expand Up @@ -721,25 +724,3 @@ func cniEventHandler(ctx context.Context, targetCNIcfg *aws.TargetCNIconfig,
}
}
}

func equalSlices[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}

for _, aElem := range a {
found := false
for _, bElem := range b {
if aElem == bElem {
found = true
break
}
}

if !found {
return false
}
}

return true
}
3 changes: 2 additions & 1 deletion worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/zalando-incubator/kube-ingress-aws-controller/certs"
"github.com/zalando-incubator/kube-ingress-aws-controller/kubernetes"
"github.com/zalando/skipper/dataclients/kubernetes/kubernetestest"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/zalando-incubator/kube-ingress-aws-controller/aws/fake"
Expand Down Expand Up @@ -1386,7 +1387,7 @@ func TestMatchIngressesToLoadbalancers(t *testing.T) {
continue
}

if lb.stack != nil && equalSlices[string](lb.stack.Subnets, []string{"a", "b", "c"}) {
if lb.stack != nil && slices.Equal[string](lb.stack.Subnets, []string{"a", "b", "c"}) {
require.Len(t, lb.ingresses, 0)
} else {
require.Len(t, lb.ingresses, 1)
Expand Down

0 comments on commit c0857e1

Please sign in to comment.