@@ -27,7 +27,7 @@ import (
2727 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2828)
2929
30- func Test_cleanupTopologyDryRunAnnotation (t * testing.T ) {
30+ func Test_cleanupManagedFieldsAndAnnotation (t * testing.T ) {
3131 rawManagedFieldWithAnnotation := `{"f:metadata":{"f:annotations":{"f:topology.cluster.x-k8s.io/dry-run":{}}}}`
3232 rawManagedFieldWithAnnotationSpecLabels := `{"f:metadata":{"f:annotations":{"f:topology.cluster.x-k8s.io/dry-run":{}},"f:labels":{}},"f:spec":{"f:foo":{}}}`
3333 rawManagedFieldWithSpecLabels := `{"f:metadata":{"f:labels":{}},"f:spec":{"f:foo":{}}}`
@@ -53,33 +53,30 @@ func Test_cleanupTopologyDryRunAnnotation(t *testing.T) {
5353 Build (),
5454 },
5555 {
56- name : "managedFields: manager does not match " ,
56+ name : "managedFields: should drop managed fields of other manager " ,
5757 obj : newObjectBuilder ().
5858 WithManagedFieldsEntry ("other" , "" , metav1 .ManagedFieldsOperationApply , []byte (`{}` ), nil ).
5959 Build (),
6060 wantErr : false ,
6161 want : newObjectBuilder ().
62- WithManagedFieldsEntry ("other" , "" , metav1 .ManagedFieldsOperationApply , []byte (`{}` ), nil ).
6362 Build (),
6463 },
6564 {
66- name : "managedFields: subresource does not match " ,
65+ name : "managedFields: should drop managed fields of a subresource " ,
6766 obj : newObjectBuilder ().
6867 WithManagedFieldsEntry (TopologyManagerName , "status" , metav1 .ManagedFieldsOperationApply , []byte (`{}` ), nil ).
6968 Build (),
7069 wantErr : false ,
7170 want : newObjectBuilder ().
72- WithManagedFieldsEntry (TopologyManagerName , "status" , metav1 .ManagedFieldsOperationApply , []byte (`{}` ), nil ).
7371 Build (),
7472 },
7573 {
76- name : "managedFields: operation does not match " ,
74+ name : "managedFields: should drop managed fields of another operation " ,
7775 obj : newObjectBuilder ().
7876 WithManagedFieldsEntry (TopologyManagerName , "" , metav1 .ManagedFieldsOperationUpdate , []byte (`{}` ), nil ).
7977 Build (),
8078 wantErr : false ,
8179 want : newObjectBuilder ().
82- WithManagedFieldsEntry (TopologyManagerName , "" , metav1 .ManagedFieldsOperationUpdate , []byte (`{}` ), nil ).
8380 Build (),
8481 },
8582 {
@@ -93,7 +90,7 @@ func Test_cleanupTopologyDryRunAnnotation(t *testing.T) {
9390 Build (),
9491 },
9592 {
96- name : "managedFields: cleanup the managed field entry but preserve other ownership" ,
93+ name : "managedFields: cleanup the managed field entry and preserve other ownership" ,
9794 obj : newObjectBuilder ().
9895 WithManagedFieldsEntry (TopologyManagerName , "" , metav1 .ManagedFieldsOperationApply , []byte (rawManagedFieldWithAnnotationSpecLabels ), nil ).
9996 Build (),
@@ -117,8 +114,8 @@ func Test_cleanupTopologyDryRunAnnotation(t *testing.T) {
117114 t .Run (tt .name , func (t * testing.T ) {
118115 g := NewWithT (t )
119116
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 )
122119 }
123120 if tt .want != nil {
124121 g .Expect (tt .obj ).To (BeEquivalentTo (tt .want ))
@@ -132,14 +129,26 @@ type objectBuilder struct {
132129}
133130
134131func 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+ }
136139}
137140
138141func (b objectBuilder ) DeepCopy () objectBuilder {
139142 return objectBuilder {b .u .DeepCopy ()}
140143}
141144
142145func (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+ }
143152 return b .u .DeepCopy ()
144153}
145154
0 commit comments