diff --git a/controllers/topology/desired_state.go b/controllers/topology/desired_state.go index 6589220b3f5a..4541f5e06555 100644 --- a/controllers/topology/desired_state.go +++ b/controllers/topology/desired_state.go @@ -401,6 +401,14 @@ func computeMachineDeployment(_ context.Context, s *scope.Scope, desiredControlP labels[clusterv1.ClusterTopologyMachineDeploymentLabelName] = machineDeploymentTopology.Name desiredMachineDeploymentObj.SetLabels(labels) + // Set the select with the subset of labels identifying controlled machines. + // NOTE: this prevents the web hook to add cluster.x-k8s.io/deployment-name label, that is + // redundant for managed MachineDeployments given that we already have topology.cluster.x-k8s.io/deployment-name. + desiredMachineDeploymentObj.Spec.Selector.MatchLabels = map[string]string{} + desiredMachineDeploymentObj.Spec.Selector.MatchLabels[clusterv1.ClusterLabelName] = s.Current.Cluster.Name + desiredMachineDeploymentObj.Spec.Selector.MatchLabels[clusterv1.ClusterTopologyOwnedLabel] = "" + desiredMachineDeploymentObj.Spec.Selector.MatchLabels[clusterv1.ClusterTopologyMachineDeploymentLabelName] = machineDeploymentTopology.Name + // 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. diff --git a/controllers/topology/desired_state_test.go b/controllers/topology/desired_state_test.go index af32ae6a8d40..ab5f56def733 100644 --- a/controllers/topology/desired_state_test.go +++ b/controllers/topology/desired_state_test.go @@ -740,6 +740,9 @@ func TestComputeMachineDeployment(t *testing.T) { g.Expect(actualMd.Labels).To(HaveKey(clusterv1.ClusterTopologyOwnedLabel)) g.Expect(controllerutil.ContainsFinalizer(actualMd, clusterv1.MachineDeploymentTopologyFinalizer)).To(BeTrue()) + g.Expect(actualMd.Spec.Selector.MatchLabels).To(HaveKey(clusterv1.ClusterTopologyOwnedLabel)) + g.Expect(actualMd.Spec.Selector.MatchLabels).To(HaveKeyWithValue(clusterv1.ClusterTopologyMachineDeploymentLabelName, "big-pool-of-machines")) + 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))