@@ -33,12 +33,14 @@ import (
33
33
"k8s.io/apimachinery/pkg/types"
34
34
"k8s.io/apimachinery/pkg/util/runtime"
35
35
"k8s.io/apimachinery/pkg/util/wait"
36
+ kubernetesclient "k8s.io/client-go/kubernetes"
36
37
"k8s.io/client-go/metadata"
37
38
"k8s.io/client-go/tools/cache"
38
39
"k8s.io/client-go/util/workqueue"
39
40
"k8s.io/klog/v2"
40
41
41
42
tenancyv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/tenancy/v1alpha1"
43
+ "github.com/kcp-dev/kcp/pkg/apis/tenancy/v1alpha1/helper"
42
44
kcpclient "github.com/kcp-dev/kcp/pkg/client/clientset/versioned"
43
45
tenancyinformers "github.com/kcp-dev/kcp/pkg/client/informers/externalversions/tenancy/v1alpha1"
44
46
tenancylisters "github.com/kcp-dev/kcp/pkg/client/listers/tenancy/v1alpha1"
@@ -50,7 +52,13 @@ const (
50
52
controllerName = "kcp-clusterworkspacedeletion"
51
53
)
52
54
55
+ var (
56
+ background = metav1 .DeletePropagationBackground
57
+ backgroudDeletion = metav1.DeleteOptions {PropagationPolicy : & background }
58
+ )
59
+
53
60
func NewController (
61
+ kubeClusterClient kubernetesclient.ClusterInterface ,
54
62
kcpClusterClient kcpclient.Interface ,
55
63
metadataClusterClient metadata.Interface ,
56
64
workspaceInformer tenancyinformers.ClusterWorkspaceInformer ,
@@ -60,6 +68,7 @@ func NewController(
60
68
61
69
c := & Controller {
62
70
queue : queue ,
71
+ kubeClusterClient : kubeClusterClient ,
63
72
kcpClusterClient : kcpClusterClient ,
64
73
metadataClusterClient : metadataClusterClient ,
65
74
workspaceLister : workspaceInformer .Lister (),
@@ -87,6 +96,7 @@ func NewController(
87
96
type Controller struct {
88
97
queue workqueue.RateLimitingInterface
89
98
99
+ kubeClusterClient kubernetesclient.ClusterInterface
90
100
kcpClusterClient kcpclient.Interface
91
101
metadataClusterClient metadata.Interface
92
102
@@ -248,9 +258,23 @@ func (c *Controller) finalizeWorkspace(ctx context.Context, workspace *tenancyv1
248
258
if workspace .Finalizers [i ] == deletion .WorkspaceFinalizer {
249
259
workspace .Finalizers = append (workspace .Finalizers [:i ], workspace .Finalizers [i + 1 :]... )
250
260
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
+ }
251
275
logger .V (2 ).Info ("removing finalizer from ClusterWorkspace" )
252
276
_ , err := c .kcpClusterClient .TenancyV1alpha1 ().ClusterWorkspaces ().Update (
253
- logicalcluster .WithCluster (ctx , logicalcluster . From ( workspace ) ), workspace , metav1.UpdateOptions {})
277
+ logicalcluster .WithCluster (ctx , clusterName ), workspace , metav1.UpdateOptions {})
254
278
return err
255
279
}
256
280
}
0 commit comments