Skip to content

Commit 4005772

Browse files
authored
Merge pull request kubernetes-sigs#6646 from Nordix/add-checks-for-not-topology-owned-templates-to-never-reconcile/adil
✨ Add checks for not topology owned templates to never reconcile.
2 parents 1eebf58 + e8743aa commit 4005772

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

internal/controllers/topology/cluster/current_state.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (r *Reconciler) getCurrentInfrastructureClusterState(ctx context.Context, c
8181
// Nb. This is to make sure that a managed topology cluster does not have a reference to an object that is not
8282
// owned by the topology.
8383
if !labels.IsTopologyOwned(infra) {
84-
return nil, fmt.Errorf("referenced infra cluster object %s is not topology owned", tlog.KObj{Obj: infra})
84+
return nil, fmt.Errorf("infra cluster object %s referenced from cluster %s is not topology owned", tlog.KObj{Obj: infra}, tlog.KObj{Obj: cluster})
8585
}
8686
return infra, nil
8787
}
@@ -102,7 +102,7 @@ func (r *Reconciler) getCurrentControlPlaneState(ctx context.Context, cluster *c
102102
// Nb. This is to make sure that a managed topology cluster does not have a reference to an object that is not
103103
// owned by the topology.
104104
if !labels.IsTopologyOwned(res.Object) {
105-
return nil, fmt.Errorf("referenced control plane object %s is not topology owned", tlog.KObj{Obj: res.Object})
105+
return nil, fmt.Errorf("control plane object %s referenced from cluster %s is not topology owned", tlog.KObj{Obj: res.Object}, tlog.KObj{Obj: cluster})
106106
}
107107

108108
// If the clusterClass does not mandate the controlPlane has infrastructureMachines, return.
@@ -119,6 +119,12 @@ func (r *Reconciler) getCurrentControlPlaneState(ctx context.Context, cluster *c
119119
if err != nil {
120120
return nil, errors.Wrapf(err, "failed to get InfrastructureMachineTemplate for %s", tlog.KObj{Obj: res.Object})
121121
}
122+
// check that the referenced object has the ClusterTopologyOwnedLabel label.
123+
// Nb. This is to make sure that a managed topology cluster does not have a reference to an object that is not
124+
// owned by the topology.
125+
if !labels.IsTopologyOwned(res.InfrastructureMachineTemplate) {
126+
return nil, fmt.Errorf("control plane InfrastructureMachineTemplate object %s referenced from cluster %s is not topology owned", tlog.KObj{Obj: res.InfrastructureMachineTemplate}, tlog.KObj{Obj: cluster})
127+
}
122128

123129
mhc := &clusterv1.MachineHealthCheck{}
124130
// MachineHealthCheck always has the same name and namespace as the ControlPlane object it belongs to.
@@ -181,6 +187,12 @@ func (r *Reconciler) getCurrentMachineDeploymentState(ctx context.Context, clust
181187
if err != nil {
182188
return nil, errors.Wrap(err, fmt.Sprintf("%s Bootstrap reference could not be retrieved", tlog.KObj{Obj: m}))
183189
}
190+
// check that the referenced object has the ClusterTopologyOwnedLabel label.
191+
// Nb. This is to make sure that a managed topology cluster does not have a reference to an object that is not
192+
// owned by the topology.
193+
if !labels.IsTopologyOwned(b) {
194+
return nil, fmt.Errorf("BootstrapTemplate object %s referenced from MD %s is not topology owned", tlog.KObj{Obj: b}, tlog.KObj{Obj: m})
195+
}
184196

185197
// Gets the InfrastructureMachineTemplate
186198
infraRef := m.Spec.Template.Spec.InfrastructureRef
@@ -191,6 +203,12 @@ func (r *Reconciler) getCurrentMachineDeploymentState(ctx context.Context, clust
191203
if err != nil {
192204
return nil, errors.Wrap(err, fmt.Sprintf("%s Infrastructure reference could not be retrieved", tlog.KObj{Obj: m}))
193205
}
206+
// check that the referenced object has the ClusterTopologyOwnedLabel label.
207+
// Nb. This is to make sure that a managed topology cluster does not have a reference to an object that is not
208+
// owned by the topology.
209+
if !labels.IsTopologyOwned(infra) {
210+
return nil, fmt.Errorf("InfrastructureMachineTemplate object %s referenced from MD %s is not topology owned", tlog.KObj{Obj: infra}, tlog.KObj{Obj: m})
211+
}
194212

195213
// Gets the MachineHealthCheck.
196214
mhc := &clusterv1.MachineHealthCheck{}

internal/controllers/topology/cluster/current_state_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,25 @@ func TestGetCurrentState(t *testing.T) {
5757
// ControlPlane and ControlPlaneInfrastructureMachineTemplate objects.
5858
controlPlaneInfrastructureMachineTemplate := builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "cpInfraTemplate").
5959
Build()
60+
controlPlaneInfrastructureMachineTemplate.SetLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""})
61+
controlPlaneInfrastructureMachineTemplateNotTopologyOwned := builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "cpInfraTemplate").
62+
Build()
6063
controlPlaneTemplateWithInfrastructureMachine := builder.ControlPlaneTemplate(metav1.NamespaceDefault, "cpTemplateWithInfra1").
6164
WithInfrastructureMachineTemplate(controlPlaneInfrastructureMachineTemplate).
6265
Build()
66+
controlPlaneTemplateWithInfrastructureMachineNotTopologyOwned := builder.ControlPlaneTemplate(metav1.NamespaceDefault, "cpTemplateWithInfra1").
67+
WithInfrastructureMachineTemplate(controlPlaneInfrastructureMachineTemplateNotTopologyOwned).
68+
Build()
6369
controlPlane := builder.ControlPlane(metav1.NamespaceDefault, "cp1").
6470
Build()
6571
controlPlane.SetLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""})
6672
controlPlaneWithInfra := builder.ControlPlane(metav1.NamespaceDefault, "cp1").
6773
WithInfrastructureMachineTemplate(controlPlaneInfrastructureMachineTemplate).
6874
Build()
6975
controlPlaneWithInfra.SetLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""})
70-
76+
controlPlaneWithInfraNotTopologyOwned := builder.ControlPlane(metav1.NamespaceDefault, "cp1").
77+
WithInfrastructureMachineTemplate(controlPlaneInfrastructureMachineTemplateNotTopologyOwned).
78+
Build()
7179
controlPlaneNotTopologyOwned := builder.ControlPlane(metav1.NamespaceDefault, "cp1").
7280
Build()
7381

@@ -76,6 +84,10 @@ func TestGetCurrentState(t *testing.T) {
7684
WithControlPlaneTemplate(controlPlaneTemplateWithInfrastructureMachine).
7785
WithControlPlaneInfrastructureMachineTemplate(controlPlaneInfrastructureMachineTemplate).
7886
Build()
87+
clusterClassWithControlPlaneInfraNotTopologyOwned := builder.ClusterClass(metav1.NamespaceDefault, "class1").
88+
WithControlPlaneTemplate(controlPlaneTemplateWithInfrastructureMachineNotTopologyOwned).
89+
WithControlPlaneInfrastructureMachineTemplate(controlPlaneInfrastructureMachineTemplateNotTopologyOwned).
90+
Build()
7991
clusterClassWithNoControlPlaneInfra := builder.ClusterClass(metav1.NamespaceDefault, "class2").
8092
Build()
8193

@@ -84,8 +96,10 @@ func TestGetCurrentState(t *testing.T) {
8496

8597
machineDeploymentInfrastructure := builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infra1").
8698
Build()
99+
machineDeploymentInfrastructure.SetLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""})
87100
machineDeploymentBootstrap := builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap1").
88101
Build()
102+
machineDeploymentBootstrap.SetLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""})
89103

90104
machineDeployment := builder.MachineDeployment(metav1.NamespaceDefault, "md1").
91105
WithLabels(map[string]string{
@@ -312,6 +326,19 @@ func TestGetCurrentState(t *testing.T) {
312326
"md1": {Object: machineDeployment, BootstrapTemplate: machineDeploymentBootstrap, InfrastructureMachineTemplate: machineDeploymentInfrastructure}},
313327
},
314328
},
329+
{
330+
name: "Fails if the ControlPlane references an InfrastructureMachineTemplate that is not topology owned",
331+
cluster: builder.Cluster(metav1.NamespaceDefault, "cluster1").
332+
// No InfrastructureCluster!
333+
WithControlPlane(controlPlaneWithInfraNotTopologyOwned).
334+
Build(),
335+
class: clusterClassWithControlPlaneInfraNotTopologyOwned,
336+
objects: []client.Object{
337+
controlPlaneWithInfraNotTopologyOwned,
338+
controlPlaneInfrastructureMachineTemplateNotTopologyOwned,
339+
},
340+
wantErr: true,
341+
},
315342
{
316343
name: "Fails if the ControlPlane references an InfrastructureMachineTemplate that does not exist",
317344
cluster: builder.Cluster(metav1.NamespaceDefault, "cluster1").

0 commit comments

Comments
 (0)