Skip to content

Commit ac22acd

Browse files
committed
Move mergeMap to util
1 parent 8ecf669 commit ac22acd

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

internal/controllers/topology/cluster/desired_state.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/scope"
3939
"sigs.k8s.io/cluster-api/internal/hooks"
4040
tlog "sigs.k8s.io/cluster-api/internal/log"
41+
"sigs.k8s.io/cluster-api/util"
4142
)
4243

4344
// computeDesiredState computes the desired state of the cluster topology.
@@ -185,14 +186,14 @@ func (r *Reconciler) computeControlPlane(ctx context.Context, s *scope.Scope, in
185186
topologyMetadata := s.Blueprint.Topology.ControlPlane.Metadata
186187
clusterClassMetadata := s.Blueprint.ClusterClass.Spec.ControlPlane.Metadata
187188

188-
controlPlaneLabels := mergeMap(topologyMetadata.Labels, clusterClassMetadata.Labels)
189+
controlPlaneLabels := util.MergeMap(topologyMetadata.Labels, clusterClassMetadata.Labels)
189190
if controlPlaneLabels == nil {
190191
controlPlaneLabels = map[string]string{}
191192
}
192193
controlPlaneLabels[clusterv1.ClusterNameLabel] = cluster.Name
193194
controlPlaneLabels[clusterv1.ClusterTopologyOwnedLabel] = ""
194195

195-
controlPlaneAnnotations := mergeMap(topologyMetadata.Annotations, clusterClassMetadata.Annotations)
196+
controlPlaneAnnotations := util.MergeMap(topologyMetadata.Annotations, clusterClassMetadata.Annotations)
196197

197198
controlPlane, err := templateToObject(templateToInput{
198199
template: template,
@@ -248,8 +249,8 @@ func (r *Reconciler) computeControlPlane(ctx context.Context, s *scope.Scope, in
248249
return nil, errors.Wrap(err, "failed to get spec.machineTemplate.metadata from the ControlPlane object")
249250
}
250251

251-
controlPlaneMachineTemplateMetadata.Labels = mergeMap(controlPlaneLabels, controlPlaneMachineTemplateMetadata.Labels)
252-
controlPlaneMachineTemplateMetadata.Annotations = mergeMap(controlPlaneAnnotations, controlPlaneMachineTemplateMetadata.Annotations)
252+
controlPlaneMachineTemplateMetadata.Labels = util.MergeMap(controlPlaneLabels, controlPlaneMachineTemplateMetadata.Labels)
253+
controlPlaneMachineTemplateMetadata.Annotations = util.MergeMap(controlPlaneAnnotations, controlPlaneMachineTemplateMetadata.Annotations)
253254

254255
if err := contract.ControlPlane().MachineTemplate().Metadata().Set(controlPlane,
255256
&clusterv1.ObjectMeta{
@@ -695,7 +696,7 @@ func computeMachineDeployment(_ context.Context, s *scope.Scope, desiredControlP
695696
}
696697

697698
// Apply annotations
698-
machineDeploymentAnnotations := mergeMap(machineDeploymentTopology.Metadata.Annotations, machineDeploymentBlueprint.Metadata.Annotations)
699+
machineDeploymentAnnotations := util.MergeMap(machineDeploymentTopology.Metadata.Annotations, machineDeploymentBlueprint.Metadata.Annotations)
699700
// Ensure the annotations used to control the upgrade sequence are never propagated.
700701
delete(machineDeploymentAnnotations, clusterv1.ClusterTopologyHoldUpgradeSequenceAnnotation)
701702
delete(machineDeploymentAnnotations, clusterv1.ClusterTopologyDeferUpgradeAnnotation)
@@ -705,7 +706,7 @@ func computeMachineDeployment(_ context.Context, s *scope.Scope, desiredControlP
705706
// Apply Labels
706707
// NOTE: On top of all the labels applied to managed objects we are applying the ClusterTopologyMachineDeploymentLabel
707708
// keeping track of the MachineDeployment name from the Topology; this will be used to identify the object in next reconcile loops.
708-
machineDeploymentLabels := mergeMap(machineDeploymentTopology.Metadata.Labels, machineDeploymentBlueprint.Metadata.Labels)
709+
machineDeploymentLabels := util.MergeMap(machineDeploymentTopology.Metadata.Labels, machineDeploymentBlueprint.Metadata.Labels)
709710
if machineDeploymentLabels == nil {
710711
machineDeploymentLabels = map[string]string{}
711712
}
@@ -1002,25 +1003,6 @@ func templateToTemplate(in templateToInput) *unstructured.Unstructured {
10021003
return template
10031004
}
10041005

1005-
// mergeMap merges maps.
1006-
// NOTE: In case a key exists in multiple maps, the value of the first map is preserved.
1007-
func mergeMap(maps ...map[string]string) map[string]string {
1008-
m := make(map[string]string)
1009-
for i := len(maps) - 1; i >= 0; i-- {
1010-
for k, v := range maps[i] {
1011-
m[k] = v
1012-
}
1013-
}
1014-
1015-
// Nil the result if the map is empty, thus avoiding triggering infinite reconcile
1016-
// given that at json level label: {} or annotation: {} is different from no field, which is the
1017-
// corresponding value stored in etcd given that those fields are defined as omitempty.
1018-
if len(m) == 0 {
1019-
return nil
1020-
}
1021-
return m
1022-
}
1023-
10241006
func ownerReferenceTo(obj client.Object) *metav1.OwnerReference {
10251007
return &metav1.OwnerReference{
10261008
Kind: obj.GetObjectKind().GroupVersionKind().Kind,

internal/controllers/topology/cluster/desired_state_test.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"sigs.k8s.io/cluster-api/internal/hooks"
4242
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
4343
"sigs.k8s.io/cluster-api/internal/test/builder"
44+
"sigs.k8s.io/cluster-api/util"
4445
)
4546

4647
var (
@@ -328,8 +329,8 @@ func TestComputeControlPlane(t *testing.T) {
328329
template: blueprint.ControlPlane.Template,
329330
currentRef: nil,
330331
obj: obj,
331-
labels: mergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
332-
annotations: mergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
332+
labels: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
333+
annotations: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
333334
})
334335

335336
assertNestedField(g, obj, version, contract.ControlPlane().Version().Path()...)
@@ -419,8 +420,8 @@ func TestComputeControlPlane(t *testing.T) {
419420
template: blueprint.ControlPlane.Template,
420421
currentRef: nil,
421422
obj: obj,
422-
labels: mergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
423-
annotations: mergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
423+
labels: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
424+
annotations: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
424425
})
425426

426427
assertNestedField(g, obj, version, contract.ControlPlane().Version().Path()...)
@@ -471,18 +472,18 @@ func TestComputeControlPlane(t *testing.T) {
471472
template: controlPlaneTemplateWithoutMachineTemplate,
472473
currentRef: nil,
473474
obj: obj,
474-
labels: mergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
475-
annotations: mergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
475+
labels: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
476+
annotations: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
476477
})
477478
gotMetadata, err := contract.ControlPlane().MachineTemplate().Metadata().Get(obj)
478479
g.Expect(err).ToNot(HaveOccurred())
479480

480-
expectedLabels := mergeMap(s.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels, controlPlaneMachineTemplateLabels)
481+
expectedLabels := util.MergeMap(s.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels, controlPlaneMachineTemplateLabels)
481482
expectedLabels[clusterv1.ClusterNameLabel] = cluster.Name
482483
expectedLabels[clusterv1.ClusterTopologyOwnedLabel] = ""
483484
g.Expect(gotMetadata).To(Equal(&clusterv1.ObjectMeta{
484485
Labels: expectedLabels,
485-
Annotations: mergeMap(s.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations, controlPlaneMachineTemplateAnnotations),
486+
Annotations: util.MergeMap(s.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations, controlPlaneMachineTemplateAnnotations),
486487
}))
487488

488489
assertNestedField(g, obj, version, contract.ControlPlane().Version().Path()...)
@@ -525,8 +526,8 @@ func TestComputeControlPlane(t *testing.T) {
525526
template: blueprint.ControlPlane.Template,
526527
currentRef: scope.Current.Cluster.Spec.ControlPlaneRef,
527528
obj: obj,
528-
labels: mergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
529-
annotations: mergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
529+
labels: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
530+
annotations: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
530531
})
531532
})
532533
t.Run("Should choose the correct version for control plane", func(t *testing.T) {
@@ -1485,13 +1486,13 @@ func TestComputeMachineDeployment(t *testing.T) {
14851486
g.Expect(actualMd.Name).To(ContainSubstring("cluster1"))
14861487
g.Expect(actualMd.Name).To(ContainSubstring("big-pool-of-machines"))
14871488

1488-
expectedAnnotations := mergeMap(mdTopology.Metadata.Annotations, md1.Template.Metadata.Annotations)
1489+
expectedAnnotations := util.MergeMap(mdTopology.Metadata.Annotations, md1.Template.Metadata.Annotations)
14891490
delete(expectedAnnotations, clusterv1.ClusterTopologyHoldUpgradeSequenceAnnotation)
14901491
delete(expectedAnnotations, clusterv1.ClusterTopologyDeferUpgradeAnnotation)
14911492
g.Expect(actualMd.Annotations).To(Equal(expectedAnnotations))
14921493
g.Expect(actualMd.Spec.Template.ObjectMeta.Annotations).To(Equal(expectedAnnotations))
14931494

1494-
g.Expect(actualMd.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
1495+
g.Expect(actualMd.Labels).To(Equal(util.MergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
14951496
clusterv1.ClusterNameLabel: cluster.Name,
14961497
clusterv1.ClusterTopologyOwnedLabel: "",
14971498
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
@@ -1501,7 +1502,7 @@ func TestComputeMachineDeployment(t *testing.T) {
15011502
clusterv1.ClusterTopologyOwnedLabel: "",
15021503
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
15031504
}))
1504-
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
1505+
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(Equal(util.MergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
15051506
clusterv1.ClusterNameLabel: cluster.Name,
15061507
clusterv1.ClusterTopologyOwnedLabel: "",
15071508
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
@@ -1578,13 +1579,13 @@ func TestComputeMachineDeployment(t *testing.T) {
15781579
g.Expect(*actualMd.Spec.Template.Spec.FailureDomain).To(Equal(topologyFailureDomain))
15791580
g.Expect(actualMd.Name).To(Equal("existing-deployment-1"))
15801581

1581-
expectedAnnotations := mergeMap(mdTopology.Metadata.Annotations, md1.Template.Metadata.Annotations)
1582+
expectedAnnotations := util.MergeMap(mdTopology.Metadata.Annotations, md1.Template.Metadata.Annotations)
15821583
delete(expectedAnnotations, clusterv1.ClusterTopologyHoldUpgradeSequenceAnnotation)
15831584
delete(expectedAnnotations, clusterv1.ClusterTopologyDeferUpgradeAnnotation)
15841585
g.Expect(actualMd.Annotations).To(Equal(expectedAnnotations))
15851586
g.Expect(actualMd.Spec.Template.ObjectMeta.Annotations).To(Equal(expectedAnnotations))
15861587

1587-
g.Expect(actualMd.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
1588+
g.Expect(actualMd.Labels).To(Equal(util.MergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
15881589
clusterv1.ClusterNameLabel: cluster.Name,
15891590
clusterv1.ClusterTopologyOwnedLabel: "",
15901591
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
@@ -1594,7 +1595,7 @@ func TestComputeMachineDeployment(t *testing.T) {
15941595
clusterv1.ClusterTopologyOwnedLabel: "",
15951596
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
15961597
}))
1597-
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
1598+
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(Equal(util.MergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
15981599
clusterv1.ClusterNameLabel: cluster.Name,
15991600
clusterv1.ClusterTopologyOwnedLabel: "",
16001601
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
@@ -2267,7 +2268,7 @@ func TestMergeMap(t *testing.T) {
22672268
t.Run("Merge maps", func(t *testing.T) {
22682269
g := NewWithT(t)
22692270

2270-
m := mergeMap(
2271+
m := util.MergeMap(
22712272
map[string]string{
22722273
"a": "a",
22732274
"b": "b",
@@ -2283,7 +2284,7 @@ func TestMergeMap(t *testing.T) {
22832284
t.Run("Nils empty maps", func(t *testing.T) {
22842285
g := NewWithT(t)
22852286

2286-
m := mergeMap(map[string]string{}, map[string]string{})
2287+
m := util.MergeMap(map[string]string{}, map[string]string{})
22872288
g.Expect(m).To(BeNil())
22882289
})
22892290
}

util/util.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,22 @@ func IsNil(i interface{}) bool {
592592
}
593593
return false
594594
}
595+
596+
// MergeMap merges maps.
597+
// NOTE: In case a key exists in multiple maps, the value of the first map is preserved.
598+
func MergeMap(maps ...map[string]string) map[string]string {
599+
m := make(map[string]string)
600+
for i := len(maps) - 1; i >= 0; i-- {
601+
for k, v := range maps[i] {
602+
m[k] = v
603+
}
604+
}
605+
606+
// Nil the result if the map is empty, thus avoiding triggering infinite reconcile
607+
// given that at json level label: {} or annotation: {} is different from no field, which is the
608+
// corresponding value stored in etcd given that those fields are defined as omitempty.
609+
if len(m) == 0 {
610+
return nil
611+
}
612+
return m
613+
}

0 commit comments

Comments
 (0)