From 86e397eb8435462e01e6d97781f4a1d00270ee14 Mon Sep 17 00:00:00 2001
From: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
Date: Mon, 12 Jun 2023 18:15:55 +0200
Subject: [PATCH] use slices.Equal for subnet comparison

Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
---
 go.mod         |  1 +
 go.sum         |  2 ++
 worker.go      | 27 ++++-----------------------
 worker_test.go |  3 ++-
 4 files changed, 9 insertions(+), 24 deletions(-)

diff --git a/go.mod b/go.mod
index 2a5dfb09..12eaf42e 100644
--- a/go.mod
+++ b/go.mod
@@ -13,6 +13,7 @@ require (
 	github.com/sirupsen/logrus v1.9.0
 	github.com/stretchr/testify v1.8.4
 	github.com/zalando/skipper v0.16.6
+	golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
 	gopkg.in/alecthomas/kingpin.v2 v2.2.6
 	k8s.io/api v0.22.17
 	k8s.io/apimachinery v0.22.17
diff --git a/go.sum b/go.sum
index 8e7cf170..60ca5512 100644
--- a/go.sum
+++ b/go.sum
@@ -389,6 +389,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
 golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
 golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
+golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
+golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
 golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
 golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
 golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
diff --git a/worker.go b/worker.go
index abb15ae8..8c739b6c 100644
--- a/worker.go
+++ b/worker.go
@@ -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 {
@@ -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
 				}
 			}
@@ -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
-}
diff --git a/worker_test.go b/worker_test.go
index abed8fe2..87d214ab 100644
--- a/worker_test.go
+++ b/worker_test.go
@@ -25,6 +25,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"
@@ -1160,7 +1161,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)