Skip to content

Commit 5c6f68b

Browse files
authored
Merge pull request #10843 from chess-knight/remediation_template_namespace_defaulting
🐛 Fix default namespace of RemediationTemplate for ClusterClass and Topology
2 parents b232145 + 8752f4f commit 5c6f68b

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

internal/webhooks/cluster.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ func (webhook *Cluster) Default(ctx context.Context, obj runtime.Object) error {
119119
return apierrors.NewInvalid(clusterv1.GroupVersion.WithKind("Cluster").GroupKind(), cluster.Name, allErrs)
120120
}
121121

122+
if cluster.Spec.Topology.ControlPlane.MachineHealthCheck != nil &&
123+
cluster.Spec.Topology.ControlPlane.MachineHealthCheck.MachineHealthCheckClass.RemediationTemplate != nil &&
124+
cluster.Spec.Topology.ControlPlane.MachineHealthCheck.MachineHealthCheckClass.RemediationTemplate.Namespace == "" {
125+
cluster.Spec.Topology.ControlPlane.MachineHealthCheck.MachineHealthCheckClass.RemediationTemplate.Namespace = cluster.Namespace
126+
}
127+
128+
if cluster.Spec.Topology.Workers != nil {
129+
for i := range cluster.Spec.Topology.Workers.MachineDeployments {
130+
md := cluster.Spec.Topology.Workers.MachineDeployments[i]
131+
if md.MachineHealthCheck != nil &&
132+
md.MachineHealthCheck.MachineHealthCheckClass.RemediationTemplate != nil &&
133+
md.MachineHealthCheck.MachineHealthCheckClass.RemediationTemplate.Namespace == "" {
134+
md.MachineHealthCheck.MachineHealthCheckClass.RemediationTemplate.Namespace = cluster.Namespace
135+
}
136+
}
137+
}
138+
122139
clusterClass, err := webhook.pollClusterClassForCluster(ctx, cluster)
123140
if err != nil {
124141
// If the ClusterClass can't be found or is not up to date ignore the error.

internal/webhooks/cluster_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,57 @@ func TestClusterDefaultNamespaces(t *testing.T) {
6868
g.Expect(c.Spec.ControlPlaneRef.Namespace).To(Equal(c.Namespace))
6969
}
7070

71+
func TestClusterTopologyDefaultNamespaces(t *testing.T) {
72+
// NOTE: ClusterTopology feature flag is disabled by default, thus preventing to set Cluster.Topologies.
73+
// Enabling the feature flag temporarily for this test.
74+
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()
75+
76+
g := NewWithT(t)
77+
78+
c := builder.Cluster("fooboo", "cluster1").
79+
WithTopology(builder.ClusterTopology().
80+
WithClass("foo").
81+
WithVersion("v1.19.1").
82+
WithControlPlaneMachineHealthCheck(&clusterv1.MachineHealthCheckTopology{
83+
MachineHealthCheckClass: clusterv1.MachineHealthCheckClass{
84+
RemediationTemplate: &corev1.ObjectReference{},
85+
},
86+
}).
87+
WithMachineDeployment(
88+
builder.MachineDeploymentTopology("md1").
89+
WithClass("aa").
90+
WithMachineHealthCheck(&clusterv1.MachineHealthCheckTopology{
91+
MachineHealthCheckClass: clusterv1.MachineHealthCheckClass{
92+
RemediationTemplate: &corev1.ObjectReference{},
93+
},
94+
}).
95+
Build()).
96+
Build()).
97+
Build()
98+
99+
clusterClass := builder.ClusterClass("fooboo", "foo").
100+
WithControlPlaneInfrastructureMachineTemplate(&unstructured.Unstructured{}).
101+
WithWorkerMachineDeploymentClasses(*builder.MachineDeploymentClass("aa").Build()).
102+
Build()
103+
conditions.MarkTrue(clusterClass, clusterv1.ClusterClassVariablesReconciledCondition)
104+
// Sets up the fakeClient for the test case. This is required because the test uses a Managed Topology.
105+
fakeClient := fake.NewClientBuilder().
106+
WithObjects(clusterClass).
107+
WithScheme(fakeScheme).
108+
Build()
109+
110+
// Create the webhook and add the fakeClient as its client.
111+
webhook := &Cluster{Client: fakeClient}
112+
t.Run("for Cluster", util.CustomDefaultValidateTest(ctx, c, webhook))
113+
114+
g.Expect(webhook.Default(ctx, c)).To(Succeed())
115+
116+
g.Expect(c.Spec.Topology.ControlPlane.MachineHealthCheck.MachineHealthCheckClass.RemediationTemplate.Namespace).To(Equal(c.Namespace))
117+
for i := range c.Spec.Topology.Workers.MachineDeployments {
118+
g.Expect(c.Spec.Topology.Workers.MachineDeployments[i].MachineHealthCheck.MachineHealthCheckClass.RemediationTemplate.Namespace).To(Equal(c.Namespace))
119+
}
120+
}
121+
71122
// TestClusterDefaultAndValidateVariables cases where cluster.spec.topology.class is altered.
72123
func TestClusterDefaultAndValidateVariables(t *testing.T) {
73124
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()

internal/webhooks/clusterclass.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,17 @@ func (webhook *ClusterClass) Default(_ context.Context, obj runtime.Object) erro
7575
defaultNamespace(in.Spec.ControlPlane.MachineInfrastructure.Ref, in.Namespace)
7676
}
7777

78+
if in.Spec.ControlPlane.MachineHealthCheck != nil {
79+
defaultNamespace(in.Spec.ControlPlane.MachineHealthCheck.RemediationTemplate, in.Namespace)
80+
}
81+
7882
for i := range in.Spec.Workers.MachineDeployments {
7983
defaultNamespace(in.Spec.Workers.MachineDeployments[i].Template.Bootstrap.Ref, in.Namespace)
8084
defaultNamespace(in.Spec.Workers.MachineDeployments[i].Template.Infrastructure.Ref, in.Namespace)
85+
86+
if in.Spec.Workers.MachineDeployments[i].MachineHealthCheck != nil {
87+
defaultNamespace(in.Spec.Workers.MachineDeployments[i].MachineHealthCheck.RemediationTemplate, in.Namespace)
88+
}
8189
}
8290

8391
for i := range in.Spec.Workers.MachinePools {

internal/webhooks/clusterclass_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,18 @@ func TestClusterClassDefaultNamespaces(t *testing.T) {
6565
WithControlPlaneInfrastructureMachineTemplate(
6666
builder.InfrastructureMachineTemplate("", "cpInfra1").
6767
Build()).
68+
WithControlPlaneMachineHealthCheck(&clusterv1.MachineHealthCheckClass{
69+
RemediationTemplate: &corev1.ObjectReference{},
70+
}).
6871
WithWorkerMachineDeploymentClasses(
6972
*builder.MachineDeploymentClass("aa").
7073
WithInfrastructureTemplate(
7174
builder.InfrastructureMachineTemplate("", "infra1").Build()).
7275
WithBootstrapTemplate(
7376
builder.BootstrapTemplate("", "bootstrap1").Build()).
77+
WithMachineHealthCheckClass(&clusterv1.MachineHealthCheckClass{
78+
RemediationTemplate: &corev1.ObjectReference{},
79+
}).
7480
Build()).
7581
WithWorkerMachinePoolClasses(
7682
*builder.MachinePoolClass("aa").
@@ -97,9 +103,11 @@ func TestClusterClassDefaultNamespaces(t *testing.T) {
97103
g.Expect(in.Spec.Infrastructure.Ref.Namespace).To(Equal(namespace))
98104
g.Expect(in.Spec.ControlPlane.Ref.Namespace).To(Equal(namespace))
99105
g.Expect(in.Spec.ControlPlane.MachineInfrastructure.Ref.Namespace).To(Equal(namespace))
106+
g.Expect(in.Spec.ControlPlane.MachineHealthCheck.RemediationTemplate.Namespace).To(Equal(namespace))
100107
for i := range in.Spec.Workers.MachineDeployments {
101108
g.Expect(in.Spec.Workers.MachineDeployments[i].Template.Bootstrap.Ref.Namespace).To(Equal(namespace))
102109
g.Expect(in.Spec.Workers.MachineDeployments[i].Template.Infrastructure.Ref.Namespace).To(Equal(namespace))
110+
g.Expect(in.Spec.Workers.MachineDeployments[i].MachineHealthCheck.RemediationTemplate.Namespace).To(Equal(namespace))
103111
}
104112
for i := range in.Spec.Workers.MachinePools {
105113
g.Expect(in.Spec.Workers.MachinePools[i].Template.Bootstrap.Ref.Namespace).To(Equal(namespace))

0 commit comments

Comments
 (0)