[COMMON] Make imagePullSecrets configurable
[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 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: {{ $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: mariadb.mmontes.io/v1alpha1
151 kind: User
152 metadata:
153   name: {{ $dbinst }}-{{ $dbuser }}
154 spec:
155   name: {{ $dbuser }}
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 {{- $name := default $dbinst $dot.Values.backup.nameOverride -}}
198 ---
199 apiVersion: mariadb.mmontes.io/v1alpha1
200 kind: Backup
201 metadata:
202   name: {{ $name }}
203 spec:
204   mariaDbRef:
205     name: {{ $dbinst }}
206   schedule:
207     cron: {{ $dot.Values.backup.cron }}
208     suspend: false
209   maxRetention: {{ $dot.Values.backup.maxRetention }}
210   storage:
211     {{- if eq $dot.Values.backup.storageType "PVC" }}
212     persistentVolumeClaim:
213       resources:
214         requests:
215           storage: {{ $dot.Values.backup.persistence.size }}
216       {{- if $dot.Values.mariadbOperator.storageClassName }}
217       storageClassName: {{ $dot.Values.mariadbOperator.storageClassName }}
218       {{- end }}
219       accessModes:
220         - {{ $dot.Values.backup.persistence.accessMode }}
221     {{- end }}
222     {{- if eq $dot.Values.backup.storageType "S3" }}
223     s3: {{- include "common.tplValue" ( dict "value" .Values.backup.s3 "context" $) | nindent 6 }}
224     {{- end }}
225     {{- if eq $dot.Values.backup.storageType "volume" }}
226     volume: {{- include "common.tplValue" ( dict "value" .Values.backup.volume "context" $) | nindent 6 }}
227     {{- end }}
228   resources:
229     requests:
230       cpu: "100m"
231       memory: "100Mi"
232     limits:
233       cpu: "300m"
234       memory: "500Mi"
235 {{- end -}}
236
237 {{/*
238   Create a MariaDB instance via mariadb-operator
239 */}}
240 {{- define "common.mariadbOpInstance" -}}
241 {{- $dot := default . .dot -}}
242 {{- $global := $dot.Values.global -}}
243 {{- $dbinst := include "common.name" $dot -}}
244 {{- $dbrootsecret := tpl (default (include "common.mariadb.secret.rootPassSecretName" (dict "dot" $dot "chartName" "")) $dot.Values.rootUser.externalSecret) $dot -}}
245 {{- $dbusersecret := tpl (default (include "common.mariadb.secret.userCredentialsSecretName" (dict "dot" $dot "chartName" "")) $dot.Values.db.externalSecret) $dot -}}
246 ---
247 apiVersion: mariadb.mmontes.io/v1alpha1
248 kind: MariaDB
249 metadata:
250   name: {{ $dbinst }}
251 spec:
252   podSecurityContext:
253     runAsUser: 10001
254     runAsGroup: 10001
255     fsGroup: 10001
256   inheritMetadata:
257     {{ if .Values.podAnnotations -}}
258     annotations: {{ toYaml .Values.podAnnotations | nindent 6 }}
259     {{- end }}
260     labels:
261       app: {{ $dbinst }}
262       version: {{ .Values.mariadbOperator.appVersion }}
263   rootPasswordSecretKeyRef:
264     name: {{ $dbrootsecret }}
265     key: password
266   image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ .Values.mariadbOperator.image }}:{{ $dot.Values.mariadbOperator.appVersion }}
267   imagePullPolicy: IfNotPresent
268   {{- include "common.imagePullSecrets" . | nindent 2 }}
269   port: 3306
270   replicas: {{ $dot.Values.replicaCount }}
271   {{- if $dot.Values.mariadbOperator.galera.enabled }}
272   galera:
273     enabled: true
274     sst: mariabackup
275     replicaThreads: 1
276     agent:
277       image: {{ include "repositoryGenerator.githubContainerRegistry" . }}/{{ .Values.mariadbOperator.galera.agentImage }}:{{ $dot.Values.mariadbOperator.galera.agentVersion }}
278       imagePullPolicy: IfNotPresent
279       port: 5555
280       kubernetesAuth:
281         enabled: true
282         authDelegatorRoleName: {{ $dbinst }}-auth
283       gracefulShutdownTimeout: 5s
284     recovery:
285       enabled: true
286       clusterHealthyTimeout: 5m0s
287       clusterBootstrapTimeout: 10m0s
288       podRecoveryTimeout: 5m0s
289       podSyncTimeout: 10m0s
290     initContainer:
291       image: {{ include "repositoryGenerator.githubContainerRegistry" . }}/{{ $dot.Values.mariadbOperator.galera.initImage }}:{{ $dot.Values.mariadbOperator.galera.initVersion }}
292       imagePullPolicy: IfNotPresent
293     volumeClaimTemplate:
294       {{- if .Values.mariadbOperator.storageClassName }}
295       storageClassName: {{ .Values.mariadbOperator.storageClassName }}
296       {{- end }}
297       resources:
298         requests:
299           storage: 50Mi
300       accessModes:
301         - ReadWriteOnce
302   {{- end }}
303   livenessProbe:
304     exec:
305       command:
306         - bash
307         - '-c'
308         - mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" -e "SELECT 1;"
309     initialDelaySeconds: 20
310     periodSeconds: 10
311     timeoutSeconds: 5
312   readinessProbe:
313     exec:
314       command:
315         - bash
316         - '-c'
317         - mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" -e "SELECT 1;"
318     initialDelaySeconds: 20
319     periodSeconds: 10
320     timeoutSeconds: 5
321   {{- if default false .Values.global.metrics.enabled }}
322   metrics:
323     enabled: true
324   {{- end }}
325   affinity:
326     podAntiAffinity:
327       requiredDuringSchedulingIgnoredDuringExecution:
328         - topologyKey: kubernetes.io/hostname
329   tolerations:
330     - key: mariadb.mmontes.io/ha
331       operator: Exists
332       effect: NoSchedule
333   podDisruptionBudget:
334     maxUnavailable: 50%
335   updateStrategy:
336     type: RollingUpdate
337
338   myCnfConfigMapKeyRef:
339     key: my.cnf
340     name: {{ printf "%s-configuration" (include "common.fullname" $dot) }}
341   resources: {{ include "common.resources" . | nindent 4 }}
342   volumeClaimTemplate:
343     {{- if $dot.Values.mariadbOperator.storageClassName }}
344     storageClassName: {{ $dot.Values.mariadbOperator.storageClassName }}
345     {{- end }}
346     resources:
347       requests:
348         storage: {{ $dot.Values.mariadbOperator.persistence.size | quote }}
349     accessModes:
350       - ReadWriteOnce
351 {{-  if $dot.Values.db.user }}
352 {{ include "common.mariadbOpUser" (dict "dot" . "dbuser" $dot.Values.db.user "dbinst" $dbinst "dbsecret" $dbusersecret) }}
353 {{-  end }}
354 {{-  if $dot.Values.db.name }}
355 {{ include "common.mariadbOpDatabase" (dict "dot" . "dbname" $dot.Values.db.name "dbinst" $dbinst) }}
356 {{-  end }}
357 {{-  if and $dot.Values.db.user $dot.Values.db.name }}
358 {{ include "common.mariadbOpGrants" (dict "dot" . "dbuser" $dot.Values.db.user "dbname" $dot.Values.db.name "dbinst" $dbinst) }}
359 {{-  end }}
360 {{- end -}}