[MARIADB] Update mariadb-operator and templates to 0.27.0
[oom.git] / kubernetes / common / common / templates / _mariadb.tpl
1 {{/*
2 # Copyright © 2019 Orange
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 */}}
16
17 {{/*
18   UID of mariadb root password
19 */}}
20 {{- define "common.mariadb.secret.rootPassUID" -}}
21   {{- printf "db-root-password" }}
22 {{- end -}}
23
24 {{/*
25   Name of mariadb secret
26 */}}
27 {{- define "common.mariadb.secret._secretName" -}}
28   {{- $global := .dot }}
29   {{- $chartName := tpl .chartName $global -}}
30   {{- include "common.secret.genName" (dict "global" $global "uid" (include .uidTemplate $global) "chartName" $chartName) }}
31 {{- end -}}
32
33 {{/*
34   Name of mariadb root password secret
35 */}}
36 {{- define "common.mariadb.secret.rootPassSecretName" -}}
37   {{- include "common.mariadb.secret._secretName" (set . "uidTemplate" "common.mariadb.secret.rootPassUID") }}
38 {{- end -}}
39
40 {{/*
41   UID of mariadb user credentials
42 */}}
43 {{- define "common.mariadb.secret.userCredentialsUID" -}}
44   {{- printf "db-user-credentials" }}
45 {{- end -}}
46
47 {{/*
48   UID of mariadb backup credentials
49 */}}
50 {{- define "common.mariadb.secret.backupCredentialsUID" -}}
51   {{- printf "db-backup-credentials" }}
52 {{- end -}}
53
54 {{/*
55   Name of mariadb user credentials secret
56 */}}
57 {{- define "common.mariadb.secret.userCredentialsSecretName" -}}
58   {{- include "common.mariadb.secret._secretName" (set . "uidTemplate" "common.mariadb.secret.userCredentialsUID") }}
59 {{- end -}}
60
61 {{/*
62   Choose the name of the mariadb app label to use.
63 */}}
64 {{- define "common.mariadbAppName" -}}
65   {{- if .Values.global.mariadbGalera.localCluster -}}
66     {{- index .Values "mariadb-galera" "nameOverride" -}}
67   {{- else -}}
68     {{- .Values.global.mariadbGalera.nameOverride -}}
69   {{- end -}}
70 {{- end -}}
71
72 {{/*
73   Choose the name of the mariadb service to use.
74 */}}
75 {{- define "common.mariadbService" -}}
76   {{- if .Values.global.mariadbGalera.localCluster -}}
77   {{-   if and .Values.global.mariadbGalera.useOperator  (index .Values "mariadb-galera" "mariadbOperator" "galera" "enabled") }}
78     {{- printf "%s-primary" (index .Values "mariadb-galera" "nameOverride") -}}
79   {{-   else }}
80     {{- index .Values "mariadb-galera" "nameOverride" -}}
81   {{-   end }}
82   {{- else -}}
83   {{-   if .Values.global.mariadbGalera.useOperator }}
84     {{- printf "%s-primary" (.Values.global.mariadbGalera.service) }}
85   {{-   else }}
86     {{- .Values.global.mariadbGalera.service -}}
87   {{-   end }}
88   {{- end -}}
89 {{- end -}}
90
91 {{/*
92   Choose the value of mariadb port to use.
93 */}}
94 {{- define "common.mariadbPort" -}}
95   {{- if .Values.global.mariadbGalera.localCluster -}}
96     {{- index .Values "mariadb-galera" "service" "internalPort" -}}
97   {{- else -}}
98     {{- .Values.global.mariadbGalera.internalPort -}}
99   {{- end -}}
100 {{- end -}}
101
102 {{/*
103   Choose the value of secret to retrieve user value.
104 */}}
105 {{- define "common.mariadbSecret" -}}
106   {{- if .Values.global.mariadbGalera.localCluster -}}
107     {{ printf "%s-%s-db-user-credentials" (include "common.fullname" .) (index .Values "mariadb-galera" "nameOverride") -}}
108   {{- else -}}
109     {{ printf "%s-%s-%s" ( include "common.release" .) (index .Values "mariadb-init" "nameOverride") (index .Values "mariadb-init" "config" "mysqlDatabase" ) -}}
110   {{- end -}}
111 {{- end -}}
112
113 {{/*
114   Choose the value of secret param to retrieve user value.
115 */}}
116 {{- define "common.mariadbSecretParam" -}}
117   {{ printf "password" -}}
118 {{- end -}}
119
120 {{/*
121   Create MariaDB Database via mariadb-operator
122 */}}
123 {{- define "common.mariadbOpDatabase" -}}
124 {{- $dot := default . .dot -}}
125 {{- $dbname := (required "'dbame' param, is required." .dbname) -}}
126 {{- $dbinst := (required "'dbinst' param, is required." .dbinst) -}}
127 ---
128 apiVersion: k8s.mariadb.com/v1alpha1
129 kind: Database
130 metadata:
131   name: {{ $dbinst }}-{{ $dbname }}
132 spec:
133   name: {{ $dbname }}
134   mariaDbRef:
135     name: {{ $dbinst }}
136   characterSet: utf8
137   collate: utf8_general_ci
138   retryInterval: 5s
139 {{- end -}}
140
141 {{/*
142   Create MariaDB User via mariadb-operator
143 */}}
144 {{- define "common.mariadbOpUser" -}}
145 {{- $dot := default . .dot -}}
146 {{- $dbuser := (required "'dbuser' param, is required." .dbuser) -}}
147 {{- $dbinst := (required "'dbinst' param, is required." .dbinst) -}}
148 {{- $dbsecret := (required "'dbsecret' param, is required." .dbsecret) -}}
149 ---
150 apiVersion: k8s.mariadb.com/v1alpha1
151 kind: User
152 metadata:
153   name: {{ $dbinst }}-{{ $dbuser }}
154 spec:
155   name: {{ $dbuser }}
156   mariaDbRef:
157     name: {{ $dbinst }}
158     waitForIt: true
159   passwordSecretKeyRef:
160     name: {{ $dbsecret }}
161     key: password
162   # This field is immutable and defaults to 10
163   maxUserConnections: 100
164   retryInterval: 5s
165 {{- end -}}
166
167 {{/*
168   Grant rights to a MariaDB User via mariadb-operator
169 */}}
170 {{- define "common.mariadbOpGrants" -}}
171 {{- $dot := default . .dot -}}
172 {{- $dbuser := (required "'dbuser' param, is required." .dbuser) -}}
173 {{- $dbname := (required "'dbame' param, is required." .dbname) -}}
174 {{- $dbinst := (required "'dbinst' param, is required." .dbinst) -}}
175 ---
176 apiVersion: k8s.mariadb.com/v1alpha1
177 kind: Grant
178 metadata:
179   name: {{ $dbuser }}-{{ $dbname }}-{{ $dbinst }}
180 spec:
181   mariaDbRef:
182     name: {{ $dbinst }}
183     waitForIt: true
184   privileges:
185     - "ALL"
186   database: {{ $dbname }}
187   table: "*"
188   username: {{ $dbuser }}
189   retryInterval: 5s
190   grantOption: true
191 {{- end -}}
192
193 {{/*
194   MariaDB Backup via mariadb-operator
195 */}}
196 {{- define "common.mariadbOpBackup" -}}
197 {{- $dot := default . .dot -}}
198 {{- $dbinst := include "common.name" $dot -}}
199 {{- $name := default $dbinst $dot.Values.backup.nameOverride -}}
200 ---
201 apiVersion: k8s.mariadb.com/v1alpha1
202 kind: Backup
203 metadata:
204   name: {{ $name }}
205 spec:
206   inheritMetadata:
207     labels:
208       sidecar.istio.io/inject: 'false'
209   backoffLimit: 5
210   logLevel: info
211   mariaDbRef:
212     name: {{ $dbinst }}
213     waitForIt: true
214   schedule:
215     cron: {{ $dot.Values.backup.cron }}
216     suspend: false
217   maxRetention: {{ $dot.Values.backup.maxRetention }}
218   storage:
219     {{- if eq $dot.Values.backup.storageType "PVC" }}
220     persistentVolumeClaim:
221       resources:
222         requests:
223           storage: {{ $dot.Values.backup.persistence.size }}
224       {{- if $dot.Values.mariadbOperator.storageClassName }}
225       storageClassName: {{ $dot.Values.mariadbOperator.storageClassName }}
226       {{- end }}
227       accessModes:
228         - {{ $dot.Values.backup.persistence.accessMode }}
229     {{- end }}
230     {{- if eq $dot.Values.backup.storageType "S3" }}
231     s3: {{- include "common.tplValue" ( dict "value" .Values.backup.s3 "context" $) | nindent 6 }}
232     {{- end }}
233     {{- if eq $dot.Values.backup.storageType "volume" }}
234     volume: {{- include "common.tplValue" ( dict "value" .Values.backup.volume "context" $) | nindent 6 }}
235     {{- end }}
236   resources:
237     requests:
238       cpu: "100m"
239       memory: "100Mi"
240     limits:
241       cpu: "300m"
242       memory: "500Mi"
243 {{- end -}}
244
245 {{/*
246   Create a MariaDB instance via mariadb-operator
247 */}}
248 {{- define "common.mariadbOpInstance" -}}
249 {{- $dot := default . .dot -}}
250 {{- $global := $dot.Values.global -}}
251 {{- $dbinst := include "common.name" $dot -}}
252 {{- $dbrootsecret := tpl (default (include "common.mariadb.secret.rootPassSecretName" (dict "dot" $dot "chartName" "")) $dot.Values.rootUser.externalSecret) $dot -}}
253 {{- $dbusersecret := tpl (default (include "common.mariadb.secret.userCredentialsSecretName" (dict "dot" $dot "chartName" "")) $dot.Values.db.externalSecret) $dot -}}
254 ---
255 apiVersion: k8s.mariadb.com/v1alpha1
256 kind: MariaDB
257 metadata:
258   name: {{ $dbinst }}
259 spec:
260   podSecurityContext:
261     runAsUser: 10001
262     runAsGroup: 10001
263     fsGroup: 10001
264   inheritMetadata:
265     {{ if .Values.podAnnotations -}}
266     annotations: {{ toYaml .Values.podAnnotations | nindent 6 }}
267     {{- end }}
268     labels:
269       # temporarily test mariaDB without sidecar (fix initial Job, Backup and Metrics)
270       # will be obsolete with "native-sidecars" feature in K8S and Istio
271       sidecar.istio.io/inject: "false"
272       app: {{ $dbinst }}
273       version: {{ .Values.mariadbOperator.appVersion }}
274   rootPasswordSecretKeyRef:
275     name: {{ $dbrootsecret }}
276     key: password
277   image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ .Values.mariadbOperator.image }}:{{ $dot.Values.mariadbOperator.appVersion }}
278   imagePullPolicy: IfNotPresent
279   {{- include "common.imagePullSecrets" . | nindent 2 }}
280   port: 3306
281   replicas: {{ $dot.Values.replicaCount }}
282   {{- if $dot.Values.mariadbOperator.galera.enabled }}
283   galera:
284     enabled: true
285     sst: mariabackup
286     replicaThreads: 1
287     agent:
288       image: {{ include "repositoryGenerator.githubContainerRegistry" . }}/{{ .Values.mariadbOperator.galera.agentImage }}:{{ $dot.Values.mariadbOperator.galera.agentVersion }}
289       imagePullPolicy: IfNotPresent
290       port: 5555
291       kubernetesAuth:
292         enabled: true
293         authDelegatorRoleName: {{ $dbinst }}-auth
294       gracefulShutdownTimeout: 5s
295     primary:
296       automaticFailover: true
297       podIndex: 0
298     recovery:
299       enabled: true
300       clusterHealthyTimeout: 30s
301       clusterBootstrapTimeout: 10m0s
302       minClusterSize: 50%
303       podRecoveryTimeout: 3m0s
304       podSyncTimeout: 3m0s
305     initContainer:
306       image: {{ include "repositoryGenerator.githubContainerRegistry" . }}/{{ $dot.Values.mariadbOperator.galera.initImage }}:{{ $dot.Values.mariadbOperator.galera.initVersion }}
307       imagePullPolicy: IfNotPresent
308     config:
309       reuseStorageVolume: false
310       volumeClaimTemplate:
311         {{- if .Values.mariadbOperator.persistence.storageClassName }}
312         storageClassName: {{ .Values.mariadbOperator.persistence.storageClassName }}
313         {{- end }}
314         resources:
315           requests:
316             storage: 50Mi
317         accessModes:
318           - ReadWriteOnce
319   {{- end }}
320   livenessProbe:
321     exec:
322       command:
323         - bash
324         - '-c'
325         - mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" -e "SELECT 1;"
326     initialDelaySeconds: 20
327     periodSeconds: 10
328     timeoutSeconds: 5
329   readinessProbe:
330     exec:
331       command:
332         - bash
333         - '-c'
334         - mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" -e "SELECT 1;"
335     initialDelaySeconds: 20
336     periodSeconds: 10
337     timeoutSeconds: 5
338   {{- if default false $dot.Values.global.metrics.enabled }}
339   metrics:
340     enabled: true
341   {{- end }}
342   affinity:
343     podAntiAffinity:
344       requiredDuringSchedulingIgnoredDuringExecution:
345         - topologyKey: kubernetes.io/hostname
346   tolerations:
347     - key: k8s.mariadb.com/ha
348       operator: Exists
349       effect: NoSchedule
350   podDisruptionBudget:
351     maxUnavailable: 50%
352   updateStrategy:
353     type: RollingUpdate
354
355   myCnfConfigMapKeyRef:
356     key: my.cnf
357     name: {{ printf "%s-configuration" (include "common.fullname" $dot) }}
358   resources: {{ include "common.resources" . | nindent 4 }}
359   storage:
360     {{- if $dot.Values.mariadbOperator.persistence.storageClassName }}
361     storageClassName: {{ $dot.Values.mariadbOperator.persistence.storageClassName }}
362     {{- end }}
363     size: {{ $dot.Values.mariadbOperator.persistence.size | quote }}
364 {{-  if $dot.Values.db.user }}
365 {{ include "common.mariadbOpUser" (dict "dot" . "dbuser" $dot.Values.db.user "dbinst" $dbinst "dbsecret" $dbusersecret) }}
366 {{-  end }}
367 {{-  if $dot.Values.db.name }}
368 {{ include "common.mariadbOpDatabase" (dict "dot" . "dbname" $dot.Values.db.name "dbinst" $dbinst) }}
369 {{-  end }}
370 {{-  if and $dot.Values.db.user $dot.Values.db.name }}
371 {{ include "common.mariadbOpGrants" (dict "dot" . "dbuser" $dot.Values.db.user "dbname" $dot.Values.db.name "dbinst" $dbinst) }}
372 {{-  end }}
373 {{- end -}}