Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 ClusterClass: validate MachineDeployment topology name not empty #5951

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions internal/topology/check/compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ func MachineDeploymentClassesAreUnique(clusterClass *clusterv1.ClusterClass) fie
return allErrs
}

// MachineDeploymentTopologiesAreUniqueAndDefinedInClusterClass checks that each MachineDeploymentTopology name is unique, and each class in use is defined in ClusterClass.spec.Workers.MachineDeployments.
func MachineDeploymentTopologiesAreUniqueAndDefinedInClusterClass(desired *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass) field.ErrorList {
// MachineDeploymentTopologiesAreValidAndDefinedInClusterClass checks that each MachineDeploymentTopology name is not empty
// and unique, and each class in use is defined in ClusterClass.spec.Workers.MachineDeployments.
func MachineDeploymentTopologiesAreValidAndDefinedInClusterClass(desired *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass) field.ErrorList {
var allErrs field.ErrorList
if desired.Spec.Topology.Workers == nil {
return nil
Expand All @@ -286,6 +287,19 @@ func MachineDeploymentTopologiesAreUniqueAndDefinedInClusterClass(desired *clust
),
)
}

// MachineDeploymentTopology name should not be empty.
if md.Name == "" {
allErrs = append(
allErrs,
field.Required(
field.NewPath("spec", "topology", "workers", "machineDeployments").Index(i).Child("name"),
"name must not be empty",
),
)
continue
}

if names.Has(md.Name) {
allErrs = append(allErrs,
field.Invalid(
Expand Down
30 changes: 29 additions & 1 deletion internal/topology/check/compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,34 @@ func TestMachineDeploymentTopologiesAreUniqueAndDefinedInClusterClass(t *testing
cluster *clusterv1.Cluster
wantErr bool
}{
{
name: "fail if MachineDeploymentTopologies name is empty",
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
WithInfrastructureClusterTemplate(
builder.InfrastructureClusterTemplate(metav1.NamespaceDefault, "infra1").Build()).
WithControlPlaneTemplate(
builder.ControlPlane(metav1.NamespaceDefault, "cp1").Build()).
WithWorkerMachineDeploymentClasses(
*builder.MachineDeploymentClass("aa").
WithInfrastructureTemplate(
builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infra1").Build()).
WithBootstrapTemplate(
builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap1").Build()).
Build()).
Build(),
cluster: builder.Cluster("fooboo", "cluster1").
WithTopology(builder.ClusterTopology().
WithClass("foo").
WithVersion("v1.19.1").
WithMachineDeployment(
// The name should not be empty.
builder.MachineDeploymentTopology("").
WithClass("aa").
Build()).
Build()).
Build(),
wantErr: true,
},
{
name: "pass if MachineDeploymentTopologies are unique and defined in ClusterClass",
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
Expand Down Expand Up @@ -968,7 +996,7 @@ func TestMachineDeploymentTopologiesAreUniqueAndDefinedInClusterClass(t *testing
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
allErrs := MachineDeploymentTopologiesAreUniqueAndDefinedInClusterClass(tt.cluster, tt.clusterClass)
allErrs := MachineDeploymentTopologiesAreValidAndDefinedInClusterClass(tt.cluster, tt.clusterClass)
if tt.wantErr {
g.Expect(allErrs).ToNot(BeEmpty())
return
Expand Down
2 changes: 1 addition & 1 deletion internal/webhooks/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (webhook *Cluster) validateTopology(ctx context.Context, oldCluster, newClu
return allErrs
}

allErrs = append(allErrs, check.MachineDeploymentTopologiesAreUniqueAndDefinedInClusterClass(newCluster, clusterClass)...)
allErrs = append(allErrs, check.MachineDeploymentTopologiesAreValidAndDefinedInClusterClass(newCluster, clusterClass)...)

// Check if the variables defined in the clusterClass are valid.
allErrs = append(allErrs, variables.ValidateClusterVariables(newCluster.Spec.Topology.Variables, clusterClass.Spec.Variables, field.NewPath("spec", "topology", "variables"))...)
Expand Down