Skip to content

Commit a59e975

Browse files
authored
fix(release/v0.33.x): webhook panic when management cluster has nil topology (#1381)
**What problem does this PR solve?**: Backport #1372 Fixes a panic in the addon webhook and handles the case when creating a ClusterClass workload cluster in a non ClusterClass management cluster. **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. -->
1 parent 1064bcc commit a59e975

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/webhook/addons/registry/autoenabler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ func (a *workloadClusterAutoEnabler) defaulter(
9595
if managementCluster == nil {
9696
return admission.Allowed("")
9797
}
98+
// Check if the management cluster is a ClusterClass based cluster, just return if it is not.
99+
if managementCluster.Spec.Topology == nil {
100+
return admission.Allowed("")
101+
}
98102
// Check if the addon is enabled in the management cluster, if not just return.
99103
managementClusterRegistry, err := variables.RegistryAddon(managementCluster)
100104
if err != nil {

pkg/webhook/addons/registry/webhook_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,44 @@ func TestDefaultingShouldBeSkippedForAManagementCluster(t *testing.T) {
265265
g.Expect(clusterConfig.Addons.Registry).To(BeNil())
266266
}
267267

268+
func TestDefaultingShouldBeSkippedWhenManagementClusterHasNoTopology(t *testing.T) {
269+
g := NewWithT(t)
270+
271+
// Create a non-ClusterClass management cluster.
272+
managementCluster := &clusterv1.Cluster{
273+
ObjectMeta: metav1.ObjectMeta{
274+
GenerateName: "test-mgmt-cluster-",
275+
Namespace: metav1.NamespaceDefault,
276+
},
277+
Spec: clusterv1.ClusterSpec{
278+
// No Topology field - simulates a non-ClusterClass management cluster
279+
},
280+
}
281+
g.Expect(env.Client.Create(ctx, managementCluster)).To(Succeed())
282+
t.Cleanup(func() {
283+
g.Expect(env.Client.Delete(ctx, managementCluster)).To(Succeed())
284+
})
285+
286+
// Create a workload cluster with topology.
287+
workloadCluster := createTestCluster(
288+
t,
289+
nil,
290+
&carenv1.DockerClusterConfigSpec{
291+
Addons: &carenv1.DockerAddons{
292+
GenericAddons: carenv1.GenericAddons{
293+
CNI: &carenv1.CNI{},
294+
},
295+
},
296+
},
297+
)
298+
299+
// Validate that the registry addon is not automatically enabled because
300+
// the management cluster has no topology (non-ClusterClass).
301+
clusterConfig, err := variables.UnmarshalClusterConfigVariable(workloadCluster.Spec.Topology.Variables)
302+
g.Expect(err).ToNot(HaveOccurred())
303+
g.Expect(clusterConfig.Addons.Registry).To(BeNil())
304+
}
305+
268306
func createTestManagementCluster(
269307
t *testing.T,
270308
clusterConfigSpec *carenv1.DockerClusterConfigSpec,

0 commit comments

Comments
 (0)