Files
charts/bitnami/postgresql-ha/templates/postgresql/statefulset.yaml
Steven Sheehy 97fee8da2c [bitnami/postgresql-ha] Add labels and init DB scripts secret (#2225)
* [bitnami/postgresql-ha] Add labels and init DB scripts secret

Signed-off-by: Steven Sheehy <steven.sheehy@hedera.com>
2020-04-09 09:57:25 +02:00

318 lines
16 KiB
YAML

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ include "postgresql-ha.postgresql" . }}
labels: {{- include "postgresql-ha.labels" . | nindent 4 }}
app.kubernetes.io/component: postgresql
{{- if .Values.postgresql.labels }}
{{- include "postgresql-ha.tplValue" (dict "value" .Values.postgresql.labels "context" $) | nindent 4 }}
{{- end }}
spec:
serviceName: {{ include "postgresql-ha.postgresql" . }}-headless
replicas: {{ .Values.postgresql.replicaCount }}
updateStrategy:
type: {{ .Values.postgresql.updateStrategyType }}
{{- if (eq "Recreate" .Values.postgresql.updateStrategyType) }}
rollingUpdate: null
{{- end }}
selector:
matchLabels: {{- include "postgresql-ha.matchLabels" . | nindent 6 }}
app.kubernetes.io/component: postgresql
template:
metadata:
labels: {{- include "postgresql-ha.labels" . | nindent 8 }}
app.kubernetes.io/component: postgresql
{{- if .Values.postgresql.podLabels }}
{{- include "postgresql-ha.tplValue" (dict "value" .Values.postgresql.podLabels "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.postgresql.podAnnotations }}
annotations: {{- include "postgresql-ha.tplValue" (dict "value" .Values.postgresql.podAnnotations "context" $) | nindent 8 }}
{{- end }}
spec:
{{- include "postgresql-ha.imagePullSecrets" . | indent 6 }}
{{- if .Values.postgresql.affinity }}
affinity: {{- include "postgresql-ha.tplValue" (dict "value" .Values.postgresql.affinity "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.postgresql.nodeSelector }}
nodeSelector: {{- include "postgresql-ha.tplValue" (dict "value" .Values.postgresql.nodeSelector "context" $) | nindent 8 }}
{{- end }}
priorityClassName: {{ .Values.postgresql.priorityClassName }}
{{- if .Values.postgresql.tolerations }}
tolerations: {{- include "postgresql-ha.tplValue" (dict "value" .Values.postgresql.tolerations "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.postgresql.securityContext.enabled }}
securityContext:
fsGroup: {{ .Values.postgresql.securityContext.fsGroup }}
{{- end }}
{{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
initContainers:
- name: init-chmod-data
image: {{ include "postgresql-ha.volumePermissionsImage" . }}
imagePullPolicy: {{ .Values.volumePermissionsImage.pullPolicy | quote }}
command:
- sh
- -c
- |
mkdir -p {{ .Values.persistence.mountPath }}/conf {{ .Values.persistence.mountPath }}/data
chmod 700 {{ .Values.persistence.mountPath }}/conf {{ .Values.persistence.mountPath }}/data
find {{ .Values.persistence.mountPath }} -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | \
xargs chown -R {{ .Values.postgresql.securityContext.runAsUser }}:{{ .Values.postgresql.securityContext.fsGroup }}
securityContext:
runAsUser: {{ .Values.volumePermissions.securityContext.runAsUser }}
{{- if .Values.volumePermissions.resources }}
resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
{{- end }}
volumeMounts:
- name: data
mountPath: {{ .Values.persistence.mountPath }}
{{- end }}
containers:
- name: postgresql
image: {{ include "postgresql-ha.postgresqlImage" . }}
imagePullPolicy: {{ .Values.postgresqlImage.pullPolicy | quote }}
{{- if .Values.postgresql.securityContext.enabled }}
securityContext:
runAsUser: {{ .Values.postgresql.securityContext.runAsUser }}
{{- end }}
# Auxiliar vars to populate environment variables
{{- $postgresqlReplicaCount := int .Values.postgresql.replicaCount }}
{{- $postgresqlFullname := include "postgresql-ha.postgresql" . }}
{{- $postgresqlHeadlessServiceName := printf "%s-headless" (include "postgresql-ha.postgresql" .) }}
{{- $releaseNamespace := .Release.Namespace }}
{{- $clusterDomain:= .Values.clusterDomain }}
env:
- name: BITNAMI_DEBUG
value: {{ ternary "true" "false" .Values.postgresqlImage.debug | quote }}
# PostgreSQL configuration
- name: POSTGRESQL_VOLUME_DIR
value: {{ .Values.persistence.mountPath | quote }}
- name: PGDATA
value: {{ printf "%s/%s" .Values.persistence.mountPath "data" | quote }}
- name: POSTGRES_USER
value: {{ (include "postgresql-ha.postgresqlUsername" .) | quote }}
{{- if .Values.postgresql.usePasswordFile }}
- name: POSTGRES_PASSWORD_FILE
value: "/opt/bitnami/postgresql/secrets/postgresql-password"
{{- else }}
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "postgresql-ha.postgresqlSecretName" . }}
key: postgresql-password
{{- end }}
{{- if not (empty (include "postgresql-ha.postgresqlDatabase" .)) }}
- name: POSTGRES_DB
value: {{ (include "postgresql-ha.postgresqlDatabase" .) | quote }}
{{- end }}
# Repmgr configuration
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: REPMGR_UPGRADE_EXTENSION
value: {{ ternary "yes" "no" .Values.postgresql.upgradeRepmgrExtension | quote }}
- name: REPMGR_PGHBA_TRUST_ALL
value: {{ ternary "yes" "no" .Values.postgresql.pgHbaTrustAll | quote }}
- name: REPMGR_MOUNTED_CONF_DIR
value: "/bitnami/repmgr/conf"
- name: REPMGR_PARTNER_NODES
value: {{range $e, $i := until $postgresqlReplicaCount }}{{ $postgresqlFullname }}-{{ $i }}.{{ $postgresqlHeadlessServiceName }}.{{ $releaseNamespace }}.svc.{{ $clusterDomain }},{{ end }}
- name: REPMGR_PRIMARY_HOST
value: {{ printf "%s-0.%s.%s.svc.%s" $postgresqlFullname $postgresqlHeadlessServiceName .Release.Namespace $clusterDomain | quote }}
- name: REPMGR_NODE_NAME
value: "$(MY_POD_NAME)"
- name: REPMGR_NODE_NETWORK_NAME
value: "$(MY_POD_NAME).{{ $postgresqlHeadlessServiceName }}.{{ .Release.Namespace }}.svc.{{ $clusterDomain }}"
- name: REPMGR_LOG_LEVEL
value: {{ .Values.postgresql.repmgrLogLevel | quote }}
- name: REPMGR_CONNECT_TIMEOUT
value: {{ .Values.postgresql.repmgrConnectTimeout | quote }}
- name: REPMGR_RECONNECT_ATTEMPTS
value: {{ .Values.postgresql.repmgrReconnectAttempts | quote }}
- name: REPMGR_RECONNECT_INTERVAL
value: {{ .Values.postgresql.repmgrReconnectInterval | quote }}
- name: REPMGR_USERNAME
value: {{ (include "postgresql-ha.postgresqlRepmgrUsername" .) | quote }}
{{- if .Values.postgresql.usePasswordFile }}
- name: REPMGR_PASSWORD_FILE
value: "/opt/bitnami/repmgr/secrets/repmgr-password"
{{- else }}
- name: REPMGR_PASSWORD
valueFrom:
secretKeyRef:
name: {{ include "postgresql-ha.postgresqlSecretName" . }}
key: repmgr-password
{{- end }}
{{- if (include "postgresql-ha.repmgrDatabase" .) }}
- name: REPMGR_DATABASE
value: {{ (include "postgresql-ha.repmgrDatabase" .) | quote }}
{{- end }}
ports:
- name: postgresql
containerPort: 5432
protocol: TCP
{{- if .Values.postgresql.livenessProbe.enabled }}
livenessProbe:
exec:
command:
- sh
- -c
- '{{ include "postgresql-ha.pgpassword" . }} psql -w -U {{ include "postgresql-ha.postgresqlUsername" . | quote }} -d {{ (include "postgresql-ha.postgresqlDatabase" .) | quote }} -h 127.0.0.1 -c "SELECT 1"'
initialDelaySeconds: {{ .Values.postgresql.livenessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.postgresql.livenessProbe.periodSeconds }}
timeoutSeconds: {{ .Values.postgresql.livenessProbe.timeoutSeconds }}
successThreshold: {{ .Values.postgresql.livenessProbe.successThreshold }}
failureThreshold: {{ .Values.postgresql.livenessProbe.failureThreshold }}
{{- end }}
{{- if .Values.postgresql.readinessProbe.enabled }}
readinessProbe:
exec:
command:
- sh
- -c
- '{{ include "postgresql-ha.pgpassword" . }} psql -w -U {{ include "postgresql-ha.postgresqlUsername" . | quote }} -d {{ (include "postgresql-ha.postgresqlDatabase" .) | quote }} -h 127.0.0.1 -c "SELECT 1"'
initialDelaySeconds: {{ .Values.postgresql.readinessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.postgresql.readinessProbe.periodSeconds }}
timeoutSeconds: {{ .Values.postgresql.readinessProbe.timeoutSeconds }}
successThreshold: {{ .Values.postgresql.readinessProbe.successThreshold }}
failureThreshold: {{ .Values.postgresql.readinessProbe.failureThreshold }}
{{- end }}
{{- if .Values.postgresql.resources }}
resources: {{- toYaml .Values.postgresql.resources | nindent 12 }}
{{- end }}
volumeMounts:
{{- if or (.Files.Glob "files/repmgr.conf") (.Files.Glob "files/postgresql.conf") (.Files.Glob "files/pg_hba.conf") .Values.postgresql.repmgrConfiguration .Values.postgresql.configuration .Values.postgresql.pgHbaConfiguration .Values.postgresql.configurationCM }}
- name: postgresql-config
mountPath: /bitnami/repmgr/conf
{{- end }}
{{- if or (.Files.Glob "files/conf.d/*.conf") .Values.postgresql.extendedConf .Values.postgresql.extendedConfCM }}
- name: postgresql-extended-config
mountPath: /bitnami/postgresql/conf/conf.d/
{{- end }}
{{- if or (.Files.Glob "files/docker-entrypoint-initdb.d/*.{sh,sql,sql.gz}") .Values.postgresql.initdbScriptsCM .Values.postgresql.initdbScripts }}
- name: custom-init-scripts
mountPath: /docker-entrypoint-initdb.d/
{{- end }}
{{- if .Values.postgresql.initdbScriptsSecret }}
- name: custom-init-scripts-secret
mountPath: /docker-entrypoint-initdb.d/secret
{{- end }}
{{- if .Values.postgresql.usePasswordFile }}
- name: postgresql-password
mountPath: /opt/bitnami/postgresql/secrets/
{{- end }}
{{- if .Values.persistence.enabled }}
- name: data
mountPath: {{ .Values.persistence.mountPath }}
{{- end }}
{{- if .Values.metrics.enabled }}
- name: metrics
image: {{ include "postgresql-ha.metricsImage" . }}
imagePullPolicy: {{ .Values.metricsImage.pullPolicy | quote }}
{{- if .Values.metrics.securityContext.enabled }}
securityContext:
runAsUser: {{ .Values.metrics.securityContext.runAsUser }}
{{- end }}
env:
- name: DATA_SOURCE_URI
value: {{ printf "127.0.0.1:5432/%s?sslmode=disable" (include "postgresql-ha.postgresqlDatabase" .) | quote }}
{{- if .Values.postgresql.usePasswordFile }}
- name: DATA_SOURCE_PASS_FILE
value: "/opt/bitnami/postgresql/secrets/postgresql-password"
{{- else }}
- name: DATA_SOURCE_PASS
valueFrom:
secretKeyRef:
name: {{ include "postgresql-ha.postgresqlSecretName" . }}
key: postgresql-password
{{- end }}
- name: DATA_SOURCE_USER
value: {{ (include "postgresql-ha.postgresqlUsername" .) | quote }}
ports:
- name: metrics
containerPort: 9187
protocol: TCP
{{- if .Values.metrics.livenessProbe.enabled }}
livenessProbe:
httpGet:
path: /
port: metrics
initialDelaySeconds: {{ .Values.metrics.livenessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.metrics.livenessProbe.periodSeconds }}
timeoutSeconds: {{ .Values.metrics.livenessProbe.timeoutSeconds }}
successThreshold: {{ .Values.metrics.livenessProbe.successThreshold }}
failureThreshold: {{ .Values.metrics.livenessProbe.failureThreshold }}
{{- end }}
{{- if .Values.metrics.readinessProbe.enabled }}
readinessProbe:
httpGet:
path: /
port: metrics
initialDelaySeconds: {{ .Values.metrics.readinessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.metrics.readinessProbe.periodSeconds }}
timeoutSeconds: {{ .Values.metrics.readinessProbe.timeoutSeconds }}
successThreshold: {{ .Values.metrics.readinessProbe.successThreshold }}
failureThreshold: {{ .Values.metrics.readinessProbe.failureThreshold }}
{{- end }}
{{- if .Values.metrics.resources }}
resources: {{- toYaml .Values.metrics.resources | nindent 12 }}
{{- end }}
volumeMounts:
{{- if .Values.postgresql.usePasswordFile }}
- name: postgresql-password
mountPath: /opt/bitnami/postgresql/secrets/
{{- end }}
{{- end }}
volumes:
{{- if or (.Files.Glob "files/repmgr.conf") (.Files.Glob "files/postgresql.conf") (.Files.Glob "files/pg_hba.conf") .Values.postgresql.repmgrConfiguration .Values.postgresql.configuration .Values.postgresql.pgHbaConfiguration .Values.postgresql.configurationCM }}
- name: postgresql-config
configMap:
name: {{ include "postgresql-ha.postgresqlConfigurationCM" . }}
{{- end }}
{{- if or (.Files.Glob "files/conf.d/*.conf") .Values.postgresql.extendedConf .Values.postgresql.extendedConfCM }}
- name: postgresql-extended-config
configMap:
name: {{ template "postgresql-ha.postgresqlExtendedConfCM" . }}
{{- end }}
{{- if .Values.postgresql.initdbScriptsSecret }}
- name: custom-init-scripts-secret
secret:
secretName: {{ template "postgresql-ha.postgresqlInitdbScriptsSecret" . }}
{{- end }}
{{- if .Values.postgresql.usePasswordFile }}
- name: postgresql-password
secret:
secretName: {{ include "postgresql-ha.postgresqlSecretName" . }}
{{- end }}
{{- if or (.Files.Glob "files/docker-entrypoint-initdb.d/*.{sh,sql,sql.gz}") .Values.postgresql.initdbScriptsCM .Values.postgresql.initdbScripts }}
- name: custom-init-scripts
configMap:
name: {{ template "postgresql-ha.postgresqlInitdbScriptsCM" . }}
{{- end }}
{{- if and .Values.persistence.enabled .Values.persistence.existingClaim }}
- name: data
persistentVolumeClaim:
{{- with .Values.persistence.existingClaim }}
claimName: {{ tpl . $ }}
{{- end }}
{{- else if not .Values.persistence.enabled }}
- name: data
emptyDir: {}
{{- else if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
volumeClaimTemplates:
- metadata:
name: data
{{- if .Values.persistence.annotations }}
annotations: {{- include "postgresql-ha.tplValue" (dict "value" .Values.persistence.annotations "context" $) | nindent 10 }}
{{- end }}
spec:
accessModes:
{{- range .Values.persistence.accessModes }}
- {{ . | quote }}
{{- end }}
resources:
requests:
storage: {{ .Values.persistence.size | quote }}
{{ include "postgresql-ha.storageClass" . | indent 8 }}
{{- end }}