Skip to content

Commit

Permalink
Deleting "shipwright-build-webhook" clusterrole and clusterrolebinding
Browse files Browse the repository at this point in the history
Implementing a deleteObjectsIfPresent function and checking
for presence of every resource and then deleting the given resource.
Using this to delete "shipwright-build-webhook" clusterrole and clusterrolebinding

Signed-off by : Ayush Satyam<[email protected]>
  • Loading branch information
ayushsatyam146 committed May 30, 2024
1 parent db1f9a2 commit 9876329
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
29 changes: 29 additions & 0 deletions controllers/shipwrightbuild_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/manifestival/manifestival"
tektonoperatorv1alpha1client "github.com/tektoncd/operator/pkg/client/clientset/versioned/typed/operator/v1alpha1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
crdclientv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -85,6 +86,24 @@ func (r *ShipwrightBuildReconciler) unsetFinalizer(ctx context.Context, b *v1alp
return r.Update(ctx, b, &client.UpdateOptions{})
}

// deleteObjectsIfPresent deletes all the given objects if they are present in the cluster.
func deleteObjectsIfPresent(ctx context.Context, k8sClient client.Client, objs []client.Object) error {
for _, obj := range objs {
err := k8sClient.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("getting object %s: %v", obj.GetName(), err)
}
err = k8sClient.Delete(ctx, obj)
if err != nil {
return fmt.Errorf("deleting object %s: %v", obj.GetName(), err)
}
}
return nil
}

// Reconcile performs the resource reconciliation steps to deploy or remove Shipwright Build
// instances. When deletion-timestamp is found, the removal of the previously deploy resources is
// executed, otherwise the regular deploy workflow takes place.
Expand Down Expand Up @@ -233,6 +252,16 @@ func (r *ShipwrightBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
err = r.Client.Status().Update(ctx, b)
return RequeueWithError(err)
}

err = deleteObjectsIfPresent(ctx, r.Client, []client.Object{
&rbacv1.ClusterRoleBinding{ObjectMeta: metav1.ObjectMeta{Name: "shipwright-build-webhook"}},
&rbacv1.ClusterRole{ObjectMeta: metav1.ObjectMeta{Name: "shipwright-build-webhook"}},
})
if err != nil {
logger.Error(err, "deleting'shipwright-build-webhook' role and cluster role binding")
return RequeueWithError(err)
}

if err := r.setFinalizer(ctx, b); err != nil {
logger.Info(fmt.Sprintf("%#v", b))
logger.Error(err, "setting the finalizer")
Expand Down
3 changes: 3 additions & 0 deletions controllers/shipwrightbuild_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
crdclientv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -41,6 +42,8 @@ func bootstrapShipwrightBuildReconciler(
s.AddKnownTypes(corev1.SchemeGroupVersion, &corev1.Namespace{})
s.AddKnownTypes(appsv1.SchemeGroupVersion, &appsv1.Deployment{})
s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.ShipwrightBuild{})
s.AddKnownTypes(rbacv1.SchemeGroupVersion, &rbacv1.ClusterRoleBinding{})
s.AddKnownTypes(rbacv1.SchemeGroupVersion, &rbacv1.ClusterRole{})

logger := zap.New()

Expand Down

0 comments on commit 9876329

Please sign in to comment.