fix workspace role name exceeding the length limit (#2132)
Signed-off-by: hongming <coder.scala@gmail.com>
(cherry picked from commit 7a3a99cecb)
This commit is contained in:
@@ -7,16 +7,14 @@ package workspace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/pkg/errors"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/tools/record"
|
||||
"k8s.io/client-go/util/retry"
|
||||
"k8s.io/klog/v2"
|
||||
tenantv1beta1 "kubesphere.io/api/tenant/v1beta1"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
@@ -25,6 +23,7 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
kscontroller "kubesphere.io/kubesphere/pkg/controller"
|
||||
)
|
||||
|
||||
@@ -76,7 +75,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
|
||||
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{}, fmt.Errorf("failed to add finalizer: %s", err)
|
||||
return ctrl.Result{}, errors.Wrapf(err, "failed to add finalizer to workspace %s", workspace.Name)
|
||||
}
|
||||
workspaceOperation.WithLabelValues("create", workspace.Name).Inc()
|
||||
}
|
||||
@@ -84,12 +83,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
|
||||
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)
|
||||
return ctrl.Result{}, errors.Wrapf(err, "failed to delete workspace %s", workspace.Name)
|
||||
}
|
||||
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)
|
||||
return ctrl.Result{}, errors.Wrapf(err, "failed to remove finalizer from workspace %s", workspace.Name)
|
||||
}
|
||||
workspaceOperation.WithLabelValues("delete", workspace.Name).Inc()
|
||||
}
|
||||
@@ -106,19 +105,42 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
|
||||
// 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)
|
||||
case string(metav1.DeletePropagationOrphan), "":
|
||||
if err := r.cleanUpNamespaces(ctx, workspace); err != nil {
|
||||
return false, errors.Wrapf(err, "failed to clean up namespaces in workspace %s", workspace.Name)
|
||||
}
|
||||
case string(metav1.DeletePropagationForeground), string(metav1.DeletePropagationBackground):
|
||||
if err := r.deleteNamespaces(ctx, workspace); err != nil {
|
||||
return false, errors.Wrapf(err, "failed to delete namespaces in workspace %s", workspace.Name)
|
||||
}
|
||||
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])
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (r *Reconciler) cleanUpNamespaces(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 errors.Wrapf(err, "failed to list namespaces in workspace %s", workspace.Name)
|
||||
}
|
||||
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
||||
for _, ns := range namespaces.Items {
|
||||
if err := r.Get(ctx, client.ObjectKeyFromObject(&ns), &ns); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
delete(ns.Labels, tenantv1beta1.WorkspaceLabel)
|
||||
if err := r.Update(ctx, &ns); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to clean up namespaces in workspace %s", workspace.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// deleteNamespaces deletes all namespaces associated with the given workspace.
|
||||
@@ -126,14 +148,14 @@ func (r *Reconciler) workspaceCascadingDeletion(ctx context.Context, workspace *
|
||||
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)
|
||||
return errors.Wrapf(err, "failed to list namespaces in workspace %s", workspace.Name)
|
||||
}
|
||||
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 errors.Wrapf(err, "failed to delete namespace %s in workspace %s", ns.Name, workspace.Name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -11,19 +11,18 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/controller/controllertest"
|
||||
|
||||
kscontroller "kubesphere.io/kubesphere/pkg/controller"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"k8s.io/klog/v2"
|
||||
"k8s.io/utils/ptr"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
||||
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
||||
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
|
||||
|
||||
kscontroller "kubesphere.io/kubesphere/pkg/controller"
|
||||
"kubesphere.io/kubesphere/pkg/controller/controllertest"
|
||||
"kubesphere.io/kubesphere/pkg/scheme"
|
||||
)
|
||||
|
||||
@@ -45,10 +44,9 @@ var _ = BeforeSuite(func() {
|
||||
logf.SetLogger(klog.NewKlogr())
|
||||
|
||||
By("bootstrapping test environment")
|
||||
t := true
|
||||
if os.Getenv("TEST_USE_EXISTING_CLUSTER") == "true" {
|
||||
testEnv = &envtest.Environment{
|
||||
UseExistingCluster: &t,
|
||||
UseExistingCluster: ptr.To(true),
|
||||
}
|
||||
} else {
|
||||
crdDirPaths, err := controllertest.LoadCrdPath()
|
||||
|
||||
@@ -9,17 +9,18 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
tenantv1beta1 "kubesphere.io/api/tenant/v1beta1"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
)
|
||||
|
||||
var _ = Describe("Workspace", func() {
|
||||
|
||||
const timeout = time.Second * 30
|
||||
const interval = time.Second * 1
|
||||
|
||||
@@ -28,16 +29,24 @@ var _ = Describe("Workspace", func() {
|
||||
// Avoid adding tests for vanilla CRUD operations because they would
|
||||
// test Kubernetes API server, which isn't the goal here.
|
||||
Context("Workspace Controller", func() {
|
||||
It("Should create successfully", func() {
|
||||
|
||||
It("DeletePropagationBackground", func() {
|
||||
workspace := &tenantv1beta1.Workspace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "workspace-test",
|
||||
Name: "workspace-test1",
|
||||
},
|
||||
}
|
||||
namespace := &corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "namespace-test1",
|
||||
Labels: map[string]string{
|
||||
tenantv1beta1.WorkspaceLabel: workspace.Name,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create
|
||||
Expect(k8sClient.Create(context.Background(), workspace)).Should(Succeed())
|
||||
Expect(k8sClient.Create(context.Background(), namespace)).Should(Succeed())
|
||||
|
||||
By("Expecting to create workspace successfully")
|
||||
Eventually(func() bool {
|
||||
@@ -58,15 +67,6 @@ var _ = Describe("Workspace", func() {
|
||||
return workspace.Spec.Manager == "admin"
|
||||
}, timeout, interval).Should(BeTrue())
|
||||
|
||||
// Delete
|
||||
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
|
||||
}
|
||||
return k8sClient.Delete(context.Background(), workspace)
|
||||
}, timeout, interval).Should(Succeed())
|
||||
|
||||
// Update DeletionPropagation
|
||||
By("Expecting to delete workspace successfully")
|
||||
Eventually(func() error {
|
||||
@@ -80,10 +80,78 @@ var _ = Describe("Workspace", func() {
|
||||
return k8sClient.Update(context.Background(), workspace)
|
||||
}, timeout, interval).Should(Succeed())
|
||||
|
||||
By("Expecting to delete workspace finish")
|
||||
// Delete workspace
|
||||
By("Expecting to delete workspace successfully")
|
||||
Eventually(func() error {
|
||||
return k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, workspace)
|
||||
}, timeout, interval).ShouldNot(Succeed())
|
||||
return k8sClient.Delete(context.Background(), workspace)
|
||||
}, timeout, interval).Should(Succeed())
|
||||
|
||||
By("Expecting to cascading deletion finish")
|
||||
Eventually(func() bool {
|
||||
_ = k8sClient.Get(context.Background(), types.NamespacedName{Name: namespace.Name}, namespace)
|
||||
// Deleting a namespace will seem to succeed, but the namespace will just be put in a Terminating state, and never actually be reclaimed.
|
||||
// Reference: https://book.kubebuilder.io/reference/envtest.html#namespace-usage-limitation
|
||||
return namespace.Status.Phase == corev1.NamespaceTerminating
|
||||
}, timeout, interval).Should(BeTrue())
|
||||
|
||||
By("Expecting to delete workspace finish")
|
||||
Eventually(func() bool {
|
||||
return apierrors.IsNotFound(k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, workspace))
|
||||
}, timeout, interval).Should(BeTrue())
|
||||
})
|
||||
It("DeletePropagationOrphan", func() {
|
||||
workspace := &tenantv1beta1.Workspace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "workspace-test2",
|
||||
},
|
||||
}
|
||||
namespace := &corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "namespace-test2",
|
||||
Labels: map[string]string{
|
||||
tenantv1beta1.WorkspaceLabel: workspace.Name,
|
||||
},
|
||||
},
|
||||
}
|
||||
// Create
|
||||
Expect(k8sClient.Create(context.Background(), workspace)).Should(Succeed())
|
||||
Expect(k8sClient.Create(context.Background(), namespace)).Should(Succeed())
|
||||
|
||||
By("Expecting to create workspace successfully")
|
||||
Eventually(func() bool {
|
||||
_ = k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, workspace)
|
||||
return len(workspace.Finalizers) > 0
|
||||
}, timeout, interval).Should(BeTrue())
|
||||
|
||||
// Update
|
||||
updated := &tenantv1beta1.Workspace{}
|
||||
Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, updated)).Should(Succeed())
|
||||
updated.Spec.Manager = "admin"
|
||||
Expect(k8sClient.Update(context.Background(), updated)).Should(Succeed())
|
||||
|
||||
// List workspace role bindings
|
||||
By("Expecting to update workspace successfully")
|
||||
Eventually(func() bool {
|
||||
_ = k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, workspace)
|
||||
return workspace.Spec.Manager == "admin"
|
||||
}, timeout, interval).Should(BeTrue())
|
||||
|
||||
// Delete workspace
|
||||
By("Expecting to delete workspace successfully")
|
||||
Eventually(func() error {
|
||||
return k8sClient.Delete(context.Background(), workspace)
|
||||
}, timeout, interval).Should(Succeed())
|
||||
|
||||
By("Expecting to delete workspace finish")
|
||||
Eventually(func() bool {
|
||||
return apierrors.IsNotFound(k8sClient.Get(context.Background(), types.NamespacedName{Name: workspace.Name}, workspace))
|
||||
}, timeout, interval).Should(BeTrue())
|
||||
|
||||
By("Expecting to cascading deletion finish")
|
||||
Eventually(func() bool {
|
||||
_ = k8sClient.Get(context.Background(), types.NamespacedName{Name: namespace.Name}, namespace)
|
||||
return namespace.Labels[tenantv1beta1.WorkspaceLabel] == "" && namespace.Status.Phase != corev1.NamespaceTerminating
|
||||
}, timeout, interval).Should(BeTrue())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user