Merge "[COMMON] Update MariaDB readinessChecks to fix Operator issues"
diff --git a/kubernetes/cds/components/cds-blueprints-processor/templates/deployment.yaml b/kubernetes/cds/components/cds-blueprints-processor/templates/deployment.yaml
index a6e3a52..761b031 100755
--- a/kubernetes/cds/components/cds-blueprints-processor/templates/deployment.yaml
+++ b/kubernetes/cds/components/cds-blueprints-processor/templates/deployment.yaml
@@ -37,7 +37,19 @@
     metadata: {{- include "common.templateMetadata" . | nindent 6 }}
     spec:
       initContainers:
-      {{ include "common.readinessCheck.waitFor" . | nindent 6 }}
+      {{- if .Values.global.mariadbGalera.localCluster }}
+      {{-   if .Values.global.mariadbGalera.useOperator }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_local_operator ) | indent 6 | trim }}
+      {{    else }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_local ) | indent 6 | trim }}
+      {{-   end }}
+      {{ else }}
+      {{-   if .Values.global.mariadbGalera.useOperator }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_global_operator ) | indent 6 | trim }}
+      {{   else }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_global ) | indent 6 | trim }}
+      {{-   end }}
+      {{- end }}
       - command:
         - sh
         args:
diff --git a/kubernetes/cds/components/cds-blueprints-processor/values.yaml b/kubernetes/cds/components/cds-blueprints-processor/values.yaml
index 3d931d3..a32afb0 100755
--- a/kubernetes/cds/components/cds-blueprints-processor/values.yaml
+++ b/kubernetes/cds/components/cds-blueprints-processor/values.yaml
@@ -18,6 +18,17 @@
 # Global configuration defaults.
 #################################################################
 global:
+  mariadbGalera: &mariadbGalera
+    # flag to enable the DB creation via mariadb-operator
+    useOperator: true
+    #This flag allows NBI to instantiate its own mariadb-galera cluster
+    #When changing it to "true", also set "globalCluster: false"
+    #as the dependency check will not work otherwise (Chart.yaml)
+    localCluster: true
+    globalCluster: false
+    service: mariadb-galera
+    internalPort: 3306
+    nameOverride: mariadb-galera
   # image pull policy
   pullPolicy: Always
   persistence:
@@ -218,9 +229,18 @@
   unlimited: {}
 
 readinessCheck:
-  wait_for:
+  wait_for_global_operator:
+    pods:
+      - '{{ .Values.global.mariadbGalera.nameOverride }}-0'
+  wait_for_local_operator:
+    pods:
+      - '{{ .Values.config.cdsDB.dbServer }}-0'
+  wait_for_global:
     apps:
-      - cds-db
+      - '{{ .Values.global.mariadbGalera.nameOverride }}'
+  wait_for_local:
+    apps:
+      - '{{ .Values.config.cdsDB.dbServer }}'
 
 #Pods Service Account
 serviceAccount:
diff --git a/kubernetes/common/mariadb-init/Chart.yaml b/kubernetes/common/mariadb-init/Chart.yaml
index fe13457..fa89a66 100644
--- a/kubernetes/common/mariadb-init/Chart.yaml
+++ b/kubernetes/common/mariadb-init/Chart.yaml
@@ -28,4 +28,7 @@
     repository: 'file://../repositoryGenerator'
   - name: serviceAccount
     version: ~13.x-0
-    repository: '@local'
\ No newline at end of file
+    repository: '@local'
+  - name: readinessCheck
+    version: ~13.x-0
+    repository: '@local'
diff --git a/kubernetes/common/mariadb-init/templates/job.yaml b/kubernetes/common/mariadb-init/templates/job.yaml
index 288ea8e..0180fec 100644
--- a/kubernetes/common/mariadb-init/templates/job.yaml
+++ b/kubernetes/common/mariadb-init/templates/job.yaml
@@ -37,27 +37,19 @@
       name: {{ include "common.name" . }}
     spec:
       initContainers:
-      - name: {{ include "common.name" . }}-readiness
-        command:
-        - /app/ready.py
-        args:
-        - --app-name
-        - {{ default .Values.global.mariadbGalera.nameOverride .Values.mariadbGalera.containerName }}
-        env:
-        - name: NAMESPACE
-          valueFrom:
-            fieldRef:
-              apiVersion: v1
-              fieldPath: metadata.namespace
-        image: {{ include "repositoryGenerator.image.readiness" . }}
-        imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
-        resources:
-          limits:
-            cpu: "100m"
-            memory: "0.5Gi"
-          requests:
-            cpu: "3m"
-            memory: "0.02Gi"
+      {{- if .Values.global.mariadbGalera.localCluster }}
+      {{-   if .Values.global.mariadbGalera.useOperator }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_local_operator ) | indent 6 | trim }}
+      {{    else }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_local ) | indent 6 | trim }}
+      {{-   end }}
+      {{ else }}
+      {{-   if .Values.global.mariadbGalera.useOperator }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_global_operator ) | indent 6 | trim }}
+      {{   else }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_global ) | indent 6 | trim }}
+      {{-   end }}
+      {{- end }}
       containers:
       - name: {{ include "common.name" . }}
         image: {{ include "repositoryGenerator.image.mariadb" . }}
diff --git a/kubernetes/common/mariadb-init/values.yaml b/kubernetes/common/mariadb-init/values.yaml
index 9a4b4b3..e0ba282 100644
--- a/kubernetes/common/mariadb-init/values.yaml
+++ b/kubernetes/common/mariadb-init/values.yaml
@@ -148,3 +148,17 @@
 wait_for_job_container:
   containers:
     - '{{ include "common.name" . }}'
+
+readinessCheck:
+  wait_for_global_operator:
+    pods:
+      - '{{ .Values.global.mariadbGalera.nameOverride }}-0'
+  wait_for_local_operator:
+    pods:
+      - '{{ index .Values "mariadb-galera" "nameOverride" }}-0'
+  wait_for_global:
+    apps:
+      - '{{ include "common.mariadbAppName" . }}'
+  wait_for_local:
+    apps:
+      - '{{ include "common.mariadbAppName" . }}'
diff --git a/kubernetes/policy/templates/job.yaml b/kubernetes/policy/templates/job.yaml
index 442314c..2a61429 100755
--- a/kubernetes/policy/templates/job.yaml
+++ b/kubernetes/policy/templates/job.yaml
@@ -36,26 +36,19 @@
       imagePullSecrets:
       - name: "{{ include "common.namespace" . }}-docker-registry-key"
       initContainers:
-      - name: {{ include "common.name" . }}-mariadb-readiness
-        image: {{ include "repositoryGenerator.image.readiness" . }}
-        imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
-        command:
-        - /app/ready.py
-        - --app-name
-        - {{ index .Values "mariadb-galera" "service" "name" }}
-        env:
-        - name: NAMESPACE
-          valueFrom:
-            fieldRef:
-              apiVersion: v1
-              fieldPath: metadata.namespace
-        resources:
-          limits:
-            cpu: "100m"
-            memory: "0.5Gi"
-          requests:
-            cpu: "3m"
-            memory: "0.02Gi"
+      {{- if .Values.global.mariadbGalera.localCluster }}
+      {{-   if .Values.global.mariadbGalera.useOperator }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_local_operator ) | indent 6 | trim }}
+      {{    else }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_local ) | indent 6 | trim }}
+      {{-   end }}
+      {{ else }}
+      {{-   if .Values.global.mariadbGalera.useOperator }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_global_operator ) | indent 6 | trim }}
+      {{   else }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_global ) | indent 6 | trim }}
+      {{-   end }}
+      {{- end }}
       containers:
       - name: {{ include "common.name" . }}-galera-config
         image: {{ include "repositoryGenerator.image.mariadb" . }}
diff --git a/kubernetes/policy/values.yaml b/kubernetes/policy/values.yaml
index 8dca11d..9d08080 100755
--- a/kubernetes/policy/values.yaml
+++ b/kubernetes/policy/values.yaml
@@ -275,6 +275,18 @@
 readinessCheck:
   wait_for:
     - '{{ ternary .Values.postgres.service.name "postgres" .Values.global.postgres.localCluster }}'
+  wait_for_global_operator:
+    pods:
+      - '{{ .Values.global.mariadbGalera.nameOverride }}-0'
+  wait_for_local_operator:
+    pods:
+      - '{{ index .Values "mariadb-galera" "nameOverride" }}-0'
+  wait_for_global:
+    apps:
+      - '{{ include "common.mariadbAppName" . }}'
+  wait_for_local:
+    apps:
+      - '{{ include "common.mariadbAppName" . }}'
 
 restServer:
   policyPapUserName: policyadmin
diff --git a/kubernetes/sdnc/Chart.yaml b/kubernetes/sdnc/Chart.yaml
index 6229915..4a0c9eb 100644
--- a/kubernetes/sdnc/Chart.yaml
+++ b/kubernetes/sdnc/Chart.yaml
@@ -72,3 +72,6 @@
   - name: serviceAccount
     version: ~13.x-0
     repository: '@local'
+  - name: readinessCheck
+    version: ~13.x-0
+    repository: '@local'
diff --git a/kubernetes/sdnc/templates/job.yaml b/kubernetes/sdnc/templates/job.yaml
index 451c5b0..db33114 100755
--- a/kubernetes/sdnc/templates/job.yaml
+++ b/kubernetes/sdnc/templates/job.yaml
@@ -76,27 +76,19 @@
         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
         name: {{ include "common.name" . }}-update-config
 
-      - name: {{ include "common.name" . }}-readiness
-        command:
-        - /app/ready.py
-        args:
-        - --app-name
-        - {{ include "common.mariadbAppName" . }}
-        env:
-        - name: NAMESPACE
-          valueFrom:
-            fieldRef:
-              apiVersion: v1
-              fieldPath: metadata.namespace
-        image: {{ include "repositoryGenerator.image.readiness" . }}
-        imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
-        resources:
-          limits:
-            cpu: "100m"
-            memory: "0.5Gi"
-          requests:
-            cpu: "3m"
-            memory: "0.02Gi"
+      {{- if .Values.global.mariadbGalera.localCluster }}
+      {{-   if .Values.global.mariadbGalera.useOperator }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_local_operator ) | indent 6 | trim }}
+      {{    else }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_local ) | indent 6 | trim }}
+      {{-   end }}
+      {{ else }}
+      {{-   if .Values.global.mariadbGalera.useOperator }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_global_operator ) | indent 6 | trim }}
+      {{   else }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_global ) | indent 6 | trim }}
+      {{-   end }}
+      {{- end }}
       containers:
       - name: {{ include "common.name" . }}
         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.image }}
diff --git a/kubernetes/sdnc/values.yaml b/kubernetes/sdnc/values.yaml
index 55f6a1d..1c7fa9e 100644
--- a/kubernetes/sdnc/values.yaml
+++ b/kubernetes/sdnc/values.yaml
@@ -690,3 +690,17 @@
 #Log configuration
 log:
   path: /var/log/onap
+
+readinessCheck:
+  wait_for_global_operator:
+    pods:
+      - '{{ .Values.global.mariadbGalera.nameOverride }}-0'
+  wait_for_local_operator:
+    pods:
+      - '{{ index .Values "mariadb-galera" "nameOverride" }}-0'
+  wait_for_global:
+    apps:
+      - '{{ include "common.mariadbAppName" . }}'
+  wait_for_local:
+    apps:
+      - '{{ include "common.mariadbAppName" . }}'
diff --git a/kubernetes/so/components/so-mariadb/Chart.yaml b/kubernetes/so/components/so-mariadb/Chart.yaml
index cb82906..f66f740 100755
--- a/kubernetes/so/components/so-mariadb/Chart.yaml
+++ b/kubernetes/so/components/so-mariadb/Chart.yaml
@@ -28,6 +28,9 @@
   - name: repositoryGenerator
     version: ~13.x-0
     repository: '@local'
+  - name: readinessCheck
+    version: ~13.x-0
+    repository: '@local'
   - name: serviceAccount
     version: ~13.x-0
     repository: '@local'
diff --git a/kubernetes/so/components/so-mariadb/templates/job.yaml b/kubernetes/so/components/so-mariadb/templates/job.yaml
index abbe599..a2edf33 100644
--- a/kubernetes/so/components/so-mariadb/templates/job.yaml
+++ b/kubernetes/so/components/so-mariadb/templates/job.yaml
@@ -102,27 +102,19 @@
       name: {{ include "common.name" . }}
     spec:
       initContainers:
-      - name: {{ include "common.name" . }}-readiness
-        command:
-        - /app/ready.py
-        args:
-        - --app-name
-        - {{ include "common.mariadbAppName" . }}
-        env:
-        - name: NAMESPACE
-          valueFrom:
-            fieldRef:
-              apiVersion: v1
-              fieldPath: metadata.namespace
-        image: {{ include "repositoryGenerator.image.readiness" . }}
-        imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
-        resources:
-          limits:
-            cpu: "100m"
-            memory: "0.5Gi"
-          requests:
-            cpu: "3m"
-            memory: "0.02Gi"
+      {{- if .Values.global.mariadbGalera.localCluster }}
+      {{-   if .Values.global.mariadbGalera.useOperator }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_local_operator ) | indent 6 | trim }}
+      {{    else }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_local ) | indent 6 | trim }}
+      {{-   end }}
+      {{ else }}
+      {{-   if .Values.global.mariadbGalera.useOperator }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_global_operator ) | indent 6 | trim }}
+      {{   else }}
+      {{ include "common.readinessCheck.waitFor" (dict "dot" . "wait_for" .Values.readinessCheck.wait_for_global ) | indent 6 | trim }}
+      {{-   end }}
+      {{- end }}
       containers:
       - name: {{ include "common.name" . }}-config
         image: {{ include "repositoryGenerator.repository" . }}/{{ .Values.image }}
diff --git a/kubernetes/so/components/so-mariadb/values.yaml b/kubernetes/so/components/so-mariadb/values.yaml
index 38b4d3f..6fdfd75 100755
--- a/kubernetes/so/components/so-mariadb/values.yaml
+++ b/kubernetes/so/components/so-mariadb/values.yaml
@@ -207,3 +207,17 @@
 wait_for_job_container:
   containers:
     - '{{ include "common.name" . }}-config'
+
+readinessCheck:
+  wait_for_global_operator:
+    pods:
+      - '{{ .Values.global.mariadbGalera.nameOverride }}-0'
+  wait_for_local_operator:
+    pods:
+      - '{{ index .Values "mariadb-galera" "nameOverride" }}-0'
+  wait_for_global:
+    apps:
+      - '{{ include "common.mariadbAppName" . }}'
+  wait_for_local:
+    apps:
+      - '{{ include "common.mariadbAppName" . }}'