348dda517afea16a10f005d0d09a8f066e0b7861
[oom.git] / kubernetes / common / postgres-init / templates / job.yaml
1 {{/*
2 # Copyright © 2021 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 apiVersion: batch/v1
18 kind: Job
19 metadata:
20   name: {{ include "common.fullname" . }}-config-job
21   namespace: {{ include "common.namespace" . }}
22   labels:
23     app: {{ include "common.name" . }}
24     chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
25     release: {{ include "common.release" . }}
26     heritage: {{ .Release.Service }}
27 spec:
28   backoffLimit: 20
29   template:
30     metadata:
31       annotations:
32         # Workarround to exclude K8S API from istio communication
33         # as init-container (readinessCheck) does not work with the
34         # Istio CNI plugin, see:
35         # (https://istio.io/latest/docs/setup/additional-setup/cni/#compatibility-with-application-init-containers)
36         traffic.sidecar.istio.io/excludeOutboundPorts: "443"
37       labels:
38         app: {{ include "common.name" . }}
39         release: {{ include "common.release" . }}
40       name: {{ include "common.name" . }}
41     spec:
42       initContainers: {{ include "common.readinessCheck.waitFor" . | nindent 6 }}
43       containers:
44       - command:
45         - sh
46         args:
47         - -c
48         - |
49           function prepare_password {
50             echo -n $1 | sed -e "s/'/''/g"
51           }
52           export PG_PASSWORD=`prepare_password $PG_PASSWORD_INPUT`;
53           export PG_ROOT_PASSWORD=`prepare_password $PG_ROOT_PASSWORD_INPUT`;
54           {{- if include "common.onServiceMesh" . }}
55           echo "waiting 15s for istio side cars to be up"; sleep 15s;{{- end }}
56           cd /config-input && for PFILE in `ls -1 .`; do envsubst <${PFILE} >/config/${PFILE}; done;
57           psql "postgresql://postgres:$PG_ROOT_PASSWORD@$PG_HOST" < /config/setup.sql
58         env:
59         - name: PG_HOST
60           value: "{{ .Values.global.postgres.service.name2 }}"
61         - name: PG_PRIMARY_USER
62           value: primaryuser
63         - name: MODE
64           value: postgres
65         - name: PG_PRIMARY_PASSWORD_INPUT
66           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.postgres.secret.primaryPasswordUID" .) "key" "password") | indent 10 }}
67         - name: PG_USER
68           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.postgres.secret.userCredentialsUID" .) "key" "login") | indent 10 }}
69         - name: PG_PASSWORD_INPUT
70           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.postgres.secret.userCredentialsUID" .) "key" "password") | indent 10 }}
71         - name: PG_DATABASE
72           value: "{{ .Values.config.pgDatabase }}"
73         - name: PG_ROOT_PASSWORD_INPUT
74           {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" (include "common.postgres.secret.rootPassUID" .) "key" "password") | indent 10 }}
75         image: {{ include "repositoryGenerator.image.postgres" . }}
76         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
77         name: {{ include "common.name" . }}-update-config
78         volumeMounts:
79         - mountPath: /etc/localtime
80           name: localtime
81           readOnly: true
82         - mountPath: /config-input/setup.sql
83           name: config
84           subPath: setup.sql
85         - mountPath: /config
86           name: pgconf
87         resources: {{ include "common.resources" . | nindent 10 }}
88       {{ include "common.waitForJobContainer" . | indent 6 | trim }}
89       {{- if .Values.nodeSelector }}
90       nodeSelector:
91 {{ toYaml .Values.nodeSelector | indent 10 }}
92       {{- end -}}
93       {{- if .Values.affinity }}
94       affinity:
95 {{ toYaml .Values.affinity | indent 10 }}
96       {{- end }}
97       serviceAccountName: {{ include "common.fullname" (dict "suffix" "read" "dot" . )}}
98       volumes:
99       - name: localtime
100         hostPath:
101           path: /etc/localtime
102       - name: config
103         configMap:
104           name: {{ include "common.fullname" . }}
105       - name: pgconf
106         emptyDir:
107           medium: Memory
108       restartPolicy: Never
109       {{- include "common.imagePullSecrets" . | nindent 6 }}