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