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 <pkbhowmick007@gmail.com>
  • Loading branch information
pkbhowmick committed Apr 14, 2023
1 parent 6e151b5 commit 4ac5f3d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 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:\"\"}}]", 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ maintainers of providers and consumers of our Go API.

### Other

-
- clusterctl move is adding the new annotation `clusterctl.cluster.x-k8s.io/delete-for-move` before object deletion.

### Suggested changes for providers

Expand Down
Loading

0 comments on commit 4ac5f3d

Please sign in to comment.