[MARIADB] Update mariadb template to the 0.21.0 Operator
[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     {{- index .Values "mariadb-galera" "nameOverride" -}}
78   {{- else -}}
79     {{- .Values.global.mariadbGalera.service -}}
80   {{- end -}}
81 {{- end -}}
82
83 {{/*
84   Choose the value of mariadb port to use.
85 */}}
86 {{- define "common.mariadbPort" -}}
87   {{- if .Values.global.mariadbGalera.localCluster -}}
88     {{- index .Values "mariadb-galera" "service" "internalPort" -}}
89   {{- else -}}
90     {{- .Values.global.mariadbGalera.internalPort -}}
91   {{- end -}}
92 {{- end -}}
93
94 {{/*
95   Choose the value of secret to retrieve user value.
96 */}}
97 {{- define "common.mariadbSecret" -}}
98   {{- if .Values.global.mariadbGalera.localCluster -}}
99     {{ printf "%s-%s-db-user-credentials" (include "common.fullname" .) (index .Values "mariadb-galera" "nameOverride") -}}
100   {{- else -}}
101     {{ printf "%s-%s-%s" ( include "common.release" .) (index .Values "mariadb-init" "nameOverride") (index .Values "mariadb-init" "config" "mysqlDatabase" ) -}}
102   {{- end -}}
103 {{- end -}}
104
105 {{/*
106   Choose the value of secret param to retrieve user value.
107 */}}
108 {{- define "common.mariadbSecretParam" -}}
109   {{ printf "password" -}}
110 {{- end -}}
111
112 {{/*
113   Create MariDB Database via mariadb-operator
114 */}}
115 {{- define "common.mariadbOpDatabase" -}}
116 {{- $dot := default . .dot -}}
117 {{- $dbname := (required "'dbame' param, is required." .dbname) -}}
118 {{- $dbinst := (required "'dbinst' param, is required." .dbinst) -}}
119 ---
120 apiVersion: mariadb.mmontes.io/v1alpha1
121 kind: Database
122 metadata:
123   name: {{ $dbname }}
124 spec:
125   mariaDbRef:
126     name: {{ $dbinst }}
127   characterSet: utf8
128   collate: utf8_general_ci
129   retryInterval: 5s
130 {{- end -}}
131
132 {{/*
133   Create MariaDB User via mariadb-operator
134 */}}
135 {{- define "common.mariadbOpUser" -}}
136 {{- $dot := default . .dot -}}
137 {{- $dbuser := (required "'dbuser' param, is required." .dbuser) -}}
138 {{- $dbinst := (required "'dbinst' param, is required." .dbinst) -}}
139 {{- $dbsecret := (required "'dbsecret' param, is required." .dbsecret) -}}
140 ---
141 apiVersion: mariadb.mmontes.io/v1alpha1
142 kind: User
143 metadata:
144   name: {{ $dbuser }}
145 spec:
146   # If you want the user to be created with a different name than the resource name
147   # name: user-custom
148   mariaDbRef:
149     name: {{ $dbinst }}
150   passwordSecretKeyRef:
151     name: {{ $dbsecret }}
152     key: password
153   # This field is immutable and defaults to 10
154   maxUserConnections: 100
155   retryInterval: 5s
156 {{- end -}}
157
158 {{/*
159   Grant rights to a MariaDB User via mariadb-operator
160 */}}
161 {{- define "common.mariadbOpGrants" -}}
162 {{- $dot := default . .dot -}}
163 {{- $dbuser := (required "'dbuser' param, is required." .dbuser) -}}
164 {{- $dbname := (required "'dbame' param, is required." .dbname) -}}
165 {{- $dbinst := (required "'dbinst' param, is required." .dbinst) -}}
166 ---
167 apiVersion: mariadb.mmontes.io/v1alpha1
168 kind: Grant
169 metadata:
170   name: {{ $dbuser }}-{{ $dbname }}-{{ $dbinst }}
171 spec:
172   mariaDbRef:
173     name: {{ $dbinst }}
174   privileges:
175     - "ALL"
176   database: {{ $dbname }}
177   table: "*"
178   username: {{ $dbuser }}
179   retryInterval: 5s
180   grantOption: true
181 {{- end -}}
182
183 {{/*
184   MariaDB Backup via mariadb-operator
185 */}}
186 {{- define "common.mariadbOpBackup" -}}
187 {{- $dot := default . .dot -}}
188 {{- $dbinst := include "common.name" $dot -}}
189 kind: Backup
190 metadata:
191   name: backup-scheduled
192 spec:
193   mariaDbRef:
194     name: {{ $dbinst }}
195   schedule:
196     cron: "*/1 * * * *"
197     suspend: false
198   maxRetentionDays: 30
199   storage:
200     persistentVolumeClaim:
201       resources:
202         requests:
203           storage: 100Mi
204       {{- if .Values.mariadbOperator.storageClassName }}
205       storageClassName: {{ .Values.mariadbOperator.storageClassName }}
206       {{- end }}
207       accessModes:
208         - ReadWriteOnce
209   resources:
210     requests:
211       cpu: 100m
212       memory: 128Mi
213     limits:
214       cpu: 300m
215       memory: 512Mi
216 {{- end -}}
217
218 {{/*
219   Create a MariaDB instance via mariadb-operator
220 */}}
221 {{- define "common.mariadbOpInstance" -}}
222 {{- $dot := default . .dot -}}
223 {{- $global := $dot.Values.global -}}
224 {{- $dbinst := include "common.name" $dot -}}
225 {{- $dbrootsecret := tpl (default (include "common.mariadb.secret.rootPassSecretName" (dict "dot" $dot "chartName" "")) $dot.Values.rootUser.externalSecret) $dot -}}
226 {{- $dbusersecret := tpl (default (include "common.mariadb.secret.userCredentialsSecretName" (dict "dot" $dot "chartName" "")) $dot.Values.db.externalSecret) $dot -}}
227 ---
228 apiVersion: mariadb.mmontes.io/v1alpha1
229 kind: MariaDB
230 metadata:
231   name: {{ $dbinst }}
232 spec:
233   podSecurityContext:
234     runAsUser: 10001
235     runAsGroup: 10001
236     fsGroup: 10001
237   inheritMetadata:
238     {{ if .Values.podAnnotations -}}
239     annotations: {{ toYaml .Values.podAnnotations | nindent 6 }}
240     {{- end }}
241     labels:
242       app: {{ $dbinst }}
243       version: {{ .Values.mariadbOperator.appVersion }}
244   rootPasswordSecretKeyRef:
245     name: {{ $dbrootsecret }}
246     key: password
247   image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ .Values.mariadbOperator.image }}:{{ $dot.Values.mariadbOperator.appVersion }}
248   imagePullPolicy: IfNotPresent
249   imagePullSecrets:
250     - name: {{ include "common.namespace" . }}-docker-registry-key
251   port: 3306
252   replicas: {{ $dot.Values.replicaCount }}
253   {{- if $dot.Values.mariadbOperator.galera.enabled }}
254   galera:
255     enabled: true
256     sst: mariabackup
257     replicaThreads: 1
258     agent:
259       image: {{ include "repositoryGenerator.githubContainerRegistry" . }}/{{ .Values.mariadbOperator.galera.agentImage }}:{{ $dot.Values.mariadbOperator.galera.agentVersion }}
260       imagePullPolicy: IfNotPresent
261       port: 5555
262       kubernetesAuth:
263         enabled: true
264         authDelegatorRoleName: {{ $dbinst }}-auth
265       gracefulShutdownTimeout: 5s
266     recovery:
267       enabled: true
268       clusterHealthyTimeout: 5m
269       clusterBootstrapTimeout: 10m
270       podRecoveryTimeout: 5m
271       podSyncTimeout: 10m
272     initContainer:
273       image: {{ include "repositoryGenerator.githubContainerRegistry" . }}/{{ $dot.Values.mariadbOperator.galera.initImage }}:{{ $dot.Values.mariadbOperator.galera.initVersion }}
274       imagePullPolicy: IfNotPresent
275     volumeClaimTemplate:
276       resources:
277         requests:
278           storage: 50Mi
279       accessModes:
280         - ReadWriteOnce
281   {{- end }}
282   livenessProbe:
283     exec:
284       command:
285         - bash
286         - '-c'
287         - mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" -e "SELECT 1;"
288     initialDelaySeconds: 20
289     periodSeconds: 10
290     timeoutSeconds: 5
291   readinessProbe:
292     exec:
293       command:
294         - bash
295         - '-c'
296         - mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" -e "SELECT 1;"
297     initialDelaySeconds: 20
298     periodSeconds: 10
299     timeoutSeconds: 5
300   affinity:
301     podAntiAffinity:
302       requiredDuringSchedulingIgnoredDuringExecution:
303         - topologyKey: kubernetes.io/hostname
304   tolerations:
305     - key: mariadb.mmontes.io/ha
306       operator: Exists
307       effect: NoSchedule
308   podDisruptionBudget:
309     maxUnavailable: 50%
310   updateStrategy:
311     type: RollingUpdate
312   #myCnf: |
313   #  [mysqld]
314   #  bind-address=0.0.0.0
315   #  default_storage_engine=InnoDB
316   #  binlog_format=row
317   #  innodb_autoinc_lock_mode=2
318   #  max_allowed_packet=256M
319   #  lower_case_table_names = 1
320
321   #  ## Character set
322   #  collation_server=utf8_unicode_ci
323   #  init_connect='SET NAMES utf8'
324   #  character_set_server=utf8
325
326   myCnfConfigMapKeyRef:
327     key: my.cnf
328     name: {{ printf "%s-configuration" (include "common.fullname" $dot) }}
329   resources: {{ include "common.resources" . | nindent 4 }}
330   volumeClaimTemplate:
331     {{- if $dot.Values.mariadbOperator.storageClassName }}
332     storageClassName: {{ $dot.Values.mariadbOperator.storageClassName }}
333     {{- end }}
334     resources:
335       requests:
336         storage: {{ $dot.Values.mariadbOperator.persistence.size | quote }}
337     accessModes:
338       - ReadWriteOnce
339 {{-  if $dot.Values.db.user }}
340 {{ include "common.mariadbOpUser" (dict "dot" . "dbuser" $dot.Values.db.user "dbinst" $dbinst "dbsecret" $dbusersecret) }}
341 {{-  end }}
342 {{-  if $dot.Values.db.name }}
343 {{ include "common.mariadbOpDatabase" (dict "dot" . "dbname" $dot.Values.db.name "dbinst" $dbinst) }}
344 {{-  end }}
345 {{-  if and $dot.Values.db.user $dot.Values.db.name }}
346 {{ include "common.mariadbOpGrants" (dict "dot" . "dbuser" $dot.Values.db.user "dbname" $dot.Values.db.name "dbinst" $dbinst) }}
347 {{-  end }}
348 {{- end -}}