Skip to content

Commit 6c9ec78

Browse files
Merge pull request #51 from dhoover103/main
adding reconcilers for a service account
2 parents e805a47 + 2dc5893 commit 6c9ec78

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

config/rbac/role.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rules:
2020
- ""
2121
resources:
2222
- pods
23+
- serviceaccounts
2324
- services
2425
verbs:
2526
- create

internal/controller/doclingserve_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type DoclingServeReconciler struct {
4545
// +kubebuilder:rbac:groups=docling.github.io,resources=doclingserves/status,verbs=get;update;patch
4646
// +kubebuilder:rbac:groups=docling.github.io,resources=doclingserves/finalizers,verbs=update
4747
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
48-
// +kubebuilder:rbac:groups=core,resources=pods;services,verbs=update;create;get;list;watch
48+
// +kubebuilder:rbac:groups=core,resources=pods;services;serviceaccounts,verbs=update;create;get;list;watch
4949
// +kubebuilder:rbac:groups=route.openshift.io,resources=routes;routes/custom-host,verbs=*
5050

5151
// Reconcile is part of the main kubernetes reconciliation loop which aims to
@@ -73,6 +73,7 @@ func (r *DoclingServeReconciler) Reconcile(ctx context.Context, req ctrl.Request
7373
}
7474

7575
resourceReconcilers := []reconcilers.Reconciler{
76+
reconcilers.NewServiceAccountReconciler(r.Client, r.Scheme),
7677
reconcilers.NewDeploymentReconciler(r.Client, r.Scheme),
7778
reconcilers.NewServiceReconciler(r.Client, r.Scheme),
7879
reconcilers.NewRouteReconciler(r.Client, r.Scheme),

internal/reconcilers/deployment.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (r *DeploymentReconciler) Reconcile(ctx context.Context, doclingServe *v1al
4646
Labels: labels,
4747
},
4848
Spec: corev1.PodSpec{
49+
ServiceAccountName: serviceAccountName,
4950
Containers: []corev1.Container{
5051
{
5152
Image: doclingServe.Spec.APIServer.Image,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package reconcilers
2+
3+
import (
4+
"context"
5+
6+
"github.io/opdev/docling-operator/api/v1alpha1"
7+
corev1 "k8s.io/api/core/v1"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
ctrl "sigs.k8s.io/controller-runtime"
11+
"sigs.k8s.io/controller-runtime/pkg/client"
12+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
13+
logf "sigs.k8s.io/controller-runtime/pkg/log"
14+
)
15+
16+
type ServiceAccountReconciler struct {
17+
client.Client
18+
Scheme *runtime.Scheme
19+
}
20+
21+
const serviceAccountName = "docling-serve"
22+
23+
func NewServiceAccountReconciler(client client.Client, scheme *runtime.Scheme) *ServiceAccountReconciler {
24+
return &ServiceAccountReconciler{
25+
Client: client,
26+
Scheme: scheme,
27+
}
28+
}
29+
30+
func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, doclingServe *v1alpha1.DoclingServe) (bool, error) {
31+
log := logf.FromContext(ctx)
32+
serviceAccount := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: serviceAccountName, Namespace: doclingServe.Namespace}}
33+
_, err := controllerutil.CreateOrUpdate(ctx, r.Client, serviceAccount, func() error {
34+
serviceAccount.Labels = labelsForDocling(doclingServe.Name)
35+
_ = ctrl.SetControllerReference(doclingServe, serviceAccount, r.Scheme)
36+
return nil
37+
})
38+
if err != nil {
39+
log.Error(err, "Error creating ServiceAccount", "ServiceAccount.Namespace", serviceAccount.Namespace, "ServiceAccount.Name", serviceAccount.Name)
40+
return true, err
41+
}
42+
43+
log.Info("Successfully created ServiceAccount", "ServiceAccount.Namespace", serviceAccount.Namespace, "ServiceAccount.Name", serviceAccount.Name)
44+
return false, nil
45+
}

0 commit comments

Comments
 (0)