Skip to content

Commit f36c641

Browse files
author
Yuvaraj Kakaraparthi
committed
restrict machine deployment topology name to less than 63 characters
1 parent 6a4e90c commit f36c641

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

internal/topology/check/compatibility.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"k8s.io/apimachinery/pkg/runtime/schema"
2525
"k8s.io/apimachinery/pkg/util/sets"
26+
"k8s.io/apimachinery/pkg/util/validation"
2627
"k8s.io/apimachinery/pkg/util/validation/field"
2728
"sigs.k8s.io/controller-runtime/pkg/client"
2829

@@ -277,6 +278,19 @@ func MachineDeploymentTopologiesAreValidAndDefinedInClusterClass(desired *cluste
277278
machineDeploymentClasses := classNamesFromWorkerClass(clusterClass.Spec.Workers)
278279
names := sets.String{}
279280
for i, md := range desired.Spec.Topology.Workers.MachineDeployments {
281+
if errs := validation.IsValidLabelValue(md.Name); len(errs) != 0 {
282+
for _, err := range errs {
283+
allErrs = append(
284+
allErrs,
285+
field.Invalid(
286+
field.NewPath("spec", "topology", "workers", "machineDeployments").Index(i).Child("name"),
287+
md.Name,
288+
fmt.Sprintf("must be a valid label value %s", err),
289+
),
290+
)
291+
}
292+
}
293+
280294
if !machineDeploymentClasses.Has(md.Class) {
281295
allErrs = append(allErrs,
282296
field.Invalid(

internal/topology/check/compatibility_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,36 @@ func TestMachineDeploymentTopologiesAreUniqueAndDefinedInClusterClass(t *testing
992992
Build(),
993993
wantErr: false,
994994
},
995+
{
996+
name: "fail if MachineDeploymentTopology name is longer than 63 characters",
997+
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
998+
WithInfrastructureClusterTemplate(
999+
builder.InfrastructureClusterTemplate(metav1.NamespaceDefault, "infra1").Build()).
1000+
WithControlPlaneTemplate(
1001+
builder.ControlPlane(metav1.NamespaceDefault, "cp1").Build()).
1002+
WithControlPlaneInfrastructureMachineTemplate(
1003+
builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "cpinfra1").Build()).
1004+
WithWorkerMachineDeploymentClasses(
1005+
*builder.MachineDeploymentClass("aa").
1006+
WithInfrastructureTemplate(
1007+
builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infra1").Build()).
1008+
WithBootstrapTemplate(
1009+
builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap1").Build()).
1010+
Build()).
1011+
Build(),
1012+
cluster: builder.Cluster(metav1.NamespaceDefault, "cluster1").
1013+
WithTopology(
1014+
builder.ClusterTopology().
1015+
WithClass("class1").
1016+
WithVersion("v1.22.2").
1017+
WithMachineDeployment(
1018+
builder.MachineDeploymentTopology("machine-deployment-topology-name-that-has-longerthan63characterlooooooooooooooooooooooongname").
1019+
WithClass("aa").
1020+
Build()).
1021+
Build()).
1022+
Build(),
1023+
wantErr: true,
1024+
},
9951025
}
9961026
for _, tt := range tests {
9971027
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)