Skip to content

Commit

Permalink
Add checks for not topology owned templates to never reconcile.
Browse files Browse the repository at this point in the history
  • Loading branch information
adilGhaffarDev committed Jun 13, 2022
1 parent 8a6b4c6 commit 9bdf96a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/controllers/topology/cluster/current_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ func (r *Reconciler) getCurrentMachineDeploymentState(ctx context.Context, clust
for i := range md.Items {
m := &md.Items[i]

// check that the MachineDeployment has the ClusterTopologyOwnedLabel label.
// Nb. This is to make sure that a managed topology cluster does not have a reference to an object that is not
// owned by the topology.
if !labels.IsTopologyOwned(m) {
return nil, fmt.Errorf(" %s is not topology owned", tlog.KObj{Obj: m})
}
// Retrieve the name which is assigned in Cluster's topology
// from a well-defined label.
mdTopologyName, ok := m.ObjectMeta.Labels[clusterv1.ClusterTopologyMachineDeploymentLabelName]
Expand Down
20 changes: 20 additions & 0 deletions internal/controllers/topology/cluster/current_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func TestGetCurrentState(t *testing.T) {
WithInfrastructureTemplate(machineDeploymentInfrastructure).
Build()

machineDeploymentNotTopologyOwned := builder.MachineDeployment(metav1.NamespaceDefault, "md1").
WithLabels(map[string]string{
clusterv1.ClusterLabelName: "cluster1",
}).
WithBootstrapTemplate(machineDeploymentBootstrap).
WithInfrastructureTemplate(machineDeploymentInfrastructure).
Build()

// MachineHealthChecks for the MachineDeployment and the ControlPlane.
machineHealthCheckForMachineDeployment := builder.MachineHealthCheck(machineDeployment.Namespace, machineDeployment.Name).
WithSelector(*selectorForMachineDeploymentMHC(machineDeployment)).
Expand Down Expand Up @@ -380,6 +388,18 @@ func TestGetCurrentState(t *testing.T) {
// Expect error to be thrown as no managed MachineDeployment is reconcilable unless it has a ClusterTopologyMachineDeploymentLabelName.
wantErr: true,
},
{
name: "Fails if there are MachineDeployments without the topology.cluster.x-k8s.io/owned",
cluster: builder.Cluster(metav1.NamespaceDefault, "cluster1").
Build(),
class: clusterClassWithControlPlaneInfra,
objects: []client.Object{
clusterClassWithControlPlaneInfra,
machineDeploymentNotTopologyOwned,
},
// Expect error to be thrown as no managed MachineDeployment is reconcilable unless it has a ClusterTopologyOwnedLabel.
wantErr: true,
},
{
name: "Fails if there are MachineDeployments with the same topology.cluster.x-k8s.io/deployment-name",
cluster: builder.Cluster(metav1.NamespaceDefault, "cluster1").
Expand Down

0 comments on commit 9bdf96a

Please sign in to comment.