Skip to content

Commit 0006312

Browse files
authored
Merge pull request #5589 from benluddy/nlb-conn-drain
🐛 Enable NLB connection draining for graceful apiserver shutdown.
2 parents 1315831 + c374806 commit 0006312

File tree

4 files changed

+101
-8
lines changed

4 files changed

+101
-8
lines changed

api/v1beta2/network_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ type TargetGroupAttribute string
207207
var (
208208
// TargetGroupAttributeEnablePreserveClientIP defines the attribute key for enabling preserve client IP.
209209
TargetGroupAttributeEnablePreserveClientIP = "preserve_client_ip.enabled"
210+
211+
// TargetGroupAttributeEnableConnectionTermination defines the attribute key for terminating
212+
// established connections to unhealthy targets.
213+
TargetGroupAttributeEnableConnectionTermination = "target_health_state.unhealthy.connection_termination.enabled"
214+
215+
// TargetGroupAttributeUnhealthyDrainingIntervalSeconds defines the attribute key for the
216+
// unhealthy target connection draining interval.
217+
TargetGroupAttributeUnhealthyDrainingIntervalSeconds = "target_health_state.unhealthy.draining_interval_seconds"
210218
)
211219

212220
// LoadBalancerAttribute defines a set of attributes for a V2 load balancer.

controllers/helpers_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ func mockedModifyTargetGroupAttributes(t *testing.T, m *mocks.MockELBV2APIMockRe
393393
m.ModifyTargetGroupAttributes(gomock.Any(), gomock.Eq(&elbv2.ModifyTargetGroupAttributesInput{
394394
TargetGroupArn: tgArn,
395395
Attributes: []elbv2types.TargetGroupAttribute{
396+
{
397+
Key: aws.String(infrav1.TargetGroupAttributeEnableConnectionTermination),
398+
Value: aws.String("false"),
399+
},
400+
{
401+
Key: aws.String(infrav1.TargetGroupAttributeUnhealthyDrainingIntervalSeconds),
402+
Value: aws.String("300"),
403+
},
396404
{
397405
Key: aws.String(infrav1.TargetGroupAttributeEnablePreserveClientIP),
398406
Value: aws.String("false"),

pkg/cloud/services/elb/loadbalancer.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,16 +1687,32 @@ func (s *Service) reconcileTargetGroupsAndListeners(ctx context.Context, lbARN s
16871687
}
16881688
createdTargetGroups = append(createdTargetGroups, group)
16891689

1690+
targetGroupAttributeInput := &elbv2.ModifyTargetGroupAttributesInput{TargetGroupArn: group.TargetGroupArn}
1691+
1692+
if lbSpec.LoadBalancerType == infrav1.LoadBalancerTypeNLB {
1693+
targetGroupAttributeInput.Attributes = append(targetGroupAttributeInput.Attributes,
1694+
elbv2types.TargetGroupAttribute{
1695+
Key: aws.String(infrav1.TargetGroupAttributeEnableConnectionTermination),
1696+
Value: aws.String("false"),
1697+
},
1698+
elbv2types.TargetGroupAttribute{
1699+
Key: aws.String(infrav1.TargetGroupAttributeUnhealthyDrainingIntervalSeconds),
1700+
Value: aws.String("300"),
1701+
},
1702+
)
1703+
}
1704+
16901705
if !lbSpec.PreserveClientIP {
1691-
targetGroupAttributeInput := &elbv2.ModifyTargetGroupAttributesInput{
1692-
TargetGroupArn: group.TargetGroupArn,
1693-
Attributes: []elbv2types.TargetGroupAttribute{
1694-
{
1695-
Key: aws.String(infrav1.TargetGroupAttributeEnablePreserveClientIP),
1696-
Value: aws.String("false"),
1697-
},
1706+
targetGroupAttributeInput.Attributes = append(targetGroupAttributeInput.Attributes,
1707+
elbv2types.TargetGroupAttribute{
1708+
Key: aws.String(infrav1.TargetGroupAttributeEnablePreserveClientIP),
1709+
Value: aws.String("false"),
16981710
},
1699-
}
1711+
)
1712+
}
1713+
1714+
if len(targetGroupAttributeInput.Attributes) > 0 {
1715+
s.scope.Debug("configuring target group attributes", "attributes", targetGroupAttributeInput)
17001716
if _, err := s.ELBV2Client.ModifyTargetGroupAttributes(ctx, targetGroupAttributeInput); err != nil {
17011717
return nil, nil, errors.Wrapf(err, "failed to modify target group attribute")
17021718
}

pkg/cloud/services/elb/loadbalancer_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,14 @@ func TestReconcileTargetGroupsAndListeners(t *testing.T) {
15431543
m.ModifyTargetGroupAttributes(gomock.Any(), gomock.Eq(&elbv2.ModifyTargetGroupAttributesInput{
15441544
TargetGroupArn: aws.String(tgArn),
15451545
Attributes: []elbv2types.TargetGroupAttribute{
1546+
{
1547+
Key: aws.String(infrav1.TargetGroupAttributeEnableConnectionTermination),
1548+
Value: aws.String("false"),
1549+
},
1550+
{
1551+
Key: aws.String(infrav1.TargetGroupAttributeUnhealthyDrainingIntervalSeconds),
1552+
Value: aws.String("300"),
1553+
},
15461554
{
15471555
Key: aws.String(infrav1.TargetGroupAttributeEnablePreserveClientIP),
15481556
Value: aws.String("false"),
@@ -1661,6 +1669,14 @@ func TestReconcileTargetGroupsAndListeners(t *testing.T) {
16611669
m.ModifyTargetGroupAttributes(gomock.Any(), &elbv2.ModifyTargetGroupAttributesInput{
16621670
TargetGroupArn: aws.String(tgArn),
16631671
Attributes: []elbv2types.TargetGroupAttribute{
1672+
{
1673+
Key: aws.String(infrav1.TargetGroupAttributeEnableConnectionTermination),
1674+
Value: aws.String("false"),
1675+
},
1676+
{
1677+
Key: aws.String(infrav1.TargetGroupAttributeUnhealthyDrainingIntervalSeconds),
1678+
Value: aws.String("300"),
1679+
},
16641680
{
16651681
Key: aws.String(infrav1.TargetGroupAttributeEnablePreserveClientIP),
16661682
Value: aws.String("false"),
@@ -1767,6 +1783,14 @@ func TestReconcileTargetGroupsAndListeners(t *testing.T) {
17671783
m.ModifyTargetGroupAttributes(gomock.Any(), &elbv2.ModifyTargetGroupAttributesInput{
17681784
TargetGroupArn: aws.String(tgArn),
17691785
Attributes: []elbv2types.TargetGroupAttribute{
1786+
{
1787+
Key: aws.String(infrav1.TargetGroupAttributeEnableConnectionTermination),
1788+
Value: aws.String("false"),
1789+
},
1790+
{
1791+
Key: aws.String(infrav1.TargetGroupAttributeUnhealthyDrainingIntervalSeconds),
1792+
Value: aws.String("300"),
1793+
},
17701794
{
17711795
Key: aws.String(infrav1.TargetGroupAttributeEnablePreserveClientIP),
17721796
Value: aws.String("false"),
@@ -1860,6 +1884,19 @@ func TestReconcileTargetGroupsAndListeners(t *testing.T) {
18601884
},
18611885
},
18621886
}, nil)
1887+
m.ModifyTargetGroupAttributes(gomock.Any(), gomock.Eq(&elbv2.ModifyTargetGroupAttributesInput{
1888+
TargetGroupArn: aws.String(tgArn),
1889+
Attributes: []elbv2types.TargetGroupAttribute{
1890+
{
1891+
Key: aws.String(infrav1.TargetGroupAttributeEnableConnectionTermination),
1892+
Value: aws.String("false"),
1893+
},
1894+
{
1895+
Key: aws.String(infrav1.TargetGroupAttributeUnhealthyDrainingIntervalSeconds),
1896+
Value: aws.String("300"),
1897+
},
1898+
},
1899+
})).Return(nil, nil)
18631900
m.DescribeListeners(gomock.Any(), &elbv2.DescribeListenersInput{
18641901
LoadBalancerArn: aws.String(elbArn),
18651902
}).Return(&elbv2.DescribeListenersOutput{
@@ -2000,6 +2037,14 @@ func TestReconcileTargetGroupsAndListeners(t *testing.T) {
20002037
m.ModifyTargetGroupAttributes(gomock.Any(), &elbv2.ModifyTargetGroupAttributesInput{
20012038
TargetGroupArn: aws.String(tgArn),
20022039
Attributes: []elbv2types.TargetGroupAttribute{
2040+
{
2041+
Key: aws.String(infrav1.TargetGroupAttributeEnableConnectionTermination),
2042+
Value: aws.String("false"),
2043+
},
2044+
{
2045+
Key: aws.String(infrav1.TargetGroupAttributeUnhealthyDrainingIntervalSeconds),
2046+
Value: aws.String("300"),
2047+
},
20032048
{
20042049
Key: aws.String(infrav1.TargetGroupAttributeEnablePreserveClientIP),
20052050
Value: aws.String("false"),
@@ -2115,6 +2160,14 @@ func TestReconcileTargetGroupsAndListeners(t *testing.T) {
21152160
m.ModifyTargetGroupAttributes(gomock.Any(), &elbv2.ModifyTargetGroupAttributesInput{
21162161
TargetGroupArn: aws.String(tgArn),
21172162
Attributes: []elbv2types.TargetGroupAttribute{
2163+
{
2164+
Key: aws.String(infrav1.TargetGroupAttributeEnableConnectionTermination),
2165+
Value: aws.String("false"),
2166+
},
2167+
{
2168+
Key: aws.String(infrav1.TargetGroupAttributeUnhealthyDrainingIntervalSeconds),
2169+
Value: aws.String("300"),
2170+
},
21182171
{
21192172
Key: aws.String(infrav1.TargetGroupAttributeEnablePreserveClientIP),
21202173
Value: aws.String("false"),
@@ -2403,6 +2456,14 @@ func TestReconcileV2LB(t *testing.T) {
24032456
m.ModifyTargetGroupAttributes(gomock.Any(), gomock.Eq(&elbv2.ModifyTargetGroupAttributesInput{
24042457
TargetGroupArn: aws.String(tgArn),
24052458
Attributes: []elbv2types.TargetGroupAttribute{
2459+
{
2460+
Key: aws.String(infrav1.TargetGroupAttributeEnableConnectionTermination),
2461+
Value: aws.String("false"),
2462+
},
2463+
{
2464+
Key: aws.String(infrav1.TargetGroupAttributeUnhealthyDrainingIntervalSeconds),
2465+
Value: aws.String("300"),
2466+
},
24062467
{
24072468
Key: aws.String(infrav1.TargetGroupAttributeEnablePreserveClientIP),
24082469
Value: aws.String("false"),

0 commit comments

Comments
 (0)