[POSTGRES][COMMON] Add support for postgres operator
[oom.git] / kubernetes / common / common / templates / _postgres.tpl
1 {{/*
2 # Copyright © 2019 Samsung Electronics
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   UID of postgres root password
18 */}}
19 {{- define "common.postgres.secret.rootPassUID" -}}
20   {{- printf "db-root-password" }}
21 {{- end -}}
22
23 {{/*
24   Name of postgres secret
25 */}}
26 {{- define "common.postgres.secret._secretName" -}}
27   {{- $global := .dot }}
28   {{- $chartName := tpl .chartName $global -}}
29   {{- include "common.secret.genName" (dict "global" $global "uid" (include .uidTemplate $global) "chartName" $chartName) }}
30 {{- end -}}
31
32 {{/*
33   Name of postgres root password secret
34 */}}
35 {{- define "common.postgres.secret.rootPassSecretName" -}}
36   {{- include "common.postgres.secret._secretName" (set . "uidTemplate" "common.postgres.secret.rootPassUID") }}
37 {{- end -}}
38
39 {{/*
40   UID of postgres user credentials
41 */}}
42 {{- define "common.postgres.secret.userCredentialsUID" -}}
43   {{- printf "db-user-credentials" }}
44 {{- end -}}
45
46 {{/*
47   Name of postgres user credentials secret
48 */}}
49 {{- define "common.postgres.secret.userCredentialsSecretName" -}}
50   {{- include "common.postgres.secret._secretName" (set . "uidTemplate" "common.postgres.secret.userCredentialsUID") }}
51 {{- end -}}
52
53 {{/*
54   UID of postgres primary password
55 */}}
56 {{- define "common.postgres.secret.primaryPasswordUID" -}}
57   {{- printf "primary-password" }}
58 {{- end -}}
59
60 {{/*
61   Name of postgres user credentials secret
62 */}}
63 {{- define "common.postgres.secret.primaryPasswordSecretName" -}}
64   {{- include "common.postgres.secret._secretName" (set . "uidTemplate" "common.postgres.secret.primaryPasswordUID") }}
65 {{- end -}}
66
67 {{/*
68   Create postgres cluster via postgres crunchydata-operator
69 */}}
70 {{- define "common.postgresOpInstance" -}}
71 {{- $dot := default . .dot -}}
72 {{- $global := $dot.Values.global -}}
73 {{- $dbinst := include "common.name" $dot -}}
74 ---
75 apiVersion: postgres-operator.crunchydata.com/v1beta1
76 kind: PostgresCluster
77 metadata:
78   name: {{ $dbinst }}
79   labels:
80     app: {{ $dbinst }}
81     version: "5.5"
82 spec:
83   metadata:
84     labels:
85       app: {{ $dbinst }}
86       version: "5.5"
87   {{- if .Values.postgresOperator.imagePostgres }}
88   image: {{ .Values.postgresOperator.imagePostgres | quote }}
89   {{- end }}
90   imagePullSecrets:
91     - name: {{ include "common.namespace" . }}-docker-registry-key
92   postgresVersion: {{ $dot.Values.postgresOperator.postgresVersion }}
93   instances:
94     - name: {{ default "instance1" .Values.postgresOperator.instanceName | quote }}
95       replicas: {{ default 2 .Values.postgresOperator.instanceReplicas }}
96       dataVolumeClaimSpec:
97         {{- if .Values.instanceStorageClassName }}
98         storageClassName: {{ .Values.postgresOperator.instanceStorageClassName | quote }}
99         {{- end }}
100         accessModes:
101         - "ReadWriteOnce"
102         resources:
103           requests:
104             storage: {{ default "1Gi" .Values.postgresOperator.instanceSize | quote }}
105       {{- if or .Values.instanceMemory .Values.postgresOperator.instanceCPU }}
106       resources:
107         limits:
108           cpu: {{ default "" .Values.postgresOperator.instanceCPU | quote }}
109           memory: {{ default "" .Values.postgresOperator.instanceMemory | quote }}
110       {{- end }}
111       affinity:
112         podAntiAffinity:
113           preferredDuringSchedulingIgnoredDuringExecution:
114           - weight: 1
115             podAffinityTerm:
116               topologyKey: kubernetes.io/hostname
117               labelSelector:
118                 matchLabels:
119                   postgres-operator.crunchydata.com/cluster: {{ $dbinst }}
120                   postgres-operator.crunchydata.com/instance-set: {{ default "instance1" .Values.postgresOperator.instanceName | quote }}
121   proxy:
122     pgBouncer:
123       metadata:
124         labels:
125           app: {{ $dbinst }}
126           version: "5.5"
127       {{- if .Values.postgresOperator.imagePgBouncer }}
128       image: {{ .Values.postgresOperator.imagePgBouncer | quote }}
129       {{- end }}
130       replicas: {{ default 2 .Values.postgresOperator.bouncerReplicas }}
131       affinity:
132         podAntiAffinity:
133           preferredDuringSchedulingIgnoredDuringExecution:
134           - weight: 1
135             podAffinityTerm:
136               topologyKey: kubernetes.io/hostname
137               labelSelector:
138                 matchLabels:
139                   postgres-operator.crunchydata.com/cluster: {{ $dbinst }}
140                   postgres-operator.crunchydata.com/role: pgbouncer
141   {{- if .Values.postgresOperator.monitoring }}
142   monitoring:
143     pgmonitor:
144       exporter:
145         image: {{ default "" .Values.postgresOperator.imageExporter | quote }}
146         {{- if .Values.postgresOperator.monitoringConfig }}
147 {{ toYaml .Values.monitoringConfig | indent 8 }}
148         {{- end }}
149   {{- end }}
150   users:
151     - name: postgres
152 {{- end -}}