Skip to content

Commit

Permalink
Merge pull request #11197 from chrischdi/pr-fix-cc-nil-workers
Browse files Browse the repository at this point in the history
🐛 clusterclass: fix nil pointer for empty workers in webhook
  • Loading branch information
k8s-ci-robot authored Sep 19, 2024
2 parents f6e8b3b + a503a92 commit 199e00b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
20 changes: 11 additions & 9 deletions internal/webhooks/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,17 +772,19 @@ func DefaultAndValidateVariables(ctx context.Context, cluster, oldCluster *clust
oldCPOverrides = oldCluster.Spec.Topology.ControlPlane.Variables.Overrides
}

oldMDVariables = make(map[string][]clusterv1.ClusterVariable, len(oldCluster.Spec.Topology.Workers.MachineDeployments))
for _, md := range oldCluster.Spec.Topology.Workers.MachineDeployments {
if md.Variables != nil {
oldMDVariables[md.Name] = md.Variables.Overrides
if oldCluster.Spec.Topology.Workers != nil {
oldMDVariables = make(map[string][]clusterv1.ClusterVariable, len(oldCluster.Spec.Topology.Workers.MachineDeployments))
for _, md := range oldCluster.Spec.Topology.Workers.MachineDeployments {
if md.Variables != nil {
oldMDVariables[md.Name] = md.Variables.Overrides
}
}
}

oldMPVariables = make(map[string][]clusterv1.ClusterVariable, len(oldCluster.Spec.Topology.Workers.MachinePools))
for _, mp := range oldCluster.Spec.Topology.Workers.MachinePools {
if mp.Variables != nil {
oldMPVariables[mp.Name] = mp.Variables.Overrides
oldMPVariables = make(map[string][]clusterv1.ClusterVariable, len(oldCluster.Spec.Topology.Workers.MachinePools))
for _, mp := range oldCluster.Spec.Topology.Workers.MachinePools {
if mp.Variables != nil {
oldMPVariables[mp.Name] = mp.Variables.Overrides
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions internal/webhooks/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ func TestClusterDefaultAndValidateVariables(t *testing.T) {
},
},
},
{
name: "should pass with empty oldTopology",
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
Build(),
topology: &clusterv1.Topology{},
oldTopology: &clusterv1.Topology{},
expect: &clusterv1.Topology{
Class: "class1",
Version: "v1.22.2",
Variables: []clusterv1.ClusterVariable{},
},
},
{
name: "don't change a variable if it is already set",
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
Expand Down

0 comments on commit 199e00b

Please sign in to comment.