From e3c93ac2d90bb1ccb588310715806ea961dc8a79 Mon Sep 17 00:00:00 2001 From: Kensei Nakada Date: Tue, 2 Jul 2024 10:28:46 +0900 Subject: [PATCH] truncate the addresses to 16 to make the update success Signed-off-by: Kensei Nakada --- internal/gatewayapi/status/gateway.go | 7 ++ internal/gatewayapi/status/gateway_test.go | 114 +++++++++++++++------ 2 files changed, 92 insertions(+), 29 deletions(-) diff --git a/internal/gatewayapi/status/gateway.go b/internal/gatewayapi/status/gateway.go index 48ee437331e..a5aae828887 100644 --- a/internal/gatewayapi/status/gateway.go +++ b/internal/gatewayapi/status/gateway.go @@ -96,8 +96,15 @@ func UpdateGatewayStatusProgrammedCondition(gw *gwapiv1.Gateway, svc *corev1.Ser } else { gw.Status.Addresses = nil } + // Update the programmed condition. gw.Status.Conditions = MergeConditions(gw.Status.Conditions, computeGatewayProgrammedCondition(gw, deployment)) + + if len(gw.Status.Addresses) > 16 { + // Truncate the addresses to 16 + // so that the status can be updated successfully. + gw.Status.Addresses = gw.Status.Addresses[:16] + } } func SetGatewayListenerStatusCondition(gateway *gwapiv1.Gateway, listenerStatusIdx int, diff --git a/internal/gatewayapi/status/gateway_test.go b/internal/gatewayapi/status/gateway_test.go index 6ccdf0c39e2..2d2af9a3d06 100644 --- a/internal/gatewayapi/status/gateway_test.go +++ b/internal/gatewayapi/status/gateway_test.go @@ -18,24 +18,26 @@ import ( gwapiv1 "sigs.k8s.io/gateway-api/apis/v1" ) +// TestUpdateGatewayStatusProgrammedCondition tests whether UpdateGatewayStatusProgrammedCondition correctly updates the addresses in the Gateway status. func TestUpdateGatewayStatusProgrammedCondition(t *testing.T) { type args struct { - gw *gwapiv1.Gateway - svc *corev1.Service - deployment *appsv1.Deployment - addresses []gwapiv1.GatewayStatusAddress + gw *gwapiv1.Gateway + svc *corev1.Service + deployment *appsv1.Deployment + nodeAddresses []string } tests := []struct { - name string - args args + name string + args args + wantAddresses []gwapiv1.GatewayStatusAddress }{ { name: "nil svc", args: args{ - gw: &gwapiv1.Gateway{}, - svc: nil, - addresses: nil, + gw: &gwapiv1.Gateway{}, + svc: nil, }, + wantAddresses: nil, }, { name: "LoadBalancer svc with ingress ip", @@ -58,11 +60,11 @@ func TestUpdateGatewayStatusProgrammedCondition(t *testing.T) { }, }, }, - addresses: []gwapiv1.GatewayStatusAddress{ - { - Type: ptr.To(gwapiv1.IPAddressType), - Value: "127.0.0.1", - }, + }, + wantAddresses: []gwapiv1.GatewayStatusAddress{ + { + Type: ptr.To(gwapiv1.IPAddressType), + Value: "127.0.0.1", }, }, }, @@ -87,15 +89,15 @@ func TestUpdateGatewayStatusProgrammedCondition(t *testing.T) { }, }, }, - addresses: []gwapiv1.GatewayStatusAddress{ - { - Type: ptr.To(gwapiv1.IPAddressType), - Value: "127.0.0.1", - }, - { - Type: ptr.To(gwapiv1.HostnameAddressType), - Value: "localhost", - }, + }, + wantAddresses: []gwapiv1.GatewayStatusAddress{ + { + Type: ptr.To(gwapiv1.IPAddressType), + Value: "127.0.0.1", + }, + { + Type: ptr.To(gwapiv1.HostnameAddressType), + Value: "localhost", }, }, }, @@ -111,19 +113,73 @@ func TestUpdateGatewayStatusProgrammedCondition(t *testing.T) { Type: corev1.ServiceTypeClusterIP, }, }, - addresses: []gwapiv1.GatewayStatusAddress{ - { - Type: ptr.To(gwapiv1.IPAddressType), - Value: "127.0.0.1", + }, + wantAddresses: []gwapiv1.GatewayStatusAddress{ + { + Type: ptr.To(gwapiv1.IPAddressType), + Value: "127.0.0.1", + }, + }, + }, + { + name: "Nodeport svc", + args: args{ + gw: &gwapiv1.Gateway{}, + nodeAddresses: []string{"1", "2"}, + svc: &corev1.Service{ + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{}, + Spec: corev1.ServiceSpec{ + Type: corev1.ServiceTypeNodePort, + }, + }, + }, + wantAddresses: []gwapiv1.GatewayStatusAddress{ + { + Type: ptr.To(gwapiv1.IPAddressType), + Value: "1", + }, + { + Type: ptr.To(gwapiv1.IPAddressType), + Value: "2", + }, + }, + }, + { + name: "Nodeport svc with too many node addresses", + args: args{ + gw: &gwapiv1.Gateway{}, + // 20 node addresses + nodeAddresses: func() (addr []string) { + for i := 0; i < 20; i++ { + addr = append(addr, strconv.Itoa(i)) + } + return + }(), + svc: &corev1.Service{ + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{}, + Spec: corev1.ServiceSpec{ + Type: corev1.ServiceTypeNodePort, }, }, }, + // Only the first 16 addresses should be set. + wantAddresses: func() (addr []gwapiv1.GatewayStatusAddress) { + for i := 0; i < 16; i++ { + addr = append(addr, gwapiv1.GatewayStatusAddress{ + Type: ptr.To(gwapiv1.IPAddressType), + Value: strconv.Itoa(i), + }) + } + return + }(), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - UpdateGatewayStatusProgrammedCondition(tt.args.gw, tt.args.svc, tt.args.deployment) - assert.True(t, reflect.DeepEqual(tt.args.addresses, tt.args.gw.Status.Addresses)) + UpdateGatewayStatusProgrammedCondition(tt.args.gw, tt.args.svc, tt.args.deployment, tt.args.nodeAddresses...) + assert.True(t, reflect.DeepEqual(tt.wantAddresses, tt.args.gw.Status.Addresses)) }) } }