Skip to content

Commit 30e78b8

Browse files
committed
fix(lb): backend diff now includes healthcheck diff
1 parent bcb0d5b commit 30e78b8

File tree

3 files changed

+50
-96
lines changed

3 files changed

+50
-96
lines changed

docs/loadbalancer-annotations.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,14 @@ The default value is `5`.
6767

6868
### `service.beta.kubernetes.io/scw-loadbalancer-health-check-http-uri`
6969
This is the annotation to set the URI that is used by the `http` health check.
70-
It is possible to set the uri per port, like `80:/;443,8443:/healthz`.
70+
It is possible to set the uri per port, like `80:/;443,8443:mydomain.tld/healthz`.
7171
NB: Required when setting service.beta.kubernetes.io/scw-loadbalancer-health-check-type to `http` or `https`.
7272

7373
### `service.beta.kubernetes.io/scw-loadbalancer-health-check-http-method`
7474
This is the annotation to set the HTTP method used by the `http` health check.
7575
It is possible to set the method per port, like `80:GET;443,8443:POST`.
7676
NB: Required when setting service.beta.kubernetes.io/scw-loadbalancer-health-check-type to `http` or `https`.
7777

78-
### `service.beta.kubernetes.io/scw-loadbalancer-health-check-http-host`
79-
This is the annotation to set the HTTP host header used by the "http" health check.
80-
It is possible to set the method per port, like `80:mydomain1.tld;443,8443:mydomain2.tld`.
81-
8278
### `service.beta.kubernetes.io/scw-loadbalancer-health-check-http-code`
8379
This is the annotation to set the HTTP code that the `http` health check will be matching against.
8480
It is possible to set the code per port, like `80:404;443,8443:204`.

scaleway/loadbalancers.go

Lines changed: 25 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"net"
23+
"net/url"
2324
"os"
2425
"reflect"
2526
"strconv"
@@ -79,7 +80,7 @@ const (
7980
serviceAnnotationLoadBalancerHealthCheckMaxRetries = "service.beta.kubernetes.io/scw-loadbalancer-health-check-max-retries"
8081

8182
// serviceAnnotationLoadBalancerHealthCheckHTTPURI is the URI that is used by the "http" health check
82-
// It is possible to set the uri per port, like "80:/;443,8443:/healthz"
83+
// It is possible to set the uri per port, like "80:/;443,8443:mydomain.tld/healthz"
8384
// NB: Required when setting service.beta.kubernetes.io/scw-loadbalancer-health-check-type to "http" or "https"
8485
serviceAnnotationLoadBalancerHealthCheckHTTPURI = "service.beta.kubernetes.io/scw-loadbalancer-health-check-http-uri"
8586

@@ -88,10 +89,6 @@ const (
8889
// NB: Required when setting service.beta.kubernetes.io/scw-loadbalancer-health-check-type to "http" or "https"
8990
serviceAnnotationLoadBalancerHealthCheckHTTPMethod = "service.beta.kubernetes.io/scw-loadbalancer-health-check-http-method"
9091

91-
// serviceAnnotationLoadBalancerHealthCheckHTTPHost is the HTTP host header used by the "http" health check
92-
// It is possible to set the method per port, like "80:mydomain1.tld;443,8443:mydomain2.tld"
93-
serviceAnnotationLoadBalancerHealthCheckHTTPHost = "service.beta.kubernetes.io/scw-loadbalancer-health-check-http-host"
94-
9592
// serviceAnnotationLoadBalancerHealthCheckHTTPCode is the HTTP code that the "http" health check will be matching against
9693
// It is possible to set the code per port, like "80:404;443,8443:204"
9794
// NB: Required when setting service.beta.kubernetes.io/scw-loadbalancer-health-check-type to "http" or "https"
@@ -1509,44 +1506,31 @@ func getHTTPHealthCheckMethod(service *v1.Service, nodePort int32) (string, erro
15091506
return method, nil
15101507
}
15111508

1512-
func getHTTPHealthCheckHost(service *v1.Service, nodePort int32) (string, error) {
1513-
annotation, ok := service.Annotations[serviceAnnotationLoadBalancerHealthCheckHTTPHost]
1514-
if !ok {
1515-
return "", nil
1516-
}
1517-
1518-
method, err := getValueForPort(service, nodePort, annotation)
1519-
if err != nil {
1520-
klog.Errorf("could not get value for annotation %s and port %d", serviceAnnotationLoadBalancerHealthCheckHTTPHost, nodePort)
1521-
return "", err
1522-
}
1523-
1524-
return method, nil
1525-
}
1526-
15271509
func getHTTPHealthCheck(service *v1.Service, nodePort int32) (*scwlb.HealthCheckHTTPConfig, error) {
15281510
code, err := getHTTPHealthCheckCode(service, nodePort)
15291511
if err != nil {
15301512
return nil, err
15311513
}
1532-
uri, err := getHTTPHealthCheckURI(service, nodePort)
1514+
1515+
uriStr, err := getHTTPHealthCheckURI(service, nodePort)
15331516
if err != nil {
15341517
return nil, err
15351518
}
1536-
method, err := getHTTPHealthCheckMethod(service, nodePort)
1519+
uri, err := url.Parse(fmt.Sprintf("http://%s", uriStr))
15371520
if err != nil {
15381521
return nil, err
15391522
}
1540-
host, err := getHTTPHealthCheckHost(service, nodePort)
1523+
1524+
method, err := getHTTPHealthCheckMethod(service, nodePort)
15411525
if err != nil {
15421526
return nil, err
15431527
}
15441528

15451529
return &scwlb.HealthCheckHTTPConfig{
15461530
Method: method,
15471531
Code: &code,
1548-
URI: uri,
1549-
HostHeader: host,
1532+
URI: uri.Path,
1533+
HostHeader: uri.Host,
15501534
}, nil
15511535
}
15521536

@@ -1555,19 +1539,27 @@ func getHTTPSHealthCheck(service *v1.Service, nodePort int32) (*scwlb.HealthChec
15551539
if err != nil {
15561540
return nil, err
15571541
}
1558-
uri, err := getHTTPHealthCheckURI(service, nodePort)
1542+
1543+
uriStr, err := getHTTPHealthCheckURI(service, nodePort)
1544+
if err != nil {
1545+
return nil, err
1546+
}
1547+
uri, err := url.Parse(fmt.Sprintf("https://%s", uriStr))
15591548
if err != nil {
15601549
return nil, err
15611550
}
1551+
15621552
method, err := getHTTPHealthCheckMethod(service, nodePort)
15631553
if err != nil {
15641554
return nil, err
15651555
}
15661556

15671557
return &scwlb.HealthCheckHTTPSConfig{
1568-
Method: method,
1569-
Code: &code,
1570-
URI: uri,
1558+
Method: method,
1559+
Code: &code,
1560+
URI: uri.Path,
1561+
HostHeader: uri.Host,
1562+
Sni: uri.Host,
15711563
}, nil
15721564
}
15731565

@@ -1929,65 +1921,9 @@ func backendEquals(got, want *scwlb.Backend) bool {
19291921
return false
19301922
}
19311923

1932-
// TODO
1933-
if got.HealthCheck != want.HealthCheck {
1934-
if got.HealthCheck == nil || want.HealthCheck == nil {
1935-
klog.V(3).Infof("backend.HealthCheck: %s - %s", got.HealthCheck, want.HealthCheck)
1936-
return false
1937-
}
1938-
1939-
if got.HealthCheck.Port != want.HealthCheck.Port {
1940-
klog.V(3).Infof("backend.HealthCheck.Port: %s - %s", got.HealthCheck.Port, want.HealthCheck.Port)
1941-
return false
1942-
}
1943-
if !durationPtrEqual(got.HealthCheck.CheckDelay, want.HealthCheck.CheckDelay) {
1944-
klog.V(3).Infof("backend.HealthCheck.CheckDelay: %s - %s", got.HealthCheck.CheckDelay, want.HealthCheck.CheckDelay)
1945-
return false
1946-
}
1947-
if !durationPtrEqual(got.HealthCheck.CheckTimeout, want.HealthCheck.CheckTimeout) {
1948-
klog.V(3).Infof("backend.HealthCheck.CheckTimeout: %s - %s", got.HealthCheck.CheckTimeout, want.HealthCheck.CheckTimeout)
1949-
return false
1950-
}
1951-
if got.HealthCheck.CheckMaxRetries != want.HealthCheck.CheckMaxRetries {
1952-
klog.V(3).Infof("backend.HealthCheck.CheckMaxRetries: %s - %s", got.HealthCheck.CheckMaxRetries, want.HealthCheck.CheckMaxRetries)
1953-
return false
1954-
}
1955-
if got.HealthCheck.CheckSendProxy != want.HealthCheck.CheckSendProxy {
1956-
klog.V(3).Infof("backend.HealthCheck.CheckSendProxy: %s - %s", got.HealthCheck.CheckSendProxy, want.HealthCheck.CheckSendProxy)
1957-
return false
1958-
}
1959-
if (got.HealthCheck.TCPConfig == nil) != (want.HealthCheck.TCPConfig == nil) {
1960-
klog.V(3).Infof("backend.HealthCheck.TCPConfig: %s - %s", got.HealthCheck.TCPConfig, want.HealthCheck.TCPConfig)
1961-
return false
1962-
}
1963-
if (got.HealthCheck.MysqlConfig == nil) != (want.HealthCheck.MysqlConfig == nil) {
1964-
klog.V(3).Infof("backend.HealthCheck.MysqlConfig: %s - %s", got.HealthCheck.MysqlConfig, want.HealthCheck.MysqlConfig)
1965-
return false
1966-
}
1967-
if (got.HealthCheck.PgsqlConfig == nil) != (want.HealthCheck.PgsqlConfig == nil) {
1968-
klog.V(3).Infof("backend.HealthCheck.PgsqlConfig: %s - %s", got.HealthCheck.PgsqlConfig, want.HealthCheck.PgsqlConfig)
1969-
return false
1970-
}
1971-
if (got.HealthCheck.LdapConfig == nil) != (want.HealthCheck.LdapConfig == nil) {
1972-
klog.V(3).Infof("backend.HealthCheck.LdapConfig: %s - %s", got.HealthCheck.LdapConfig, want.HealthCheck.LdapConfig)
1973-
return false
1974-
}
1975-
if (got.HealthCheck.RedisConfig == nil) != (want.HealthCheck.RedisConfig == nil) {
1976-
klog.V(3).Infof("backend.HealthCheck.RedisConfig: %s - %s", got.HealthCheck.RedisConfig, want.HealthCheck.RedisConfig)
1977-
return false
1978-
}
1979-
if (got.HealthCheck.HTTPConfig == nil) != (want.HealthCheck.HTTPConfig == nil) {
1980-
klog.V(3).Infof("backend.HealthCheck.HTTPConfig: %s - %s", got.HealthCheck.HTTPConfig, want.HealthCheck.HTTPConfig)
1981-
return false
1982-
}
1983-
if (got.HealthCheck.HTTPSConfig == nil) != (want.HealthCheck.HTTPSConfig == nil) {
1984-
klog.V(3).Infof("backend.HealthCheck.HTTPSConfig: %s - %s", got.HealthCheck.HTTPSConfig, want.HealthCheck.HTTPSConfig)
1985-
return false
1986-
}
1987-
if !scwDurationPtrEqual(got.HealthCheck.TransientCheckDelay, want.HealthCheck.TransientCheckDelay) {
1988-
klog.V(3).Infof("backend.HealthCheck.TransientCheckDelay: %s - %s", got.HealthCheck.TransientCheckDelay, want.HealthCheck.TransientCheckDelay)
1989-
return false
1990-
}
1924+
if !reflect.DeepEqual(got.HealthCheck, want.HealthCheck) {
1925+
klog.V(3).Infof("backend.HealthCheck: %s - %s", got.HealthCheck, want.HealthCheck)
1926+
return false
19911927
}
19921928

19931929
return true
@@ -2098,7 +2034,7 @@ func aclsEquals(got []*scwlb.ACL, want []*scwlb.ACLSpec) bool {
20982034

20992035
slices.SortStableFunc(got, func(a, b *scwlb.ACL) bool { return a.Index < b.Index })
21002036
slices.SortStableFunc(want, func(a, b *scwlb.ACLSpec) bool { return a.Index < b.Index })
2101-
for idx, _ := range want {
2037+
for idx := range want {
21022038
if want[idx].Name != got[idx].Name {
21032039
return false
21042040
}
@@ -2126,7 +2062,6 @@ func aclsEquals(got []*scwlb.ACL, want []*scwlb.ACLSpec) bool {
21262062
}
21272063

21282064
func (l *loadbalancers) createBackend(service *v1.Service, loadbalancer *scwlb.LB, backend *scwlb.Backend) (*scwlb.Backend, error) {
2129-
// TODO: implement createBackend
21302065
b, err := l.api.CreateBackend(&scwlb.ZonedAPICreateBackendRequest{
21312066
Zone: getLoadBalancerZone(service),
21322067
LBID: loadbalancer.ID,
@@ -2154,7 +2089,6 @@ func (l *loadbalancers) createBackend(service *v1.Service, loadbalancer *scwlb.L
21542089
}
21552090

21562091
func (l *loadbalancers) updateBackend(service *v1.Service, loadbalancer *scwlb.LB, backend *scwlb.Backend) (*scwlb.Backend, error) {
2157-
// TODO: implement updateBackend
21582092
b, err := l.api.UpdateBackend(&scwlb.ZonedAPIUpdateBackendRequest{
21592093
Zone: getLoadBalancerZone(service),
21602094
BackendID: backend.ID,

scaleway/loadbalancers_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,30 @@ func TestBackendEquals(t *testing.T) {
941941
want bool
942942
}{"with a different TimeoutTunnel", reference, diff, false})
943943

944+
httpRef := deepCloneBackend(reference)
945+
httpRef.HealthCheck.TCPConfig = nil
946+
httpRef.HealthCheck.HTTPConfig = &scwlb.HealthCheckHTTPConfig{
947+
URI: "/",
948+
Method: "POST",
949+
Code: scw.Int32Ptr(200),
950+
}
951+
httpDiff := deepCloneBackend(httpRef)
952+
matrix = append(matrix, struct {
953+
Name string
954+
a *scwlb.Backend
955+
b *scwlb.Backend
956+
want bool
957+
}{"with same HTTP healthchecks", httpRef, httpDiff, true})
958+
959+
httpDiff = deepCloneBackend(httpRef)
960+
httpDiff.HealthCheck.HTTPConfig.Code = scw.Int32Ptr(404)
961+
matrix = append(matrix, struct {
962+
Name string
963+
a *scwlb.Backend
964+
b *scwlb.Backend
965+
want bool
966+
}{"with same HTTP healthchecks", httpRef, httpDiff, false})
967+
944968
for _, tt := range matrix {
945969
t.Run(tt.Name, func(t *testing.T) {
946970
got := backendEquals(tt.a, tt.b)

0 commit comments

Comments
 (0)