Skip to content

Commit

Permalink
Implement BeforeClusterDelete hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuvaraj Kakaraparthi committed Jun 28, 2022
1 parent 001c327 commit 8b25477
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
3 changes: 3 additions & 0 deletions controllers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type ClusterReconciler struct {
Client client.Client
APIReader client.Reader

RuntimeClient runtimeclient.Client

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string
}
Expand All @@ -51,6 +53,7 @@ func (r *ClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manag
return (&clustercontroller.Reconciler{
Client: r.Client,
APIReader: r.APIReader,
RuntimeClient: r.RuntimeClient,
WatchFilterValue: r.WatchFilterValue,
}).SetupWithManager(ctx, mgr, options)
}
Expand Down
28 changes: 28 additions & 0 deletions internal/controllers/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/external"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/hooks"
runtimecatalog "sigs.k8s.io/cluster-api/internal/runtime/catalog"
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/collections"
Expand All @@ -67,6 +71,8 @@ type Reconciler struct {
Client client.Client
APIReader client.Reader

RuntimeClient runtimeclient.Client

// WatchFilterValue is the label value used to filter events prior to reconciliation.
WatchFilterValue string

Expand Down Expand Up @@ -215,6 +221,28 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster)
func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Cluster) (reconcile.Result, error) {
log := ctrl.LoggerFrom(ctx)

// Call the BeforeClusterDelete hook to before proceeding further.
if feature.Gates.Enabled(feature.RuntimeSDK) {
// CAll the hook only ifi it is not marked already. If it is already marked it would mean don't need to call it anymore.
if cluster.Spec.Topology != nil && !hooks.IsPending(runtimehooksv1.BeforeClusterDelete, cluster) {
hookRequest := &runtimehooksv1.BeforeClusterDeleteRequest{
Cluster: *cluster,
}
hookResponse := &runtimehooksv1.BeforeClusterDeleteResponse{}
if err := r.RuntimeClient.CallAllExtensions(ctx, runtimehooksv1.BeforeClusterDelete, cluster, hookRequest, hookResponse); err != nil {
return ctrl.Result{}, errors.Wrapf(err, "error calling the %s hook", runtimecatalog.HookName(runtimehooksv1.BeforeClusterDelete))
}
if hookResponse.RetryAfterSeconds != 0 {
// Cannot proceed with deleting the cluster yet. Lets requeue to retry at a later time.
return ctrl.Result{RequeueAfter: time.Duration(hookResponse.RetryAfterSeconds) * time.Second}, nil
}
// We can proceed with the delete operation. Mark the hook so that we don't call it anymore for this Cluster.
if err := hooks.MarkAsPending(ctx, r.Client, cluster, runtimehooksv1.BeforeClusterDelete); err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to mark %s hook", runtimecatalog.HookName(runtimehooksv1.BeforeClusterDelete))
}
}
}

descendants, err := r.listDescendants(ctx, cluster)
if err != nil {
log.Error(err, "Failed to list descendants")
Expand Down
157 changes: 157 additions & 0 deletions internal/controllers/cluster/cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@ package cluster

import (
"testing"
"time"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilfeature "k8s.io/component-base/featuregate/testing"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/hooks"
runtimecatalog "sigs.k8s.io/cluster-api/internal/runtime/catalog"
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
Expand Down Expand Up @@ -350,6 +356,157 @@ func TestClusterReconciler(t *testing.T) {
})
}

func TestClusterReconciler_reconcileDelete(t *testing.T) {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.RuntimeSDK, true)()

catalog := runtimecatalog.New()
_ = runtimehooksv1.AddToCatalog(catalog)

beforeClusterDeleteGVH, err := catalog.GroupVersionHook(runtimehooksv1.BeforeClusterDelete)
if err != nil {
panic(err)
}

blockingResponse := &runtimehooksv1.BeforeClusterDeleteResponse{
CommonRetryResponse: runtimehooksv1.CommonRetryResponse{
RetryAfterSeconds: int32(10),
CommonResponse: runtimehooksv1.CommonResponse{
Status: runtimehooksv1.ResponseStatusSuccess,
},
},
}
nonBlockingResponse := &runtimehooksv1.BeforeClusterDeleteResponse{
CommonRetryResponse: runtimehooksv1.CommonRetryResponse{
RetryAfterSeconds: int32(0),
CommonResponse: runtimehooksv1.CommonResponse{
Status: runtimehooksv1.ResponseStatusSuccess,
},
},
}
failureResponse := &runtimehooksv1.BeforeClusterDeleteResponse{
CommonRetryResponse: runtimehooksv1.CommonRetryResponse{
CommonResponse: runtimehooksv1.CommonResponse{
Status: runtimehooksv1.ResponseStatusFailure,
},
},
}

tests := []struct {
name string
cluster *clusterv1.Cluster
hookResponse *runtimehooksv1.BeforeClusterDeleteResponse
wantHookToBeCalled bool
wantResult ctrl.Result
wantMarked bool
wantErr bool
}{
{
name: "should succeed if the BeforeClusterDelete hook returns a non-blocking response",
cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Namespace: "test-ns",
},
Spec: clusterv1.ClusterSpec{
Topology: &clusterv1.Topology{},
},
},
hookResponse: nonBlockingResponse,
wantResult: ctrl.Result{},
wantHookToBeCalled: true,
wantMarked: true,
wantErr: false,
},
{
name: "should requeue if the BeforeClusterDelete hook returns a blocking response",
cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Namespace: "test-ns",
},
Spec: clusterv1.ClusterSpec{
Topology: &clusterv1.Topology{},
},
},
hookResponse: blockingResponse,
wantResult: ctrl.Result{RequeueAfter: time.Duration(10) * time.Second},
wantHookToBeCalled: true,
wantMarked: false,
wantErr: false,
},
{
name: "should fail if the BeforeClusterDelete hook returns a failure response",
cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Namespace: "test-ns",
},
Spec: clusterv1.ClusterSpec{
Topology: &clusterv1.Topology{},
},
},
hookResponse: failureResponse,
wantResult: ctrl.Result{},
wantHookToBeCalled: true,
wantMarked: false,
wantErr: true,
},
{
name: "should succeed if the hook is already called",
cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Namespace: "test-ns",
Annotations: map[string]string{
// If the hook is already marked the hook should not be called during cluster delete.
runtimehooksv1.PendingHooksAnnotation: "BeforeClusterDelete",
},
},
Spec: clusterv1.ClusterSpec{
Topology: &clusterv1.Topology{},
},
},
// Using a blocking response here should not matter as the hook should never be called.
// Using a blocking response to enforce the point.
hookResponse: blockingResponse,
wantResult: ctrl.Result{},
wantHookToBeCalled: false,
wantMarked: true,
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

fakeClient := fake.NewClientBuilder().WithObjects(tt.cluster).Build()
fakeRuntimeClient := fakeruntimeclient.NewRuntimeClientBuilder().
WithCallAllExtensionResponses(map[runtimecatalog.GroupVersionHook]runtimehooksv1.ResponseObject{
beforeClusterDeleteGVH: tt.hookResponse,
}).
WithCatalog(catalog).
Build()

r := &Reconciler{
Client: fakeClient,
APIReader: fakeClient,
RuntimeClient: fakeRuntimeClient,
}

res, err := r.reconcileDelete(ctx, tt.cluster)
if tt.wantErr {
g.Expect(err).NotTo(BeNil())
} else {
g.Expect(err).To(BeNil())
g.Expect(res).To(Equal(tt.wantResult))
g.Expect(hooks.IsPending(runtimehooksv1.BeforeClusterDelete, tt.cluster)).To(Equal(tt.wantMarked))
g.Expect(fakeRuntimeClient.CallAllCount(runtimehooksv1.BeforeClusterDelete) == 1).To(Equal(tt.wantHookToBeCalled))
}
})
}
}

func TestClusterReconcilerNodeRef(t *testing.T) {
t.Run("machine to cluster", func(t *testing.T) {
cluster := &clusterv1.Cluster{
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
if err := (&controllers.ClusterReconciler{
Client: mgr.GetClient(),
APIReader: mgr.GetAPIReader(),
RuntimeClient: runtimeClient,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, concurrency(clusterConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Cluster")
Expand Down

0 comments on commit 8b25477

Please sign in to comment.