Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 patch managed fields after clusterctl move so that it does not own all fields #7504

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion cmd/clusterctl/client/cluster/mover.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cluster

ykakarap marked this conversation as resolved.
Show resolved Hide resolved
import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -560,7 +561,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 @@ -888,6 +895,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 @@ -922,6 +930,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 @@ -1189,3 +1201,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
}