refactor: workspace cascading deletion logic (#6249)

Signed-off-by: hongming <coder.scala@gmail.com>
This commit is contained in:
hongming
2024-10-30 17:42:06 +08:00
committed by GitHub
parent 9aa17af5db
commit d63a24fc91
7 changed files with 190 additions and 179 deletions

View File

@@ -11,6 +11,8 @@ import (
"fmt"
"strings"
"kubesphere.io/kubesphere/pkg/constants"
"github.com/Masterminds/semver/v3"
"github.com/mitchellh/mapstructure"
corev1 "k8s.io/api/core/v1"
@@ -41,8 +43,6 @@ import (
jsonpatchutil "kubesphere.io/kubesphere/pkg/utils/josnpatchutil"
)
const orphanFinalizer = "orphan.finalizers.kubesphere.io"
type Interface interface {
ListWorkspaces(user user.Info, queryParam *query.Query) (*api.ListResult, error)
GetWorkspace(workspace string) (*tenantv1beta1.Workspace, error)
@@ -561,16 +561,18 @@ func (t *tenantOperator) ListClusters(user user.Info, queryParam *query.Query) (
func (t *tenantOperator) DeleteWorkspaceTemplate(workspaceName string, opts metav1.DeleteOptions) error {
workspace := &tenantv1beta1.WorkspaceTemplate{}
if err := t.client.Get(context.Background(), types.NamespacedName{Name: workspaceName}, workspace); err != nil {
return err
return fmt.Errorf("failed to get workspace template: %s", err)
}
if opts.PropagationPolicy != nil && *opts.PropagationPolicy == metav1.DeletePropagationOrphan {
workspace.Finalizers = append(workspace.Finalizers, orphanFinalizer)
if opts.PropagationPolicy != nil {
if workspace.Annotations == nil {
workspace.Annotations = make(map[string]string)
}
workspace.Annotations[constants.DeletionPropagationAnnotation] = string(*opts.PropagationPolicy)
if err := t.client.Update(context.Background(), workspace); err != nil {
return err
return fmt.Errorf("failed to update workspace template: %s", err)
}
}
return t.client.Delete(context.Background(), workspace, &runtimeclient.DeleteOptions{Raw: &opts})
return t.client.Delete(context.Background(), workspace)
}
func (t *tenantOperator) getClusterRoleBindingsByUser(clusterName, username string) (*iamv1beta1.ClusterRoleBindingList, error) {