Skip to content

Commit

Permalink
feat(lb): support host headers on http healthchecks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nox-404 committed Dec 20, 2023
1 parent 0dbd179 commit 8cd46f5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/loadbalancer-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ This is the annotation to set the HTTP method used by the `http` health check.
It is possible to set the method per port, like `80:GET;443,8443:POST`.
NB: Required when setting service.beta.kubernetes.io/scw-loadbalancer-health-check-type to `http` or `https`.

### `service.beta.kubernetes.io/scw-loadbalancer-health-check-http-host`
This is the annotation to set the HTTP host header used by the "http" health check.
It is possible to set the method per port, like `80:mydomain1.tld;443,8443:mydomain2.tld`.

### `service.beta.kubernetes.io/scw-loadbalancer-health-check-http-code`
This is the annotation to set the HTTP code that the `http` health check will be matching against.
It is possible to set the code per port, like `80:404;443,8443:204`.
Expand Down
30 changes: 27 additions & 3 deletions scaleway/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ const (
// NB: Required when setting service.beta.kubernetes.io/scw-loadbalancer-health-check-type to "http" or "https"
serviceAnnotationLoadBalancerHealthCheckHTTPMethod = "service.beta.kubernetes.io/scw-loadbalancer-health-check-http-method"

// serviceAnnotationLoadBalancerHealthCheckHTTPHost is the HTTP host header used by the "http" health check
// It is possible to set the method per port, like "80:mydomain1.tld;443,8443:mydomain2.tld"
serviceAnnotationLoadBalancerHealthCheckHTTPHost = "service.beta.kubernetes.io/scw-loadbalancer-health-check-http-host"

// serviceAnnotationLoadBalancerHealthCheckHTTPCode is the HTTP code that the "http" health check will be matching against
// It is possible to set the code per port, like "80:404;443,8443:204"
// NB: Required when setting service.beta.kubernetes.io/scw-loadbalancer-health-check-type to "http" or "https"
Expand Down Expand Up @@ -1955,6 +1959,21 @@ func getHTTPHealthCheckMethod(service *v1.Service, nodePort int32) (string, erro
return method, nil
}

func getHTTPHealthCheckHost(service *v1.Service, nodePort int32) (string, error) {
annotation, ok := service.Annotations[serviceAnnotationLoadBalancerHealthCheckHTTPHost]
if !ok {
return "", nil
}

method, err := getValueForPort(service, nodePort, annotation)
if err != nil {
klog.Errorf("could not get value for annotation %s and port %d", serviceAnnotationLoadBalancerHealthCheckHTTPHost, nodePort)
return "", err
}

return method, nil
}

func getHTTPHealthCheck(service *v1.Service, nodePort int32) (*scwlb.HealthCheckHTTPConfig, error) {
code, err := getHTTPHealthCheckCode(service, nodePort)
if err != nil {
Expand All @@ -1968,11 +1987,16 @@ func getHTTPHealthCheck(service *v1.Service, nodePort int32) (*scwlb.HealthCheck
if err != nil {
return nil, err
}
host, err := getHTTPHealthCheckHost(service, nodePort)
if err != nil {
return nil, err
}

return &scwlb.HealthCheckHTTPConfig{
Method: method,
Code: &code,
URI: uri,
Method: method,
Code: &code,
URI: uri,
HostHeader: host,
}, nil
}

Expand Down

0 comments on commit 8cd46f5

Please sign in to comment.