Skip to content

Commit 199e00b

Browse files
authored
Merge pull request #11197 from chrischdi/pr-fix-cc-nil-workers
🐛 clusterclass: fix nil pointer for empty workers in webhook
2 parents f6e8b3b + a503a92 commit 199e00b

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

internal/webhooks/cluster.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -772,17 +772,19 @@ func DefaultAndValidateVariables(ctx context.Context, cluster, oldCluster *clust
772772
oldCPOverrides = oldCluster.Spec.Topology.ControlPlane.Variables.Overrides
773773
}
774774

775-
oldMDVariables = make(map[string][]clusterv1.ClusterVariable, len(oldCluster.Spec.Topology.Workers.MachineDeployments))
776-
for _, md := range oldCluster.Spec.Topology.Workers.MachineDeployments {
777-
if md.Variables != nil {
778-
oldMDVariables[md.Name] = md.Variables.Overrides
775+
if oldCluster.Spec.Topology.Workers != nil {
776+
oldMDVariables = make(map[string][]clusterv1.ClusterVariable, len(oldCluster.Spec.Topology.Workers.MachineDeployments))
777+
for _, md := range oldCluster.Spec.Topology.Workers.MachineDeployments {
778+
if md.Variables != nil {
779+
oldMDVariables[md.Name] = md.Variables.Overrides
780+
}
779781
}
780-
}
781782

782-
oldMPVariables = make(map[string][]clusterv1.ClusterVariable, len(oldCluster.Spec.Topology.Workers.MachinePools))
783-
for _, mp := range oldCluster.Spec.Topology.Workers.MachinePools {
784-
if mp.Variables != nil {
785-
oldMPVariables[mp.Name] = mp.Variables.Overrides
783+
oldMPVariables = make(map[string][]clusterv1.ClusterVariable, len(oldCluster.Spec.Topology.Workers.MachinePools))
784+
for _, mp := range oldCluster.Spec.Topology.Workers.MachinePools {
785+
if mp.Variables != nil {
786+
oldMPVariables[mp.Name] = mp.Variables.Overrides
787+
}
786788
}
787789
}
788790
}

internal/webhooks/cluster_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ func TestClusterDefaultAndValidateVariables(t *testing.T) {
164164
},
165165
},
166166
},
167+
{
168+
name: "should pass with empty oldTopology",
169+
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
170+
Build(),
171+
topology: &clusterv1.Topology{},
172+
oldTopology: &clusterv1.Topology{},
173+
expect: &clusterv1.Topology{
174+
Class: "class1",
175+
Version: "v1.22.2",
176+
Variables: []clusterv1.ClusterVariable{},
177+
},
178+
},
167179
{
168180
name: "don't change a variable if it is already set",
169181
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").

0 commit comments

Comments
 (0)