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 46b80d9 commit bc8d081
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 28 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/zalando/skipper v0.21.54
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
k8s.io/api v0.28.9
k8s.io/apimachinery v0.28.9
k8s.io/client-go v0.28.9
Expand Down Expand Up @@ -70,14 +71,12 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/grpc v1.63.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
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 bc8d081

Please sign in to comment.