refactor: workspace cascading deletion logic (#6249)
Signed-off-by: hongming <coder.scala@gmail.com>
This commit is contained in:
@@ -7,9 +7,15 @@ package workspace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/tools/record"
|
||||
"k8s.io/klog/v2"
|
||||
tenantv1beta1 "kubesphere.io/api/tenant/v1beta1"
|
||||
@@ -24,7 +30,6 @@ import (
|
||||
|
||||
const (
|
||||
controllerName = "workspace"
|
||||
finalizer = "finalizers.tenant.kubesphere.io"
|
||||
)
|
||||
|
||||
var _ kscontroller.Controller = &Reconciler{}
|
||||
@@ -53,11 +58,6 @@ func (r *Reconciler) SetupWithManager(mgr *kscontroller.Manager) error {
|
||||
}
|
||||
|
||||
// +kubebuilder:rbac:groups=tenant.kubesphere.io,resources=workspaces,verbs=get;list;watch;create;update;patch;delete
|
||||
// +kubebuilder:rbac:groups=tenant.kubesphere.io,resources=workspaces/status,verbs=get;update;patch
|
||||
// +kubebuilder:rbac:groups=iam.kubesphere.io,resources=users,verbs=get;list;watch
|
||||
// +kubebuilder:rbac:groups=iam.kubesphere.io,resources=rolebases,verbs=get;list;watch
|
||||
// +kubebuilder:rbac:groups=iam.kubesphere.io,resources=workspaceroles,verbs=get;list;watch;create;update;patch;delete
|
||||
// +kubebuilder:rbac:groups=iam.kubesphere.io,resources=workspacerolebindings,verbs=get;list;watch;create;update;patch;delete
|
||||
|
||||
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
logger := r.logger.WithValues("workspace", req.NamespacedName)
|
||||
@@ -70,28 +70,71 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
|
||||
if workspace.ObjectMeta.DeletionTimestamp.IsZero() {
|
||||
// The object is not being deleted, so if it does not have our finalizer,
|
||||
// then lets add the finalizer and update the object.
|
||||
if !controllerutil.ContainsFinalizer(workspace, finalizer) {
|
||||
if !controllerutil.ContainsFinalizer(workspace, constants.CascadingDeletionFinalizer) {
|
||||
expected := workspace.DeepCopy()
|
||||
controllerutil.AddFinalizer(expected, finalizer)
|
||||
// Remove legacy finalizer
|
||||
controllerutil.RemoveFinalizer(expected, "finalizers.tenant.kubesphere.io")
|
||||
controllerutil.AddFinalizer(expected, constants.CascadingDeletionFinalizer)
|
||||
if err := r.Patch(ctx, expected, client.MergeFrom(workspace)); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
return ctrl.Result{}, fmt.Errorf("failed to add finalizer: %s", err)
|
||||
}
|
||||
workspaceOperation.WithLabelValues("create", workspace.Name).Inc()
|
||||
}
|
||||
} else {
|
||||
// The object is being deleted
|
||||
if controllerutil.ContainsFinalizer(workspace, finalizer) {
|
||||
// remove our finalizer from the list and update it.
|
||||
controllerutil.RemoveFinalizer(workspace, finalizer)
|
||||
if err := r.Update(ctx, workspace); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
if controllerutil.ContainsFinalizer(workspace, constants.CascadingDeletionFinalizer) {
|
||||
ok, err := r.workspaceCascadingDeletion(ctx, workspace)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, fmt.Errorf("failed to delete workspace: %s", err)
|
||||
}
|
||||
if ok {
|
||||
controllerutil.RemoveFinalizer(workspace, constants.CascadingDeletionFinalizer)
|
||||
if err := r.Update(ctx, workspace); err != nil {
|
||||
return ctrl.Result{}, fmt.Errorf("failed to remove finalizer: %s", err)
|
||||
}
|
||||
workspaceOperation.WithLabelValues("delete", workspace.Name).Inc()
|
||||
}
|
||||
workspaceOperation.WithLabelValues("delete", workspace.Name).Inc()
|
||||
}
|
||||
// Our finalizer has finished, so the reconciler can do nothing.
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
r.recorder.Event(workspace, corev1.EventTypeNormal, kscontroller.Synced, kscontroller.MessageResourceSynced)
|
||||
r.recorder.Event(workspace, corev1.EventTypeNormal, "Reconcile", "Reconcile workspace successfully")
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// workspaceCascadingDeletion handles the cascading deletion of a workspace based on its deletion propagation policy.
|
||||
// It returns a boolean indicating whether the deletion was successful and an error if any occurred.
|
||||
func (r *Reconciler) workspaceCascadingDeletion(ctx context.Context, workspace *tenantv1beta1.Workspace) (bool, error) {
|
||||
switch workspace.Annotations[constants.DeletionPropagationAnnotation] {
|
||||
case string(metav1.DeletePropagationOrphan):
|
||||
// If the deletion propagation policy is "Orphan", return true without deleting namespaces.
|
||||
return true, nil
|
||||
case string(metav1.DeletePropagationForeground), string(metav1.DeletePropagationBackground):
|
||||
// If the deletion propagation policy is "Foreground" or "Background", delete the namespaces.
|
||||
if err := r.deleteNamespaces(ctx, workspace); err != nil {
|
||||
return false, fmt.Errorf("failed to delete namespaces in workspace %s: %s", workspace.Name, err)
|
||||
}
|
||||
return true, nil
|
||||
default:
|
||||
// If the deletion propagation policy is invalid, return an error.
|
||||
return false, fmt.Errorf("invalid deletion propagation policy: %s", workspace.Annotations[constants.DeletionPropagationAnnotation])
|
||||
}
|
||||
}
|
||||
|
||||
// deleteNamespaces deletes all namespaces associated with the given workspace.
|
||||
// It uses the "Background" deletion propagation policy.
|
||||
func (r *Reconciler) deleteNamespaces(ctx context.Context, workspace *tenantv1beta1.Workspace) error {
|
||||
namespaces := &corev1.NamespaceList{}
|
||||
if err := r.List(ctx, namespaces, client.MatchingLabels{tenantv1beta1.WorkspaceLabel: workspace.Name}); err != nil {
|
||||
return fmt.Errorf("failed to list namespaces in workspace %s: %s", workspace.Name, err)
|
||||
}
|
||||
for _, ns := range namespaces.Items {
|
||||
if err := r.Delete(ctx, &ns); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("failed to delete namespace %s: %s", ns.Name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -39,9 +41,8 @@ var _ = Describe("Workspace", func() {
|
||||
|
||||
By("Expecting to create workspace successfully")
|
||||
Eventually(func() bool {
|
||||
f := &tenantv1beta1.Workspace{}
|
||||
_ = k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, f)
|
||||
return len(f.Finalizers) > 0
|
||||
_ = k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, workspace)
|
||||
return len(workspace.Finalizers) > 0
|
||||
}, timeout, interval).Should(BeTrue())
|
||||
|
||||
// Update
|
||||
@@ -60,15 +61,28 @@ var _ = Describe("Workspace", func() {
|
||||
// Delete
|
||||
By("Expecting to delete workspace successfully")
|
||||
Eventually(func() error {
|
||||
f := &tenantv1beta1.Workspace{}
|
||||
_ = k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, f)
|
||||
return k8sClient.Delete(context.Background(), f)
|
||||
if err := k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, workspace); err != nil {
|
||||
return err
|
||||
}
|
||||
return k8sClient.Delete(context.Background(), workspace)
|
||||
}, timeout, interval).Should(Succeed())
|
||||
|
||||
// Update DeletionPropagation
|
||||
By("Expecting to delete workspace successfully")
|
||||
Eventually(func() error {
|
||||
if err := k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, workspace); err != nil {
|
||||
return err
|
||||
}
|
||||
if workspace.Annotations == nil {
|
||||
workspace.Annotations = make(map[string]string)
|
||||
}
|
||||
workspace.Annotations[constants.DeletionPropagationAnnotation] = string(metav1.DeletePropagationBackground)
|
||||
return k8sClient.Update(context.Background(), workspace)
|
||||
}, timeout, interval).Should(Succeed())
|
||||
|
||||
By("Expecting to delete workspace finish")
|
||||
Eventually(func() error {
|
||||
f := &tenantv1beta1.Workspace{}
|
||||
return k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, f)
|
||||
return k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, workspace)
|
||||
}, timeout, interval).ShouldNot(Succeed())
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user