[COMMON] Add missing labels for etcd, mongo, postgres
[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       imagePullSecrets:
61       - name: "{{ include "common.namespace" $dot }}-docker-registry-key"
62       initContainers:
63       - command:
64         - sh
65         args:
66         - -c
67         - |
68           function prepare_password {
69             echo -n $1 | sed -e "s/'/''/g"
70           }
71           export PG_PRIMARY_PASSWORD=`prepare_password $PG_PRIMARY_PASSWORD_INPUT`;
72           export PG_PASSWORD=`prepare_password $PG_PASSWORD_INPUT`;
73           export PG_ROOT_PASSWORD=`prepare_password $PG_ROOT_PASSWORD_INPUT`;
74           cd /config-input && for PFILE in `ls -1 .`; do envsubst <${PFILE} >/config/${PFILE}; done
75         env:
76         - name: PG_PRIMARY_USER
77           value: primaryuser
78         - name: MODE
79           value: postgres
80         - name: PG_PRIMARY_PASSWORD_INPUT
81           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.primaryPasswordUID" .) "key" "password") | indent 10 }}
82         - name: PG_USER
83           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.userCredentialsUID" .) "key" "login") | indent 10 }}
84         - name: PG_PASSWORD_INPUT
85           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.userCredentialsUID" .) "key" "password") | indent 10 }}
86         - name: PG_DATABASE
87           value: "{{ $dot.Values.config.pgDatabase }}"
88         - name: PG_ROOT_PASSWORD_INPUT
89           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.rootPassUID" .) "key" "password") | indent 10 }}
90         volumeMounts:
91         - mountPath: /config-input/setup.sql
92           name: config
93           subPath: setup.sql
94         - mountPath: /config
95           name: pgconf
96         image: {{ include "repositoryGenerator.image.envsubst" $dot }}
97         imagePullPolicy: {{ $dot.Values.global.pullPolicy | default $dot.Values.pullPolicy }}
98         name: {{ include "common.name" $dot }}-update-config
99
100       - name: init-sysctl
101         command:
102         - /bin/sh
103         - -c
104         - |
105           chown 26:26 /podroot/;
106           chmod 700 /podroot/;
107         image: {{ include "repositoryGenerator.image.busybox" $dot }}
108         imagePullPolicy: {{ $dot.Values.global.pullPolicy | default $dot.Values.pullPolicy }}
109         volumeMounts:
110         - name: {{ include "common.fullname" $dot }}-data
111           mountPath: /podroot/
112       containers:
113       - name: {{ include "common.name" $dot }}
114         image: {{ include "repositoryGenerator.image.postgres" $dot }}
115         imagePullPolicy: {{ $dot.Values.global.pullPolicy | default $dot.Values.pullPolicy }}
116         ports:
117         - containerPort: {{ $dot.Values.service.internalPort }}
118           name: {{ $dot.Values.service.portName }}
119         # disable liveness probe when breakpoints set in debugger
120         # so K8s doesn't restart unresponsive container
121         {{- if eq $dot.Values.liveness.enabled true }}
122         livenessProbe:
123           tcpSocket:
124             port: {{ $dot.Values.service.internalPort }}
125           initialDelaySeconds: {{ $dot.Values.liveness.initialDelaySeconds }}
126           periodSeconds: {{ $dot.Values.liveness.periodSeconds }}
127           timeoutSeconds: {{ $dot.Values.liveness.timeoutSeconds }}
128         {{- end }}
129         readinessProbe:
130           tcpSocket:
131             port: {{ $dot.Values.service.internalPort }}
132           initialDelaySeconds: {{ $dot.Values.readiness.initialDelaySeconds }}
133           periodSeconds: {{ $dot.Values.readiness.periodSeconds }}
134         env:
135         - name: PGHOST
136           value: /tmp
137         - name: PG_PRIMARY_USER
138           value: primaryuser
139         - name: MODE
140           value: postgres
141         - name: PG_MODE
142           value: {{ $pgMode }}
143         - name: PG_PRIMARY_HOST
144           value: "{{ $dot.Values.service.name2 }}"
145         - name: PG_REPLICA_HOST
146           value: "{{ $dot.Values.service.name3 }}"
147         - name: PG_PRIMARY_PORT
148           value: "{{ $dot.Values.service.internalPort }}"
149         - name: PG_PRIMARY_PASSWORD
150           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.primaryPasswordUID" .) "key" "password") | indent 10 }}
151         - name: PG_USER
152           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.userCredentialsUID" .) "key" "login") | indent 10 }}
153         - name: PG_PASSWORD
154           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.userCredentialsUID" .) "key" "password") | indent 10 }}
155         - name: PG_DATABASE
156           value: "{{ $dot.Values.config.pgDatabase }}"
157         - name: PG_ROOT_PASSWORD
158           {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.rootPassUID" .) "key" "password") | indent 10 }}
159         - name: PGDATA_PATH_OVERRIDE
160           value: "{{ $dot.Values.config.pgDataPath }}"
161         volumeMounts:
162         - name: config
163           mountPath: /pgconf/pool_hba.conf
164           subPath: pool_hba.conf
165         - name: pgconf
166           mountPath: /pgconf/setup.sql
167           subPath: setup.sql
168         - mountPath: /pgdata
169           name: {{ include "common.fullname" $dot }}-data
170         - mountPath: /backup
171           name: {{ include "common.fullname" $dot }}-backup
172           readOnly: true
173         resources: {{ include "common.resources" $dot | nindent 10 }}
174       {{- if (default false $dot.Values.metrics.enabled) }}
175       - name: {{ include "common.name" $dot }}-metrics
176         image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ $dot.Values.metrics.image }}
177         imagePullPolicy: {{ $dot.Values.global.pullPolicy | default $dot.Values.metrics.pullPolicy | quote}}
178         env:
179           - name: POSTGRES_METRICS_EXTRA_FLAGS
180             value: {{ default "" (join " " $dot.Values.metrics.extraFlags) | quote }}
181           - name: DATA_SOURCE_USER
182             value: "{{ $dot.Values.metrics.postgresUser }}"
183           - name: DATA_SOURCE_PASS
184             {{- include "common.secret.envFromSecretFast" (dict "global" $dot "uid" (include "common.postgres.secret.rootPassUID" .) "key" "password") | indent 12 }}
185         command:
186           - sh
187           - -c
188           - |
189             DATA_SOURCE_URI="127.0.0.1:5432/?sslmode=disable" ./bin/postgres_exporter $POSTGRES_METRICS_EXTRA_FLAGS
190         ports:
191           {{- range $index, $metricPort := $dot.Values.metrics.ports }}
192           - name: {{ $metricPort.name }}
193             containerPort: {{ $metricPort.port }}
194             protocol: TCP
195         {{- end }}
196         livenessProbe:
197           httpGet:
198             path: /metrics
199             port: tcp-metrics
200           initialDelaySeconds: {{ $dot.Values.metrics.livenessProbe.initialDelaySeconds }}
201           periodSeconds: {{ $dot.Values.metrics.livenessProbe.periodSeconds }}
202           timeoutSeconds: {{ $dot.Values.metrics.livenessProbe.timeoutSeconds }}
203           successThreshold: {{ $dot.Values.metrics.livenessProbe.successThreshold }}
204           failureThreshold: {{ $dot.Values.metrics.livenessProbe.failureThreshold }}
205         readinessProbe:
206           httpGet:
207             path: /metrics
208             port: tcp-metrics
209           initialDelaySeconds: {{ $dot.Values.metrics.readinessProbe.initialDelaySeconds }}
210           periodSeconds: {{ $dot.Values.metrics.readinessProbe.periodSeconds }}
211           timeoutSeconds: {{ $dot.Values.metrics.readinessProbe.timeoutSeconds }}
212           successThreshold: {{ $dot.Values.metrics.readinessProbe.successThreshold }}
213           failureThreshold: {{ $dot.Values.metrics.readinessProbe.failureThreshold }}
214         {{ include "common.containerSecurityContext" $dot | indent 10 | trim }}
215         resources: {{- toYaml $dot.Values.metrics.resources | nindent 12 }}
216         {{ end }}
217         {{- if $dot.Values.nodeSelector }}
218         nodeSelector:
219 {{ toYaml $dot.Values.nodeSelector | indent 10 }}
220         {{- end -}}
221         {{- if $dot.Values.affinity }}
222         affinity:
223 {{ toYaml $dot.Values.affinity | indent 10 }}
224         {{- end }}
225       volumes:
226       - name: localtime
227         hostPath:
228           path: /etc/localtime
229       - name: {{ include "common.fullname" $dot }}-backup
230         emptyDir: {}
231       - name: {{ include "common.fullname" $dot }}-data
232 {{- if $dot.Values.persistence.enabled }}
233         persistentVolumeClaim:
234             claimName: {{ include "common.fullname" $dot }}-{{ $pgMode }}
235 {{- else }}
236         emptyDir: {}
237 {{ end }}
238       - name: config
239         configMap:
240           name: {{ include "common.fullname" $dot }}
241       - name: pgconf
242         emptyDir:
243           medium: Memory
244 {{- end -}}