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