Skip to content

Commit

Permalink
set MachineDeployment selector in managed clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziopandini committed Oct 25, 2021
1 parent ab0bb57 commit fb69d95
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions controllers/topology/desired_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions controllers/topology/desired_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit fb69d95

Please sign in to comment.