Skip to content

Commit 760e4e9

Browse files
authored
fix(gateway): normalize status hostname to lowercase (#4591)
1 parent d1e5554 commit 760e4e9

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

controllers/gateway/gateway_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gateway
33
import (
44
"context"
55
"fmt"
6+
"strings"
67
"time"
78

89
"sigs.k8s.io/aws-load-balancer-controller/pkg/certs"
@@ -415,13 +416,14 @@ func (r *gatewayReconciler) updateGatewayStatusSuccess(ctx context.Context, lbSt
415416
}
416417

417418
needPatch = r.gatewayConditionUpdater(gw, string(gwv1.GatewayConditionAccepted), metav1.ConditionTrue, string(gwv1.GatewayConditionAccepted), "") || needPatch
419+
normalizedDNSName := strings.ToLower(lbStatus.DNSName)
418420
if len(gw.Status.Addresses) != 1 ||
419-
gw.Status.Addresses[0].Value != lbStatus.DNSName {
421+
gw.Status.Addresses[0].Value != normalizedDNSName {
420422
ipAddressType := gwv1.HostnameAddressType
421423
gw.Status.Addresses = []gwv1.GatewayStatusAddress{
422424
{
423425
Type: &ipAddressType,
424-
Value: lbStatus.DNSName,
426+
Value: normalizedDNSName,
425427
},
426428
}
427429
needPatch = true

controllers/gateway/gateway_controller_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import (
66
"testing"
77
"time"
88

9+
elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
910
"github.com/go-logr/logr"
1011
"github.com/stretchr/testify/assert"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
"k8s.io/client-go/tools/record"
1314
ctrlerrors "sigs.k8s.io/aws-load-balancer-controller/pkg/error"
1415
gateway_constants "sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/constants"
16+
"sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/routeutils"
1517
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
18+
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
1619
"sigs.k8s.io/aws-load-balancer-controller/pkg/testutils"
1720
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
1821
)
@@ -140,3 +143,41 @@ func Test_handleReconcileError(t *testing.T) {
140143
})
141144
}
142145
}
146+
147+
func Test_updateGatewayStatusSuccess_normalizesDNSNameToLowercase(t *testing.T) {
148+
k8sClient := testutils.GenerateTestClient()
149+
gw := &gwv1.Gateway{
150+
ObjectMeta: metav1.ObjectMeta{
151+
Name: "test-gw",
152+
Namespace: "test-ns",
153+
},
154+
}
155+
err := k8sClient.Create(context.Background(), gw)
156+
assert.NoError(t, err)
157+
158+
reconciler := &gatewayReconciler{
159+
k8sClient: k8sClient,
160+
logger: logr.Discard(),
161+
eventRecorder: record.NewFakeRecorder(10),
162+
gatewayConditionUpdater: prepareGatewayConditionUpdate,
163+
}
164+
165+
lbStatus := &elbv2model.LoadBalancerStatus{
166+
LoadBalancerARN: "arn:aws:elasticloadbalancing:region:account-id:loadbalancer/app/my-alb/123456789",
167+
DNSName: "MyCamelBalancer-1234567890.EU-WEST-1.ELB.AMAZONAWS.COM",
168+
ProvisioningState: &elbv2types.LoadBalancerState{
169+
Code: elbv2types.LoadBalancerStateEnumActive,
170+
},
171+
}
172+
173+
err = reconciler.updateGatewayStatusSuccess(context.Background(), lbStatus, gw, routeutils.LoaderResult{})
174+
assert.NoError(t, err)
175+
176+
updatedGW := &gwv1.Gateway{}
177+
err = k8sClient.Get(context.Background(), k8s.NamespacedName(gw), updatedGW)
178+
assert.NoError(t, err)
179+
assert.Len(t, updatedGW.Status.Addresses, 1)
180+
assert.Equal(t, "mycamelbalancer-1234567890.eu-west-1.elb.amazonaws.com", updatedGW.Status.Addresses[0].Value)
181+
assert.NotNil(t, updatedGW.Status.Addresses[0].Type)
182+
assert.Equal(t, gwv1.HostnameAddressType, *updatedGW.Status.Addresses[0].Type)
183+
}

0 commit comments

Comments
 (0)