From 1bf3df8aec5859313921743ff7cf214509ab2dfc Mon Sep 17 00:00:00 2001 From: Yuvaraj Kakaraparthi Date: Tue, 13 Dec 2022 20:55:44 -0800 Subject: [PATCH] restrict machine deployment topology name to less than 63 characters --- internal/topology/check/compatibility.go | 14 +++++++++ internal/topology/check/compatibility_test.go | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/internal/topology/check/compatibility.go b/internal/topology/check/compatibility.go index 604d583844b0..fe7adaf9f50e 100644 --- a/internal/topology/check/compatibility.go +++ b/internal/topology/check/compatibility.go @@ -23,6 +23,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/sets" + "k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation/field" "sigs.k8s.io/controller-runtime/pkg/client" @@ -277,6 +278,19 @@ func MachineDeploymentTopologiesAreValidAndDefinedInClusterClass(desired *cluste machineDeploymentClasses := classNamesFromWorkerClass(clusterClass.Spec.Workers) names := sets.String{} for i, md := range desired.Spec.Topology.Workers.MachineDeployments { + if errs := validation.IsValidLabelValue(md.Name); len(errs) != 0 { + for _, err := range errs { + allErrs = append( + allErrs, + field.Invalid( + field.NewPath("spec", "topology", "workers", "machineDeployments").Index(i).Child("name"), + md.Name, + fmt.Sprintf("must be a valid label value %s", err), + ), + ) + } + } + if !machineDeploymentClasses.Has(md.Class) { allErrs = append(allErrs, field.Invalid( diff --git a/internal/topology/check/compatibility_test.go b/internal/topology/check/compatibility_test.go index 0f2c9074c4f1..ece2a14df892 100644 --- a/internal/topology/check/compatibility_test.go +++ b/internal/topology/check/compatibility_test.go @@ -992,6 +992,36 @@ func TestMachineDeploymentTopologiesAreUniqueAndDefinedInClusterClass(t *testing Build(), wantErr: false, }, + { + name: "fail if MachineDeploymentTopology name is longer than 63 characters", + clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1"). + WithInfrastructureClusterTemplate( + builder.InfrastructureClusterTemplate(metav1.NamespaceDefault, "infra1").Build()). + WithControlPlaneTemplate( + builder.ControlPlane(metav1.NamespaceDefault, "cp1").Build()). + WithControlPlaneInfrastructureMachineTemplate( + builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "cpinfra1").Build()). + WithWorkerMachineDeploymentClasses( + *builder.MachineDeploymentClass("aa"). + WithInfrastructureTemplate( + builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infra1").Build()). + WithBootstrapTemplate( + builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap1").Build()). + Build()). + Build(), + cluster: builder.Cluster(metav1.NamespaceDefault, "cluster1"). + WithTopology( + builder.ClusterTopology(). + WithClass("class1"). + WithVersion("v1.22.2"). + WithMachineDeployment( + builder.MachineDeploymentTopology("machine-deployment-topology-name-that-has-longerthan63characterlooooooooooooooooooooooongname"). + WithClass("aa"). + Build()). + Build()). + Build(), + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {