Skip to content

Commit

Permalink
Merge pull request #7512 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…7504-to-release-1.2

[release-1.2] 🐛 patch managed fields after `clusterctl move` so that it does not own all fields
  • Loading branch information
k8s-ci-robot committed Nov 7, 2022
2 parents d49ddb7 + 2c457bb commit cd87c3d
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion cmd/clusterctl/client/cluster/mover.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cluster

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -535,7 +536,13 @@ func setClusterPause(proxy Proxy, clusters []*node, value bool, dryRun bool) err
}

log := logf.Log
patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"spec\":{\"paused\":%t}}", value)))
patchValue := "true"
if !value {
// If the `value` is false lets drop the field.
// This makes sure that clusterctl does now own the field and would avoid any ownership conflicts.
patchValue = "null"
}
patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"spec\":{\"paused\":%s}}", patchValue)))

setClusterPauseBackoff := newWriteBackoff()
for i := range clusters {
Expand Down Expand Up @@ -863,6 +870,7 @@ func (o *objectMover) createTargetObject(nodeToCreate *node, toProxy Proxy) erro
return err
}

oldManagedFields := obj.GetManagedFields()
if err := cTo.Create(ctx, obj); err != nil {
if !apierrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "error creating %q %s/%s",
Expand Down Expand Up @@ -897,6 +905,10 @@ func (o *objectMover) createTargetObject(nodeToCreate *node, toProxy Proxy) erro
// Stores the newUID assigned to the newly created object.
nodeToCreate.newUID = obj.GetUID()

if err := patchTopologyManagedFields(ctx, oldManagedFields, obj, cTo); err != nil {
return errors.Wrap(err, "error patching the managed fields")
}

return nil
}

Expand Down Expand Up @@ -1164,3 +1176,17 @@ func (o *objectMover) checkTargetProviders(toInventory InventoryClient) error {

return kerrors.NewAggregate(errList)
}

// patchTopologyManagedFields patches the managed fields of obj.
// Without patching the managed fields, clusterctl would be the owner of the fields
// which would lead to co-ownership and preventing other controllers using SSA from deleting fields.
func patchTopologyManagedFields(ctx context.Context, oldManagedFields []metav1.ManagedFieldsEntry, obj *unstructured.Unstructured, cTo client.Client) error {
base := obj.DeepCopy()
obj.SetManagedFields(oldManagedFields)

if err := cTo.Patch(ctx, obj, client.MergeFrom(base)); err != nil {
return errors.Wrapf(err, "error patching managed fields %q %s/%s",
obj.GroupVersionKind(), obj.GetNamespace(), obj.GetName())
}
return nil
}

0 comments on commit cd87c3d

Please sign in to comment.