aae5da9195112e0bbbe0238beec9b0f6a050e11f
[oom.git] / kubernetes / common / postgres / templates / _deployment.tpl
1 {{/*
2 # Copyright © 2018 Amdocs, AT&T, Bell Canada
3 # Copyright © 2020 Samsung Electronics
4 # Copyright © 2021 Orange
5 # Modifications Copyright (C) 2021 Bell Canada.
6 # #
7 # # Licensed under the Apache License, Version 2.0 (the "License");
8 # # you may not use this file except in compliance with the License.
9 # # You may obtain a copy of the License at
10 # #
11 # #       http://www.apache.org/licenses/LICENSE-2.0
12 # #
13 # # Unless required by applicable law or agreed to in writing, software
14 # # distributed under the License is distributed on an "AS IS" BASIS,
15 # # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # # See the License for the specific language governing permissions and
17 # # limitations under the License.
18 */}}
19
20 {{- define "common.postgres.deployment" -}}
21   {{- $dot := .dot }}
22   {{- $pgMode := .pgMode }}
23 apiVersion: apps/v1
24 kind: Deployment
25 metadata:
26   name: {{ include "common.fullname" $dot }}-{{ $pgMode }}
27   namespace: {{ include "common.namespace" $dot }}
28   labels:
29     app: {{ include "common.name" $dot }}-{{ $pgMode }}
30     app.kubernetes.io/name: {{ include "common.name" $dot }}-{{ $pgMode }}
31     {{- if $dot.Chart.AppVersion }}
32     version: "{{ $dot.Chart.AppVersion | replace "+" "_" }}"
33     {{- else }}
34     version: "{{ $dot.Chart.Version | replace "+" "_" }}"
35     {{- end }}
36     chart: {{ $dot.Chart.Name }}-{{ $dot.Chart.Version | replace "+" "_" }}
37     release: {{ include "common.release" $dot }}
38     heritage: {{ $dot.Release.Service }}
39     name: "{{ index $dot.Values "container" "name" $pgMode }}"
40 spec:
41   replicas: 1
42   strategy:
43     type: Recreate
44   selector:
45     matchLabels:
46       app: {{ include "common.name" $dot }}-{{ $pgMode }}
47   template:
48     metadata:
49       labels:
50         app: {{ include "common.name" $dot }}-{{ $pgMode }}
51         app.kubernetes.io/name: {{ include "common.name" $dot }}-{{ $pgMode }}
52         {{- if $dot.Chart.AppVersion }}
53         version: "{{ $dot.Chart.AppVersion | replace "+" "_" }}"
54         {{- else }}
55         version: "{{ $dot.Chart.Version | replace "+" "_" }}"
56         {{- end }}
57         release: {{ include "common.release" $dot }}
58         name: "{{ index $dot.Values "container" "name" $pgMode }}"
59     spec:
60       {{- include "common.imagePullSecrets" $dot | nindent 6 }}
61       initContainers:
62       - command:
63         - sh
64         args:
65         - -c
66         - |
67           function prepare_password {
68             echo -n $1 | sed -e "s/'/''/g"
69           }
70           export PG_PRIMARY_PASSWORD=`prepare_password $PG_PRIMARY_PASSWORD_INPUT`;
71           export PG_PASSWORD=`prepare_password $PG_PASSWORD_INPUT`;
72           export PG_ROOT_PASSWORD=`prepare_password $PG_ROOT_PASSWORD_INPUT`;
73           cd /config-input && for PFILE in `ls -1 .`; do envsubst <${PFILE} >/config/${PFILE}; done
74         env:
75         - name: PG_PRIMARY_USER
76           value: primaryuser
77         - name: MODE
78           value: postgres
79         - name: PG_PRIMARY_PASSWORD_INPUT
80           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.primaryPasswordUID" .) "key" "password") | indent 10 }}
81         - name: PG_USER
82           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.userCredentialsUID" .) "key" "login") | indent 10 }}
83         - name: PG_PASSWORD_INPUT
84           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.userCredentialsUID" .) "key" "password") | indent 10 }}
85         - name: PG_DATABASE
86           value: "{{ $dot.Values.config.pgDatabase }}"
87         - name: PG_ROOT_PASSWORD_INPUT
88           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.rootPassUID" .) "key" "password") | indent 10 }}
89         volumeMounts:
90         - mountPath: /config-input/setup.sql
91           name: config
92           subPath: setup.sql
93         - mountPath: /config
94           name: pgconf
95         image: {{ include "repositoryGenerator.image.envsubst" $dot }}
96         imagePullPolicy: {{ $dot.Values.global.pullPolicy | default $dot.Values.pullPolicy }}
97         name: {{ include "common.name" $dot }}-update-config
98
99       - name: init-sysctl
100         command:
101         - /bin/sh
102         - -c
103         - |
104           chown 26:26 /podroot/;
105           chmod 700 /podroot/;
106         image: {{ include "repositoryGenerator.image.busybox" $dot }}
107         imagePullPolicy: {{ $dot.Values.global.pullPolicy | default $dot.Values.pullPolicy }}
108         volumeMounts:
109         - name: {{ include "common.fullname" $dot }}-data
110           mountPath: /podroot/
111       containers:
112       - name: {{ include "common.name" $dot }}
113         image: {{ include "repositoryGenerator.image.postgres" $dot }}
114         imagePullPolicy: {{ $dot.Values.global.pullPolicy | default $dot.Values.pullPolicy }}
115         ports:
116         - containerPort: {{ $dot.Values.service.internalPort }}
117           name: {{ $dot.Values.service.portName }}
118         # disable liveness probe when breakpoints set in debugger
119         # so K8s doesn't restart unresponsive container
120         {{- if eq $dot.Values.liveness.enabled true }}
121         livenessProbe:
122           tcpSocket:
123             port: {{ $dot.Values.service.internalPort }}
124           initialDelaySeconds: {{ $dot.Values.liveness.initialDelaySeconds }}
125           periodSeconds: {{ $dot.Values.liveness.periodSeconds }}
126           timeoutSeconds: {{ $dot.Values.liveness.timeoutSeconds }}
127         {{- end }}
128         readinessProbe:
129           tcpSocket:
130             port: {{ $dot.Values.service.internalPort }}
131           initialDelaySeconds: {{ $dot.Values.readiness.initialDelaySeconds }}
132           periodSeconds: {{ $dot.Values.readiness.periodSeconds }}
133         env:
134         - name: PGHOST
135           value: /tmp
136         - name: PG_PRIMARY_USER
137           value: primaryuser
138         - name: MODE
139           value: postgres
140         - name: PG_MODE
141           value: {{ $pgMode }}
142         - name: PG_PRIMARY_HOST
143           value: "{{ $dot.Values.service.name2 }}"
144         - name: PG_REPLICA_HOST
145           value: "{{ $dot.Values.service.name3 }}"
146         - name: PG_PRIMARY_PORT
147           value: "{{ $dot.Values.service.internalPort }}"
148         - name: PG_PRIMARY_PASSWORD
149           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.primaryPasswordUID" .) "key" "password") | indent 10 }}
150         - name: PG_USER
151           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.userCredentialsUID" .) "key" "login") | indent 10 }}
152         - name: PG_PASSWORD
153           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.userCredentialsUID" .) "key" "password") | indent 10 }}
154         - name: PG_DATABASE
155           value: "{{ $dot.Values.config.pgDatabase }}"
156         - name: PG_ROOT_PASSWORD
157           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.rootPassUID" .) "key" "password") | indent 10 }}
158         - name: PGDATA_PATH_OVERRIDE
159           value: "{{ $dot.Values.config.pgDataPath }}"
160         volumeMounts:
161         - name: config
162           mountPath: /pgconf/pool_hba.conf
163           subPath: pool_hba.conf
164         - name: pgconf
165           mountPath: /pgconf/setup.sql
166           subPath: setup.sql
167         - mountPath: /pgdata
168           name: {{ include "common.fullname" $dot }}-data
169         - mountPath: /backup
170           name: {{ include "common.fullname" $dot }}-backup
171           readOnly: true
172         resources: {{ include "common.resources" $dot | nindent 10 }}
173       {{- if (default false $dot.Values.metrics.enabled) }}
174       - name: {{ include "common.name" $dot }}-metrics
175         image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ $dot.Values.metrics.image }}
176         imagePullPolicy: {{ $dot.Values.global.pullPolicy | default $dot.Values.metrics.pullPolicy | quote}}
177         env:
178           - name: POSTGRES_METRICS_EXTRA_FLAGS
179             value: {{ default "" (join " " $dot.Values.metrics.extraFlags) | quote }}
180           - name: DATA_SOURCE_USER
181             value: "{{ $dot.Values.metrics.postgresUser }}"
182           - name: DATA_SOURCE_PASS
183             {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.rootPassUID" .) "key" "password") | indent 12 }}
184         command:
185           - sh
186           - -c
187           - |
188             DATA_SOURCE_URI="127.0.0.1:5432/?sslmode=disable" ./bin/postgres_exporter $POSTGRES_METRICS_EXTRA_FLAGS
189         ports:
190           {{- range $index, $metricPort := $dot.Values.metrics.ports }}
191           - name: {{ $metricPort.name }}
192             containerPort: {{ $metricPort.port }}
193             protocol: TCP
194         {{- end }}
195         livenessProbe:
196           httpGet:
197             path: /metrics
198             port: tcp-metrics
199           initialDelaySeconds: {{ $dot.Values.metrics.livenessProbe.initialDelaySeconds }}
200           periodSeconds: {{ $dot.Values.metrics.livenessProbe.periodSeconds }}
201           timeoutSeconds: {{ $dot.Values.metrics.livenessProbe.timeoutSeconds }}
202           successThreshold: {{ $dot.Values.metrics.livenessProbe.successThreshold }}
203           failureThreshold: {{ $dot.Values.metrics.livenessProbe.failureThreshold }}
204         readinessProbe:
205           httpGet:
206             path: /metrics
207             port: tcp-metrics
208           initialDelaySeconds: {{ $dot.Values.metrics.readinessProbe.initialDelaySeconds }}
209           periodSeconds: {{ $dot.Values.metrics.readinessProbe.periodSeconds }}
210           timeoutSeconds: {{ $dot.Values.metrics.readinessProbe.timeoutSeconds }}
211           successThreshold: {{ $dot.Values.metrics.readinessProbe.successThreshold }}
212           failureThreshold: {{ $dot.Values.metrics.readinessProbe.failureThreshold }}
213         {{ include "common.containerSecurityContext" $dot | indent 10 | trim }}
214         resources: {{- toYaml $dot.Values.metrics.resources | nindent 12 }}
215         {{ end }}
216         {{- if $dot.Values.nodeSelector }}
217         nodeSelector:
218 {{ toYaml $dot.Values.nodeSelector | indent 10 }}
219         {{- end -}}
220         {{- if $dot.Values.affinity }}
221         affinity:
222 {{ toYaml $dot.Values.affinity | indent 10 }}
223         {{- end }}
224       volumes:
225       - name: localtime
226         hostPath:
227           path: /etc/localtime
228       - name: {{ include "common.fullname" $dot }}-backup
229         emptyDir: {}
230       - name: {{ include "common.fullname" $dot }}-data
231 {{- if $dot.Values.persistence.enabled }}
232         persistentVolumeClaim:
233             claimName: {{ include "common.fullname" $dot }}-{{ $pgMode }}
234 {{- else }}
235         emptyDir: {}
236 {{ end }}
237       - name: config
238         configMap:
239           name: {{ include "common.fullname" $dot }}
240       - name: pgconf
241         emptyDir:
242           medium: Memory
243 {{- end -}}