Skip to content

Commit bcc95ba

Browse files
committed
Replace NLBs on subnets change
Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>
1 parent bac4add commit bcc95ba

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

aws/cf.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Stack struct {
3535
WAFWebACLID string
3636
CertificateARNs map[string]time.Time
3737
tags map[string]string
38+
Subnets []string
3839
}
3940

4041
// IsComplete returns true if the stack status is a complete state.
@@ -480,6 +481,11 @@ func mapToManagedStack(stack *cloudformation.Stack) *Stack {
480481
http2 = false
481482
}
482483

484+
var subnets []string
485+
if parameters[parameterLoadBalancerSubnetsParameter] != "" {
486+
subnets = strings.Split(parameters[parameterLoadBalancerSubnetsParameter], ",")
487+
}
488+
483489
return &Stack{
484490
Name: aws.StringValue(stack.StackName),
485491
DNSName: outputs.dnsName(),
@@ -497,6 +503,7 @@ func mapToManagedStack(stack *cloudformation.Stack) *Stack {
497503
statusReason: aws.StringValue(stack.StackStatusReason),
498504
CWAlarmConfigHash: tags[cwAlarmConfigHashTag],
499505
WAFWebACLID: parameters[parameterLoadBalancerWAFWebACLIDParameter],
506+
Subnets: subnets,
500507
}
501508
}
502509

worker.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func doWork(
333333
awsAdapter.UpdateTargetGroupsAndAutoScalingGroups(stacks, problems)
334334

335335
certs := NewCertificates(certificateSummaries)
336-
model := buildManagedModel(certs, certsPerALB, certTTL, ingresses, stacks, cwAlarms, globalWAFACL)
336+
model := buildManagedModel(certs, certsPerALB, certTTL, ingresses, stacks, cwAlarms, globalWAFACL, awsAdapter.FindLBSubnets)
337337
log.Debugf("Have %d model(s)", len(model))
338338
for _, loadBalancer := range model {
339339
switch loadBalancer.Status() {
@@ -408,6 +408,7 @@ func matchIngressesToLoadBalancers(
408408
certs CertificatesFinder,
409409
certsPerALB int,
410410
ingresses []*kubernetes.Ingress,
411+
subnetsByScheme func(scheme string) []string,
411412
) []*loadBalancer {
412413
clusterLocalLB := &loadBalancer{
413414
clusterLocal: true,
@@ -451,6 +452,15 @@ func matchIngressesToLoadBalancers(
451452
continue
452453
}
453454

455+
// Ignore NLBs with a wrong set of subnets
456+
if lb.loadBalancerType == aws.LoadBalancerTypeNetwork {
457+
subnets := subnetsByScheme(lb.scheme)
458+
459+
if !equalSlices[string](lb.stack.Subnets, subnets) {
460+
continue
461+
}
462+
}
463+
454464
if lb.addIngress(certificateARNs, ingress, certsPerALB) {
455465
added = true
456466
break
@@ -516,11 +526,12 @@ func buildManagedModel(
516526
stacks []*aws.Stack,
517527
cwAlarms aws.CloudWatchAlarmList,
518528
globalWAFACL string,
529+
subnetsByScheme func(scheme string) []string,
519530
) []*loadBalancer {
520531
sortStacks(stacks)
521532
attachGlobalWAFACL(ingresses, globalWAFACL)
522533
model := getAllLoadBalancers(certs, certTTL, stacks)
523-
model = matchIngressesToLoadBalancers(model, certs, certsPerALB, ingresses)
534+
model = matchIngressesToLoadBalancers(model, certs, certsPerALB, ingresses, subnetsByScheme)
524535
attachCloudWatchAlarms(model, cwAlarms)
525536

526537
return model
@@ -710,3 +721,25 @@ func cniEventHandler(ctx context.Context, targetCNIcfg *aws.TargetCNIconfig,
710721
}
711722
}
712723
}
724+
725+
func equalSlices[T comparable](a, b []T) bool {
726+
if len(a) != len(b) {
727+
return false
728+
}
729+
730+
for _, aElem := range a {
731+
found := false
732+
for _, bElem := range b {
733+
if aElem == bElem {
734+
found = true
735+
break
736+
}
737+
}
738+
739+
if !found {
740+
return false
741+
}
742+
}
743+
744+
return true
745+
}

worker_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
log "github.com/sirupsen/logrus"
2121
"github.com/stretchr/testify/assert"
2222
"github.com/stretchr/testify/require"
23+
"github.com/zalando-incubator/kube-ingress-aws-controller/aws"
2324
awsAdapter "github.com/zalando-incubator/kube-ingress-aws-controller/aws"
2425
"github.com/zalando-incubator/kube-ingress-aws-controller/certs"
2526
"github.com/zalando-incubator/kube-ingress-aws-controller/kubernetes"
@@ -953,6 +954,7 @@ func TestMatchIngressesToLoadbalancers(t *testing.T) {
953954
maxCertsPerLB int
954955
lbs []*loadBalancer
955956
ingresses []*kubernetes.Ingress
957+
subnets []string
956958
validate func(*testing.T, []*loadBalancer)
957959
}{{
958960
title: "only cluster local",
@@ -1135,6 +1137,37 @@ func TestMatchIngressesToLoadbalancers(t *testing.T) {
11351137
require.Equal(t, 1, len(lb.ingresses["foo"]))
11361138
}
11371139
},
1140+
}, {
1141+
title: "load balancer with invalid subnets",
1142+
ingresses: []*kubernetes.Ingress{{
1143+
Name: "foo-ingress",
1144+
Hostnames: []string{
1145+
"foo.org",
1146+
"bar.org",
1147+
},
1148+
LoadBalancerType: awsAdapter.LoadBalancerTypeNetwork,
1149+
Shared: true,
1150+
}},
1151+
lbs: []*loadBalancer{{
1152+
loadBalancerType: awsAdapter.LoadBalancerTypeNetwork,
1153+
ingresses: make(map[string][]*kubernetes.Ingress),
1154+
stack: &aws.Stack{Subnets: []string{"a", "b", "c"}},
1155+
}},
1156+
validate: func(t *testing.T, lbs []*loadBalancer) {
1157+
require.Equal(t, 3, len(lbs))
1158+
for _, lb := range lbs {
1159+
if lb.clusterLocal {
1160+
continue
1161+
}
1162+
1163+
if lb.stack != nil && equalSlices[string](lb.stack.Subnets, []string{"a", "b", "c"}) {
1164+
require.Len(t, lb.ingresses, 0)
1165+
} else {
1166+
require.Len(t, lb.ingresses, 1)
1167+
}
1168+
}
1169+
},
1170+
subnets: []string{"x", "y", "z"},
11381171
}} {
11391172
t.Run(test.title, func(t *testing.T) {
11401173
var certs CertificatesFinder = defaultCerts
@@ -1147,7 +1180,11 @@ func TestMatchIngressesToLoadbalancers(t *testing.T) {
11471180
maxCertsPerLB = test.maxCertsPerLB
11481181
}
11491182

1150-
lbs := matchIngressesToLoadBalancers(test.lbs, certs, maxCertsPerLB, test.ingresses)
1183+
subnetsByScheme := func(scheme string) []string {
1184+
return test.subnets
1185+
}
1186+
1187+
lbs := matchIngressesToLoadBalancers(test.lbs, certs, maxCertsPerLB, test.ingresses, subnetsByScheme)
11511188
test.validate(t, lbs)
11521189
})
11531190
}
@@ -1177,6 +1214,7 @@ func TestBuildModel(t *testing.T) {
11771214
stacks []*awsAdapter.Stack
11781215
alarms awsAdapter.CloudWatchAlarmList
11791216
globalWAFACL string
1217+
subnets []string
11801218
validate func(*testing.T, []*loadBalancer)
11811219
}{{
11821220
title: "no alarm, no waf",
@@ -1323,6 +1361,10 @@ func TestBuildModel(t *testing.T) {
13231361
maxCertsPerLB = test.maxCertsPerLB
13241362
}
13251363

1364+
subnetsByScheme := func(scheme string) []string {
1365+
return test.subnets
1366+
}
1367+
13261368
m := buildManagedModel(
13271369
certs,
13281370
maxCertsPerLB,
@@ -1331,6 +1373,7 @@ func TestBuildModel(t *testing.T) {
13311373
test.stacks,
13321374
test.alarms,
13331375
test.globalWAFACL,
1376+
subnetsByScheme,
13341377
)
13351378

13361379
test.validate(t, m)

0 commit comments

Comments
 (0)