Skip to content

Commit daedbe0

Browse files
authored
Merge pull request #5004 from nrb/fix-5002
🐛 Reconcile target groups and listeners as their own entities
2 parents f04cb3e + cc2cfe4 commit daedbe0

File tree

5 files changed

+923
-376
lines changed

5 files changed

+923
-376
lines changed

controllers/awscluster_controller_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
303303
mockedCreateLBV2Calls(t, e)
304304
mockedDescribeInstanceCall(m)
305305
mockedDescribeAvailabilityZones(m, []string{"us-east-1c", "us-east-1a"})
306+
mockedDescribeTargetGroupsCall(t, e)
307+
mockedCreateTargetGroupCall(t, e)
308+
mockedModifyTargetGroupAttributes(t, e)
309+
mockedDescribeListenersCall(t, e)
310+
mockedCreateListenerCall(t, e)
306311
}
307312

308313
expect(ec2Mock.EXPECT(), elbv2Mock.EXPECT())

controllers/helpers_test.go

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
3131
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
32+
"sigs.k8s.io/cluster-api-provider-aws/v2/test/helpers"
3233
"sigs.k8s.io/cluster-api-provider-aws/v2/test/mocks"
3334
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3435
"sigs.k8s.io/cluster-api/util/conditions"
@@ -39,6 +40,7 @@ const DNSName = "www.google.com"
3940
var (
4041
lbName = aws.String("test-cluster-apiserver")
4142
lbArn = aws.String("loadbalancer::arn")
43+
tgArn = aws.String("arn::target-group")
4244
describeLBInput = &elb.DescribeLoadBalancersInput{
4345
LoadBalancerNames: aws.StringSlice([]string{"test-cluster-apiserver"}),
4446
}
@@ -291,6 +293,157 @@ func mockedCreateLBV2Calls(t *testing.T, m *mocks.MockELBV2APIMockRecorder) {
291293
})).MaxTimes(1)
292294
}
293295

296+
func mockedDescribeTargetGroupsCall(t *testing.T, m *mocks.MockELBV2APIMockRecorder) {
297+
t.Helper()
298+
m.DescribeTargetGroups(gomock.Eq(&elbv2.DescribeTargetGroupsInput{
299+
LoadBalancerArn: lbArn,
300+
})).
301+
Return(&elbv2.DescribeTargetGroupsOutput{
302+
NextMarker: new(string),
303+
TargetGroups: []*elbv2.TargetGroup{
304+
{
305+
HealthCheckEnabled: aws.Bool(true),
306+
HealthCheckIntervalSeconds: new(int64),
307+
HealthCheckPath: new(string),
308+
HealthCheckPort: new(string),
309+
HealthCheckProtocol: new(string),
310+
HealthCheckTimeoutSeconds: new(int64),
311+
HealthyThresholdCount: new(int64),
312+
IpAddressType: new(string),
313+
LoadBalancerArns: []*string{lbArn},
314+
Matcher: &elbv2.Matcher{},
315+
Port: new(int64),
316+
Protocol: new(string),
317+
ProtocolVersion: new(string),
318+
TargetGroupArn: tgArn,
319+
TargetGroupName: new(string),
320+
TargetType: new(string),
321+
UnhealthyThresholdCount: new(int64),
322+
VpcId: new(string),
323+
}},
324+
}, nil)
325+
}
326+
327+
func mockedCreateTargetGroupCall(t *testing.T, m *mocks.MockELBV2APIMockRecorder) {
328+
t.Helper()
329+
m.CreateTargetGroup(helpers.PartialMatchCreateTargetGroupInput(t, &elbv2.CreateTargetGroupInput{
330+
HealthCheckEnabled: aws.Bool(true),
331+
HealthCheckIntervalSeconds: aws.Int64(infrav1.DefaultAPIServerHealthCheckIntervalSec),
332+
HealthCheckPort: aws.String(infrav1.DefaultAPIServerPortString),
333+
HealthCheckProtocol: aws.String("TCP"),
334+
HealthCheckTimeoutSeconds: aws.Int64(infrav1.DefaultAPIServerHealthCheckTimeoutSec),
335+
HealthyThresholdCount: aws.Int64(infrav1.DefaultAPIServerHealthThresholdCount),
336+
// Note: this is treated as a prefix with the partial matcher.
337+
Name: aws.String("apiserver-target"),
338+
Port: aws.Int64(infrav1.DefaultAPIServerPort),
339+
Protocol: aws.String("TCP"),
340+
Tags: []*elbv2.Tag{
341+
{
342+
Key: aws.String("Name"),
343+
Value: aws.String("bar-apiserver"),
344+
},
345+
{
346+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster"),
347+
Value: aws.String("owned"),
348+
},
349+
{
350+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
351+
Value: aws.String("apiserver"),
352+
},
353+
},
354+
UnhealthyThresholdCount: aws.Int64(infrav1.DefaultAPIServerUnhealthThresholdCount),
355+
VpcId: aws.String("vpc-exists"),
356+
})).Return(&elbv2.CreateTargetGroupOutput{
357+
TargetGroups: []*elbv2.TargetGroup{{
358+
HealthCheckEnabled: aws.Bool(true),
359+
HealthCheckIntervalSeconds: aws.Int64(infrav1.DefaultAPIServerHealthCheckIntervalSec),
360+
HealthCheckPort: aws.String(infrav1.DefaultAPIServerPortString),
361+
HealthCheckProtocol: aws.String("TCP"),
362+
HealthCheckTimeoutSeconds: aws.Int64(infrav1.DefaultAPIServerHealthCheckTimeoutSec),
363+
HealthyThresholdCount: aws.Int64(infrav1.DefaultAPIServerHealthThresholdCount),
364+
LoadBalancerArns: []*string{lbArn},
365+
Matcher: &elbv2.Matcher{},
366+
Port: aws.Int64(infrav1.DefaultAPIServerPort),
367+
Protocol: aws.String("TCP"),
368+
TargetGroupArn: tgArn,
369+
TargetGroupName: aws.String("apiserver-target"),
370+
UnhealthyThresholdCount: aws.Int64(infrav1.DefaultAPIServerUnhealthThresholdCount),
371+
VpcId: aws.String("vpc-exists"),
372+
}},
373+
}, nil)
374+
}
375+
376+
func mockedModifyTargetGroupAttributes(t *testing.T, m *mocks.MockELBV2APIMockRecorder) {
377+
t.Helper()
378+
m.ModifyTargetGroupAttributes(gomock.Eq(&elbv2.ModifyTargetGroupAttributesInput{
379+
TargetGroupArn: tgArn,
380+
Attributes: []*elbv2.TargetGroupAttribute{
381+
{
382+
Key: aws.String(infrav1.TargetGroupAttributeEnablePreserveClientIP),
383+
Value: aws.String("false"),
384+
},
385+
},
386+
})).Return(nil, nil)
387+
}
388+
389+
func mockedDescribeListenersCall(t *testing.T, m *mocks.MockELBV2APIMockRecorder) {
390+
t.Helper()
391+
m.DescribeListeners(gomock.Eq(&elbv2.DescribeListenersInput{
392+
LoadBalancerArn: lbArn,
393+
})).
394+
Return(&elbv2.DescribeListenersOutput{
395+
Listeners: []*elbv2.Listener{{
396+
DefaultActions: []*elbv2.Action{{
397+
TargetGroupArn: aws.String("arn::targetgroup-not-found"),
398+
}},
399+
ListenerArn: aws.String("arn::listener"),
400+
LoadBalancerArn: lbArn,
401+
}},
402+
}, nil)
403+
}
404+
405+
func mockedCreateListenerCall(t *testing.T, m *mocks.MockELBV2APIMockRecorder) {
406+
t.Helper()
407+
m.CreateListener(gomock.Eq(&elbv2.CreateListenerInput{
408+
DefaultActions: []*elbv2.Action{
409+
{
410+
TargetGroupArn: tgArn,
411+
Type: aws.String(elbv2.ActionTypeEnumForward),
412+
},
413+
},
414+
LoadBalancerArn: lbArn,
415+
Port: aws.Int64(infrav1.DefaultAPIServerPort),
416+
Protocol: aws.String("TCP"),
417+
Tags: []*elbv2.Tag{
418+
{
419+
Key: aws.String("Name"),
420+
Value: aws.String("test-cluster-apiserver"),
421+
},
422+
{
423+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster"),
424+
Value: aws.String("owned"),
425+
},
426+
{
427+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
428+
Value: aws.String("apiserver"),
429+
},
430+
},
431+
})).Return(&elbv2.CreateListenerOutput{
432+
Listeners: []*elbv2.Listener{
433+
{
434+
DefaultActions: []*elbv2.Action{
435+
{
436+
TargetGroupArn: tgArn,
437+
Type: aws.String(elbv2.ActionTypeEnumForward),
438+
},
439+
},
440+
ListenerArn: aws.String("listener::arn"),
441+
Port: aws.Int64(infrav1.DefaultAPIServerPort),
442+
Protocol: aws.String("TCP"),
443+
},
444+
}}, nil)
445+
}
446+
294447
func mockedDeleteLBCalls(expectV2Call bool, mv2 *mocks.MockELBV2APIMockRecorder, m *mocks.MockELBAPIMockRecorder) {
295448
if expectV2Call {
296449
mv2.DescribeLoadBalancers(gomock.Any()).Return(describeLBOutputV2, nil)

0 commit comments

Comments
 (0)