[COMMON/VID] Modified VID to use Shared DB.

This change modifies VID to use the shared DB instance of mariadb.
In addition, we create a mechanism in mariadb-init to allow a shared DB
user to run a script after the DB has been created.
The script can be passed as a configMap (as is the case with VID) or it
can be a standalone script that is passed as a string.

Issue-ID: OOM-1226
Change-Id: Ie51823338562a6fbe7472025d62cd4dba688168d
Signed-off-by: RPMishra <rpmishra@aarnanetworks.com>
Signed-off-by: Sylvain Desbureaux <sylvain.desbureaux@orange.com>
diff --git a/kubernetes/common/mariadb-galera/values.yaml b/kubernetes/common/mariadb-galera/values.yaml
index 8a46098..55606ea 100644
--- a/kubernetes/common/mariadb-galera/values.yaml
+++ b/kubernetes/common/mariadb-galera/values.yaml
@@ -131,10 +131,10 @@
 ## Configure MariaDB-Galera with a custom my.cnf file
 ## ref: https://mariadb.com/kb/en/mariadb/configuring-mariadb-with-mycnf/#example-of-configuration-file
 ##
-externalConfig: ""
-# externalConfig: |-
-  # [mysqld]
-  # innodb_buffer_pool_size=2G
+#externalConfig: ""
+externalConfig: |-
+   [mysqld]
+   lower_case_table_names = 1
 
 #resources: {}
   # We usually recommend not to specify default resources and to leave this as a conscious
diff --git a/kubernetes/common/mariadb-init/templates/configmap.yaml b/kubernetes/common/mariadb-init/templates/configmap.yaml
index d021d60..6708efd 100644
--- a/kubernetes/common/mariadb-init/templates/configmap.yaml
+++ b/kubernetes/common/mariadb-init/templates/configmap.yaml
@@ -27,3 +27,19 @@
     heritage: {{ .Release.Service }}
 data:
 {{ tpl (.Files.Glob "resources/config/*").AsConfig . | indent 2 }}
+---
+{{ if .Values.dbScript }}
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ include "common.fullname" . }}-dbscript
+  namespace: {{ include "common.namespace" . }}
+  labels:
+    app: {{ include "common.name" . }}
+    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
+    release: {{ include "common.release" . }}
+    heritage: {{ .Release.Service }}
+data:
+  db_cmd.sh: |
+    {{ tpl .Values.dbScript . | indent 4 }}
+{{- end }}
diff --git a/kubernetes/common/mariadb-init/templates/job.yaml b/kubernetes/common/mariadb-init/templates/job.yaml
index cccb118..fb06568 100644
--- a/kubernetes/common/mariadb-init/templates/job.yaml
+++ b/kubernetes/common/mariadb-init/templates/job.yaml
@@ -56,8 +56,11 @@
         image: "{{ include "common.repository" . }}/{{ .Values.image }}"
         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
         command:
-        - bash
-        - /db_init/db_init.sh
+          - /bin/sh
+          - -c
+          - |
+            /db_init/db_init.sh {{ if or .Values.dbScriptConfigMap .Values.dbScript }} &&
+            /db_config/db_cmd.sh{{ end }}
         env:
         - name: DB_HOST
           value: "{{ default .Values.global.mariadbGalera.nameOverride .Values.mariadbGalera.serviceName }}"
@@ -82,7 +85,10 @@
           readOnly: true
         - name: mariadb-conf
           mountPath: /db_init/
-          readOnly: true
+{{- if or .Values.dbScriptConfigMap .Values.dbScript }}
+        - name: mariadb-init
+          mountPath: /db_config/
+{{- end }}
         resources:
 {{ include "common.resources" . | indent 12 }}
       {{- if .Values.nodeSelector }}
@@ -94,12 +100,23 @@
 {{ toYaml .Values.affinity | indent 10 }}
       {{- end }}
       volumes:
-      - name: mariadb-conf
-        configMap:
-          name: {{ include "mariadbInit.configMap" . }}
       - name: localtime
         hostPath:
           path: /etc/localtime
+{{- if  or .Values.dbScriptConfigMap .Values.dbScript }}
+      - name: mariadb-init
+        configMap:
+{{-   if  .Values.dbScriptConfigMap }}
+          name: {{ tpl .Values.dbScriptConfigMap . }}
+{{-   else -}}
+          name: {{ include "common.fullname" . }}-dbscript
+{{-   end }}
+          defaultMode: 0755
+{{- end }}
+      - name: mariadb-conf
+        configMap:
+          name: {{ include "mariadbInit.configMap" . }}
+          defaultMode: 0755
       restartPolicy: Never
       imagePullSecrets:
       - name: "{{ include "common.namespace" . }}-docker-registry-key"
diff --git a/kubernetes/common/mariadb-init/values.yaml b/kubernetes/common/mariadb-init/values.yaml
index dd5d208..42a1513 100644
--- a/kubernetes/common/mariadb-init/values.yaml
+++ b/kubernetes/common/mariadb-init/values.yaml
@@ -48,6 +48,13 @@
 image: mariadb:10.1.38
 pullPolicy: IfNotPresent
 
+# These two values are used to supply commands that are run after the DB is created.
+# Components using the shared DB can either pass a string which has a set of commands
+# or a config map that contains a shell script. If both are specified only the config
+# map will be executed. For reference, please see the VID components for config map
+dbScript: ""
+dbScriptConfigMap: ""
+
 # Set it if you want to change the name of the different components
 # nameOverride:
 
diff --git a/kubernetes/vid/requirements.yaml b/kubernetes/vid/requirements.yaml
index e7764e0..ed8b805 100644
--- a/kubernetes/vid/requirements.yaml
+++ b/kubernetes/vid/requirements.yaml
@@ -22,3 +22,9 @@
   - name: mariadb-galera
     version: ~6.x-0
     repository: '@local'
+    condition: global.mariadbGalera.localCluster
+  - name: mariadb-init
+    version: ~6.x-0
+    repository: '@local'
+    condition: not global.mariadbGalera.localCluster
+
diff --git a/kubernetes/vid/resources/config/db_cmd.sh b/kubernetes/vid/resources/config/db_cmd.sh
old mode 100644
new mode 100755
index e573c02..efd92b2
--- a/kubernetes/vid/resources/config/db_cmd.sh
+++ b/kubernetes/vid/resources/config/db_cmd.sh
@@ -1,6 +1,7 @@
 #!/bin/sh
 {{/*
 # Copyright © 2018 AT&T
+# Copyright © 2020 Aarna Networks
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -15,10 +16,29 @@
 # limitations under the License.
 */}}
 
-echo "Going to run mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -h${MYSQL_HOST} -P${MYSQL_PORT} ..."
-mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -h${MYSQL_HOST} -P${MYSQL_PORT} < /db-config/vid-pre-init.sql
+DB={{index .Values "mariadb-galera" "config" "mysqlDatabase" | upper }}
+eval "MYSQL_USER=\$MYSQL_USER_${DB}"
+eval "MYSQL_PASSWORD=\$MYSQL_PASSWORD_${DB}"
+
+#echo "Going to run mysql ${DB} -u${MYSQL_USER} -p${MYSQL_PASSWORD} -h${DB_HOST} -P${DB_PORT} ..."
+mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -h${DB_HOST} -P${DB_PORT} <<'EOD'
+CREATE TABLE IF NOT EXISTS `{{index .Values "mariadb-galera" "config" "mysqlDatabase" }}`.`schema_info` (
+`SCHEMA_ID` VARCHAR(25) NOT NULL,
+`SCHEMA_DESC` VARCHAR(75) NOT NULL,
+`DATASOURCE_TYPE` VARCHAR(100) NULL DEFAULT NULL,
+`CONNECTION_URL` VARCHAR(200) NOT NULL,
+`USER_NAME` VARCHAR(45) NOT NULL,
+`PASSWORD` VARCHAR(45) NULL DEFAULT NULL,
+`DRIVER_CLASS` VARCHAR(100) NOT NULL,
+`MIN_POOL_SIZE` INT(11) NOT NULL,
+`MAX_POOL_SIZE` INT(11) NOT NULL,
+`IDLE_CONNECTION_TEST_PERIOD` INT(11) NOT NULL)
+ENGINE = InnoDB
+DEFAULT CHARACTER SET = utf8;
+EOD
+
 if [ $? -ne 0 ];then
-        echo "ERROR: Failed to run ${cmd} vid-pre-init.sql"
+        echo "ERROR: Failed to run cmd vid-pre-init.sql"
         exit 1
 else
         echo "INFO: Database initialized successfully"
diff --git a/kubernetes/vid/resources/config/vid-pre-init.sql b/kubernetes/vid/resources/config/vid-pre-init.sql
deleted file mode 100644
index 2dbbbcc..0000000
--- a/kubernetes/vid/resources/config/vid-pre-init.sql
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
-# Copyright © 2018 AT&T
-#
-# 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.
-*/
-
-CREATE TABLE IF NOT EXISTS `vid_openecomp_epsdk`.`schema_info` (
-`SCHEMA_ID` VARCHAR(25) NOT NULL,
-`SCHEMA_DESC` VARCHAR(75) NOT NULL,
-`DATASOURCE_TYPE` VARCHAR(100) NULL DEFAULT NULL,
-`CONNECTION_URL` VARCHAR(200) NOT NULL,
-`USER_NAME` VARCHAR(45) NOT NULL,
-`PASSWORD` VARCHAR(45) NULL DEFAULT NULL,
-`DRIVER_CLASS` VARCHAR(100) NOT NULL,
-`MIN_POOL_SIZE` INT(11) NOT NULL,
-`MAX_POOL_SIZE` INT(11) NOT NULL,
-`IDLE_CONNECTION_TEST_PERIOD` INT(11) NOT NULL)
-ENGINE = InnoDB
-DEFAULT CHARACTER SET = utf8;
\ No newline at end of file
diff --git a/kubernetes/vid/templates/configmap.yaml b/kubernetes/vid/templates/configmap.yaml
index b1509a9..0ba466d 100644
--- a/kubernetes/vid/templates/configmap.yaml
+++ b/kubernetes/vid/templates/configmap.yaml
@@ -43,7 +43,7 @@
 apiVersion: v1
 kind: ConfigMap
 metadata:
-  name: {{ include "common.fullname" . }}
+  name: {{ include "common.fullname" . }}-db-init
   namespace: {{ include "common.namespace" . }}
   labels:
     app: {{ include "common.name" . }}
@@ -51,4 +51,4 @@
     release: {{ include "common.release" . }}
     heritage: {{ .Release.Service }}
 data:
-{{ tpl (.Files.Glob "resources/config/*").AsConfig . | indent 2 }}
\ No newline at end of file
+{{ tpl (.Files.Glob "resources/config/db_cmd.sh").AsConfig . | indent 2 }}
diff --git a/kubernetes/vid/templates/deployment.yaml b/kubernetes/vid/templates/deployment.yaml
index 5423feb..41b0019 100644
--- a/kubernetes/vid/templates/deployment.yaml
+++ b/kubernetes/vid/templates/deployment.yaml
@@ -41,7 +41,7 @@
         - /app/ready.py
         args:
         - --job-name
-        - {{ include "common.fullname" . }}-galera-config
+        - {{ include "common.fullname" . }}-mariadb-init-config-job
         env:
         - name: NAMESPACE
           valueFrom:
@@ -105,9 +105,9 @@
             - name: VID_UEB_URL_LIST
               value: message-router.{{ include "common.namespace" . }}
             - name: VID_MYSQL_HOST
-              value: {{ index .Values "mariadb-galera" "service" "name" }}
+              value: {{  include "common.mariadbService" . }}
             - name: VID_MYSQL_PORT
-              value: "{{ index .Values "mariadb-galera" "service" "internalPort" }}"
+              value: "{{ include "common.mariadbPort" . }}"
             - name: VID_MYSQL_DBNAME
               value: {{ index .Values "mariadb-galera" "config" "mysqlDatabase" }}
             - name: VID_MYSQL_USER
diff --git a/kubernetes/vid/templates/job.yaml b/kubernetes/vid/templates/job.yaml
deleted file mode 100644
index b051cde..0000000
--- a/kubernetes/vid/templates/job.yaml
+++ /dev/null
@@ -1,82 +0,0 @@
-{{/*
-# Copyright © 2018 Amdocs, Bell Canada
-# Copyright © 2020 Samsung Electronics
-#
-# 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.
-*/}}
-
-apiVersion: batch/v1
-kind: Job
-metadata:
-  name: {{ include "common.fullname" . }}-galera-config
-  namespace: {{ include "common.namespace" . }}
-  labels:
-    app: {{ include "common.name" . }}-job
-    release: {{ include "common.release" . }}
-spec:
-  template:
-    metadata:
-      labels:
-        app: {{ include "common.name" . }}-job
-        release: {{ include "common.release" . }}
-    spec:
-      initContainers:
-#This container checks that all galera instances are up before initializing it.
-      - name: {{ include "common.name" . }}-readiness
-        image: "{{ include "common.repository" . }}/{{ .Values.global.readinessImage }}"
-        imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
-        command:
-        - /app/ready.py
-        - --container-name
-        - {{ index .Values "mariadb-galera" "service" "name" }}
-        env:
-        - name: NAMESPACE
-          valueFrom:
-            fieldRef:
-              apiVersion: v1
-              fieldPath: metadata.namespace
-      containers:
-      - name: {{ include "common.name" . }}-job
-        image: {{ .Values.mariadb_image }}
-        imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
-        volumeMounts:
-        - mountPath: /dbcmd-config/db_cmd.sh
-          name: {{ include "common.fullname" . }}-config
-          subPath: db_cmd.sh
-        - mountPath: /db-config/vid-pre-init.sql
-          name: {{ include "common.fullname" . }}-config
-          subPath: vid-pre-init.sql
-        command:
-        - /bin/sh
-        args:
-        - -x
-        - /dbcmd-config/db_cmd.sh
-        env:
-        - name: MYSQL_PASSWORD
-          {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "vid-db-user-secret" "key" "password") | indent 10 }}
-        - name: MYSQL_HOST
-          value: {{ index .Values "mariadb-galera" "service" "name" }}
-        - name: MYSQL_USER
-          {{- include "common.secret.envFromSecretFast" (dict "global" . "uid" "vid-db-user-secret" "key" "login") | indent 10 }}
-        - name: MYSQL_PORT
-          value: "{{ index .Values "mariadb-galera" "service" "internalPort" }}"
-      restartPolicy: Never
-      volumes:
-        - name: {{ include "common.fullname" . }}-config
-          configMap:
-            name: {{ include "common.fullname" . }}
-            items:
-              - key: db_cmd.sh
-                path: db_cmd.sh
-              - key: vid-pre-init.sql
-                path: vid-pre-init.sql
diff --git a/kubernetes/vid/values.yaml b/kubernetes/vid/values.yaml
index 3c9b8ce..93de57e 100644
--- a/kubernetes/vid/values.yaml
+++ b/kubernetes/vid/values.yaml
@@ -21,6 +21,12 @@
   readinessImage: onap/oom/readiness:3.0.1
   loggingRepository: docker.elastic.co
   loggingImage: beats/filebeat:5.5.0
+  mariadbGalera: &mariadbGalera
+    #This flag allows VID to instantiate its own mariadb-galera cluster
+    localCluster: false
+    service: mariadb-galera
+    internalPort: 3306
+    nameOverride: mariadb-galera
 
 #################################################################
 # Secrets metaconfig
@@ -41,9 +47,6 @@
 image: onap/vid:7.0.0
 pullPolicy: Always
 
-# mariadb image for initializing
-mariadb_image: library/mariadb:10
-
 # application configuration
 config:
   db:
@@ -67,7 +70,9 @@
   roleaccesscentralized: remote
 
 mariadb-galera:
-  config:
+ # '&mariadbConfig' means we "store" the values for  later use in the file
+ # with '*mariadbConfig' pointer.
+  config: &mariadbConfig
     userCredentialsExternalSecret: '{{ include "common.release" . }}-vid-db-user-secret'
     mysqlDatabase: vid_openecomp_epsdk
   nameOverride: vid-galera
@@ -83,6 +88,13 @@
     [mysqld]
     lower_case_table_names = 1
 
+mariadb-init:
+  config: *mariadbConfig
+  nameOverride: vid-mariadb-init
+  # A configMap of same name is created. It points to file that will be run after
+  # The DB has been created.
+  dbScriptConfigMap: '{{ include "common.release" . }}-vid-db-init'
+
 # default number of instances
 replicaCount: 1