From 2b7b4b4508e66caaed6ae46b9eef9a72e6a3f5e4 Mon Sep 17 00:00:00 2001 From: Xiaopei Liu Date: Thu, 12 Dec 2024 15:27:33 +0800 Subject: [PATCH] Truncate VPC display name if it exceeds 80 chars VPC display name needs to be <=80 when VPC in VC is enabled. Tests: For a NetworkInfo obj with Name: "test-ns-03a2def3-0087-4077-904e-23e4dd788fb7" and UID: "ecc6eb9f-92b5-4893-b809-e3ebc1fcf59e", test that the VPC name created is truncated to ""test-ns-03a2def3-0087-4077-904e-23e4dd788fb7_f4f0080e". --- pkg/nsx/services/vpc/builder.go | 2 +- pkg/nsx/services/vpc/builder_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/nsx/services/vpc/builder.go b/pkg/nsx/services/vpc/builder.go index 7e5e2b5de..79a8326ea 100644 --- a/pkg/nsx/services/vpc/builder.go +++ b/pkg/nsx/services/vpc/builder.go @@ -67,7 +67,7 @@ func buildNSXVPC(obj *v1alpha1.NetworkInfo, nsObj *v1.Namespace, nc common.VPCNe *vpc = *nsxVPC } else { // for creating vpc case, fill in vpc properties based on networkconfig - vpcName := util.GenerateIDByObjectByLimit(obj, common.MaxNameLength) + vpcName := util.GenerateIDByObjectByLimit(obj, common.MaxSubnetNameLength) vpc.DisplayName = &vpcName vpc.Id = common.String(util.GenerateIDByObject(obj)) vpc.IpAddressType = &DefaultVPCIPAddressType diff --git a/pkg/nsx/services/vpc/builder_test.go b/pkg/nsx/services/vpc/builder_test.go index 647f88b35..e6683715b 100644 --- a/pkg/nsx/services/vpc/builder_test.go +++ b/pkg/nsx/services/vpc/builder_test.go @@ -96,6 +96,7 @@ func TestBuildNSXVPC(t *testing.T) { existingVPC *model.Vpc ncPrivateIps []string useAVILB bool + netInfoObj *v1alpha1.NetworkInfo expVPC *model.Vpc lbProviderChanged bool }{ @@ -159,6 +160,29 @@ func TestBuildNSXVPC(t *testing.T) { }, }, }, + { + name: "create new VPC with 81 chars networkinfo name", + ncPrivateIps: []string{"192.168.3.0/24"}, + useAVILB: false, + lbProviderChanged: false, + netInfoObj: &v1alpha1.NetworkInfo{ + ObjectMeta: metav1.ObjectMeta{Namespace: "ns1", Name: "test-ns-03a2def3-0087-4077-904e-23e4dd788fb7", UID: "ecc6eb9f-92b5-4893-b809-e3ebc1fcf59e"}, + VPCs: nil, + }, + expVPC: &model.Vpc{ + Id: common.String("test-ns-03a2def3-0087-4077-904e-23e4dd788fb7_ecc6eb9f-92b5-4893-b809-e3ebc1fcf59e"), + DisplayName: common.String("test-ns-03a2def3-0087-4077-904e-23e4dd788fb7_f4f0080e"), + PrivateIps: []string{"192.168.3.0/24"}, + IpAddressType: common.String("IPV4"), + Tags: []model.Tag{ + {Scope: common.String("nsx-op/cluster"), Tag: common.String("cluster1")}, + {Scope: common.String("nsx-op/version"), Tag: common.String("1.0.0")}, + {Scope: common.String("nsx-op/namespace"), Tag: common.String("ns1")}, + {Scope: common.String("nsx-op/namespace_uid"), Tag: common.String("nsuid1")}, + {Scope: common.String("nsx/managed-by"), Tag: common.String("nsx-op")}, + }, + }, + }, { name: "update VPC with AVI load balancer disabled -> enabled", ncPrivateIps: []string{"192.168.3.0/24"}, @@ -199,6 +223,9 @@ func TestBuildNSXVPC(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { nc.PrivateIPs = tc.ncPrivateIps + if tc.netInfoObj != nil { + netInfoObj = tc.netInfoObj + } got, err := buildNSXVPC(netInfoObj, nsObj, nc, clusterStr, tc.existingVPC, tc.useAVILB, tc.lbProviderChanged) assert.Nil(t, err) assert.Equal(t, tc.expVPC, got)