Skip to content

Commit

Permalink
Add move annotation on objects for cluster move operation
Browse files Browse the repository at this point in the history
Signed-off-by: Pulak Kanti Bhowmick <[email protected]>
  • Loading branch information
pkbhowmick committed Mar 29, 2023
1 parent 8970ad2 commit 9163716
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
6 changes: 6 additions & 0 deletions cmd/clusterctl/api/v1alpha3/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ const (
// Note: Only CRDs that are referenced by core Cluster API CRDs have to comply with the naming scheme.
// See the following issue for more information: https://github.com/kubernetes-sigs/cluster-api/issues/5686#issuecomment-1260897278
SkipCRDNamePreflightCheckAnnotation = "clusterctl.cluster.x-k8s.io/skip-crd-name-preflight-check"

// DeleteForMoveAnnotation will be set to objects that are going to be deleted from the
// source cluster after being moved to the target cluster during the clusterctl move operation.
//
// It will help any validation webhook to take decision based on it.
DeleteForMoveAnnotation = "clusterctl.cluster.x-k8s.io/delete-for-move"
)
9 changes: 8 additions & 1 deletion cmd/clusterctl/client/cluster/mover.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
Expand Down Expand Up @@ -1064,7 +1065,8 @@ func (o *objectMover) deleteGroup(group moveGroup) error {
}

var (
removeFinalizersPatch = client.RawPatch(types.MergePatchType, []byte("{\"metadata\":{\"finalizers\":[]}}"))
removeFinalizersPatch = client.RawPatch(types.MergePatchType, []byte("{\"metadata\":{\"finalizers\":[]}}"))
addDeleteForMoveAnnotationPatch = client.RawPatch(types.JSONPatchType, []byte(fmt.Sprintf("[{\"op\": \"add\", \"path\":\"/metadata/annotations\", \"value\":{%q:\"true\"}}]", clusterctlv1.DeleteForMoveAnnotation)))
)

// deleteSourceObject deletes the Kubernetes object corresponding to the node from the source management cluster, taking care of removing all the finalizers so
Expand Down Expand Up @@ -1106,6 +1108,11 @@ func (o *objectMover) deleteSourceObject(nodeToDelete *node) error {
sourceObj.GroupVersionKind(), sourceObj.GetNamespace(), sourceObj.GetName())
}

if err := cFrom.Patch(ctx, sourceObj, addDeleteForMoveAnnotationPatch); err != nil {
return errors.Wrapf(err, "error adding delete-for-move annotation from %q %s/%s",
sourceObj.GroupVersionKind(), sourceObj.GetNamespace(), sourceObj.GetName())
}

if len(sourceObj.GetFinalizers()) > 0 {
if err := cFrom.Patch(ctx, sourceObj, removeFinalizersPatch); err != nil {
return errors.Wrapf(err, "error removing finalizers from %q %s/%s",
Expand Down
Loading

0 comments on commit 9163716

Please sign in to comment.