Skip to content

Commit 28656a5

Browse files
pengzhoumlPeng Zhou
andauthored
MLE-21222: Support to add ServiceAccountName (#86)
* MLE-21222: Support to add ServiceAccountName * add description in manifests * Fix Service Account Not Assigned Issue * update service account implementation by review * add delete logic after test is done * add debug information for minikube version * fix the missing RBAC --------- Co-authored-by: Peng Zhou <[email protected]>
1 parent ed9ec47 commit 28656a5

12 files changed

+82
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ endif
157157

158158
.PHONY: e2e-setup-minikube
159159
e2e-setup-minikube: kustomize controller-gen build docker-build
160+
minikube version
160161
minikube delete || true
161162
minikube start --driver=docker --kubernetes-version=$(E2E_KUBERNETES_VERSION) --memory=8192 --cpus=2
162163
minikube addons enable ingress

api/v1/marklogiccluster_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ type MarklogicClusterSpec struct {
4242
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
4343

4444
Auth *AdminAuth `json:"auth,omitempty"`
45+
// +kubebuilder:default:="marklogic-workload"
46+
// +kubebuilder:validation:XValidation:rule="self == oldSelf", message="ServiceAccountName can not be changed"
47+
// The name of the service account to assigned to the MarkLogic pods
48+
ServiceAccountName string `json:"serviceAccountName,omitempty"`
4549
// +kubebuilder:default:={enabled: true, size: "10Gi"}
4650
Persistence *Persistence `json:"persistence,omitempty"`
4751
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`

api/v1/marklogicgroup_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type MarklogicGroupSpec struct {
3838
ImagePullPolicy string `json:"imagePullPolicy,omitempty"`
3939
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
4040
Auth *AdminAuth `json:"auth,omitempty"`
41+
ServiceAccountName string `json:"serviceAccountName,omitempty"`
4142
Persistence *Persistence `json:"persistence,omitempty"`
4243
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
4344
TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"`

config/crd/bases/marklogic.progress.com_marklogicclusters.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10814,6 +10814,14 @@ spec:
1081410814
type: string
1081510815
type: object
1081610816
type: object
10817+
serviceAccountName:
10818+
default: marklogic-workload
10819+
description: The name of the service account to assigned to the MarkLogic
10820+
pods
10821+
type: string
10822+
x-kubernetes-validations:
10823+
- message: ServiceAccountName can not be changed
10824+
rule: self == oldSelf
1081710825
terminationGracePeriodSeconds:
1081810826
format: int64
1081910827
type: integer

config/crd/bases/marklogic.progress.com_marklogicgroups.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,6 +4557,8 @@ spec:
45574557
a service
45584558
type: string
45594559
type: object
4560+
serviceAccountName:
4561+
type: string
45604562
terminationGracePeriodSeconds:
45614563
format: int64
45624564
type: integer

config/rbac/role.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rules:
1010
- configmaps
1111
- pods
1212
- secrets
13+
- serviceaccounts
1314
- services
1415
verbs:
1516
- create

internal/controller/marklogiccluster_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type MarklogicClusterReconciler struct {
4444
//+kubebuilder:rbac:groups=marklogic.progress.com,resources=marklogicclusters,verbs=get;list;watch;create;update;patch;delete
4545
//+kubebuilder:rbac:groups=marklogic.progress.com,resources=marklogicclusters/status,verbs=get;update;patch
4646
//+kubebuilder:rbac:groups=marklogic.progress.com,resources=marklogicclusters/finalizers,verbs=update
47+
//+kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs=get;list;watch;create;update;patch;delete
4748

4849
// Reconcile is part of the main kubernetes reconciliation loop which aims to
4950
// move the current state of the cluster closer to the desired state.

pkg/k8sutil/handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func (oc *OperatorContext) ReconsileMarklogicGroupHandler() (reconcile.Result, e
3030
func (cc *ClusterContext) ReconsileMarklogicClusterHandler() (reconcile.Result, error) {
3131
SetCommonAnnotations(cc.MarklogicCluster.GetAnnotations())
3232
SetCommonLabels(cc.MarklogicCluster.GetLabels())
33+
if result := cc.ReconcileServiceAccount(); result.Completed() {
34+
return result.Output()
35+
}
3336
if result := cc.ReconcileSecret(); result.Completed() {
3437
return result.Output()
3538
}

pkg/k8sutil/marklogicServer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
type MarkLogicGroupParameters struct {
1919
Replicas *int32
2020
Name string
21+
ServiceAccountName string
2122
GroupConfig *marklogicv1.GroupConfig
2223
Image string
2324
ImagePullPolicy string
@@ -52,6 +53,7 @@ type MarkLogicClusterParameters struct {
5253
Auth *marklogicv1.AdminAuth
5354
Replicas *int32
5455
Name string
56+
ServiceAccountName string
5557
Image string
5658
ImagePullPolicy string
5759
ImagePullSecrets []corev1.LocalObjectReference
@@ -109,6 +111,7 @@ func GenerateMarkLogicGroupDef(cr *marklogicv1.MarklogicCluster, index int, para
109111
Name: params.Name,
110112
GroupConfig: params.GroupConfig,
111113
Auth: params.Auth,
114+
ServiceAccountName: params.ServiceAccountName,
112115
Image: params.Image,
113116
ImagePullSecrets: params.ImagePullSecrets,
114117
License: params.License,
@@ -208,6 +211,7 @@ func generateMarkLogicClusterParams(cr *marklogicv1.MarklogicCluster) *MarkLogic
208211
Image: cr.Spec.Image,
209212
ImagePullPolicy: cr.Spec.ImagePullPolicy,
210213
ImagePullSecrets: cr.Spec.ImagePullSecrets,
214+
ServiceAccountName: cr.Spec.ServiceAccountName,
211215
ClusterDomain: cr.Spec.ClusterDomain,
212216
Persistence: cr.Spec.Persistence,
213217
Affinity: cr.Spec.Affinity,
@@ -228,6 +232,7 @@ func generateMarkLogicClusterParams(cr *marklogicv1.MarklogicCluster) *MarkLogic
228232
AdditionalVolumeMounts: cr.Spec.AdditionalVolumeMounts,
229233
AdditionalVolumeClaimTemplates: cr.Spec.AdditionalVolumeClaimTemplates,
230234
}
235+
231236
if cr.Spec.HAProxy == nil || cr.Spec.HAProxy.PathBasedRouting == nil || !cr.Spec.HAProxy.Enabled || !*cr.Spec.HAProxy.PathBasedRouting {
232237
markLogicClusterParameters.PathBasedRouting = false
233238
} else {
@@ -247,6 +252,7 @@ func generateMarkLogicGroupParams(cr *marklogicv1.MarklogicCluster, index int, c
247252
ImagePullPolicy: clusterParams.ImagePullPolicy,
248253
ImagePullSecrets: clusterParams.ImagePullSecrets,
249254
Auth: clusterParams.Auth,
255+
ServiceAccountName: clusterParams.ServiceAccountName,
250256
License: clusterParams.License,
251257
Persistence: clusterParams.Persistence,
252258
TerminationGracePeriodSeconds: clusterParams.TerminationGracePeriodSeconds,

pkg/k8sutil/serviceaccount.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package k8sutil
2+
3+
import (
4+
"github.com/marklogic/marklogic-operator-kubernetes/pkg/result"
5+
corev1 "k8s.io/api/core/v1"
6+
apierrors "k8s.io/apimachinery/pkg/api/errors"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"k8s.io/apimachinery/pkg/types"
9+
)
10+
11+
func (cc *ClusterContext) ReconcileServiceAccount() result.ReconcileResult {
12+
logger := cc.ReqLogger
13+
cr := cc.MarklogicCluster
14+
namespace := cr.Namespace
15+
saName := cr.Spec.ServiceAccountName
16+
namespacedName := types.NamespacedName{Name: saName, Namespace: namespace}
17+
sa := &corev1.ServiceAccount{}
18+
logger.Info("Reconciling ServiceAccount", "namespace", namespacedName.Namespace, "name", namespacedName.Name)
19+
err := cc.Client.Get(cc.Ctx, namespacedName, sa)
20+
if err != nil {
21+
if apierrors.IsNotFound(err) {
22+
logger.Info("ServiceAccount not found, creating a new one", "namespace", namespacedName.Namespace, "name", namespacedName.Name)
23+
saDef := generateServiceAccountDef(namespacedName)
24+
err = cc.Client.Create(cc.Ctx, saDef)
25+
if err != nil {
26+
logger.Error(err, "Failed to create service account", "namespace", namespacedName.Namespace, "name", namespacedName.Name)
27+
}
28+
} else {
29+
logger.Error(err, "Failed to get ServiceAccount", "namespace", namespacedName.Namespace, "name", namespacedName.Name)
30+
return result.Error(err)
31+
}
32+
} else {
33+
logger.Info("ServiceAccount already exists")
34+
}
35+
36+
return result.Continue()
37+
}
38+
39+
func generateServiceAccountDef(namespacedName types.NamespacedName) *corev1.ServiceAccount {
40+
serviceAccount := &corev1.ServiceAccount{
41+
ObjectMeta: metav1.ObjectMeta{
42+
Name: namespacedName.Name,
43+
Namespace: namespacedName.Namespace,
44+
},
45+
}
46+
return serviceAccount
47+
}

0 commit comments

Comments
 (0)