Skip to content

Commit

Permalink
Set proper the ownerReferences when we create a rolebinding
Browse files Browse the repository at this point in the history
  • Loading branch information
atheo89 committed Nov 7, 2024
1 parent d937dec commit 4d7146a
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions components/odh-notebook-controller/controllers/notebook_rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)

// NewRoleBinding defines the desired RoleBinding or ClusterRoleBinding object.
Expand Down Expand Up @@ -123,6 +124,13 @@ func (r *OpenshiftNotebookReconciler) reconcileRoleBinding(
err = r.Get(ctx, types.NamespacedName{Name: rolebindingName, Namespace: notebook.Namespace}, found)
if err != nil && apierrs.IsNotFound(err) {
log.Info("Creating RoleBinding", "RoleBinding.Namespace", roleBinding.Namespace, "RoleBinding.Name", roleBinding.Name)

// Add .metatada.ownerReferences to the Rolebinding to be deleted by
// the Kubernetes garbage collector if the notebook is deleted
if err := ctrl.SetControllerReference(notebook, roleBinding, r.Scheme); err != nil {
log.Error(err, "Failed to set controller reference for RoleBinding")
return err
}
err = r.Create(ctx, roleBinding)
if err != nil {
log.Error(err, "Failed to create RoleBinding", "RoleBinding.Namespace", roleBinding.Namespace, "RoleBinding.Name", roleBinding.Name)
Expand Down Expand Up @@ -152,11 +160,6 @@ func (r *OpenshiftNotebookReconciler) ReconcileRoleBindings(
notebook *nbv1.Notebook, ctx context.Context) error {

roleBindingName := "elyra-pipelines-" + notebook.Name
// If the notebook is marked for deletion, remove the associated RoleBinding
if !notebook.ObjectMeta.DeletionTimestamp.IsZero() {
return r.deleteRoleBinding(ctx, roleBindingName, notebook.Namespace)
}

// Reconcile a RoleBinding for pipelines for the notebook service account
if err := r.reconcileRoleBinding(notebook, ctx, roleBindingName, "Role", "ds-pipeline-user-access-dspa"); err != nil {
return err
Expand Down

0 comments on commit 4d7146a

Please sign in to comment.