Skip to content

Commit

Permalink
Add RBAC for the workflow worker
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed Sep 26, 2023
1 parent e5e8a38 commit 8c2869c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
82 changes: 82 additions & 0 deletions manageiq-operator/api/v1alpha1/helpers/miq-components/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

miqv1alpha1 "github.com/ManageIQ/manageiq-pods/manageiq-operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -52,3 +53,84 @@ func DefaultServiceAccount(cr *miqv1alpha1.ManageIQ, scheme *runtime.Scheme) (*c

return sa, f
}

func WorkflowRole(cr *miqv1alpha1.ManageIQ, scheme *runtime.Scheme) (*rbacv1.Role, controllerutil.MutateFn) {
role := &rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: "manageiq-workflow",
Namespace: cr.ObjectMeta.Namespace,
},
}

f := func() error {
if err := controllerutil.SetControllerReference(cr, role, scheme); err != nil {
return err
}

role.Rules = []rbacv1.PolicyRule{
rbacv1.PolicyRule{
APIGroups: []string{""},
Resources: []string{"pods", "secrets"},
Verbs: []string{"create", "delete", "get", "list", "patch", "update", "watch"},
},
}

return nil
}

return role, f
}

func WorkflowRoleBinding(cr *miqv1alpha1.ManageIQ, scheme *runtime.Scheme) (*rbacv1.RoleBinding, controllerutil.MutateFn) {
rb := &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "manageiq-workflow",
Namespace: cr.ObjectMeta.Namespace,
},
}

f := func() error {
if err := controllerutil.SetControllerReference(cr, rb, scheme); err != nil {
return err
}

rb.RoleRef = rbacv1.RoleRef{
Kind: "Role",
Name: "manageiq-workflow",
APIGroup: "rbac.authorization.k8s.io",
}
rb.Subjects = []rbacv1.Subject{
rbacv1.Subject{
Kind: "ServiceAccount",
Name: "manageiq-workflow",
},
}

return nil
}

return rb, f
}

func WorkflowServiceAccount(cr *miqv1alpha1.ManageIQ, scheme *runtime.Scheme) (*corev1.ServiceAccount, controllerutil.MutateFn) {
sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "manageiq-workflow",
Namespace: cr.ObjectMeta.Namespace,
},
}

f := func() error {
if err := controllerutil.SetControllerReference(cr, sa, scheme); err != nil {
return err
}

if cr.Spec.ImagePullSecret != "" {
addSAPullSecret(sa, cr.Spec.ImagePullSecret)
}

return nil
}

return sa, f
}
23 changes: 23 additions & 0 deletions manageiq-operator/internal/controller/manageiq_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ func (r *ManageIQReconciler) Reconcile(ctx context.Context, request ctrl.Request
if e := r.manageApplicationResources(miqInstance); e != nil {
return reconcile.Result{}, e
}
logger.Info("Reconciling the CR status...")
if err := r.updateManageIQStatus(miqInstance); err != nil {
reqLogger.Error(err, "Failed setting ManageIQ status")
return reconcile.Result{}, err
}

logger.Info("Reconcile complete.")
return reconcile.Result{}, nil
}

Expand Down Expand Up @@ -832,5 +834,26 @@ func (r *ManageIQReconciler) manageApplicationResources(cr *miqv1alpha1.ManageIQ
logger.Info("ConfigMap has been reconciled", "component", "application remote console", "result", result)
}

role, mutateFunc := miqtool.WorkflowRole(cr, r.Scheme)
if result, err := controllerutil.CreateOrUpdate(context.TODO(), r.Client, role, mutateFunc); err != nil {
return err
} else if result != controllerutil.OperationResultNone {
logger.Info("Role has been reconciled", "component", "workflow", "result", result)
}

roleBinding, mutateFunc := miqtool.WorkflowRoleBinding(cr, r.Scheme)
if result, err := controllerutil.CreateOrUpdate(context.TODO(), r.Client, roleBinding, mutateFunc); err != nil {
return err
} else if result != controllerutil.OperationResultNone {
logger.Info("RoleBinding has been reconciled", "component", "workflow", "result", result)
}

serviceAccount, mutateFunc := miqtool.WorkflowServiceAccount(cr, r.Scheme)
if result, err := controllerutil.CreateOrUpdate(context.TODO(), r.Client, serviceAccount, mutateFunc); err != nil {
return err
} else if result != controllerutil.OperationResultNone {
logger.Info("ServiceAccount has been reconciled", "component", "workflow", "result", result)
}

return nil
}

0 comments on commit 8c2869c

Please sign in to comment.