Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Move mergeMap to util #8377

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions internal/controllers/topology/cluster/desired_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/scope"
"sigs.k8s.io/cluster-api/internal/hooks"
tlog "sigs.k8s.io/cluster-api/internal/log"
"sigs.k8s.io/cluster-api/util"
)

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

controlPlaneLabels := mergeMap(topologyMetadata.Labels, clusterClassMetadata.Labels)
controlPlaneLabels := util.MergeMap(topologyMetadata.Labels, clusterClassMetadata.Labels)
if controlPlaneLabels == nil {
controlPlaneLabels = map[string]string{}
}
controlPlaneLabels[clusterv1.ClusterNameLabel] = cluster.Name
controlPlaneLabels[clusterv1.ClusterTopologyOwnedLabel] = ""

controlPlaneAnnotations := mergeMap(topologyMetadata.Annotations, clusterClassMetadata.Annotations)
controlPlaneAnnotations := util.MergeMap(topologyMetadata.Annotations, clusterClassMetadata.Annotations)

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

controlPlaneMachineTemplateMetadata.Labels = mergeMap(controlPlaneLabels, controlPlaneMachineTemplateMetadata.Labels)
controlPlaneMachineTemplateMetadata.Annotations = mergeMap(controlPlaneAnnotations, controlPlaneMachineTemplateMetadata.Annotations)
controlPlaneMachineTemplateMetadata.Labels = util.MergeMap(controlPlaneLabels, controlPlaneMachineTemplateMetadata.Labels)
controlPlaneMachineTemplateMetadata.Annotations = util.MergeMap(controlPlaneAnnotations, controlPlaneMachineTemplateMetadata.Annotations)

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

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

// mergeMap merges maps.
// NOTE: In case a key exists in multiple maps, the value of the first map is preserved.
func mergeMap(maps ...map[string]string) map[string]string {
m := make(map[string]string)
for i := len(maps) - 1; i >= 0; i-- {
for k, v := range maps[i] {
m[k] = v
}
}

// Nil the result if the map is empty, thus avoiding triggering infinite reconcile
// given that at json level label: {} or annotation: {} is different from no field, which is the
// corresponding value stored in etcd given that those fields are defined as omitempty.
if len(m) == 0 {
return nil
}
return m
}

func ownerReferenceTo(obj client.Object) *metav1.OwnerReference {
return &metav1.OwnerReference{
Kind: obj.GetObjectKind().GroupVersionKind().Kind,
Expand Down
37 changes: 19 additions & 18 deletions internal/controllers/topology/cluster/desired_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"sigs.k8s.io/cluster-api/internal/hooks"
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util"
)

var (
Expand Down Expand Up @@ -328,8 +329,8 @@ func TestComputeControlPlane(t *testing.T) {
template: blueprint.ControlPlane.Template,
currentRef: nil,
obj: obj,
labels: mergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
annotations: mergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
labels: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
annotations: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
})

assertNestedField(g, obj, version, contract.ControlPlane().Version().Path()...)
Expand Down Expand Up @@ -419,8 +420,8 @@ func TestComputeControlPlane(t *testing.T) {
template: blueprint.ControlPlane.Template,
currentRef: nil,
obj: obj,
labels: mergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
annotations: mergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
labels: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
annotations: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
})

assertNestedField(g, obj, version, contract.ControlPlane().Version().Path()...)
Expand Down Expand Up @@ -471,18 +472,18 @@ func TestComputeControlPlane(t *testing.T) {
template: controlPlaneTemplateWithoutMachineTemplate,
currentRef: nil,
obj: obj,
labels: mergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
annotations: mergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
labels: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels),
annotations: util.MergeMap(blueprint.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations),
})
gotMetadata, err := contract.ControlPlane().MachineTemplate().Metadata().Get(obj)
g.Expect(err).ToNot(HaveOccurred())

expectedLabels := mergeMap(s.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels, controlPlaneMachineTemplateLabels)
expectedLabels := util.MergeMap(s.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Labels, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Labels, controlPlaneMachineTemplateLabels)
expectedLabels[clusterv1.ClusterNameLabel] = cluster.Name
expectedLabels[clusterv1.ClusterTopologyOwnedLabel] = ""
g.Expect(gotMetadata).To(Equal(&clusterv1.ObjectMeta{
Labels: expectedLabels,
Annotations: mergeMap(s.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations, controlPlaneMachineTemplateAnnotations),
Annotations: util.MergeMap(s.Current.Cluster.Spec.Topology.ControlPlane.Metadata.Annotations, blueprint.ClusterClass.Spec.ControlPlane.Metadata.Annotations, controlPlaneMachineTemplateAnnotations),
}))

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

expectedAnnotations := mergeMap(mdTopology.Metadata.Annotations, md1.Template.Metadata.Annotations)
expectedAnnotations := util.MergeMap(mdTopology.Metadata.Annotations, md1.Template.Metadata.Annotations)
delete(expectedAnnotations, clusterv1.ClusterTopologyHoldUpgradeSequenceAnnotation)
delete(expectedAnnotations, clusterv1.ClusterTopologyDeferUpgradeAnnotation)
g.Expect(actualMd.Annotations).To(Equal(expectedAnnotations))
g.Expect(actualMd.Spec.Template.ObjectMeta.Annotations).To(Equal(expectedAnnotations))

g.Expect(actualMd.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
g.Expect(actualMd.Labels).To(Equal(util.MergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
Expand All @@ -1501,7 +1502,7 @@ func TestComputeMachineDeployment(t *testing.T) {
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
}))
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(Equal(util.MergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
Expand Down Expand Up @@ -1578,13 +1579,13 @@ func TestComputeMachineDeployment(t *testing.T) {
g.Expect(*actualMd.Spec.Template.Spec.FailureDomain).To(Equal(topologyFailureDomain))
g.Expect(actualMd.Name).To(Equal("existing-deployment-1"))

expectedAnnotations := mergeMap(mdTopology.Metadata.Annotations, md1.Template.Metadata.Annotations)
expectedAnnotations := util.MergeMap(mdTopology.Metadata.Annotations, md1.Template.Metadata.Annotations)
delete(expectedAnnotations, clusterv1.ClusterTopologyHoldUpgradeSequenceAnnotation)
delete(expectedAnnotations, clusterv1.ClusterTopologyDeferUpgradeAnnotation)
g.Expect(actualMd.Annotations).To(Equal(expectedAnnotations))
g.Expect(actualMd.Spec.Template.ObjectMeta.Annotations).To(Equal(expectedAnnotations))

g.Expect(actualMd.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
g.Expect(actualMd.Labels).To(Equal(util.MergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
Expand All @@ -1594,7 +1595,7 @@ func TestComputeMachineDeployment(t *testing.T) {
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
}))
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(Equal(mergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
g.Expect(actualMd.Spec.Template.ObjectMeta.Labels).To(Equal(util.MergeMap(mdTopology.Metadata.Labels, md1.Template.Metadata.Labels, map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.ClusterTopologyOwnedLabel: "",
clusterv1.ClusterTopologyMachineDeploymentNameLabel: "big-pool-of-machines",
Expand Down Expand Up @@ -2267,7 +2268,7 @@ func TestMergeMap(t *testing.T) {
t.Run("Merge maps", func(t *testing.T) {
g := NewWithT(t)

m := mergeMap(
m := util.MergeMap(
map[string]string{
"a": "a",
"b": "b",
Expand All @@ -2283,7 +2284,7 @@ func TestMergeMap(t *testing.T) {
t.Run("Nils empty maps", func(t *testing.T) {
g := NewWithT(t)

m := mergeMap(map[string]string{}, map[string]string{})
m := util.MergeMap(map[string]string{}, map[string]string{})
g.Expect(m).To(BeNil())
})
}
Expand Down
19 changes: 19 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,22 @@ func IsNil(i interface{}) bool {
}
return false
}

// MergeMap merges maps.
// NOTE: In case a key exists in multiple maps, the value of the first map is preserved.
func MergeMap(maps ...map[string]string) map[string]string {
m := make(map[string]string)
for i := len(maps) - 1; i >= 0; i-- {
for k, v := range maps[i] {
m[k] = v
}
}

// Nil the result if the map is empty, thus avoiding triggering infinite reconcile
// given that at json level label: {} or annotation: {} is different from no field, which is the
// corresponding value stored in etcd given that those fields are defined as omitempty.
if len(m) == 0 {
return nil
}
return m
}