From 8bcbf891886e7b3ffd262a5ade7eca45e65e3e8b Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Wed, 10 Apr 2024 13:46:53 +0200
Subject: [PATCH] OZG-3328 refactor imagePullSecret

---
 src/main/helm/templates/_helpers.tpl             |  6 ------
 src/main/helm/templates/deployment.yaml          |  2 +-
 src/test/helm-linter-values.yaml                 |  1 +
 src/test/helm/deployment_database_test.yaml      |  1 +
 .../helm/deployment_defaults_labels_test.yaml    |  2 +-
 src/test/helm/deployment_defaults_sso_test.yaml  |  1 +
 .../helm/deployment_image_pull_secret_test.yaml  | 16 +++++++---------
 src/test/helm/deployment_resources_test.yaml     |  1 +
 .../helm/deployment_spring_profile_test.yaml     |  1 +
 src/test/helm/deployment_test.yaml               |  4 +---
 src/test/helm/deployment_volume_test.yaml        |  2 +-
 11 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/src/main/helm/templates/_helpers.tpl b/src/main/helm/templates/_helpers.tpl
index 95cbd1ab..91bca425 100644
--- a/src/main/helm/templates/_helpers.tpl
+++ b/src/main/helm/templates/_helpers.tpl
@@ -22,12 +22,6 @@ app.kubernetes.io/name: {{ .Release.Name }}
 app.kubernetes.io/namespace: {{ include "app.namespace" . }}
 {{- end -}}
 
-{{- define "app.imagePullSecret" }}
-{{- with .Values.imageCredentials }}
-{{- printf "{\"auths\":{\"%s\":{\"username\":\"%s\",\"password\":\"%s\",\"email\":\"%s\",\"auth\":\"%s\"}}}" .registry .username .password .email (printf "%s:%s" .username .password | b64enc) | b64enc }}
-{{- end }}
-{{- end }}
-
 {{- define "app.nameToIdentifier" -}}
 {{- trimAll "-" ( regexReplaceAll "[^a-zA-Z0-9-]" (lower .) "" ) | trunc 20 }}
 {{- end -}}
diff --git a/src/main/helm/templates/deployment.yaml b/src/main/helm/templates/deployment.yaml
index dfbadbc7..7d1d4762 100644
--- a/src/main/helm/templates/deployment.yaml
+++ b/src/main/helm/templates/deployment.yaml
@@ -145,7 +145,7 @@ spec:
       dnsConfig: {}
       dnsPolicy: ClusterFirst
       imagePullSecrets:
-        - name: {{ .Values.imagePullSecret | default "administration-image-pull-secret" }}
+        - name: {{ required "image-pull-secret must be set" .Values.imagePullSecret }}
       restartPolicy: Always
       schedulerName: default-scheduler
       securityContext: {}
diff --git a/src/test/helm-linter-values.yaml b/src/test/helm-linter-values.yaml
index b58fe662..35b73c5d 100644
--- a/src/test/helm-linter-values.yaml
+++ b/src/test/helm-linter-values.yaml
@@ -45,3 +45,4 @@ sso:
     client_roles:
       - ROLE_DUMMY
 
+imagePullSecret: image-pull-secret
\ No newline at end of file
diff --git a/src/test/helm/deployment_database_test.yaml b/src/test/helm/deployment_database_test.yaml
index 8c747b65..b007ea3e 100644
--- a/src/test/helm/deployment_database_test.yaml
+++ b/src/test/helm/deployment_database_test.yaml
@@ -34,6 +34,7 @@ set:
     bezeichner: helm
   sso:
     serverUrl: https://sso.company.local
+  imagePullSecret: image-pull-secret
 tests:
   - it: should have a volume for the mongodb certificate
     asserts:
diff --git a/src/test/helm/deployment_defaults_labels_test.yaml b/src/test/helm/deployment_defaults_labels_test.yaml
index b01cb297..c3902cfc 100644
--- a/src/test/helm/deployment_defaults_labels_test.yaml
+++ b/src/test/helm/deployment_defaults_labels_test.yaml
@@ -34,7 +34,7 @@ set:
     bezeichner: helm
   sso:
     serverUrl: https://sso.company.local
-
+  imagePullSecret: image-pull-secret
 tests:
   - it: check metadata.labels
     asserts:
diff --git a/src/test/helm/deployment_defaults_sso_test.yaml b/src/test/helm/deployment_defaults_sso_test.yaml
index 907a5923..78662094 100644
--- a/src/test/helm/deployment_defaults_sso_test.yaml
+++ b/src/test/helm/deployment_defaults_sso_test.yaml
@@ -36,6 +36,7 @@ set:
     bezeichner: helm
   sso:
     serverUrl: https://sso.company.local
+  imagePullSecret: image-pull-secret
 tests:
   - it: check default values
     asserts:
diff --git a/src/test/helm/deployment_image_pull_secret_test.yaml b/src/test/helm/deployment_image_pull_secret_test.yaml
index 44f285aa..db2d17c3 100644
--- a/src/test/helm/deployment_image_pull_secret_test.yaml
+++ b/src/test/helm/deployment_image_pull_secret_test.yaml
@@ -34,17 +34,15 @@ set:
   sso:
     serverUrl: https://sso.company.local
 tests:
-  - it: should use default image pull secret
-    asserts:
-      - contains:
-          path: spec.template.spec.imagePullSecrets
-          content:
-            name: administration-image-pull-secret
-  - it: should use customizable image pull secret
+  - it: should set image pull secret
     set:
-      imagePullSecret: custom-image-pull-secret
+      imagePullSecret: image-pull-secret
     asserts:
       - contains:
           path: spec.template.spec.imagePullSecrets
           content:
-            name: custom-image-pull-secret
+            name: image-pull-secret
+  - it: should fail template when image pull secret not set
+    asserts:
+      - failedTemplate:
+          errormessage: image-pull-secret must be set
diff --git a/src/test/helm/deployment_resources_test.yaml b/src/test/helm/deployment_resources_test.yaml
index 888f63ff..f6cb1d4b 100644
--- a/src/test/helm/deployment_resources_test.yaml
+++ b/src/test/helm/deployment_resources_test.yaml
@@ -33,6 +33,7 @@ set:
     bezeichner: helm
   sso:
     serverUrl: https://sso.company.local
+  imagePullSecret: image-pull-secret
 tests:
   - it: should have customizable resources
     set:
diff --git a/src/test/helm/deployment_spring_profile_test.yaml b/src/test/helm/deployment_spring_profile_test.yaml
index 1a9326cb..f22f7ac5 100644
--- a/src/test/helm/deployment_spring_profile_test.yaml
+++ b/src/test/helm/deployment_spring_profile_test.yaml
@@ -34,6 +34,7 @@ set:
     bezeichner: helm
   sso:
     serverUrl: https://sso.company.local
+  imagePullSecret: image-pull-secret
 tests:
   - it: should override the spring profiles
     set:
diff --git a/src/test/helm/deployment_test.yaml b/src/test/helm/deployment_test.yaml
index 5d6ec1cf..38931a0d 100644
--- a/src/test/helm/deployment_test.yaml
+++ b/src/test/helm/deployment_test.yaml
@@ -34,6 +34,7 @@ set:
     bezeichner: helm
   sso:
     serverUrl: https://sso.company.local
+  imagePullSecret: image-pull-secret
 tests:
   - it: should have metadata values
     asserts:
@@ -96,9 +97,6 @@ tests:
       - equal:
           path: spec.template.spec.dnsPolicy
           value: "ClusterFirst"
-      - equal:
-          path: spec.template.spec.imagePullSecrets[0].name
-          value: "administration-image-pull-secret"
       - equal:
           path: spec.template.spec.restartPolicy
           value: "Always"
diff --git a/src/test/helm/deployment_volume_test.yaml b/src/test/helm/deployment_volume_test.yaml
index 6546763a..0cdf8aea 100644
--- a/src/test/helm/deployment_volume_test.yaml
+++ b/src/test/helm/deployment_volume_test.yaml
@@ -28,13 +28,13 @@ release:
 templates:
   - templates/deployment.yaml
 set:
-  Release.Name: "administration"
   ozgcloud:
     environment: "default"
     bundesland: sh
     bezeichner: helm
   sso:
     serverUrl: https://sso.company.local
+  imagePullSecret: image-pull-secret
 tests:
   - it: should have volumeMounts on it's container
     asserts:
-- 
GitLab