From 5af50ff7f4e65a89bd69b5774d2fe81d7fd8615d Mon Sep 17 00:00:00 2001
From: "Danylo Vanin (EPAM)" <danylo_vanin@epam.com>
Date: Fri, 1 Jul 2022 11:02:08 +0000
Subject: [PATCH] [GONRG-5111] Refactor Helm

---
 devops/gcp/configmap/README.md                | 74 +++++++++++++++++++
 devops/gcp/configmap/templates/configmap.yaml | 16 ++++
 .../templates/notification-configmap.yaml     | 16 ----
 devops/gcp/configmap/values.yaml              | 16 ++--
 devops/gcp/deploy/README.md                   | 70 ++++++++++++++++++
 devops/gcp/deploy/templates/deployment.yaml   | 24 +++---
 .../gcp/deploy/templates/service-account.yaml |  2 +-
 devops/gcp/deploy/templates/service.yaml      | 10 +--
 .../gcp/deploy/templates/virtual-service.yaml |  8 +-
 devops/gcp/deploy/values.yaml                 | 16 ++--
 .../src/main/resources/application.properties | 17 ++++-
 11 files changed, 212 insertions(+), 57 deletions(-)
 create mode 100644 devops/gcp/configmap/README.md
 create mode 100644 devops/gcp/configmap/templates/configmap.yaml
 delete mode 100644 devops/gcp/configmap/templates/notification-configmap.yaml
 create mode 100644 devops/gcp/deploy/README.md

diff --git a/devops/gcp/configmap/README.md b/devops/gcp/configmap/README.md
new file mode 100644
index 000000000..ce6bc6d02
--- /dev/null
+++ b/devops/gcp/configmap/README.md
@@ -0,0 +1,74 @@
+<!--- Configmap -->
+
+# Configmap helm chart
+
+## Introduction
+
+This chart bootstraps a configmap deployment on a [Kubernetes](https://kubernetes.io) cluster using [Helm](https://helm.sh) package manager.
+
+## Prerequisites
+
+The code was tested on **Kubernetes cluster** (v1.21.11) with **Istio** (1.12.6)
+> It is possible to use other versions, but it hasn't been tested
+
+### Operation system
+
+The code works in Debian-based Linux (Debian 10 and Ubuntu 20.04) and Windows WSL 2. Also, it works but is not guaranteed in Google Cloud Shell. All other operating systems, including macOS, are not verified and supported.
+
+### Packages
+
+Packages are only needed for installation from a local computer.
+
+- **HELM** (version: v3.7.1 or higher) [helm](https://helm.sh/docs/intro/install/)
+- **Kubectl** (version: v1.21.0 or higher) [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl)
+## Installation
+This Helm chart should be installed before [deploy Helm Chart](../deploy)
+First you need to set variables in **values.yaml** file using any code editor. Some of the values are prefilled, but you need to specify some values as well. You can find more information about them below.
+
+### Common variables
+
+| Name | Description | Type | Default |Required |
+|------|-------------|------|---------|---------|
+**logLevel** | logging level | string | INFO | yes
+**springProfilesActive** | active spring profile | string | gcp | yes
+
+### GCP variables
+
+| Name | Description | Type | Default |Required |
+|------|-------------|------|---------|---------|
+**entitlementsHost** | entitlements service host address | string | `http://entitlements` | yes
+**registerHost** | register service host address | string | `http://register` | yes
+**partitionHost** | partition service host address | string | `http://partition` | yes
+**googleAudiences** | your GCP client ID | string | - | yes
+
+> googleAudiences: If you are connected to GCP console with `gcloud auth application-default login --no-browser` from your terminal, you can get your client_id using the command:
+
+```console
+cat ~/.config/gcloud/application_default_credentials.json | grep client_id
+```
+
+### Config variables
+
+| Name | Description | Type | Default |Required |
+|------|-------------|------|---------|---------|
+**configmap** | configmap name | string | notification-config | yes
+**appName** | name of the app | string | notification | yes
+**onPremEnabled** | whether on-prem is enabled | boolean | false | yes
+
+### Install the helm chart
+
+Run this command from within this directory:
+
+```bash
+helm install gcp-notification-configmap .
+```
+
+## Uninstalling the Chart
+
+To uninstall the helm deployment:
+
+```bash
+helm uninstall gcp-notification-configmap
+```
+
+[Move-to-Top](#configmap-helm-chart)
diff --git a/devops/gcp/configmap/templates/configmap.yaml b/devops/gcp/configmap/templates/configmap.yaml
new file mode 100644
index 000000000..237389ec3
--- /dev/null
+++ b/devops/gcp/configmap/templates/configmap.yaml
@@ -0,0 +1,16 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  labels:
+    app: "{{ .Values.conf.appName }}"
+  name: "{{ .Values.conf.configmap }}"
+  namespace: "{{ .Release.Namespace }}"
+data:
+  LOG_LEVEL: "{{ .Values.data.logLevel }}"
+  SPRING_PROFILES_ACTIVE: "{{ .Values.data.springProfilesActive }}"
+  ENTITLEMENTS_HOST: "{{ .Values.data.entitlementsHost }}"
+  REGISTER_HOST: "{{ .Values.data.registerHost }}"
+  PARTITION_HOST: "{{ .Values.data.partitionHost }}"
+  {{- if not $.Values.conf.onPremEnabled }}
+  GOOGLE_AUDIENCES: "{{ .Values.data.googleAudiences }}"
+  {{- end }}
diff --git a/devops/gcp/configmap/templates/notification-configmap.yaml b/devops/gcp/configmap/templates/notification-configmap.yaml
deleted file mode 100644
index c3c1cbbf8..000000000
--- a/devops/gcp/configmap/templates/notification-configmap.yaml
+++ /dev/null
@@ -1,16 +0,0 @@
-apiVersion: v1
-kind: ConfigMap
-metadata:
-  labels:
-    app: "{{ .Values.conf.app_name }}"
-  name: "{{ .Values.conf.configmap }}"
-  namespace: "{{ .Release.Namespace }}"
-data:
-  LOG_LEVEL: "{{ .Values.data.log_level }}"
-  SPRING_PROFILES_ACTIVE: "{{ .Values.data.spring_profiles_active }}"
-  APP_ENTITLEMENTS: "{{ .Values.data.app_entitlements }}"
-  APP_REGISTER: "{{ .Values.data.app_register }}"
-  PARTITION_API: "{{ .Values.data.partition_api }}"
-  {{- if not .Values.conf.on_prem_enabled }}
-  GOOGLE_AUDIENCES: "{{ .Values.data.google_audiences }}"
-  {{- end }}
diff --git a/devops/gcp/configmap/values.yaml b/devops/gcp/configmap/values.yaml
index 2316222a0..38a7b11cd 100644
--- a/devops/gcp/configmap/values.yaml
+++ b/devops/gcp/configmap/values.yaml
@@ -1,13 +1,13 @@
 data:
   # common
-  log_level: "INFO"
-  spring_profiles_active: "gcp"
-  app_entitlements: "http://entitlements/api/entitlements/v2/"
-  app_register: "http://register/api/register/v1"
-  partition_api: "http://partition/api/partition/v1/"
+  logLevel: "INFO"
+  springProfilesActive: "gcp"
+  entitlementsHost: "http://entitlements"
+  registerHost: "http://register"
+  partitionHost: "http://partition"
   # gcp
-  google_audiences: ""
+  googleAudiences: ""
 conf:
   configmap: "notification-config"
-  app_name: "notification"
-  on_prem_enabled: false
+  appName: "notification"
+  onPremEnabled: false
diff --git a/devops/gcp/deploy/README.md b/devops/gcp/deploy/README.md
new file mode 100644
index 000000000..af91d321c
--- /dev/null
+++ b/devops/gcp/deploy/README.md
@@ -0,0 +1,70 @@
+<!--- Deploy -->
+
+# Deploy helm chart
+
+## Introduction
+
+This chart bootstraps a deployment on a [Kubernetes](https://kubernetes.io) cluster using [Helm](https://helm.sh) package manager.
+
+## Prerequisites
+
+The code was tested on **Kubernetes cluster** (v1.21.11) with **Istio** (1.12.6)
+> It is possible to use other versions, but it hasn't been tested
+
+### Operation system
+
+The code works in Debian-based Linux (Debian 10 and Ubuntu 20.04) and Windows WSL 2. Also, it works but is not guaranteed in Google Cloud Shell. All other operating systems, including macOS, are not verified and supported.
+
+### Packages
+
+Packages are only needed for installation from a local computer.
+
+- **HELM** (version: v3.7.1 or higher) [helm](https://helm.sh/docs/intro/install/)
+- **Kubectl** (version: v1.21.0 or higher) [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl)
+
+## Installation
+
+Before installing deploy Helm chart you need to install [configmap Helm chart](../configmap).
+First you need to set variables in **values.yaml** file using any code editor. Some of the values are prefilled, but you need to specify some values as well. You can find more information about them below.
+
+### Common variables
+
+| Name | Description | Type | Default |Required |
+|------|-------------|------|---------|---------|
+**image** | your image name | string | - | yes
+**requestsCpu** | amount of requested CPU | string | 0.1 | yes
+**requestsMemory** | amount of requested memory| string | 256M | yes
+**limitsCpu** | CPU limit | string | 1 | yes
+**limitsMemory** | memory limit | string | 1G | yes
+**serviceAccountName** | name of your service account | string | notification | yes
+**imagePullPolicy** | when to pull image | string | IfNotPresent | yes
+**image** | service image | string | - | yes
+
+### Config variables
+
+| Name | Description | Type | Default |Required |
+|------|-------------|------|---------|---------|
+**domain** | your domain | string | - | yes
+**appName** | name of the app | string | `notification` | yes
+**configmap** | configmap to be used | string | `notification-config` | yes
+**onPremEnabled** | whether on-prem is enabled | boolean | false | yes
+**rabbitmqSecretName** | secret for rabbitmq | string | `rabbitmq-secret` | yes
+**openidSecretName** | secret for openid client | string | `notification-keycloak-secret` | yes
+
+### Install the helm chart
+
+Run this command from within this directory:
+
+```console
+helm install gcp-notification-deploy .
+```
+
+## Uninstalling the Chart
+
+To uninstall the helm deployment:
+
+```console
+helm uninstall gcp-notification-deploy
+```
+
+[Move-to-Top](#deploy-helm-chart)
diff --git a/devops/gcp/deploy/templates/deployment.yaml b/devops/gcp/deploy/templates/deployment.yaml
index 11d6d40c1..af1194fc1 100644
--- a/devops/gcp/deploy/templates/deployment.yaml
+++ b/devops/gcp/deploy/templates/deployment.yaml
@@ -2,33 +2,33 @@ apiVersion: apps/v1
 kind: Deployment
 metadata:
   labels:
-    app: "{{ .Values.conf.app_name }}"
-  name: "{{ .Values.conf.app_name }}"
+    app: "{{ .Values.conf.appName }}"
+  name: "{{ .Values.conf.appName }}"
   namespace: "{{ .Release.Namespace }}"
 spec:
   selector:
     matchLabels:
-      app: "{{ .Values.conf.app_name }}"
+      app: "{{ .Values.conf.appName }}"
   replicas: 1
   template:
     metadata:
       labels:
-        app: "{{ .Values.conf.app_name }}"
+        app: "{{ .Values.conf.appName }}"
       annotations:
         rollme: {{ randAlphaNum 5 | quote }}
     spec:
       containers:
-        - name: "{{ .Values.conf.app_name }}"
+        - name: "{{ .Values.conf.appName }}"
           image: "{{ .Values.data.image }}"
           imagePullPolicy: "{{ .Values.data.imagePullPolicy }}"
           envFrom:
           - configMapRef:
               name: "{{ .Values.conf.configmap }}"
-          {{- if .Values.conf.on_prem_enabled }}
+          {{- if .Values.conf.onPremEnabled }}
           - secretRef:
-              name: "{{ .Values.conf.rabbitmq_secret_name }}"
+              name: "{{ .Values.conf.rabbitmqSecretName }}"
           - secretRef:
-              name: "{{ .Values.conf.openid_secret_name }}"
+              name: "{{ .Values.conf.openidSecretName }}"
           {{- end }}
           securityContext:
             allowPrivilegeEscalation: false
@@ -37,9 +37,9 @@ spec:
             - containerPort: 8080
           resources:
             requests:
-              cpu: "{{ .Values.data.requests_cpu }}"
-              memory: "{{ .Values.data.requests_memory }}"
+              cpu: "{{ .Values.data.requestsCpu }}"
+              memory: "{{ .Values.data.requestsMemory }}"
             limits:
-              cpu: "{{ .Values.data.limits_cpu }}"
-              memory: "{{ .Values.data.limits_memory }}"
+              cpu: "{{ .Values.data.limitsCpu }}"
+              memory: "{{ .Values.data.limitsMemory }}"
       serviceAccountName: "{{ .Values.data.serviceAccountName }}"
diff --git a/devops/gcp/deploy/templates/service-account.yaml b/devops/gcp/deploy/templates/service-account.yaml
index 3fede170c..25fd74efb 100644
--- a/devops/gcp/deploy/templates/service-account.yaml
+++ b/devops/gcp/deploy/templates/service-account.yaml
@@ -1,4 +1,4 @@
-{{- if .Values.conf.on_prem_enabled }}
+{{- if .Values.conf.onPremEnabled }}
 apiVersion: v1
 kind: ServiceAccount
 metadata:
diff --git a/devops/gcp/deploy/templates/service.yaml b/devops/gcp/deploy/templates/service.yaml
index 7f88561aa..49dc28423 100644
--- a/devops/gcp/deploy/templates/service.yaml
+++ b/devops/gcp/deploy/templates/service.yaml
@@ -1,15 +1,15 @@
 apiVersion: v1
 kind: Service
 metadata:
-  name: "{{ .Values.conf.app_name }}"
+  name: "{{ .Values.conf.appName }}"
   annotations:
-  {{- if not .Values.conf.on_prem_enabled }}
+  {{- if not .Values.conf.onPremEnabled }}
     cloud.google.com/neg: '{"ingress": true}'
   {{- end }}
   namespace: "{{ .Release.Namespace }}"
   labels:
-    app: "{{ .Values.conf.app_name }}"
-    service: "{{ .Values.conf.app_name }}"
+    app: "{{ .Values.conf.appName }}"
+    service: "{{ .Values.conf.appName }}"
 spec:
   ports:
     - protocol: TCP
@@ -17,4 +17,4 @@ spec:
       targetPort: 8080
       name: http
   selector:
-    app: "{{ .Values.conf.app_name }}"
+    app: "{{ .Values.conf.appName }}"
diff --git a/devops/gcp/deploy/templates/virtual-service.yaml b/devops/gcp/deploy/templates/virtual-service.yaml
index 176fa89b8..3a5f6650f 100644
--- a/devops/gcp/deploy/templates/virtual-service.yaml
+++ b/devops/gcp/deploy/templates/virtual-service.yaml
@@ -1,12 +1,14 @@
 apiVersion: networking.istio.io/v1alpha3
 kind: VirtualService
 metadata:
-  name: "{{ .Values.conf.app_name }}"
+  name: "{{ .Values.conf.appName }}"
   namespace: "{{ .Release.Namespace }}"
 spec:
   hosts:
-    {{- if .Values.conf.domain }}
+    {{- if and .Values.conf.domain .Values.conf.onPremEnabled }}
     - {{ printf "osdu.%s" .Values.conf.domain | quote }}
+    {{- else if .Values.conf.domain }}
+    - {{ .Values.conf.domain | quote }}
     {{- else }}
     - "*"
     {{- end }}
@@ -20,4 +22,4 @@ spec:
         - destination:
             port:
               number: 80
-            host: "{{ .Values.conf.app_name }}.{{ .Release.Namespace }}.svc.cluster.local"
+            host: "{{ .Values.conf.appName }}.{{ .Release.Namespace }}.svc.cluster.local"
diff --git a/devops/gcp/deploy/values.yaml b/devops/gcp/deploy/values.yaml
index 83f34fe98..6904a3b5f 100644
--- a/devops/gcp/deploy/values.yaml
+++ b/devops/gcp/deploy/values.yaml
@@ -1,16 +1,16 @@
 data:
-  requests_cpu: "0.1"
-  requests_memory: "256M"
-  limits_cpu: "1"
-  limits_memory: "1.5G"
+  requestsCpu: "0.1"
+  requestsMemory: "256M"
+  limitsCpu: "1"
+  limitsMemory: "1.5G"
   serviceAccountName: "notification"
   imagePullPolicy: "IfNotPresent"
   image: ""
 
 conf:
   configmap: "notification-config"
-  app_name: "notification"
-  rabbitmq_secret_name: "rabbitmq-secret"
-  openid_secret_name: "notification-keycloak-secret"
-  on_prem_enabled: false
+  appName: "notification"
+  rabbitmqSecretName: "rabbitmq-secret"
+  openidSecretName: "notification-keycloak-secret"
+  onPremEnabled: false
   domain: ""
diff --git a/provider/notification-gcp/src/main/resources/application.properties b/provider/notification-gcp/src/main/resources/application.properties
index 80608c45f..44979e699 100644
--- a/provider/notification-gcp/src/main/resources/application.properties
+++ b/provider/notification-gcp/src/main/resources/application.properties
@@ -24,11 +24,20 @@ app.maxCacheSize=10
 server.error.whitelabel.enabled=false
 
 # External services
-app.entitlements=http://entitlements/api/entitlements/v2/
-app.register=http://register/api/register/v1
-partition.api=http://partition/api/partition/v1/
+ENTITLEMENTS_PATH=/api/entitlements/v2/
+ENTITLEMENTS_HOST=http://entitlements
+app.entitlements=${ENTITLEMENTS_HOST}${ENTITLEMENTS_PATH}
+
+REGISTER_PATH=/api/register/v1
+REGISTER_HOST=http://register
+app.register=${REGISTER_HOST}${REGISTER_PATH}
+
+PARTITION_PATH=/api/partition/v1/
+PARTITION_HOST=http://partition
+PARTITION_API=${PARTITION_HOST}${PARTITION_PATH}
+partition.api=${PARTITION_API}
 
 # No profile defaults
 service.token.provider=GCP
 partition-auth-enabled=true
-oqmDriver=pubsub
\ No newline at end of file
+oqmDriver=pubsub
-- 
GitLab