From 7d64fb2184c4ec945f1a4ddb86ff4b33d3fb4895 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Wed, 8 May 2024 12:07:07 +0200
Subject: [PATCH] ozg-5100 dict to list

---
 .../src/main/helm/templates/_helpers.tpl      | 18 +++++++++++
 .../src/main/helm/templates/deployment.yaml   |  4 +--
 .../helm/deployment_env_customList_test.yaml  | 30 +++++++++++++++++--
 .../src/main/helm/templates/_helpers.tpl      | 18 +++++++++++
 .../src/main/helm/templates/deployment.yaml   |  4 +--
 .../src/test/helm/deployment_env_test.yaml    | 30 +++++++++++++++++--
 6 files changed, 96 insertions(+), 8 deletions(-)

diff --git a/ozgcloud-elasticsearch-operator/src/main/helm/templates/_helpers.tpl b/ozgcloud-elasticsearch-operator/src/main/helm/templates/_helpers.tpl
index 5b08fa9..f3d0b8e 100644
--- a/ozgcloud-elasticsearch-operator/src/main/helm/templates/_helpers.tpl
+++ b/ozgcloud-elasticsearch-operator/src/main/helm/templates/_helpers.tpl
@@ -4,4 +4,22 @@
 {{- define "app.matchLabels" }}
 app.kubernetes.io/name: {{ .Release.Name }}
 app.kubernetes.io/namespace: {{ .Release.Namespace }}
+{{- end -}}
+
+{{- define "app.getCustomList" -}}
+{{- with (.Values.env).customList -}}
+{{- if kindIs "map" . -}}
+{{ include "app.dictToList" . }}
+{{- else if kindIs "slice" . -}}
+{{ . | toYaml }}
+{{- end -}}
+{{- end -}}
+{{- end -}}
+
+{{- define "app.dictToList" -}}
+{{- $customList := list -}}
+{{- range $key, $value := . -}}
+{{- $customList = append $customList (dict "name" $key "value" $value) }}
+{{- end -}}
+{{- $customList | toYaml -}}
 {{- end -}}
\ No newline at end of file
diff --git a/ozgcloud-elasticsearch-operator/src/main/helm/templates/deployment.yaml b/ozgcloud-elasticsearch-operator/src/main/helm/templates/deployment.yaml
index 549b897..b3139b3 100644
--- a/ozgcloud-elasticsearch-operator/src/main/helm/templates/deployment.yaml
+++ b/ozgcloud-elasticsearch-operator/src/main/helm/templates/deployment.yaml
@@ -49,8 +49,8 @@ spec:
       - name: ozgcloud-elasticsearch-operator
         image: "{{ required "image.repo must be set" (.Values.image).repo }}/{{ required "image.name must be set" (.Values.image).name }}:{{ required "image.tag must be set" (.Values.image).tag }}"
         env:
-        {{- with (.Values.env).customList }}
-{{ toYaml . | indent 8 }}
+        {{- with include "app.getCustomList" . }}
+{{ . | indent 8 }}
         {{- end }}
         - name: SERVICE_BINDING_ROOT
           value: "/bindings"
diff --git a/ozgcloud-elasticsearch-operator/src/test/helm/deployment_env_customList_test.yaml b/ozgcloud-elasticsearch-operator/src/test/helm/deployment_env_customList_test.yaml
index 49a663b..0ba2af6 100644
--- a/ozgcloud-elasticsearch-operator/src/test/helm/deployment_env_customList_test.yaml
+++ b/ozgcloud-elasticsearch-operator/src/test/helm/deployment_env_customList_test.yaml
@@ -30,12 +30,34 @@ set:
     namespace: elastic-system
     adminSecretName: ozg-search-cluster-es-elastic-user
     certificateSecretName: ozg-search-cluster-es-http-ca-internal
+  image:
+    name: hase
+    tag: latest
+  imagePullSecret: imagePullSecret
 tests:
-  - it: check customList
+  - it: check customList as list
     set:
       env.customList:
         - name: my_test_environment_name
           value: "A test value"
+        - name: test_environment
+          value: "B test value"
+    asserts:
+      - contains:
+          path: spec.template.spec.containers[0].env
+          content:
+            name: my_test_environment_name
+            value: "A test value"
+      - contains:
+          path: spec.template.spec.containers[0].env
+          content:
+            name: test_environment
+            value: "B test value"
+  - it: check customList as dict
+    set:
+      env.customList:
+        my_test_environment_name: "A test value"
+        test_environment: "B test value"
       image:
         name: hase
         tag: latest
@@ -46,4 +68,8 @@ tests:
           content:
             name: my_test_environment_name
             value: "A test value"
-
+      - contains:
+          path: spec.template.spec.containers[0].env
+          content:
+            name: test_environment
+            value: "B test value"
diff --git a/ozgcloud-keycloak-operator/src/main/helm/templates/_helpers.tpl b/ozgcloud-keycloak-operator/src/main/helm/templates/_helpers.tpl
index 26abf5b..88374fc 100644
--- a/ozgcloud-keycloak-operator/src/main/helm/templates/_helpers.tpl
+++ b/ozgcloud-keycloak-operator/src/main/helm/templates/_helpers.tpl
@@ -3,4 +3,22 @@
 {{- define "app.matchLabels" }}
 app.kubernetes.io/name: {{ .Release.Name }}
 app.kubernetes.io/namespace: {{ .Release.Namespace }}
+{{- end -}}
+
+{{- define "app.getCustomList" -}}
+{{- with (.Values.env).customList -}}
+{{- if kindIs "map" . -}}
+{{ include "app.dictToList" . }}
+{{- else if kindIs "slice" . -}}
+{{ . | toYaml }}
+{{- end -}}
+{{- end -}}
+{{- end -}}
+
+{{- define "app.dictToList" -}}
+{{- $customList := list -}}
+{{- range $key, $value := . -}}
+{{- $customList = append $customList (dict "name" $key "value" $value) }}
+{{- end -}}
+{{- $customList | toYaml -}}
 {{- end -}}
\ No newline at end of file
diff --git a/ozgcloud-keycloak-operator/src/main/helm/templates/deployment.yaml b/ozgcloud-keycloak-operator/src/main/helm/templates/deployment.yaml
index 648ac58..5691861 100644
--- a/ozgcloud-keycloak-operator/src/main/helm/templates/deployment.yaml
+++ b/ozgcloud-keycloak-operator/src/main/helm/templates/deployment.yaml
@@ -49,8 +49,8 @@ spec:
       - name: ozgcloud-keycloak-operator
         image: "{{ required "image.repo must be set" (.Values.image).repo }}/{{ required "image.name must be set" (.Values.image).name }}:{{ required "image.tag must be set" (.Values.image).tag }}"
         env:
-        {{- with (.Values.env).customList }}
-{{ toYaml . | indent 8 }}
+        {{- with include "app.getCustomList" . }}
+{{ . | indent 8 }}
         {{- end }}
         imagePullPolicy: Always
 #        readinessProbe:
diff --git a/ozgcloud-keycloak-operator/src/test/helm/deployment_env_test.yaml b/ozgcloud-keycloak-operator/src/test/helm/deployment_env_test.yaml
index 55882a0..1e21ede 100644
--- a/ozgcloud-keycloak-operator/src/test/helm/deployment_env_test.yaml
+++ b/ozgcloud-keycloak-operator/src/test/helm/deployment_env_test.yaml
@@ -31,16 +31,42 @@ set:
     name: hase
     tag: latest
 tests:
-  - it: check customList
-    template: deployment.yaml
+  - it: check customList as list
     set:
       env.customList:
         - name: my_test_environment_name
           value: "A test value"
+        - name: test_environment
+          value: "B test value"
     asserts:
       - contains:
           path: spec.template.spec.containers[0].env
           content:
             name: my_test_environment_name
             value: "A test value"
+      - contains:
+          path: spec.template.spec.containers[0].env
+          content:
+            name: test_environment
+            value: "B test value"
+  - it: check customList as dict
+    set:
+      env.customList:
+        my_test_environment_name: "A test value"
+        test_environment: "B test value"
+      image:
+        name: hase
+        tag: latest
+      imagePullSecret: imagePullSecret
+    asserts:
+      - contains:
+          path: spec.template.spec.containers[0].env
+          content:
+            name: my_test_environment_name
+            value: "A test value"
+      - contains:
+          path: spec.template.spec.containers[0].env
+          content:
+            name: test_environment
+            value: "B test value"
 
-- 
GitLab