Skip to content

Commit 5ef91a3

Browse files
committed
ClusterClass: don't propagate MD upgrade annotations
Signed-off-by: Stefan Büringer [email protected]
1 parent 13158ae commit 5ef91a3

File tree

2 files changed

+65
-30
lines changed

2 files changed

+65
-30
lines changed

internal/controllers/topology/cluster/desired_state.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,9 @@ func computeMachineDeployment(_ context.Context, s *scope.Scope, desiredControlP
687687

688688
// Apply annotations
689689
machineDeploymentAnnotations := mergeMap(machineDeploymentTopology.Metadata.Annotations, machineDeploymentBlueprint.Metadata.Annotations)
690+
// Ensure the annotations used to control the upgrade sequence are never propagated.
691+
delete(machineDeploymentAnnotations, clusterv1.ClusterTopologyHoldUpgradeSequenceAnnotation)
692+
delete(machineDeploymentAnnotations, clusterv1.ClusterTopologyDeferUpgradeAnnotation)
690693
desiredMachineDeploymentObj.SetAnnotations(machineDeploymentAnnotations)
691694
desiredMachineDeploymentObj.Spec.Template.Annotations = machineDeploymentAnnotations
692695

@@ -990,15 +993,14 @@ func templateToTemplate(in templateToInput) *unstructured.Unstructured {
990993
return template
991994
}
992995

993-
// mergeMap merges two maps into another one.
994-
// NOTE: In case a key exists in both maps, the value in the first map is preserved.
995-
func mergeMap(a, b map[string]string) map[string]string {
996+
// mergeMap merges maps.
997+
// NOTE: In case a key exists in multiple maps, the value of the first map is preserved.
998+
func mergeMap(maps ...map[string]string) map[string]string {
996999
m := make(map[string]string)
997-
for k, v := range b {
998-
m[k] = v
999-
}
1000-
for k, v := range a {
1001-
m[k] = v
1000+
for i := len(maps) - 1; i >= 0; i-- {
1001+
for k, v := range maps[i] {
1002+
m[k] = v
1003+
}
10021004
}
10031005

10041006
// Nil the result if the map is empty, thus avoiding triggering infinite reconcile

internal/controllers/topology/cluster/desired_state_test.go

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,8 +1324,8 @@ func TestComputeMachineDeployment(t *testing.T) {
13241324
Build()
13251325
workerBootstrapTemplate := builder.BootstrapTemplate(metav1.NamespaceDefault, "linux-worker-bootstraptemplate").
13261326
Build()
1327-
labels := map[string]string{"fizz": "buzz", "foo": "bar"}
1328-
annotations := map[string]string{"annotation-1": "annotation-1-val"}
1327+
labels := map[string]string{"fizzLabel": "buzz", "fooLabel": "bar"}
1328+
annotations := map[string]string{"fizzAnnotation": "buzz", "fooAnnotation": "bar"}
13291329

13301330
unhealthyConditions := []clusterv1.UnhealthyCondition{
13311331
{
@@ -1410,7 +1410,17 @@ func TestComputeMachineDeployment(t *testing.T) {
14101410
}
14111411
mdTopology := clusterv1.MachineDeploymentTopology{
14121412
Metadata: clusterv1.ObjectMeta{
1413-
Labels: map[string]string{"foo": "baz"},
1413+
Labels: map[string]string{
1414+
// Should overwrite the label from the MachineDeployment class.
1415+
"fooLabel": "baz",
1416+
},
1417+
Annotations: map[string]string{
1418+
// Should overwrite the annotation from the MachineDeployment class.
1419+
"fooAnnotation": "baz",
1420+
// These annotations should not be propagated to the MachineDeployment.
1421+
clusterv1.ClusterTopologyDeferUpgradeAnnotation: "",
1422+
clusterv1.ClusterTopologyHoldUpgradeSequenceAnnotation: "",
1423+
},
14141424
},
14151425
Class: "linux-worker",
14161426
Name: "big-pool-of-machines",
@@ -1457,18 +1467,28 @@ func TestComputeMachineDeployment(t *testing.T) {
14571467
g.Expect(actualMd.Name).To(ContainSubstring("cluster1"))
14581468
g.Expect(actualMd.Name).To(ContainSubstring("big-pool-of-machines"))
14591469

1460-
g.Expect(actualMd.Labels).To(HaveKeyWithValue(clusterv1.ClusterTopologyMachineDeploymentNameLabel, "big-pool-of-machines"))
1461-
g.Expect(actualMd.Labels).To(HaveKey(clusterv1.ClusterTopologyOwnedLabel))
1462-
for k, v := range mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels) {
1463-
g.Expect(actualMd.Labels).To(HaveKeyWithValue(k, v))
1464-
}
1465-
1466-
g.Expect(actualMd.Spec.Selector.MatchLabels).To(HaveKey(clusterv1.ClusterTopologyOwnedLabel))
1467-
g.Expect(actualMd.Spec.Selector.MatchLabels).To(HaveKeyWithValue(clusterv1.ClusterTopologyMachineDeploymentNameLabel, "big-pool-of-machines"))
1470+
expectedAnnotations := mergeMap(mdTopology.Metadata.Annotations, md1.Template.Metadata.Annotations)
1471+
delete(expectedAnnotations, clusterv1.ClusterTopologyHoldUpgradeSequenceAnnotation)
1472+
delete(expectedAnnotations, clusterv1.ClusterTopologyDeferUpgradeAnnotation)
1473+
g.Expect(actualMd.Annotations).To(Equal(expectedAnnotations))
1474+
g.Expect(actualMd.Spec.Template.ObjectMeta.Annotations).To(Equal(expectedAnnotations))
1475+
1476+
g.Expect(actualMd.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
1477+
clusterv1.ClusterNameLabel: cluster.Name,
1478+
clusterv1.ClusterTopologyOwnedLabel: "",
1479+
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
1480+
})))
1481+
g.Expect(actualMd.Spec.Selector.MatchLabels).To(Equal(map[string]string{
1482+
clusterv1.ClusterNameLabel: cluster.Name,
1483+
clusterv1.ClusterTopologyOwnedLabel: "",
1484+
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
1485+
}))
1486+
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
1487+
clusterv1.ClusterNameLabel: cluster.Name,
1488+
clusterv1.ClusterTopologyOwnedLabel: "",
1489+
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
1490+
})))
14681491

1469-
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue("foo", "baz"))
1470-
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue("fizz", "buzz"))
1471-
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(HaveKey(clusterv1.ClusterTopologyOwnedLabel))
14721492
g.Expect(actualMd.Spec.Template.Spec.InfrastructureRef.Name).ToNot(Equal("linux-worker-inframachinetemplate"))
14731493
g.Expect(actualMd.Spec.Template.Spec.Bootstrap.ConfigRef.Name).ToNot(Equal("linux-worker-bootstraptemplate"))
14741494
})
@@ -1540,15 +1560,28 @@ func TestComputeMachineDeployment(t *testing.T) {
15401560
g.Expect(*actualMd.Spec.Template.Spec.FailureDomain).To(Equal(topologyFailureDomain))
15411561
g.Expect(actualMd.Name).To(Equal("existing-deployment-1"))
15421562

1543-
g.Expect(actualMd.Labels).To(HaveKeyWithValue(clusterv1.ClusterTopologyMachineDeploymentNameLabel, "big-pool-of-machines"))
1544-
g.Expect(actualMd.Labels).To(HaveKey(clusterv1.ClusterTopologyOwnedLabel))
1545-
for k, v := range mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels) {
1546-
g.Expect(actualMd.Labels).To(HaveKeyWithValue(k, v))
1547-
}
1563+
expectedAnnotations := mergeMap(mdTopology.Metadata.Annotations, md1.Template.Metadata.Annotations)
1564+
delete(expectedAnnotations, clusterv1.ClusterTopologyHoldUpgradeSequenceAnnotation)
1565+
delete(expectedAnnotations, clusterv1.ClusterTopologyDeferUpgradeAnnotation)
1566+
g.Expect(actualMd.Annotations).To(Equal(expectedAnnotations))
1567+
g.Expect(actualMd.Spec.Template.ObjectMeta.Annotations).To(Equal(expectedAnnotations))
1568+
1569+
g.Expect(actualMd.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
1570+
clusterv1.ClusterNameLabel: cluster.Name,
1571+
clusterv1.ClusterTopologyOwnedLabel: "",
1572+
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
1573+
})))
1574+
g.Expect(actualMd.Spec.Selector.MatchLabels).To(Equal(map[string]string{
1575+
clusterv1.ClusterNameLabel: cluster.Name,
1576+
clusterv1.ClusterTopologyOwnedLabel: "",
1577+
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
1578+
}))
1579+
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
1580+
clusterv1.ClusterNameLabel: cluster.Name,
1581+
clusterv1.ClusterTopologyOwnedLabel: "",
1582+
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
1583+
})))
15481584

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

0 commit comments

Comments
 (0)