@@ -33,12 +33,14 @@ import (
3333 "k8s.io/apimachinery/pkg/types"
3434 "k8s.io/apimachinery/pkg/util/runtime"
3535 "k8s.io/apimachinery/pkg/util/wait"
36+ kubernetesclient "k8s.io/client-go/kubernetes"
3637 "k8s.io/client-go/metadata"
3738 "k8s.io/client-go/tools/cache"
3839 "k8s.io/client-go/util/workqueue"
3940 "k8s.io/klog/v2"
4041
4142 tenancyv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/tenancy/v1alpha1"
43+ "github.com/kcp-dev/kcp/pkg/apis/tenancy/v1alpha1/helper"
4244 kcpclient "github.com/kcp-dev/kcp/pkg/client/clientset/versioned"
4345 tenancyinformers "github.com/kcp-dev/kcp/pkg/client/informers/externalversions/tenancy/v1alpha1"
4446 tenancylisters "github.com/kcp-dev/kcp/pkg/client/listers/tenancy/v1alpha1"
@@ -50,7 +52,13 @@ const (
5052 controllerName = "kcp-clusterworkspacedeletion"
5153)
5254
55+ var (
56+ background = metav1 .DeletePropagationBackground
57+ backgroudDeletion = metav1.DeleteOptions {PropagationPolicy : & background }
58+ )
59+
5360func NewController (
61+ kubeClusterClient kubernetesclient.ClusterInterface ,
5462 kcpClusterClient kcpclient.Interface ,
5563 metadataClusterClient metadata.Interface ,
5664 workspaceInformer tenancyinformers.ClusterWorkspaceInformer ,
@@ -60,6 +68,7 @@ func NewController(
6068
6169 c := & Controller {
6270 queue : queue ,
71+ kubeClusterClient : kubeClusterClient ,
6372 kcpClusterClient : kcpClusterClient ,
6473 metadataClusterClient : metadataClusterClient ,
6574 workspaceLister : workspaceInformer .Lister (),
@@ -87,6 +96,7 @@ func NewController(
8796type Controller struct {
8897 queue workqueue.RateLimitingInterface
8998
99+ kubeClusterClient kubernetesclient.ClusterInterface
90100 kcpClusterClient kcpclient.Interface
91101 metadataClusterClient metadata.Interface
92102
@@ -248,9 +258,23 @@ func (c *Controller) finalizeWorkspace(ctx context.Context, workspace *tenancyv1
248258 if workspace .Finalizers [i ] == deletion .WorkspaceFinalizer {
249259 workspace .Finalizers = append (workspace .Finalizers [:i ], workspace .Finalizers [i + 1 :]... )
250260
261+ clusterName := logicalcluster .From (workspace )
262+ listOpts := metav1.ListOptions {
263+ LabelSelector : helper .WorkspaceLabelSelector (workspace .Name ),
264+ }
265+
266+ // TODO(hasheddan): ClusterRole and ClusterRoleBinding cleanup
267+ // should be handled by garbage collection when the controller is
268+ // implemented.
269+ if err := c .kubeClusterClient .Cluster (clusterName ).RbacV1 ().ClusterRoles ().DeleteCollection (ctx , backgroudDeletion , listOpts ); err != nil && ! apierrors .IsNotFound (err ) {
270+ return fmt .Errorf ("could not delete clusterroles for workspace %s: %w" , clusterName , err )
271+ }
272+ if err := c .kubeClusterClient .Cluster (clusterName ).RbacV1 ().ClusterRoleBindings ().DeleteCollection (ctx , backgroudDeletion , listOpts ); err != nil && ! apierrors .IsNotFound (err ) {
273+ return fmt .Errorf ("could not delete clusterrolebindings for workspace %s: %w" , clusterName , err )
274+ }
251275 logger .V (2 ).Info ("removing finalizer from ClusterWorkspace" )
252276 _ , err := c .kcpClusterClient .TenancyV1alpha1 ().ClusterWorkspaces ().Update (
253- logicalcluster .WithCluster (ctx , logicalcluster . From ( workspace ) ), workspace , metav1.UpdateOptions {})
277+ logicalcluster .WithCluster (ctx , clusterName ), workspace , metav1.UpdateOptions {})
254278 return err
255279 }
256280 }
0 commit comments