Skip to content

Commit 1b2a319

Browse files
authored
Merge pull request #5580 from fabriziopandini/topology-should-not-generate-empty-metadata
🐛 topology should not generate empty patch for metadata
2 parents b1cff70 + 9430c03 commit 1b2a319

File tree

4 files changed

+126
-9
lines changed

4 files changed

+126
-9
lines changed

controllers/topology/desired_state.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ func computeControlPlane(_ context.Context, s *scope.Scope, infrastructureMachin
162162
clusterClassMetadata := s.Blueprint.ClusterClass.Spec.ControlPlane.Metadata
163163

164164
machineLabels := mergeMap(topologyMetadata.Labels, clusterClassMetadata.Labels)
165+
if machineLabels == nil {
166+
machineLabels = map[string]string{}
167+
}
165168
machineLabels[clusterv1.ClusterLabelName] = cluster.Name
166169
machineLabels[clusterv1.ClusterTopologyOwnedLabel] = ""
167170
if err := contract.ControlPlane().MachineTemplate().Metadata().Set(controlPlane,
@@ -401,7 +404,7 @@ func computeMachineDeployment(_ context.Context, s *scope.Scope, desiredControlP
401404
labels[clusterv1.ClusterTopologyMachineDeploymentLabelName] = machineDeploymentTopology.Name
402405
desiredMachineDeploymentObj.SetLabels(labels)
403406

404-
// Set the select with the subset of labels identifying controlled machines.
407+
// Set the selector with the subset of labels identifying controlled machines.
405408
// NOTE: this prevents the web hook to add cluster.x-k8s.io/deployment-name label, that is
406409
// redundant for managed MachineDeployments given that we already have topology.cluster.x-k8s.io/deployment-name.
407410
desiredMachineDeploymentObj.Spec.Selector.MatchLabels = map[string]string{}
@@ -412,6 +415,9 @@ func computeMachineDeployment(_ context.Context, s *scope.Scope, desiredControlP
412415
// Also set the labels in .spec.template.labels so that they are propagated to
413416
// MachineSet.labels and MachineSet.spec.template.labels and thus to Machine.labels.
414417
// Note: the labels in MachineSet are used to properly cleanup templates when the MachineSet is deleted.
418+
if desiredMachineDeploymentObj.Spec.Template.Labels == nil {
419+
desiredMachineDeploymentObj.Spec.Template.Labels = map[string]string{}
420+
}
415421
desiredMachineDeploymentObj.Spec.Template.Labels[clusterv1.ClusterLabelName] = s.Current.Cluster.Name
416422
desiredMachineDeploymentObj.Spec.Template.Labels[clusterv1.ClusterTopologyOwnedLabel] = ""
417423
desiredMachineDeploymentObj.Spec.Template.Labels[clusterv1.ClusterTopologyMachineDeploymentLabelName] = machineDeploymentTopology.Name
@@ -618,5 +624,12 @@ func mergeMap(a, b map[string]string) map[string]string {
618624
for k, v := range a {
619625
m[k] = v
620626
}
627+
628+
// Nil the result if the map is empty, thus avoiding triggering infinite reconcile
629+
// given that at json level label: {} or annotation: {} is different from no field, which is the
630+
// corresponding value stored in etcd given that those fields are defined as omitempty.
631+
if len(m) == 0 {
632+
return nil
633+
}
621634
return m
622635
}

controllers/topology/desired_state_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,3 +1325,28 @@ func duplicateMachineDeploymentsState(s scope.MachineDeploymentsStateMap) scope.
13251325
}
13261326
return n
13271327
}
1328+
1329+
func TestMergeMap(t *testing.T) {
1330+
t.Run("Merge maps", func(t *testing.T) {
1331+
g := NewWithT(t)
1332+
1333+
m := mergeMap(
1334+
map[string]string{
1335+
"a": "a",
1336+
"b": "b",
1337+
}, map[string]string{
1338+
"a": "ax",
1339+
"c": "c",
1340+
},
1341+
)
1342+
g.Expect(m).To(HaveKeyWithValue("a", "a"))
1343+
g.Expect(m).To(HaveKeyWithValue("b", "b"))
1344+
g.Expect(m).To(HaveKeyWithValue("c", "c"))
1345+
})
1346+
t.Run("Nils empty maps", func(t *testing.T) {
1347+
g := NewWithT(t)
1348+
1349+
m := mergeMap(map[string]string{}, map[string]string{})
1350+
g.Expect(m).To(BeNil())
1351+
})
1352+
}

controllers/topology/internal/contract/metadata.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package contract
1818

1919
import (
20-
"strings"
21-
2220
"github.com/pkg/errors"
2321
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2422
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -42,7 +40,7 @@ func (m *Metadata) Get(obj *unstructured.Unstructured) (*clusterv1.ObjectMeta, e
4240
return nil, errors.Wrap(err, "failed to retrieve control plane metadata.labels")
4341
}
4442
if !ok {
45-
return nil, errors.Errorf("%s not found", "."+strings.Join(labelsPath, "."))
43+
labelsValue = map[string]string{}
4644
}
4745

4846
annotationsPath := append(m.path, "annotations")
@@ -51,7 +49,7 @@ func (m *Metadata) Get(obj *unstructured.Unstructured) (*clusterv1.ObjectMeta, e
5149
return nil, errors.Wrap(err, "failed to retrieve control plane metadata.annotations")
5250
}
5351
if !ok {
54-
return nil, errors.Errorf("%s not found", "."+strings.Join(annotationsPath, "."))
52+
annotationsValue = map[string]string{}
5553
}
5654

5755
return &clusterv1.ObjectMeta{
@@ -61,15 +59,24 @@ func (m *Metadata) Get(obj *unstructured.Unstructured) (*clusterv1.ObjectMeta, e
6159
}
6260

6361
// Set sets the metadata value.
62+
// Note: We are blanking out empty label annotations, thus avoiding triggering infinite reconcile
63+
// given that at json level label: {} or annotation: {} is different from no field, which is the
64+
// corresponding value stored in etcd given that those fields are defined as omitempty.
6465
func (m *Metadata) Set(obj *unstructured.Unstructured, metadata *clusterv1.ObjectMeta) error {
6566
labelsPath := append(m.path, "labels")
66-
if err := unstructured.SetNestedStringMap(obj.UnstructuredContent(), metadata.Labels, labelsPath...); err != nil {
67-
return errors.Wrap(err, "failed to set control plane metadata.labels")
67+
unstructured.RemoveNestedField(obj.UnstructuredContent(), labelsPath...)
68+
if len(metadata.Labels) > 0 {
69+
if err := unstructured.SetNestedStringMap(obj.UnstructuredContent(), metadata.Labels, labelsPath...); err != nil {
70+
return errors.Wrap(err, "failed to set control plane metadata.labels")
71+
}
6872
}
6973

7074
annotationsPath := append(m.path, "annotations")
71-
if err := unstructured.SetNestedStringMap(obj.UnstructuredContent(), metadata.Annotations, annotationsPath...); err != nil {
72-
return errors.Wrap(err, "failed to set control plane metadata.annotations")
75+
unstructured.RemoveNestedField(obj.UnstructuredContent(), annotationsPath...)
76+
if len(metadata.Annotations) > 0 {
77+
if err := unstructured.SetNestedStringMap(obj.UnstructuredContent(), metadata.Annotations, annotationsPath...); err != nil {
78+
return errors.Wrap(err, "failed to set control plane metadata.annotations")
79+
}
7380
}
7481
return nil
7582
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package contract
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/gomega"
23+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
25+
)
26+
27+
func TestMetadata(t *testing.T) {
28+
obj := &unstructured.Unstructured{Object: map[string]interface{}{}}
29+
30+
t.Run("Manages metadata", func(t *testing.T) {
31+
g := NewWithT(t)
32+
33+
metadata := &clusterv1.ObjectMeta{
34+
Labels: map[string]string{
35+
"label1": "labelValue1",
36+
},
37+
Annotations: map[string]string{
38+
"annotation1": "annotationValue1",
39+
},
40+
}
41+
42+
m := Metadata{path: Path{"foo"}}
43+
g.Expect(m.Path()).To(Equal(Path{"foo"}))
44+
45+
err := m.Set(obj, metadata)
46+
g.Expect(err).ToNot(HaveOccurred())
47+
48+
got, err := m.Get(obj)
49+
g.Expect(err).ToNot(HaveOccurred())
50+
g.Expect(got).ToNot(BeNil())
51+
g.Expect(got).To(Equal(metadata))
52+
})
53+
t.Run("Manages empty metadata", func(t *testing.T) {
54+
g := NewWithT(t)
55+
56+
metadata := &clusterv1.ObjectMeta{
57+
Labels: map[string]string{},
58+
Annotations: map[string]string{},
59+
}
60+
61+
m := Metadata{path: Path{"foo"}}
62+
g.Expect(m.Path()).To(Equal(Path{"foo"}))
63+
64+
err := m.Set(obj, metadata)
65+
g.Expect(err).ToNot(HaveOccurred())
66+
67+
got, err := m.Get(obj)
68+
g.Expect(err).ToNot(HaveOccurred())
69+
g.Expect(got).ToNot(BeNil())
70+
g.Expect(got).To(Equal(metadata))
71+
})
72+
}

0 commit comments

Comments
 (0)