From 5986efb2a3dbf77fe65b6601435eebbe1b2d2439 Mon Sep 17 00:00:00 2001 From: Andrew Stucki Date: Fri, 29 Apr 2022 07:12:34 -0400 Subject: [PATCH] Bug: LoadBalancers - Use Hostnames in addition to IP (#187) * Use Hostnames in addition to IPs * Add changelog entry --- .changelog/187.txt | 3 +++ internal/k8s/reconciler/gateway.go | 10 ++++++++-- internal/k8s/reconciler/gateway_test.go | 17 ++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 .changelog/187.txt diff --git a/.changelog/187.txt b/.changelog/187.txt new file mode 100644 index 000000000..8a758c791 --- /dev/null +++ b/.changelog/187.txt @@ -0,0 +1,3 @@ +```release-note:bug +k8s/reconciler: gateway addresses have invalid empty string when LoadBalancer services use a hostname for ExternalIP (like EKS) +``` diff --git a/internal/k8s/reconciler/gateway.go b/internal/k8s/reconciler/gateway.go index baf242104..0b81fbcfb 100644 --- a/internal/k8s/reconciler/gateway.go +++ b/internal/k8s/reconciler/gateway.go @@ -187,8 +187,14 @@ func (g *K8sGateway) assignGatewayIPFromServiceIngress(ctx context.Context, serv } for _, ingress := range updated.Status.LoadBalancer.Ingress { - g.serviceReady = true - g.addresses = append(g.addresses, ingress.IP) + if ingress.IP != "" { + g.serviceReady = true + g.addresses = append(g.addresses, ingress.IP) + } + if ingress.Hostname != "" { + g.serviceReady = true + g.addresses = append(g.addresses, ingress.Hostname) + } } return nil diff --git a/internal/k8s/reconciler/gateway_test.go b/internal/k8s/reconciler/gateway_test.go index b651b10c8..825dd2b1e 100644 --- a/internal/k8s/reconciler/gateway_test.go +++ b/internal/k8s/reconciler/gateway_test.go @@ -103,6 +103,9 @@ func TestGatewayValidateGatewayIP(t *testing.T) { { IP: "4.4.4.4", }, + { + Hostname: "this.is.a.hostname", + }, }, }, }, @@ -110,7 +113,7 @@ func TestGatewayValidateGatewayIP(t *testing.T) { for _, tc := range []struct { // What IP address do we expect the Gateway to be assigned? - expectedIP string + expectedIPs []string // Should the mock client expect a request for the Service? // If false, the mock client expects a request for the Pod instead. @@ -120,22 +123,22 @@ func TestGatewayValidateGatewayIP(t *testing.T) { serviceType *core.ServiceType }{ { - expectedIP: pod.Status.PodIP, + expectedIPs: []string{pod.Status.PodIP}, expectedIPFromSvc: false, serviceType: nil, }, { - expectedIP: pod.Status.HostIP, + expectedIPs: []string{pod.Status.HostIP}, expectedIPFromSvc: false, serviceType: serviceType(core.ServiceTypeNodePort), }, { - expectedIP: svc.Status.LoadBalancer.Ingress[0].IP, + expectedIPs: []string{svc.Status.LoadBalancer.Ingress[0].IP, svc.Status.LoadBalancer.Ingress[1].Hostname}, expectedIPFromSvc: true, serviceType: serviceType(core.ServiceTypeLoadBalancer), }, { - expectedIP: svc.Spec.ClusterIP, + expectedIPs: []string{svc.Spec.ClusterIP}, expectedIPFromSvc: true, serviceType: serviceType(core.ServiceTypeClusterIP), }, @@ -163,8 +166,8 @@ func TestGatewayValidateGatewayIP(t *testing.T) { } assert.NoError(t, gateway.validateGatewayIP(context.Background())) - require.Len(t, gateway.addresses, 1) - assert.Equal(t, tc.expectedIP, gateway.addresses[0]) + require.Len(t, gateway.addresses, len(tc.expectedIPs)) + assert.Equal(t, tc.expectedIPs, gateway.addresses) assert.True(t, gateway.serviceReady) })