[POSTGRES][COMMON] Add support for postgres operator

Add template for the postgres operator instance setup and
modifications to use the postgres instance via Operator
TBD: Update of documentation and fix for clients (DMaaP, CPS)

Issue-ID: OOM-3247

Change-Id: I56c34400dc73c71b936a51260efd231017adaeae
Signed-off-by: rajesh.kumar <rk00747546@techmahindra.com>
diff --git a/kubernetes/common/common/templates/_postgres.tpl b/kubernetes/common/common/templates/_postgres.tpl
index 45d903e..d21d8c7 100644
--- a/kubernetes/common/common/templates/_postgres.tpl
+++ b/kubernetes/common/common/templates/_postgres.tpl
@@ -63,3 +63,90 @@
 {{- define "common.postgres.secret.primaryPasswordSecretName" -}}
   {{- include "common.postgres.secret._secretName" (set . "uidTemplate" "common.postgres.secret.primaryPasswordUID") }}
 {{- end -}}
+
+{{/*
+  Create postgres cluster via postgres crunchydata-operator
+*/}}
+{{- define "common.postgresOpInstance" -}}
+{{- $dot := default . .dot -}}
+{{- $global := $dot.Values.global -}}
+{{- $dbinst := include "common.name" $dot -}}
+---
+apiVersion: postgres-operator.crunchydata.com/v1beta1
+kind: PostgresCluster
+metadata:
+  name: {{ $dbinst }}
+  labels:
+    app: {{ $dbinst }}
+    version: "5.5"
+spec:
+  metadata:
+    labels:
+      app: {{ $dbinst }}
+      version: "5.5"
+  {{- if .Values.postgresOperator.imagePostgres }}
+  image: {{ .Values.postgresOperator.imagePostgres | quote }}
+  {{- end }}
+  imagePullSecrets:
+    - name: {{ include "common.namespace" . }}-docker-registry-key
+  postgresVersion: {{ $dot.Values.postgresOperator.postgresVersion }}
+  instances:
+    - name: {{ default "instance1" .Values.postgresOperator.instanceName | quote }}
+      replicas: {{ default 2 .Values.postgresOperator.instanceReplicas }}
+      dataVolumeClaimSpec:
+        {{- if .Values.instanceStorageClassName }}
+        storageClassName: {{ .Values.postgresOperator.instanceStorageClassName | quote }}
+        {{- end }}
+        accessModes:
+        - "ReadWriteOnce"
+        resources:
+          requests:
+            storage: {{ default "1Gi" .Values.postgresOperator.instanceSize | quote }}
+      {{- if or .Values.instanceMemory .Values.postgresOperator.instanceCPU }}
+      resources:
+        limits:
+          cpu: {{ default "" .Values.postgresOperator.instanceCPU | quote }}
+          memory: {{ default "" .Values.postgresOperator.instanceMemory | quote }}
+      {{- end }}
+      affinity:
+        podAntiAffinity:
+          preferredDuringSchedulingIgnoredDuringExecution:
+          - weight: 1
+            podAffinityTerm:
+              topologyKey: kubernetes.io/hostname
+              labelSelector:
+                matchLabels:
+                  postgres-operator.crunchydata.com/cluster: {{ $dbinst }}
+                  postgres-operator.crunchydata.com/instance-set: {{ default "instance1" .Values.postgresOperator.instanceName | quote }}
+  proxy:
+    pgBouncer:
+      metadata:
+        labels:
+          app: {{ $dbinst }}
+          version: "5.5"
+      {{- if .Values.postgresOperator.imagePgBouncer }}
+      image: {{ .Values.postgresOperator.imagePgBouncer | quote }}
+      {{- end }}
+      replicas: {{ default 2 .Values.postgresOperator.bouncerReplicas }}
+      affinity:
+        podAntiAffinity:
+          preferredDuringSchedulingIgnoredDuringExecution:
+          - weight: 1
+            podAffinityTerm:
+              topologyKey: kubernetes.io/hostname
+              labelSelector:
+                matchLabels:
+                  postgres-operator.crunchydata.com/cluster: {{ $dbinst }}
+                  postgres-operator.crunchydata.com/role: pgbouncer
+  {{- if .Values.postgresOperator.monitoring }}
+  monitoring:
+    pgmonitor:
+      exporter:
+        image: {{ default "" .Values.postgresOperator.imageExporter | quote }}
+        {{- if .Values.postgresOperator.monitoringConfig }}
+{{ toYaml .Values.monitoringConfig | indent 8 }}
+        {{- end }}
+  {{- end }}
+  users:
+    - name: postgres
+{{- end -}}
diff --git a/kubernetes/common/postgres-init/Chart.yaml b/kubernetes/common/postgres-init/Chart.yaml
index 59008d3..81f566f 100644
--- a/kubernetes/common/postgres-init/Chart.yaml
+++ b/kubernetes/common/postgres-init/Chart.yaml
@@ -16,7 +16,7 @@
 apiVersion: v2
 description: Chart for Postgres init job
 name: postgres-init
-version: 13.0.0
+version: 13.0.1
 
 
 dependencies:
@@ -26,6 +26,9 @@
   - name: repositoryGenerator
     version: ~13.x-0
     repository: 'file://../repositoryGenerator'
+  - name: readinessCheck
+    version: ~13.x-0
+    repository: '@local'
   - name: serviceAccount
     version: ~13.x-0
     repository: '@local'
\ No newline at end of file
diff --git a/kubernetes/common/postgres-init/templates/job.yaml b/kubernetes/common/postgres-init/templates/job.yaml
index 09c21fe..7fa2e9f 100644
--- a/kubernetes/common/postgres-init/templates/job.yaml
+++ b/kubernetes/common/postgres-init/templates/job.yaml
@@ -33,21 +33,7 @@
         release: {{ include "common.release" . }}
       name: {{ include "common.name" . }}
     spec:
-      initContainers:
-      - name: {{ include "common.name" . }}-readiness
-        command:
-        - /app/ready.py
-        args:
-        - --container-name
-        - {{ .Values.global.postgres.container.name }}
-        env:
-        - name: NAMESPACE
-          valueFrom:
-            fieldRef:
-              apiVersion: v1
-              fieldPath: metadata.namespace
-        image: {{ include "repositoryGenerator.image.readiness" . }}
-        imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
+      initContainers: {{ include "common.readinessCheck.waitFor" . | nindent 6 }}
       containers:
       - command:
         - sh
diff --git a/kubernetes/common/postgres-init/values.yaml b/kubernetes/common/postgres-init/values.yaml
index 1b9e72b..5a9f445 100644
--- a/kubernetes/common/postgres-init/values.yaml
+++ b/kubernetes/common/postgres-init/values.yaml
@@ -96,6 +96,10 @@
   roles:
     - read
 
+readinessCheck:
+  wait_for:
+    - '{{ .Values.global.postgres.container.name }}'
+
 wait_for_job_container:
   containers:
     - '{{ include "common.name" . }}-update-config'
\ No newline at end of file
diff --git a/kubernetes/common/postgres/Chart.yaml b/kubernetes/common/postgres/Chart.yaml
index e81fc3f..3920d8e 100644
--- a/kubernetes/common/postgres/Chart.yaml
+++ b/kubernetes/common/postgres/Chart.yaml
@@ -17,7 +17,7 @@
 apiVersion: v2
 description: ONAP Postgres Server
 name: postgres
-version: 13.0.0
+version: 13.1.0
 
 dependencies:
   - name: common
diff --git a/kubernetes/common/postgres/templates/configmap.yaml b/kubernetes/common/postgres/templates/configmap.yaml
index e8bfd11..ff4f976 100644
--- a/kubernetes/common/postgres/templates/configmap.yaml
+++ b/kubernetes/common/postgres/templates/configmap.yaml
@@ -14,6 +14,7 @@
 # limitations under the License.
 */}}
 
+{{- if not .Values.global.postgres.useOperator }}
 apiVersion: v1
 kind: ConfigMap
 metadata:
@@ -26,4 +27,4 @@
     heritage: {{ .Release.Service }}
 data:
 {{ tpl (.Files.Glob "configs/*").AsConfig . | indent 2 }}
-
+{{- end }}
diff --git a/kubernetes/common/postgres/templates/deployment-primary.yaml b/kubernetes/common/postgres/templates/deployment-primary.yaml
index c8a0001..535eefa 100644
--- a/kubernetes/common/postgres/templates/deployment-primary.yaml
+++ b/kubernetes/common/postgres/templates/deployment-primary.yaml
@@ -13,4 +13,6 @@
 # # See the License for the specific language governing permissions and
 # # limitations under the License.
 */}}
+{{- if not .Values.global.postgres.useOperator }}
 {{ include "common.postgres.deployment" (dict "dot" . "pgMode" "primary") }}
+{{- end }}
\ No newline at end of file
diff --git a/kubernetes/common/postgres/templates/deployment-replica.yaml b/kubernetes/common/postgres/templates/deployment-replica.yaml
index dc19c2d..97c7e11 100644
--- a/kubernetes/common/postgres/templates/deployment-replica.yaml
+++ b/kubernetes/common/postgres/templates/deployment-replica.yaml
@@ -13,4 +13,6 @@
 # # See the License for the specific language governing permissions and
 # # limitations under the License.
 */}}
+{{- if not .Values.global.postgres.useOperator }}
 {{ include "common.postgres.deployment" (dict "dot" . "pgMode" "replica") }}
+{{- end }}
\ No newline at end of file
diff --git a/kubernetes/common/postgres/templates/metrics-svc-primary.yaml b/kubernetes/common/postgres/templates/metrics-svc-primary.yaml
index 790dd47..00a5182 100644
--- a/kubernetes/common/postgres/templates/metrics-svc-primary.yaml
+++ b/kubernetes/common/postgres/templates/metrics-svc-primary.yaml
@@ -14,6 +14,7 @@
 # limitations under the License.
 */}}
 
+{{- if not .Values.global.postgres.useOperator }}
 {{- if default false .Values.metrics.enabled }}
 apiVersion: v1
 kind: Service
@@ -33,4 +34,5 @@
   selector:
     name: {{ .Values.container.name.primary }}
     release: {{ include "common.release" . }}
+{{- end }}
 {{- end }}
\ No newline at end of file
diff --git a/kubernetes/common/postgres/templates/metrics-svc-replica.yaml b/kubernetes/common/postgres/templates/metrics-svc-replica.yaml
index 5aa8d76..b8b9e79 100644
--- a/kubernetes/common/postgres/templates/metrics-svc-replica.yaml
+++ b/kubernetes/common/postgres/templates/metrics-svc-replica.yaml
@@ -14,6 +14,7 @@
 # limitations under the License.
 */}}
 
+{{- if not .Values.global.postgres.useOperator }}
 {{- if default false .Values.metrics.enabled }}
 apiVersion: v1
 kind: Service
@@ -33,4 +34,5 @@
   selector:
     name: {{ .Values.container.name.replica }}
     release: {{ include "common.release" . }}
+{{- end }}
 {{- end }}
\ No newline at end of file
diff --git a/kubernetes/common/postgres/templates/postgres.yaml b/kubernetes/common/postgres/templates/postgres.yaml
new file mode 100644
index 0000000..aca6aa2
--- /dev/null
+++ b/kubernetes/common/postgres/templates/postgres.yaml
@@ -0,0 +1,19 @@
+{{/*
+# Copyright © 2023 Deutsche Telekom AG
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+*/}}
+
+{{- if .Values.global.postgres.useOperator }}
+{{ include "common.postgresOpInstance" . }}
+{{- end }}
\ No newline at end of file
diff --git a/kubernetes/common/postgres/templates/pv-primary.yaml b/kubernetes/common/postgres/templates/pv-primary.yaml
index e1ff1f9..8db79d6 100644
--- a/kubernetes/common/postgres/templates/pv-primary.yaml
+++ b/kubernetes/common/postgres/templates/pv-primary.yaml
@@ -13,6 +13,7 @@
 # # See the License for the specific language governing permissions and
 # # limitations under the License.
 */}}
+{{- if not .Values.global.postgres.useOperator }}
 {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) -}}
 {{- if include "common.needPV" . -}}
 kind: PersistentVolume
@@ -37,3 +38,4 @@
     path: {{ .Values.global.persistence.mountPath | default .Values.persistence.mountPath }}/{{ include "common.release" . }}/{{ .Values.persistence.mountSubPath }}/primary
 {{- end -}}
 {{- end -}}
+{{- end }}
\ No newline at end of file
diff --git a/kubernetes/common/postgres/templates/pv-replica.yaml b/kubernetes/common/postgres/templates/pv-replica.yaml
index d553c36..af46f61 100644
--- a/kubernetes/common/postgres/templates/pv-replica.yaml
+++ b/kubernetes/common/postgres/templates/pv-replica.yaml
@@ -13,6 +13,7 @@
 # # See the License for the specific language governing permissions and
 # # limitations under the License.
 */}}
+{{- if not .Values.global.postgres.useOperator }}
 {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) -}}
 {{- if include "common.needPV" . -}}
 kind: PersistentVolume
@@ -35,5 +36,6 @@
   persistentVolumeReclaimPolicy: {{ .Values.persistence.volumeReclaimPolicy }}
   hostPath:
     path: {{ .Values.global.persistence.mountPath | default .Values.persistence.mountPath }}/{{ include "common.release" . }}/{{ .Values.persistence.mountSubPath }}/replica
-{{- end -}}
-{{- end -}}
+{{- end }}
+{{- end }}
+{{- end }}
diff --git a/kubernetes/common/postgres/templates/pvc-primary.yaml b/kubernetes/common/postgres/templates/pvc-primary.yaml
index a47d3ed..6aaa529 100644
--- a/kubernetes/common/postgres/templates/pvc-primary.yaml
+++ b/kubernetes/common/postgres/templates/pvc-primary.yaml
@@ -14,6 +14,7 @@
 # limitations under the License.
 */}}
 
+{{- if not .Values.global.postgres.useOperator }}
 {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) -}}
 kind: PersistentVolumeClaim
 apiVersion: v1
@@ -41,4 +42,5 @@
 {{- else }}
   storageClassName: {{ include "common.storageClass" . }}
 {{- end }}
-{{- end -}}
+{{- end }}
+{{- end }}
diff --git a/kubernetes/common/postgres/templates/pvc-replica.yaml b/kubernetes/common/postgres/templates/pvc-replica.yaml
index 1e453fb..f59adf7 100644
--- a/kubernetes/common/postgres/templates/pvc-replica.yaml
+++ b/kubernetes/common/postgres/templates/pvc-replica.yaml
@@ -14,6 +14,7 @@
 # limitations under the License.
 */}}
 
+{{- if not .Values.global.postgres.useOperator }}
 {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) -}}
 kind: PersistentVolumeClaim
 apiVersion: v1
@@ -41,4 +42,5 @@
 {{- else }}
   storageClassName: {{ include "common.storageClass" . }}
 {{- end }}
-{{- end -}}
+{{- end }}
+{{- end }}
\ No newline at end of file
diff --git a/kubernetes/common/postgres/templates/service-common.yaml b/kubernetes/common/postgres/templates/service-common.yaml
index 6e74a06..6ec83dc 100644
--- a/kubernetes/common/postgres/templates/service-common.yaml
+++ b/kubernetes/common/postgres/templates/service-common.yaml
@@ -13,6 +13,7 @@
 # # See the License for the specific language governing permissions and
 # # limitations under the License.
 */}}
+{{- if not .Values.global.postgres.useOperator }}
 apiVersion: v1
 kind: Service
 metadata:
@@ -39,3 +40,4 @@
   selector:
     app: {{ include "common.name" . }}
     release: {{ include "common.release" . }}
+{{- end }}
diff --git a/kubernetes/common/postgres/templates/service-primary.yaml b/kubernetes/common/postgres/templates/service-primary.yaml
index 2965b7d..bc60d27 100644
--- a/kubernetes/common/postgres/templates/service-primary.yaml
+++ b/kubernetes/common/postgres/templates/service-primary.yaml
@@ -13,6 +13,7 @@
 # # See the License for the specific language governing permissions and
 # # limitations under the License.
 */}}
+{{- if not .Values.global.postgres.useOperator }}
 apiVersion: v1
 kind: Service
 metadata:
@@ -39,3 +40,4 @@
   selector:
     name: "{{.Values.container.name.primary}}"
     release: {{ include "common.release" . }}
+{{- end }}
diff --git a/kubernetes/common/postgres/templates/service-replica.yaml b/kubernetes/common/postgres/templates/service-replica.yaml
index d92a083..6869456 100644
--- a/kubernetes/common/postgres/templates/service-replica.yaml
+++ b/kubernetes/common/postgres/templates/service-replica.yaml
@@ -13,6 +13,7 @@
 # # See the License for the specific language governing permissions and
 # # limitations under the License.
 */}}
+{{- if not .Values.global.postgres.useOperator }}
 apiVersion: v1
 kind: Service
 metadata:
@@ -39,3 +40,4 @@
   selector:
     name: "{{.Values.container.name.replica}}"
     release: {{ include "common.release" . }}
+{{- end }}
\ No newline at end of file
diff --git a/kubernetes/common/postgres/templates/servicemonitor.yaml b/kubernetes/common/postgres/templates/servicemonitor.yaml
index 73faba6..522e515 100644
--- a/kubernetes/common/postgres/templates/servicemonitor.yaml
+++ b/kubernetes/common/postgres/templates/servicemonitor.yaml
@@ -13,7 +13,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 */}}
-
+{{- if not .Values.global.postgres.useOperator }}
 {{- if .Values.metrics.serviceMonitor.enabled }}
 {{  include "common.serviceMonitor" . }}
 {{- end }}
+{{- end }}
\ No newline at end of file
diff --git a/kubernetes/common/postgres/values.yaml b/kubernetes/common/postgres/values.yaml
index 51b7759..977e7d5 100644
--- a/kubernetes/common/postgres/values.yaml
+++ b/kubernetes/common/postgres/values.yaml
@@ -19,6 +19,9 @@
 global:
   nodePortPrefix: 302
   persistence: {}
+  postgres:
+    # flag to enable the DB creation via mariadb-operator
+    useOperator: false
 
 #################################################################
 # Secrets metaconfig
@@ -39,6 +42,27 @@
     password: '{{ .Values.config.pgPrimaryPassword }}'
 
 #################################################################
+# Postgres Operator configuration defaults.
+# Example: https://github.com/CrunchyData/postgres-operator-examples/tree/main/helm/postgres
+#################################################################
+postgresOperator:
+  postgresVersion: 16
+  # Possibility to override images
+  #imagePostgres:
+  #imagePgBouncer:
+  #imageExporter:
+  #imagePgBackRest:
+  instanceName: instance1
+  instanceReplicas: 2
+  #instanceStorageClassName:
+  instanceSize: 1Gi
+  #instanceCPU:
+  #instanceMemory:
+  bouncerReplicas: 2
+  monitoring: true
+  #monitoringConfig: {}
+
+#################################################################
 # Application configuration defaults.
 #################################################################