@@ -53,6 +53,7 @@ func (r *KubeconfigRBACReconciler) SetupWithManager(mgr ctrl.Manager) error {
5353}
5454
5555// +kubebuilder:rbac:groups=operator.kcp.io,resources=kubeconfigs,verbs=get;update;patch
56+ // +kubebuilder:rbac:groups=operator.kcp.io,resources=kubeconfigs/status,verbs=get;update;patch
5657// +kubebuilder:rbac:groups=operator.kcp.io,resources=kubeconfigs/finalizers,verbs=update
5758
5859// Reconcile is part of the main kubernetes reconciliation loop which aims to
@@ -171,26 +172,37 @@ func (r *KubeconfigRBACReconciler) handleDeletion(ctx context.Context, kc *opera
171172 return nil
172173 }
173174
174- targetClient , err := client .NewInternalKubeconfigClient (ctx , r .Client , kc , logicalcluster .Name (kc .Spec .Authorization .ClusterRoleBindings .Cluster ), nil )
175- if err != nil {
176- return fmt .Errorf ("failed to create client to kubeconfig target: %w" , err )
177- }
175+ // This should always be true, unless cleanup succeeded but removing the finalizer failed in a
176+ // previous reconcile cycle.
177+ if cluster := kc .Status .Authorization .ProvisionedCluster ; cluster != "" {
178+ targetClient , err := client .NewInternalKubeconfigClient (ctx , r .Client , kc , logicalcluster .Name (cluster ), nil )
179+ if err != nil {
180+ return fmt .Errorf ("failed to create client to kubeconfig target: %w" , err )
181+ }
178182
179- // find all existing bindings
180- ownerLabels := kubeconfig .OwnerLabels (kc )
181- crbList := & rbacv1.ClusterRoleBindingList {}
182- if err := targetClient .List (ctx , crbList , ctrlruntimeclient .MatchingLabels (ownerLabels )); err != nil {
183- return fmt .Errorf ("failed to list existing ClusterRoleBindings: %w" , err )
184- }
183+ // find all existing bindings
184+ ownerLabels := kubeconfig .OwnerLabels (kc )
185+ crbList := & rbacv1.ClusterRoleBindingList {}
186+ if err := targetClient .List (ctx , crbList , ctrlruntimeclient .MatchingLabels (ownerLabels )); err != nil {
187+ return fmt .Errorf ("failed to list existing ClusterRoleBindings: %w" , err )
188+ }
185189
186- // delete all of them
187- logger := log .FromContext (ctx )
190+ // delete all of them
191+ logger := log .FromContext (ctx )
188192
189- for _ , crb := range crbList .Items {
190- logger .V (2 ).WithValues ("name" , crb .Name ).Info ("Deleting ClusterRoleBinding" )
193+ for _ , crb := range crbList .Items {
194+ logger .V (2 ).WithValues ("name" , crb .Name ).Info ("Deleting ClusterRoleBinding" )
195+
196+ if err := targetClient .Delete (ctx , & crb ); err != nil {
197+ return fmt .Errorf ("failed to delete ClusterRoleBinding %s: %w" , crb .Name , err )
198+ }
199+ }
191200
192- if err := targetClient .Delete (ctx , & crb ); err != nil {
193- return fmt .Errorf ("failed to delete ClusterRoleBinding %s: %w" , crb .Name , err )
201+ // clean status
202+ oldKubeconfig := kc .DeepCopy ()
203+ kc .Status .Authorization .ProvisionedCluster = ""
204+ if err := r .Status ().Patch (ctx , kc , ctrlruntimeclient .MergeFrom (oldKubeconfig )); err != nil {
205+ return fmt .Errorf ("failed to finish cleanup by updating status: %w" , err )
194206 }
195207 }
196208
0 commit comments