0afe02ada784c504b740cb5403f18042d5c5e65e
[oom.git] / kubernetes / so / components / so-mariadb / templates / job.yaml
1 {{/*
2 # Copyright © 2017 Amdocs, Bell Canada
3 # Modifications Copyright (C) 2022/23 Nordix Foundation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 */}}
17 {{- if .Values.global.migration.enabled }}
18 apiVersion: batch/v1
19 kind: Job
20 metadata:
21   name: {{ include "common.fullname" . }}-backup
22   namespace: {{ include "common.namespace" . }}
23   labels:
24     app: {{ include "common.name" . }}
25     chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
26     release: {{ include "common.release" . }}
27     heritage: {{ .Release.Service }}
28   annotations:
29     "helm.sh/hook": pre-upgrade,pre-install
30     "helm.sh/hook-weight": "1"
31     "helm.sh/hook-delete-policy": before-hook-creation
32 spec:
33   backoffLimit: 20
34   template:
35     metadata:
36       labels:
37         app: {{ include "common.name" . }}
38         release: {{ include "common.release" . }}
39       name: {{ include "common.name" . }}
40     spec:
41       containers:
42       - name: {{ include "common.fullname" . }}
43         image: {{ include "repositoryGenerator.image.mariadb" . }}
44         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
45         env:
46         - name: DB_HOST
47           value: {{ .Values.global.migration.dbHost }}
48         - name: DB_USER
49           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "db-backup-creds" "key" "login") | indent 10 }}
50         - name: DB_PORT
51           value: "{{ .Values.global.migration.dbPort }}"
52         - name: DB_PASS
53           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "db-backup-creds" "key" "password") | indent 10 }}
54         command:
55         - /bin/bash
56         - -c
57         - |
58           {{- if include "common.onServiceMesh" . }}
59           echo "waiting 15s for istio side cars to be up"; sleep 15s;{{- end }}
60           mysqldump -vv --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} --port=${DB_PORT} --databases --single-transaction --quick --lock-tables=false catalogdb requestdb nfvo cnfm > /var/data/mariadb/backup-`date +%s`.sql
61         volumeMounts:
62         - mountPath: /etc/localtime
63           name: localtime
64           readOnly: true
65         - name: backup-storage
66           mountPath: /var/data/mariadb
67       {{ include "common.waitForJobContainer" . | indent 6 | trim }}
68       serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
69       volumes:
70       - name: localtime
71         hostPath:
72           path: /etc/localtime
73       - name: backup-storage
74         persistentVolumeClaim:
75           claimName: {{ include "common.fullname" . }}-migration
76       imagePullSecrets:
77         - name: "{{ include "common.namespace" . }}-docker-registry-key"
78       restartPolicy: Never
79 ---
80 {{- end }}
81 apiVersion: batch/v1
82 kind: Job
83 metadata:
84   name: {{ include "common.fullname" . }}-config-job
85   namespace: {{ include "common.namespace" . }}
86   labels:
87     app: {{ include "common.name" . }}
88     chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
89     release: {{ include "common.release" . }}
90     heritage: {{ .Release.Service }}
91   annotations:
92     "helm.sh/hook": post-upgrade,post-rollback,post-install
93     "helm.sh/hook-weight": "0"
94     "helm.sh/hook-delete-policy": before-hook-creation
95 spec:
96   backoffLimit: 20
97   template:
98     metadata:
99       labels:
100         app: {{ include "common.name" . }}-job
101         release: {{ include "common.release" . }}
102       name: {{ include "common.name" . }}
103     spec:
104       initContainers:
105       - name: {{ include "common.name" . }}-readiness
106         command:
107         - /app/ready.py
108         args:
109         - --app-name
110         - {{ include "common.mariadbAppName" . }}
111         env:
112         - name: NAMESPACE
113           valueFrom:
114             fieldRef:
115               apiVersion: v1
116               fieldPath: metadata.namespace
117         image: {{ include "repositoryGenerator.image.readiness" . }}
118         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
119       containers:
120       - name: {{ include "common.name" . }}-config
121         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.image }}
122         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
123         command:
124         - /bin/bash
125         - -c
126         - >
127           {{- if include "common.onServiceMesh" . }}
128           echo "waiting 15s for istio side cars to be up"; sleep 15s;{{- end }}
129           mysql() { /usr/bin/mysql  -h ${DB_HOST} -P ${DB_PORT} "$@"; };
130           export -f mysql;
131           mysql --user=root --password=${MYSQL_ROOT_PASSWORD} requestdb -e exit > /dev/null 2>&1 {{ if not .Values.global.migration.enabled }} && echo "Database already initialized!!!" && exit 0 {{ end }};
132           for f in /docker-entrypoint-initdb.d/*; do case "$f" in  *.sh) echo "$0: running $f"; . "$f" ;;  *.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;; *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;  *) echo "$0: ignoring $f" ;;  esac; echo; done;
133           {{- if .Values.global.migration.enabled }}
134           mysql -vv --user=root --password=${MYSQL_ROOT_PASSWORD} < `ls -tr /var/data/mariadb/* | tail -1`;
135           {{- end }}
136         env:
137         - name: DB_HOST
138           value: {{ include "common.mariadbService" . }}
139         - name: DB_PORT
140           value: {{ include "common.mariadbPort" . | quote }}
141         - name: MYSQL_ROOT_PASSWORD
142           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "db-root-pass" "key" "password") | indent 10 }}
143         - name: DB_USER
144           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "db-user-creds" "key" "login") | indent 10 }}
145         - name: DB_PASSWORD
146           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "db-user-creds" "key" "password") | indent 10 }}
147         - name: DB_ADMIN
148           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "db-admin-creds" "key" "login") | indent 10 }}
149         - name: DB_ADMIN_PASSWORD
150           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "db-admin-creds" "key" "password") | indent 10 }}
151         - name: CAMUNDA_DB_USER
152           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "camunda-db-creds" "key" "login") | indent 10 }}
153         - name: CAMUNDA_DB_PASSWORD
154           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "camunda-db-creds" "key" "password") | indent 10 }}
155         - name: REQUEST_DB_USER
156           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "request-db-creds" "key" "login") | indent 10 }}
157         - name: REQUEST_DB_PASSWORD
158           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "request-db-creds" "key" "password") | indent 10 }}
159         - name: CATALOG_DB_USER
160           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "catalog-db-creds" "key" "login") | indent 10 }}
161         - name: CATALOG_DB_PASSWORD
162           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "catalog-db-creds" "key" "password") | indent 10 }}
163         - name: NFVO_DB_USER
164           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "nfvo-db-creds" "key" "login") | indent 10 }}
165         - name: NFVO_DB_PASSWORD
166           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "nfvo-db-creds" "key" "password") | indent 10 }}
167         - name: CNFM_DB_USER
168           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "cnfm-db-creds" "key" "login") | indent 10 }}
169         - name: CNFM_DB_PASSWORD
170           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "cnfm-db-creds" "key" "password") | indent 10 }}
171         volumeMounts:
172         - mountPath: /etc/localtime
173           name: localtime
174           readOnly: true
175         - name: docker-entrypoint-initdb-d-sh
176           mountPath: "/docker-entrypoint-initdb.d"
177         - name: docker-entrypoint-initdb-d-sql
178           mountPath: "/docker-entrypoint-initdb.d/db-sql-scripts"
179         {{- if .Values.global.migration.enabled }}
180         - name: backup-storage
181           mountPath: /var/data/mariadb
182         {{- end }}
183         resources: {{ include "common.resources" . | nindent 10 }}
184       {{ include "common.waitForJobContainer" . | indent 6 | trim }}
185       {{- if .Values.nodeSelector }}
186       nodeSelector:
187 {{ toYaml .Values.nodeSelector | indent 10 }}
188       {{- end -}}
189       {{- if .Values.affinity }}
190       affinity:
191 {{ toYaml .Values.affinity | indent 10 }}
192       {{- end }}
193       serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
194       volumes:
195       - name: localtime
196         hostPath:
197           path: /etc/localtime
198       - name: docker-entrypoint-initdb-d-sh
199         configMap:
200           name: {{ include "common.fullname" . }}-mariadb-sh
201       - name: docker-entrypoint-initdb-d-sql
202         configMap:
203           name: {{ include "common.fullname" . }}-mariadb-sql
204       {{- if .Values.global.migration.enabled }}
205       - name: backup-storage
206         persistentVolumeClaim:
207           claimName: {{ include "common.fullname" . }}-migration
208       {{- end }}
209       restartPolicy: Never
210       imagePullSecrets:
211       - name: "{{ include "common.namespace" . }}-docker-registry-key"