Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Propagate topology labels to MachineSets and Machines #5211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, so they are propagated down to Machines.
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