From 4db49525226e1c9ab518abe9147ae68c69936f3b Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Mon, 6 Sep 2021 14:52:57 +0200 Subject: [PATCH] Propagate topology labels to MachineSets and Machines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan Büringer buringerst@vmware.com --- controllers/topology/desired_state.go | 14 +++++++++++++- controllers/topology/desired_state_test.go | 10 +++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/controllers/topology/desired_state.go b/controllers/topology/desired_state.go index cc8af6447fbe..eb5286b86d5c 100644 --- a/controllers/topology/desired_state.go +++ b/controllers/topology/desired_state.go @@ -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") @@ -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 diff --git a/controllers/topology/desired_state_test.go b/controllers/topology/desired_state_test.go index 9b2e28a68adb..fe6686f77321 100644 --- a/controllers/topology/desired_state_test.go +++ b/controllers/topology/desired_state_test.go @@ -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), })) @@ -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")) }) @@ -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")) })