Skip to content

Commit c8dbbab

Browse files
committed
🐛 fix: ELBv2 health check for Load Balancer API
This change intents to synchronize w/ CLB the health check attributes for API LB, and use the correct health check path when using HTTP or HTTPS. The following target group health check attributes for ELBv2 are setting based in the CLB configuration for all protocols: - interval: current(30) want(10) - timeout: current(10) want(5) - health threshold count: current(5) want(5) - unhealthy threshold count: current(2) want(3) When using HTTP or HTTPS, the following attributes will be fixed: - path: current("/") want("/readyz") *'current' is the default values set by AWS, 'want' is the value set in the CLB.
1 parent 3a00c39 commit c8dbbab

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

api/v1beta2/network_types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ const (
2727
DefaultAPIServerPort = 6443
2828
// DefaultAPIServerPortString defines the API server port as a string for convenience.
2929
DefaultAPIServerPortString = "6443"
30+
// DefaultAPIServerHealthCheckPath the API server health check path.
31+
DefaultAPIServerHealthCheckPath = "/readyz"
32+
// DefaultAPIServerHealthCheckIntervalSec the API server health check interval in seconds.
33+
DefaultAPIServerHealthCheckIntervalSec = 10
34+
// DefaultAPIServerHealthCheckTimeoutSec the API server health check timeout in seconds.
35+
DefaultAPIServerHealthCheckTimeoutSec = 5
36+
// DefaultAPIServerHealthThresholdCount the API server health check threshold count.
37+
DefaultAPIServerHealthThresholdCount = 5
38+
// DefaultAPIServerUnhealthThresholdCount the API server unhealthy check threshold count.
39+
DefaultAPIServerUnhealthThresholdCount = 3
3040
)
3141

3242
// NetworkStatus encapsulates AWS networking resources.

pkg/cloud/services/elb/loadbalancer.go

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,23 @@ func (s *Service) getAPIServerLBSpec(elbName string, lbSpec *infrav1.AWSLoadBala
172172
scheme = *lbSpec.Scheme
173173
}
174174

175+
// The default API health check is TCP, allowing customization to HTTP or HTTPS when HealthCheckProtocol is set.
176+
apiHealthCheckProtocol := infrav1.ELBProtocolTCP
177+
if lbSpec != nil && lbSpec.HealthCheckProtocol != nil {
178+
s.scope.Trace("Found API health check protocol override in the Load Balancer spec, applying it to the API Target Group", "api-server-elb", lbSpec.HealthCheckProtocol)
179+
apiHealthCheckProtocol = *lbSpec.HealthCheckProtocol
180+
}
181+
apiHealthCheck := &infrav1.TargetGroupHealthCheck{
182+
Protocol: aws.String(apiHealthCheckProtocol.String()),
183+
Port: aws.String(infrav1.DefaultAPIServerPortString),
184+
Path: nil,
185+
IntervalSeconds: aws.Int64(infrav1.DefaultAPIServerHealthCheckIntervalSec),
186+
TimeoutSeconds: aws.Int64(infrav1.DefaultAPIServerHealthCheckTimeoutSec),
187+
ThresholdCount: aws.Int64(infrav1.DefaultAPIServerHealthThresholdCount),
188+
}
189+
if apiHealthCheckProtocol == infrav1.ELBProtocolHTTP || apiHealthCheckProtocol == infrav1.ELBProtocolHTTPS {
190+
apiHealthCheck.Path = aws.String(infrav1.DefaultAPIServerHealthCheckPath)
191+
}
175192
res := &infrav1.LoadBalancer{
176193
Name: elbName,
177194
Scheme: scheme,
@@ -181,14 +198,11 @@ func (s *Service) getAPIServerLBSpec(elbName string, lbSpec *infrav1.AWSLoadBala
181198
Protocol: infrav1.ELBProtocolTCP,
182199
Port: infrav1.DefaultAPIServerPort,
183200
TargetGroup: infrav1.TargetGroupSpec{
184-
Name: fmt.Sprintf("apiserver-target-%d", time.Now().Unix()),
185-
Port: infrav1.DefaultAPIServerPort,
186-
Protocol: infrav1.ELBProtocolTCP,
187-
VpcID: s.scope.VPC().ID,
188-
HealthCheck: &infrav1.TargetGroupHealthCheck{
189-
Protocol: aws.String(string(infrav1.ELBProtocolTCP)),
190-
Port: aws.String(infrav1.DefaultAPIServerPortString),
191-
},
201+
Name: fmt.Sprintf("apiserver-target-%d", time.Now().Unix()),
202+
Port: infrav1.DefaultAPIServerPort,
203+
Protocol: infrav1.ELBProtocolTCP,
204+
VpcID: s.scope.VPC().ID,
205+
HealthCheck: apiHealthCheck,
192206
},
193207
},
194208
},
@@ -321,6 +335,19 @@ func (s *Service) createLB(spec *infrav1.LoadBalancer, lbSpec *infrav1.AWSLoadBa
321335
targetGroupInput.HealthCheckEnabled = aws.Bool(true)
322336
targetGroupInput.HealthCheckProtocol = ln.TargetGroup.HealthCheck.Protocol
323337
targetGroupInput.HealthCheckPort = ln.TargetGroup.HealthCheck.Port
338+
targetGroupInput.UnhealthyThresholdCount = aws.Int64(infrav1.DefaultAPIServerUnhealthThresholdCount)
339+
if ln.TargetGroup.HealthCheck.Path != nil {
340+
targetGroupInput.HealthCheckPath = ln.TargetGroup.HealthCheck.Path
341+
}
342+
if ln.TargetGroup.HealthCheck.IntervalSeconds != nil {
343+
targetGroupInput.HealthCheckIntervalSeconds = ln.TargetGroup.HealthCheck.IntervalSeconds
344+
}
345+
if ln.TargetGroup.HealthCheck.TimeoutSeconds != nil {
346+
targetGroupInput.HealthCheckTimeoutSeconds = ln.TargetGroup.HealthCheck.TimeoutSeconds
347+
}
348+
if ln.TargetGroup.HealthCheck.ThresholdCount != nil {
349+
targetGroupInput.HealthyThresholdCount = ln.TargetGroup.HealthCheck.ThresholdCount
350+
}
324351
}
325352
s.scope.Debug("creating target group", "group", targetGroupInput, "listener", ln)
326353
group, err := s.ELBV2Client.CreateTargetGroup(targetGroupInput)
@@ -1002,10 +1029,10 @@ func (s *Service) getAPIServerClassicELBSpec(elbName string) (*infrav1.LoadBalan
10021029
},
10031030
HealthCheck: &infrav1.ClassicELBHealthCheck{
10041031
Target: s.getHealthCheckTarget(),
1005-
Interval: 10 * time.Second,
1006-
Timeout: 5 * time.Second,
1007-
HealthyThreshold: 5,
1008-
UnhealthyThreshold: 3,
1032+
Interval: infrav1.DefaultAPIServerHealthCheckIntervalSec * time.Second,
1033+
Timeout: infrav1.DefaultAPIServerHealthCheckTimeoutSec * time.Second,
1034+
HealthyThreshold: infrav1.DefaultAPIServerHealthThresholdCount,
1035+
UnhealthyThreshold: infrav1.DefaultAPIServerUnhealthThresholdCount,
10091036
},
10101037
SecurityGroupIDs: securityGroupIDs,
10111038
ClassicElbAttributes: infrav1.ClassicELBAttributes{
@@ -1501,7 +1528,7 @@ func (s *Service) getHealthCheckTarget() string {
15011528
if controlPlaneELB != nil && controlPlaneELB.HealthCheckProtocol != nil {
15021529
protocol = controlPlaneELB.HealthCheckProtocol
15031530
if protocol.String() == infrav1.ELBProtocolHTTP.String() || protocol.String() == infrav1.ELBProtocolHTTPS.String() {
1504-
return fmt.Sprintf("%v:%d/readyz", protocol, infrav1.DefaultAPIServerPort)
1531+
return fmt.Sprintf("%v:%d%s", protocol, infrav1.DefaultAPIServerPort, infrav1.DefaultAPIServerHealthCheckPath)
15051532
}
15061533
}
15071534
return fmt.Sprintf("%v:%d", protocol, infrav1.DefaultAPIServerPort)

0 commit comments

Comments
 (0)