@@ -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
5458func (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