Skip to content

Commit 7bf903b

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

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
@@ -18,6 +18,7 @@ import (
1818
log "github.com/sirupsen/logrus"
1919
"github.com/stretchr/testify/assert"
2020
"github.com/stretchr/testify/require"
21+
"github.com/zalando-incubator/kube-ingress-aws-controller/aws"
2122
awsAdapter "github.com/zalando-incubator/kube-ingress-aws-controller/aws"
2223
"github.com/zalando-incubator/kube-ingress-aws-controller/certs"
2324
"github.com/zalando-incubator/kube-ingress-aws-controller/kubernetes"
@@ -1179,6 +1180,7 @@ func TestMatchIngressesToLoadbalancers(t *testing.T) {
11791180
maxCertsPerLB int
11801181
lbs []*loadBalancer
11811182
ingresses []*kubernetes.Ingress
1183+
subnets []string
11821184
validate func(*testing.T, []*loadBalancer)
11831185
}{{
11841186
title: "only cluster local",
@@ -1361,6 +1363,37 @@ func TestMatchIngressesToLoadbalancers(t *testing.T) {
13611363
require.Equal(t, 1, len(lb.ingresses["foo"]))
13621364
}
13631365
},
1366+
}, {
1367+
title: "load balancer with invalid subnets",
1368+
ingresses: []*kubernetes.Ingress{{
1369+
Name: "foo-ingress",
1370+
Hostnames: []string{
1371+
"foo.org",
1372+
"bar.org",
1373+
},
1374+
LoadBalancerType: awsAdapter.LoadBalancerTypeNetwork,
1375+
Shared: true,
1376+
}},
1377+
lbs: []*loadBalancer{{
1378+
loadBalancerType: awsAdapter.LoadBalancerTypeNetwork,
1379+
ingresses: make(map[string][]*kubernetes.Ingress),
1380+
stack: &aws.Stack{Subnets: []string{"a", "b", "c"}},
1381+
}},
1382+
validate: func(t *testing.T, lbs []*loadBalancer) {
1383+
require.Equal(t, 3, len(lbs))
1384+
for _, lb := range lbs {
1385+
if lb.clusterLocal {
1386+
continue
1387+
}
1388+
1389+
if lb.stack != nil && equalSlices[string](lb.stack.Subnets, []string{"a", "b", "c"}) {
1390+
require.Len(t, lb.ingresses, 0)
1391+
} else {
1392+
require.Len(t, lb.ingresses, 1)
1393+
}
1394+
}
1395+
},
1396+
subnets: []string{"x", "y", "z"},
13641397
}} {
13651398
t.Run(test.title, func(t *testing.T) {
13661399
var certs CertificatesFinder = defaultCerts
@@ -1373,7 +1406,11 @@ func TestMatchIngressesToLoadbalancers(t *testing.T) {
13731406
maxCertsPerLB = test.maxCertsPerLB
13741407
}
13751408

1376-
lbs := matchIngressesToLoadBalancers(test.lbs, certs, maxCertsPerLB, test.ingresses)
1409+
subnetsByScheme := func(scheme string) []string {
1410+
return test.subnets
1411+
}
1412+
1413+
lbs := matchIngressesToLoadBalancers(test.lbs, certs, maxCertsPerLB, test.ingresses, subnetsByScheme)
13771414
test.validate(t, lbs)
13781415
})
13791416
}
@@ -1403,6 +1440,7 @@ func TestBuildModel(t *testing.T) {
14031440
stacks []*awsAdapter.Stack
14041441
alarms awsAdapter.CloudWatchAlarmList
14051442
globalWAFACL string
1443+
subnets []string
14061444
validate func(*testing.T, []*loadBalancer)
14071445
}{{
14081446
title: "no alarm, no waf",
@@ -1549,6 +1587,10 @@ func TestBuildModel(t *testing.T) {
15491587
maxCertsPerLB = test.maxCertsPerLB
15501588
}
15511589

1590+
subnetsByScheme := func(scheme string) []string {
1591+
return test.subnets
1592+
}
1593+
15521594
m := buildManagedModel(
15531595
certs,
15541596
maxCertsPerLB,
@@ -1557,6 +1599,7 @@ func TestBuildModel(t *testing.T) {
15571599
test.stacks,
15581600
test.alarms,
15591601
test.globalWAFACL,
1602+
subnetsByScheme,
15601603
)
15611604

15621605
test.validate(t, m)

0 commit comments

Comments
 (0)