Skip to content

Commit 9334acf

Browse files
Romain JACQUETrjacquet31
authored andcommitted
feat: add the chart to instanciate mongodb database with the mongodb
operator for kubernetes (community edition)
1 parent 8f2aaba commit 9334acf

File tree

7 files changed

+332
-0
lines changed

7 files changed

+332
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
# Common backup files
9+
*.swp
10+
*.bak
11+
*.tmp
12+
*.orig
13+
*~
14+
# Various IDEs
15+
.project
16+
.idea/
17+
*.tmproj
18+
.vscode/

charts/mongodb-operated/Chart.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: v2
2+
name: mongodb-operated
3+
description: A Helm chart to instanciate a mongodb from the community operator
4+
annotations:
5+
category: database
6+
type: application
7+
8+
# This is the chart version. This version number should be incremented each time you make changes
9+
# to the chart and its templates, including the app version.
10+
# Versions are expected to follow Semantic Versioning (https://semver.org/)
11+
version: 0.2.0
12+
13+
# This is the version number of the application being deployed. This version number should be
14+
# incremented each time you make changes to the application. Versions are not expected to
15+
# follow Semantic Versioning. They should reflect the version the application is using.
16+
# It is recommended to use it with quotes.
17+
appVersion: "7.0.29"
18+
home: https://github.com/mongodb/mongodb-kubernetes/tree/1.6.0/docs/mongodbcommunityhttps://github.com/mongodb/mongodb-kubernetes/tree/master/docs/mongodbcommunity
19+
keywords:
20+
- mongodb
21+
maintainers:
22+
- name: Kalisio
23+
email: contact@kalisio.com
24+
url: https://kalisio.com
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1. Get the name of mongo instance by running these commands:
2+
kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.metadata.name}" services {{ .Release.Name }}-mongodb-operated-svc
3+
2. Get the connection strings for the users defined:
4+
{{ range .Values.users }}
5+
kubectl get secret {{ $.Release.Name }}-mongodb-operated-{{ .db }}-{{ .name }} -o 'go-template={{ "{{" }} index .data "connectionString.standard" {{ "}}" }}' | base64 -d -w0
6+
{{ end }}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{{/*
2+
Expand the name of the chart.
3+
*/}}
4+
{{- define "mongodb-operated.name" -}}
5+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
6+
{{- end }}
7+
8+
{{/*
9+
Create a default fully qualified app name.
10+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
11+
If release name contains chart name it will be used as a full name.
12+
*/}}
13+
{{- define "mongodb-operated.fullname" -}}
14+
{{- if .Values.fullnameOverride }}
15+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
16+
{{- else }}
17+
{{- $name := default .Chart.Name .Values.nameOverride }}
18+
{{- if contains $name .Release.Name }}
19+
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
20+
{{- else }}
21+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
22+
{{- end }}
23+
{{- end }}
24+
{{- end }}
25+
26+
{{/*
27+
Create chart name and version as used by the chart label.
28+
*/}}
29+
{{- define "mongodb-operated.chart" -}}
30+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
31+
{{- end }}
32+
33+
{{/*
34+
Common labels
35+
*/}}
36+
{{- define "mongodb-operated.labels" -}}
37+
helm.sh/chart: {{ include "mongodb-operated.chart" . }}
38+
{{ include "mongodb-operated.selectorLabels" . }}
39+
{{- if .Chart.AppVersion }}
40+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
41+
{{- end }}
42+
app.kubernetes.io/managed-by: {{ .Release.Service }}
43+
{{- end }}
44+
45+
{{/*
46+
Selector labels
47+
*/}}
48+
{{- define "mongodb-operated.selectorLabels" -}}
49+
app.kubernetes.io/name: {{ include "mongodb-operated.name" . }}
50+
app.kubernetes.io/instance: {{ .Release.Name }}
51+
{{- end }}
52+
53+
{{/*
54+
Create the name of the service account to use
55+
*/}}
56+
{{- define "mongodb-operated.serviceAccountName" -}}
57+
{{- if .Values.serviceAccount.create }}
58+
{{- default (include "mongodb-operated.fullname" .) .Values.serviceAccount.name }}
59+
{{- else }}
60+
{{- default "default" .Values.serviceAccount.name }}
61+
{{- end }}
62+
{{- end }}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
apiVersion: mongodbcommunity.mongodb.com/v1
2+
kind: MongoDBCommunity
3+
metadata:
4+
name: {{ .Release.Name }}-mongodb-operated
5+
spec:
6+
members: {{ .Values.replicaCount }}
7+
type: ReplicaSet
8+
# important: control the version of the mongodb
9+
version: {{ .Values.version | default .Chart.AppVersion }}
10+
security:
11+
authentication:
12+
modes: ["SCRAM"]
13+
users:
14+
{{- with .Values.users -}}
15+
{{ . | toYaml | nindent 4 }}
16+
{{- end}}
17+
statefulSet:
18+
spec:
19+
template:
20+
spec:
21+
{{- with .Values.imagePullSecrets }}
22+
imagePullSecrets:
23+
{{- toYaml . | nindent 12 }}
24+
{{- end }}
25+
containers:
26+
- name: mongod
27+
imagePullPolicy: {{ .Values.image.pullPolicy }}
28+
{{- with .Values.resources }}
29+
resources:
30+
{{- toYaml . | nindent 16 }}
31+
{{- end }}
32+
volumeMounts:
33+
- name: backup-volume-{{ .Release.Name }}
34+
mountPath: /backups
35+
{{- if .Values.initdbScripts }}
36+
- name: init-scripts
37+
mountPath: /docker-entrypoint-initdb.d
38+
{{- end }}
39+
- name: mongodb-agent
40+
imagePullPolicy: {{ .Values.image.pullPolicy }}
41+
{{- with .Values.agent.resources }}
42+
resources:
43+
{{- toYaml . | nindent 16 }}
44+
{{- end }}
45+
volumes:
46+
{{- if .Values.initdbScripts }}
47+
- name: init-scripts
48+
configMap:
49+
name: {{ .Release.Name }}-init-scripts
50+
{{- end }}
51+
volumeClaimTemplates:
52+
- metadata:
53+
name: data-volume-{{ .Release.Name }}
54+
spec:
55+
accessModes: ["ReadWriteOnce"]
56+
storageClassName: {{ .Values.pvc.data.storageClass }}
57+
resources:
58+
requests:
59+
storage: {{ .Values.pvc.data.size }}
60+
- metadata:
61+
name: backup-volume-{{ .Release.Name }}
62+
spec:
63+
accessModes: ["ReadWriteOnce"]
64+
storageClassName: {{ .Values.pvc.backup.storageClass }}
65+
resources:
66+
requests:
67+
storage: {{ .Values.pvc.backup.size }}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# create required Role, RoleBinding and Service account to run the mongo operator instance
2+
# Information from https://wiki.ul2i.fr/fr/kubernetes/operator-mongodb
3+
---
4+
{{- if .Values.serviceAccount.create -}}
5+
# ServiceAccount for database
6+
apiVersion: v1
7+
kind: ServiceAccount
8+
metadata:
9+
name: mongodb-kubernetes-database-pods
10+
labels:
11+
{{- include "mongodb-operated.labels" . | nindent 4 }}
12+
{{- with .Values.serviceAccount.annotations }}
13+
annotations:
14+
{{- toYaml . | nindent 4 }}
15+
{{- end }}
16+
automountServiceAccountToken: {{ .Values.serviceAccount.automount }}
17+
---
18+
# ServiceAccount for mongo pod
19+
apiVersion: v1
20+
kind: ServiceAccount
21+
metadata:
22+
name: mongodb-kubernetes-appdb
23+
labels:
24+
{{- include "mongodb-operated.labels" . | nindent 4 }}
25+
{{- with .Values.serviceAccount.annotations }}
26+
annotations:
27+
{{- toYaml . | nindent 4 }}
28+
{{- end }}
29+
automountServiceAccountToken: {{ .Values.serviceAccount.automount }}
30+
31+
---
32+
# Role to read secret and modify pod
33+
apiVersion: rbac.authorization.k8s.io/v1
34+
kind: Role
35+
metadata:
36+
name: mongodb-kubernetes-{{ .Release.Name }}
37+
rules:
38+
- apiGroups:
39+
- ""
40+
resources:
41+
- secrets
42+
verbs:
43+
- get
44+
- apiGroups:
45+
- ""
46+
resources:
47+
- pods
48+
verbs:
49+
- patch
50+
- delete
51+
- get
52+
53+
---
54+
# Bind role and SA
55+
apiVersion: rbac.authorization.k8s.io/v1
56+
kind: RoleBinding
57+
metadata:
58+
name: mongodb-kubernetes-{{ .Release.Name }}
59+
roleRef:
60+
apiGroup: rbac.authorization.k8s.io
61+
kind: Role
62+
name: mongodb-kubernetes-{{ .Release.Name }}
63+
subjects:
64+
- kind: ServiceAccount
65+
name: mongodb-kubernetes-appdb
66+
{{- end }}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Default values for mongodb-operated.
2+
# This is a YAML-formatted file.
3+
# Declare variables to be passed into your templates.
4+
5+
# number of replicas in the replicaset of mongodb
6+
replicaCount: 1
7+
8+
# overide mongodb version
9+
# ex version: appVersion: "7.0.14"
10+
version: ""
11+
12+
image:
13+
# warning : there is difference with classical helm chart
14+
# repository image is managed by the operator
15+
# tag is manage by appVersion
16+
pullPolicy: IfNotPresent
17+
# special case of ASNR
18+
# suffix of the docker image tag
19+
# see quay.io/repository/mongodb/mongodb-community-server
20+
#type: ""
21+
22+
# This is for the secrets for pulling an image from a private repository more information can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/
23+
imagePullSecrets: []
24+
25+
26+
# This section builds out the service account more information can be found here: https://kubernetes.io/docs/concepts/security/service-accounts/
27+
# currenty, at ASNR, a kyverno policy automatically created the service account
28+
# role and role binding
29+
serviceAccount:
30+
# Specifies whether a service account should be created
31+
create: false
32+
# Automatically mount a ServiceAccount's API credentials?
33+
automount: true
34+
# Annotations to add to the service account
35+
annotations: {}
36+
# The name of the service account to use.
37+
# If not set and create is true, a name is generated using the fullname template
38+
name: ""
39+
40+
resources:
41+
# resources value for the replicas must be adjusted given the environment
42+
# staging, preprod, prod
43+
limits:
44+
cpu: "2"
45+
memory: 2Gi
46+
requests:
47+
cpu: 100m
48+
memory: 200Mi
49+
50+
# configuration for container mongo-agent
51+
agent:
52+
# This sets the pull policy for images.
53+
pullPolicy: IfNotPresent
54+
# resources specific to agent
55+
resources:
56+
limits:
57+
cpu: "1"
58+
memory: 512Mi
59+
requests:
60+
cpu: 50m
61+
memory: 100Mi
62+
63+
# pvc configuration
64+
# specify size and storage class for data volume and backup volume
65+
pvc:
66+
data:
67+
storageClass: ""
68+
size: 10Gi
69+
backup:
70+
storageClass: ""
71+
size: 10Gi
72+
73+
# user configuration
74+
# this fied is used to fill the `spec.users` field from the MongoDBCommunity
75+
# ressource
76+
# the root must be included
77+
# spec: https://github.com/mongodb/mongodb-kubernetes/blob/master/docs/mongodbcommunity/users.md#modify-the-mongodbcommunity-resource
78+
users:
79+
# example
80+
# - name: user
81+
# db: mydb
82+
# secretName: mysecret
83+
# secretKeyPassword: secret-user-password
84+
# roles:
85+
# - name: readWrite
86+
# db: mydb
87+
# - name: clusterMonitor
88+
# db: admin
89+
# scramCredentialsSecretName: scram-user

0 commit comments

Comments
 (0)