@@ -27,7 +27,7 @@ import (
27
27
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
28
28
)
29
29
30
- func Test_cleanupTopologyDryRunAnnotation (t * testing.T ) {
30
+ func Test_cleanupManagedFieldsAndAnnotation (t * testing.T ) {
31
31
rawManagedFieldWithAnnotation := `{"f:metadata":{"f:annotations":{"f:topology.cluster.x-k8s.io/dry-run":{}}}}`
32
32
rawManagedFieldWithAnnotationSpecLabels := `{"f:metadata":{"f:annotations":{"f:topology.cluster.x-k8s.io/dry-run":{}},"f:labels":{}},"f:spec":{"f:foo":{}}}`
33
33
rawManagedFieldWithSpecLabels := `{"f:metadata":{"f:labels":{}},"f:spec":{"f:foo":{}}}`
@@ -53,33 +53,30 @@ func Test_cleanupTopologyDryRunAnnotation(t *testing.T) {
53
53
Build (),
54
54
},
55
55
{
56
- name : "managedFields: manager does not match " ,
56
+ name : "managedFields: should drop managed fields of other manager " ,
57
57
obj : newObjectBuilder ().
58
58
WithManagedFieldsEntry ("other" , "" , metav1 .ManagedFieldsOperationApply , []byte (`{}` ), nil ).
59
59
Build (),
60
60
wantErr : false ,
61
61
want : newObjectBuilder ().
62
- WithManagedFieldsEntry ("other" , "" , metav1 .ManagedFieldsOperationApply , []byte (`{}` ), nil ).
63
62
Build (),
64
63
},
65
64
{
66
- name : "managedFields: subresource does not match " ,
65
+ name : "managedFields: should drop managed fields of a subresource " ,
67
66
obj : newObjectBuilder ().
68
67
WithManagedFieldsEntry (TopologyManagerName , "status" , metav1 .ManagedFieldsOperationApply , []byte (`{}` ), nil ).
69
68
Build (),
70
69
wantErr : false ,
71
70
want : newObjectBuilder ().
72
- WithManagedFieldsEntry (TopologyManagerName , "status" , metav1 .ManagedFieldsOperationApply , []byte (`{}` ), nil ).
73
71
Build (),
74
72
},
75
73
{
76
- name : "managedFields: operation does not match " ,
74
+ name : "managedFields: should drop managed fields of another operation " ,
77
75
obj : newObjectBuilder ().
78
76
WithManagedFieldsEntry (TopologyManagerName , "" , metav1 .ManagedFieldsOperationUpdate , []byte (`{}` ), nil ).
79
77
Build (),
80
78
wantErr : false ,
81
79
want : newObjectBuilder ().
82
- WithManagedFieldsEntry (TopologyManagerName , "" , metav1 .ManagedFieldsOperationUpdate , []byte (`{}` ), nil ).
83
80
Build (),
84
81
},
85
82
{
@@ -93,7 +90,7 @@ func Test_cleanupTopologyDryRunAnnotation(t *testing.T) {
93
90
Build (),
94
91
},
95
92
{
96
- name : "managedFields: cleanup the managed field entry but preserve other ownership" ,
93
+ name : "managedFields: cleanup the managed field entry and preserve other ownership" ,
97
94
obj : newObjectBuilder ().
98
95
WithManagedFieldsEntry (TopologyManagerName , "" , metav1 .ManagedFieldsOperationApply , []byte (rawManagedFieldWithAnnotationSpecLabels ), nil ).
99
96
Build (),
@@ -117,8 +114,8 @@ func Test_cleanupTopologyDryRunAnnotation(t *testing.T) {
117
114
t .Run (tt .name , func (t * testing.T ) {
118
115
g := NewWithT (t )
119
116
120
- if err := cleanupTopologyDryRunAnnotation (tt .obj ); (err != nil ) != tt .wantErr {
121
- t .Errorf ("cleanupTopologyDryRunAnnotation () error = %v, wantErr %v" , err , tt .wantErr )
117
+ if err := cleanupManagedFieldsAndAnnotation (tt .obj ); (err != nil ) != tt .wantErr {
118
+ t .Errorf ("cleanupManagedFieldsAndAnnotation () error = %v, wantErr %v" , err , tt .wantErr )
122
119
}
123
120
if tt .want != nil {
124
121
g .Expect (tt .obj ).To (BeEquivalentTo (tt .want ))
@@ -132,14 +129,26 @@ type objectBuilder struct {
132
129
}
133
130
134
131
func newObjectBuilder () objectBuilder {
135
- return objectBuilder {& unstructured.Unstructured {Object : map [string ]interface {}{}}}
132
+ u := & unstructured.Unstructured {Object : map [string ]interface {}{}}
133
+ u .SetName ("test" )
134
+ u .SetNamespace ("default" )
135
+
136
+ return objectBuilder {
137
+ u : u ,
138
+ }
136
139
}
137
140
138
141
func (b objectBuilder ) DeepCopy () objectBuilder {
139
142
return objectBuilder {b .u .DeepCopy ()}
140
143
}
141
144
142
145
func (b objectBuilder ) Build () * unstructured.Unstructured {
146
+ // Setting an empty managed field array if no managed field is set so there is
147
+ // no difference between an object which never had a managed field and one from
148
+ // which all managed field entries were removed.
149
+ if b .u .GetManagedFields () == nil {
150
+ b .u .SetManagedFields ([]metav1.ManagedFieldsEntry {})
151
+ }
143
152
return b .u .DeepCopy ()
144
153
}
145
154
0 commit comments