Skip to content

Commit 33ba2de

Browse files
committed
Fix permissions for pipeline deploy task
1 parent 2f0005b commit 33ba2de

5 files changed

Lines changed: 101 additions & 3 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
# Role needed for service accounts running the deploy task of the deploy pipeline
3+
4+
apiVersion: rbac.authorization.k8s.io/v1
5+
kind: ClusterRole
6+
metadata:
7+
name: deploy-function
8+
rules:
9+
- apiGroups:
10+
- "serving.knative.dev"
11+
resources:
12+
- services
13+
- routes
14+
verbs:
15+
- create
16+
- delete
17+
- get
18+
- list
19+
- patch
20+
- update
21+
- watch
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRoleBinding
3+
metadata:
4+
name: manager-deploy-function
5+
roleRef:
6+
# we need to bind this to the controller/manager too, otherwise we cannot grant it from the controller
7+
apiGroup: rbac.authorization.k8s.io
8+
kind: ClusterRole
9+
name: deploy-function
10+
subjects:
11+
- kind: ServiceAccount
12+
name: controller-manager
13+
namespace: system

config/rbac/kustomization.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ resources:
2626
- function_editor_role.yaml
2727
- function_viewer_role.yaml
2828

29+
- deploy_function_clusterrole.yaml
30+
- deploy_function_clusterrole_binding.yaml
31+

config/rbac/role.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ rules:
4343
- get
4444
- patch
4545
- update
46+
- apiGroups:
47+
- rbac.authorization.k8s.io
48+
resources:
49+
- rolebindings
50+
verbs:
51+
- create
52+
- delete
53+
- get
54+
- list
55+
- patch
56+
- update
57+
- watch
4658
- apiGroups:
4759
- tekton.dev
4860
resources:

internal/controller/function_controller.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ import (
2424
"github.com/creydr/func-operator/internal/funccli"
2525
"github.com/creydr/func-operator/internal/git"
2626
v1 "k8s.io/api/core/v1"
27+
"k8s.io/apimachinery/pkg/api/equality"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2729
"k8s.io/apimachinery/pkg/runtime"
2830
"k8s.io/apimachinery/pkg/types"
2931
"k8s.io/client-go/tools/record"
32+
"k8s.io/utils/pointer"
3033
ctrl "sigs.k8s.io/controller-runtime"
3134
"sigs.k8s.io/controller-runtime/pkg/client"
3235
logf "sigs.k8s.io/controller-runtime/pkg/log"
3336

34-
v1alpha1 "github.com/creydr/func-operator/api/v1alpha1"
37+
"github.com/creydr/func-operator/api/v1alpha1"
38+
rbacv1 "k8s.io/api/rbac/v1"
3539
apierrors "k8s.io/apimachinery/pkg/api/errors"
3640
)
3741

@@ -46,9 +50,9 @@ type FunctionReconciler struct {
4650
// +kubebuilder:rbac:groups=functions.dev,resources=functions,verbs=get;list;watch;create;update;patch;delete
4751
// +kubebuilder:rbac:groups=functions.dev,resources=functions/status,verbs=get;update;patch
4852
// +kubebuilder:rbac:groups=functions.dev,resources=functions/finalizers,verbs=update
49-
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
50-
// +kubebuilder:rbac:groups="",resources=persistentvolumeclaims,verbs=get;list;watch;create;update;patch;delete
53+
// +kubebuilder:rbac:groups="",resources=secrets;persistentvolumeclaims,verbs=get;list;watch;create;update;patch;delete
5154
// +kubebuilder:rbac:groups=tekton.dev,resources=pipelines;pipelineruns,verbs=get;list;watch;create;update;patch;delete
55+
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=rolebindings,verbs=get;list;watch;create;update;patch;delete
5256

5357
// Reconcile a Function
5458
func (r *FunctionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
@@ -70,6 +74,51 @@ func (r *FunctionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
7074

7175
logger.Info("Reconciling Function", "function", req.NamespacedName)
7276

77+
logger.Info("Create rolebinding for deploy-function role")
78+
expectedRoleBinding := &rbacv1.RoleBinding{
79+
ObjectMeta: metav1.ObjectMeta{
80+
Name: "deploy-function-default",
81+
Namespace: function.Namespace,
82+
OwnerReferences: []metav1.OwnerReference{
83+
{
84+
APIVersion: function.APIVersion,
85+
Kind: function.Kind,
86+
Name: function.Name,
87+
UID: function.UID,
88+
Controller: pointer.BoolPtr(true),
89+
},
90+
},
91+
},
92+
Subjects: []rbacv1.Subject{{
93+
Kind: "ServiceAccount",
94+
Name: "default",
95+
Namespace: function.Namespace,
96+
}},
97+
RoleRef: rbacv1.RoleRef{
98+
Kind: "ClusterRole",
99+
Name: "func-operator-deploy-function",
100+
APIGroup: "rbac.authorization.k8s.io",
101+
},
102+
}
103+
foundRoleBinding := &rbacv1.RoleBinding{}
104+
err = r.Get(ctx, types.NamespacedName{Name: expectedRoleBinding.Name, Namespace: expectedRoleBinding.Namespace}, foundRoleBinding)
105+
if err != nil {
106+
if apierrors.IsNotFound(err) {
107+
err = r.Create(ctx, expectedRoleBinding)
108+
if err != nil {
109+
return ctrl.Result{}, fmt.Errorf("failed to create role binding for deploy-function role: %w", err)
110+
}
111+
}
112+
return ctrl.Result{}, fmt.Errorf("failed to check if deploy-function role binding already exists: %w", err)
113+
} else {
114+
if !equality.Semantic.DeepDerivative(expectedRoleBinding, foundRoleBinding) {
115+
err = r.Update(ctx, foundRoleBinding)
116+
if err != nil {
117+
return ctrl.Result{}, fmt.Errorf("failed to update deploy-function role binding: %w", err)
118+
}
119+
}
120+
}
121+
73122
// clone src code
74123
repo, err := git.NewRepository(ctx, function.Spec.Source.RepositoryURL, "main")
75124
if err != nil {

0 commit comments

Comments
 (0)