Skip to content

Commit

Permalink
Propagate topology labels to MachineSets and Machines
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Sep 6, 2021
1 parent b99ff45 commit 5ea80cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
14 changes: 13 additions & 1 deletion controllers/topology/desired_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,16 @@ func computeControlPlane(_ context.Context, s *scope.Scope, infrastructureMachin

// Compute the labels and annotations to be applied to ControlPlane machines.
// We merge the labels and annotations from topology and ClusterClass.
// We also add the cluster-name and the topology owned labels.
topologyMetadata := s.Blueprint.Topology.ControlPlane.Metadata
clusterClassMetadata := s.Blueprint.ClusterClass.Spec.ControlPlane.Metadata

machineLabels := mergeMap(topologyMetadata.Labels, clusterClassMetadata.Labels)
machineLabels[clusterv1.ClusterLabelName] = cluster.Name
machineLabels[clusterv1.ClusterTopologyOwnedLabel] = ""
if err := contract.ControlPlane().MachineTemplate().Metadata().Set(controlPlane,
&clusterv1.ObjectMeta{
Labels: mergeMap(topologyMetadata.Labels, clusterClassMetadata.Labels),
Labels: machineLabels,
Annotations: mergeMap(topologyMetadata.Annotations, clusterClassMetadata.Annotations),
}); err != nil {
return nil, errors.Wrap(err, "failed to spec.machineTemplate.metadata in the ControlPlane object")
Expand Down Expand Up @@ -294,6 +299,13 @@ func computeMachineDeployment(_ context.Context, s *scope.Scope, machineDeployme
labels[clusterv1.ClusterTopologyMachineDeploymentLabelName] = machineDeploymentTopology.Name
desiredMachineDeploymentObj.SetLabels(labels)

// Also set the labels in .spec.template.labels so that they are propagated to
// MachineSet.labels and MachineSet.spec.template.labels and thus to Machine.labels.
// Note: the labels in MachineSet are used to properly cleanup templates when the MachineSet is deleted.
desiredMachineDeploymentObj.Spec.Template.Labels[clusterv1.ClusterLabelName] = s.Current.Cluster.Name
desiredMachineDeploymentObj.Spec.Template.Labels[clusterv1.ClusterTopologyOwnedLabel] = ""
desiredMachineDeploymentObj.Spec.Template.Labels[clusterv1.ClusterTopologyMachineDeploymentLabelName] = machineDeploymentTopology.Name

// Set the desired replicas.
desiredMachineDeploymentObj.Spec.Replicas = machineDeploymentTopology.Replicas

Expand Down
10 changes: 9 additions & 1 deletion controllers/topology/desired_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,12 @@ func TestComputeControlPlane(t *testing.T) {
})
gotMetadata, err := contract.ControlPlane().MachineTemplate().Metadata().Get(obj)
g.Expect(err).ToNot(HaveOccurred())

expectedLabels := mergeMap(scope.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels)
expectedLabels[clusterv1.ClusterLabelName] = cluster.Name
expectedLabels[clusterv1.ClusterTopologyOwnedLabel] = ""
g.Expect(gotMetadata).To(Equal(&clusterv1.ObjectMeta{
Labels: mergeMap(scope.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
Labels: expectedLabels,
Annotations: mergeMap(scope.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
}))

Expand Down Expand Up @@ -504,9 +508,11 @@ func TestComputeMachineDeployment(t *testing.T) {
g.Expect(actualMd.Name).To(ContainSubstring("big-pool-of-machines"))

g.Expect(actualMd.Labels).To(HaveKeyWithValue(clusterv1.ClusterTopologyMachineDeploymentLabelName, "big-pool-of-machines"))
g.Expect(actualMd.Labels).To(HaveKey(clusterv1.ClusterTopologyOwnedLabel))

g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue("foo", "baz"))
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue("fizz", "buzz"))
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(HaveKey(clusterv1.ClusterTopologyOwnedLabel))
g.Expect(actualMd.Spec.Template.Spec.InfrastructureRef.Name).ToNot(Equal("linux-worker-inframachinetemplate"))
g.Expect(actualMd.Spec.Template.Spec.Bootstrap.ConfigRef.Name).ToNot(Equal("linux-worker-bootstraptemplate"))
})
Expand Down Expand Up @@ -549,9 +555,11 @@ func TestComputeMachineDeployment(t *testing.T) {
g.Expect(actualMd.Name).To(Equal("existing-deployment-1"))

g.Expect(actualMd.Labels).To(HaveKeyWithValue(clusterv1.ClusterTopologyMachineDeploymentLabelName, "big-pool-of-machines"))
g.Expect(actualMd.Labels).To(HaveKey(clusterv1.ClusterTopologyOwnedLabel))

g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue("foo", "baz"))
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue("fizz", "buzz"))
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(HaveKey(clusterv1.ClusterTopologyOwnedLabel))
g.Expect(actualMd.Spec.Template.Spec.InfrastructureRef.Name).To(Equal("linux-worker-inframachinetemplate"))
g.Expect(actualMd.Spec.Template.Spec.Bootstrap.ConfigRef.Name).To(Equal("linux-worker-bootstraptemplate"))
})
Expand Down

0 comments on commit 5ea80cc

Please sign in to comment.