@@ -17,6 +17,7 @@ limitations under the License.
17
17
package cluster
18
18
19
19
import (
20
+ "context"
20
21
"fmt"
21
22
"os"
22
23
"path/filepath"
@@ -560,7 +561,13 @@ func setClusterPause(proxy Proxy, clusters []*node, value bool, dryRun bool) err
560
561
}
561
562
562
563
log := logf .Log
563
- patch := client .RawPatch (types .MergePatchType , []byte (fmt .Sprintf ("{\" spec\" :{\" paused\" :%t}}" , value )))
564
+ patchValue := "true"
565
+ if ! value {
566
+ // If the `value` is false lets drop the field.
567
+ // This makes sure that clusterctl does now own the field and would avoid any ownership conflicts.
568
+ patchValue = "null"
569
+ }
570
+ patch := client .RawPatch (types .MergePatchType , []byte (fmt .Sprintf ("{\" spec\" :{\" paused\" :%s}}" , patchValue )))
564
571
565
572
setClusterPauseBackoff := newWriteBackoff ()
566
573
for i := range clusters {
@@ -888,6 +895,7 @@ func (o *objectMover) createTargetObject(nodeToCreate *node, toProxy Proxy) erro
888
895
return err
889
896
}
890
897
898
+ oldManagedFields := obj .GetManagedFields ()
891
899
if err := cTo .Create (ctx , obj ); err != nil {
892
900
if ! apierrors .IsAlreadyExists (err ) {
893
901
return errors .Wrapf (err , "error creating %q %s/%s" ,
@@ -922,6 +930,10 @@ func (o *objectMover) createTargetObject(nodeToCreate *node, toProxy Proxy) erro
922
930
// Stores the newUID assigned to the newly created object.
923
931
nodeToCreate .newUID = obj .GetUID ()
924
932
933
+ if err := patchTopologyManagedFields (ctx , oldManagedFields , obj , cTo ); err != nil {
934
+ return errors .Wrap (err , "error patching the managed fields" )
935
+ }
936
+
925
937
return nil
926
938
}
927
939
@@ -1189,3 +1201,17 @@ func (o *objectMover) checkTargetProviders(toInventory InventoryClient) error {
1189
1201
1190
1202
return kerrors .NewAggregate (errList )
1191
1203
}
1204
+
1205
+ // patchTopologyManagedFields patches the managed fields of obj.
1206
+ // Without patching the managed fields, clusterctl would be the owner of the fields
1207
+ // which would lead to co-ownership and preventing other controllers using SSA from deleting fields.
1208
+ func patchTopologyManagedFields (ctx context.Context , oldManagedFields []metav1.ManagedFieldsEntry , obj * unstructured.Unstructured , cTo client.Client ) error {
1209
+ base := obj .DeepCopy ()
1210
+ obj .SetManagedFields (oldManagedFields )
1211
+
1212
+ if err := cTo .Patch (ctx , obj , client .MergeFrom (base )); err != nil {
1213
+ return errors .Wrapf (err , "error patching managed fields %q %s/%s" ,
1214
+ obj .GroupVersionKind (), obj .GetNamespace (), obj .GetName ())
1215
+ }
1216
+ return nil
1217
+ }
0 commit comments