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