Skip to content

Commit 09bf6ef

Browse files
authored
Merge pull request #7504 from ykakarap/clusterctl-move-managed-fields-fix
🐛 patch managed fields after `clusterctl move` so that it does not own all fields
2 parents 97a685a + 3a717cb commit 09bf6ef

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

cmd/clusterctl/client/cluster/mover.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cluster
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"os"
2223
"path/filepath"
@@ -560,7 +561,13 @@ func setClusterPause(proxy Proxy, clusters []*node, value bool, dryRun bool) err
560561
}
561562

562563
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)))
564571

565572
setClusterPauseBackoff := newWriteBackoff()
566573
for i := range clusters {
@@ -888,6 +895,7 @@ func (o *objectMover) createTargetObject(nodeToCreate *node, toProxy Proxy) erro
888895
return err
889896
}
890897

898+
oldManagedFields := obj.GetManagedFields()
891899
if err := cTo.Create(ctx, obj); err != nil {
892900
if !apierrors.IsAlreadyExists(err) {
893901
return errors.Wrapf(err, "error creating %q %s/%s",
@@ -922,6 +930,10 @@ func (o *objectMover) createTargetObject(nodeToCreate *node, toProxy Proxy) erro
922930
// Stores the newUID assigned to the newly created object.
923931
nodeToCreate.newUID = obj.GetUID()
924932

933+
if err := patchTopologyManagedFields(ctx, oldManagedFields, obj, cTo); err != nil {
934+
return errors.Wrap(err, "error patching the managed fields")
935+
}
936+
925937
return nil
926938
}
927939

@@ -1189,3 +1201,17 @@ func (o *objectMover) checkTargetProviders(toInventory InventoryClient) error {
11891201

11901202
return kerrors.NewAggregate(errList)
11911203
}
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

Comments
 (0)