From 0d4cc76c1eb1c338f6467efb90fa3e41503aa635 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Tue, 20 Feb 2024 11:26:03 +0100
Subject: [PATCH] Revert "OZG-4971-refactor trunc 63 chars in helm helper"

This reverts commit 795d45259e7ab5abe3c197a8557011bbd26c8370.
---
 src/main/helm/templates/_helpers.tpl          | 31 +++++-----
 src/main/helm/templates/deployment.yaml       |  6 +-
 .../helm/templates/image-pull-secret.yaml     |  2 +-
 src/main/helm/templates/ingress.yaml          |  6 +-
 src/main/helm/templates/network_policy.yaml   |  2 +-
 src/main/helm/templates/service.yaml          |  2 +-
 src/main/helm/templates/service_monitor.yaml  |  2 +-
 .../tests/test-ingress-connection.yaml        |  2 +-
 .../tests/test-service-connection.yaml        |  4 +-
 src/test/helm/deployment_63_chars_test.yaml   | 62 -------------------
 .../src/main/helm/templates/_helpers.tpl      |  4 +-
 .../helm/templates/image-pull-secret.yaml     |  2 +-
 .../helm/templates/xta_adapter_cronjob.yaml   |  4 +-
 13 files changed, 34 insertions(+), 95 deletions(-)
 delete mode 100644 src/test/helm/deployment_63_chars_test.yaml

diff --git a/src/main/helm/templates/_helpers.tpl b/src/main/helm/templates/_helpers.tpl
index 0915f0f23..410348cb1 100644
--- a/src/main/helm/templates/_helpers.tpl
+++ b/src/main/helm/templates/_helpers.tpl
@@ -1,44 +1,45 @@
 {{/* vim: set filetype=mustache: */}}
 
-{{/* error check 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec) */}}
+{{/* Truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec) */}}
+{{/* Name */}}
+{{- define "app.name" -}}
+{{- default .Release.Name | toString | trunc 63 | trimSuffix "-" -}}
+{{- end -}}
+
 {{/* Namespace */}}
 {{- define "app.namespace" -}}
-{{- if gt (len (.Release.Namespace)) 63 -}}
-{{- fail (printf ".Release.Namespace %s ist zu lang (max. 63 Zeichen)" .Release.Namespace) -}}
+{{- default .Release.Namespace | toString | trunc 63 | trimSuffix "-" -}}
 {{- end -}}
-{{ printf "%s" .Release.Namespace }}
+
+{{/* Version */}}
+{{- define "app.version" -}}
+{{- default .Chart.Version | toString | trunc 63 | trimSuffix "-" -}}
 {{- end -}}
 
 {{/* Chart: Name + Version */}}
 {{- define "app.chart" -}}
-{{- if gt (len (printf "%s-%s" .Chart.Name .Chart.Version)) 63 -}}
-{{- fail (printf ".Chart.Name-.Chart.Version %s-%s ist zu lang (max. 63 Zeichen)" .Chart.Name .Chart.Version) -}}
-{{- end -}}
-{{ printf "%s-%s" .Chart.Name .Chart.Version }}
+{{ printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
 {{- end -}}
 
 {{/* Managed-by -> On Helm, this value is always Helm */}}
 {{- define "app.managedBy" -}}
-{{- if gt (len (.Release.Service)) 63 -}}
-{{- fail (printf ".Release.Service %s ist zu lang (max. 63 Zeichen)" .Release.Service) -}}
-{{- end -}}
-{{ printf "%s" .Release.Service }}
+{{- default .Release.Service | toString | trunc 63 | trimSuffix "-" -}}
 {{- end -}}
 
 {{/* Default Labels: Helm recommended best-practice labels https://helm.sh/docs/chart_best_practices/labels/ */}}
 {{- define "app.defaultLabels" }}
 app.kubernetes.io/instance: afm-adapter
 app.kubernetes.io/managed-by: {{ include "app.managedBy" . }}
-app.kubernetes.io/name: {{ .Release.Name }}
+app.kubernetes.io/name: {{ include "app.name" . }}
 app.kubernetes.io/part-of: ozgcloud
-app.kubernetes.io/version: {{ .Chart.Version }}
+app.kubernetes.io/version: {{ include "app.version" . }}
 app.kubernetes.io/namespace: {{ include "app.namespace" . }}
 helm.sh/chart: {{ include "app.chart" . }}
 ozg-component: eingangsadapter
 {{- end -}}
 
 {{- define "app.matchLabels" }}
-app.kubernetes.io/name: {{ .Release.Name }}
+app.kubernetes.io/name: {{ include "app.name" . }}
 app.kubernetes.io/namespace: {{ include "app.namespace" . }}
 {{- end -}}
 
diff --git a/src/main/helm/templates/deployment.yaml b/src/main/helm/templates/deployment.yaml
index 83555f367..5074738e2 100644
--- a/src/main/helm/templates/deployment.yaml
+++ b/src/main/helm/templates/deployment.yaml
@@ -25,7 +25,7 @@
 apiVersion: apps/v1
 kind: Deployment
 metadata:
-  name: {{ .Release.Name }}
+  name: {{ include "app.name" . }}
   namespace: {{ include "app.namespace" . }}
   labels:
     {{- include "app.defaultLabels" . | indent 4 }}
@@ -56,7 +56,7 @@ spec:
         whenUnsatisfiable: DoNotSchedule
         labelSelector:
           matchLabels:
-            app.kubernetes.io/name: {{ .Release.Name }}
+            app.kubernetes.io/name: {{ include "app.name" . }}
       containers:
       - env:
         {{- range (.Values.env).grpc }}
@@ -144,7 +144,7 @@ spec:
       {{- if .Values.imagePullSecret }}
       - name: {{ .Values.imagePullSecret }}
       {{ else }}
-      - name: {{ .Release.Name }}-image-pull-secret
+      - name: {{ include "app.name" . }}-image-pull-secret
       {{- end }}
       restartPolicy: Always
       {{- with .Values.hostAliases }}
diff --git a/src/main/helm/templates/image-pull-secret.yaml b/src/main/helm/templates/image-pull-secret.yaml
index 327b37f8b..9ac29e90a 100644
--- a/src/main/helm/templates/image-pull-secret.yaml
+++ b/src/main/helm/templates/image-pull-secret.yaml
@@ -26,7 +26,7 @@
 apiVersion: v1
 kind: Secret
 metadata:
-  name: {{ .Release.Name }}-image-pull-secret
+  name: {{ include "app.name" . }}-image-pull-secret
   namespace: {{ include "app.namespace" . }}
 type: kubernetes.io/dockerconfigjson
 data:
diff --git a/src/main/helm/templates/ingress.yaml b/src/main/helm/templates/ingress.yaml
index d04a5339b..fe85cf3ea 100644
--- a/src/main/helm/templates/ingress.yaml
+++ b/src/main/helm/templates/ingress.yaml
@@ -37,7 +37,7 @@ metadata:
     cert-manager.io/cluster-issuer: letsencrypt-prod
     {{- end }}
     nginx.ingress.kubernetes.io/proxy-body-size: 42m
-  name: {{ .Release.Name }}
+  name: {{ include "app.name" . }}
   namespace: {{ include "app.namespace" . }}
 spec:
   {{- if and (.Values.ingress).className (ne (.Values).cluster_env "dataport") }}
@@ -50,7 +50,7 @@ spec:
               service:
                 port:
                   number: 8080
-                name: {{ .Release.Name }}
+                name: {{ include "app.name" . }}
             path: ''
             pathType: ImplementationSpecific
       host: {{ include "app.ingress.host" . }}
@@ -60,6 +60,6 @@ spec:
       {{- if (.Values.ingress).tlsSecretName }}
       secretName: {{ (.Values.ingress).tlsSecretName }}
       {{- else if ne (.Values).cluster_env "dataport" }}
-      secretName: {{ .Values.ozgcloud.bezeichner }}-{{ .Release.Name }}-tls
+      secretName: {{ .Values.ozgcloud.bezeichner }}-{{ include "app.name" . }}-tls
       {{- end }}
 {{- end -}}
\ No newline at end of file
diff --git a/src/main/helm/templates/network_policy.yaml b/src/main/helm/templates/network_policy.yaml
index 2d70106bb..929afd7c8 100644
--- a/src/main/helm/templates/network_policy.yaml
+++ b/src/main/helm/templates/network_policy.yaml
@@ -26,7 +26,7 @@
 apiVersion: networking.k8s.io/v1
 kind: NetworkPolicy
 metadata:
-  name: network-policy-{{ .Release.Name}}
+  name: network-policy-{{ include "app.name" .}}
   namespace: {{ .Release.Namespace }} 
 spec:
   podSelector:
diff --git a/src/main/helm/templates/service.yaml b/src/main/helm/templates/service.yaml
index 93574b29b..541a2679c 100644
--- a/src/main/helm/templates/service.yaml
+++ b/src/main/helm/templates/service.yaml
@@ -25,7 +25,7 @@
 apiVersion: v1
 kind: Service
 metadata:
-  name: {{ .Release.Name }}
+  name: {{ include "app.name" . }}
   namespace: {{ include "app.namespace" . }}
   labels:
     {{- include "app.defaultLabels" . | indent 4 }}
diff --git a/src/main/helm/templates/service_monitor.yaml b/src/main/helm/templates/service_monitor.yaml
index 053a25810..ae25136b8 100644
--- a/src/main/helm/templates/service_monitor.yaml
+++ b/src/main/helm/templates/service_monitor.yaml
@@ -25,7 +25,7 @@
 apiVersion: monitoring.coreos.com/v1
 kind: ServiceMonitor
 metadata:
-  name: {{ .Release.Name }}
+  name: {{ include "app.name" . }}
   namespace: {{ include "app.namespace" . }}
   labels:
     {{- include "app.defaultLabels" . | indent 4 }}
diff --git a/src/main/helm/templates/tests/test-ingress-connection.yaml b/src/main/helm/templates/tests/test-ingress-connection.yaml
index 6d33a4e54..13551e9ef 100644
--- a/src/main/helm/templates/tests/test-ingress-connection.yaml
+++ b/src/main/helm/templates/tests/test-ingress-connection.yaml
@@ -25,7 +25,7 @@
 apiVersion: v1
 kind: Pod
 metadata:
-  name: "{{ .Release.Name }}-test-ingress"
+  name: "{{ include "app.name" . }}-test-ingress"
   labels:
     {{- include "app.matchLabels" . | nindent 4 }}
   annotations:
diff --git a/src/main/helm/templates/tests/test-service-connection.yaml b/src/main/helm/templates/tests/test-service-connection.yaml
index 62dda4f5e..9bc4d39f4 100644
--- a/src/main/helm/templates/tests/test-service-connection.yaml
+++ b/src/main/helm/templates/tests/test-service-connection.yaml
@@ -25,7 +25,7 @@
 apiVersion: v1
 kind: Pod
 metadata:
-  name: "{{ .Release.Name }}-test-connection"
+  name: "{{ include "app.name" . }}-test-connection"
   labels:
     {{- include "app.matchLabels" . | nindent 4 }}
   annotations:
@@ -35,5 +35,5 @@ spec:
     - name: wget
       image: busybox
       command: ['wget']
-      args: ['{{ .Release.Name }}:8080/ws/intelliform_formDatas.wsdl']
+      args: ['{{ include "app.name" . }}:8080/ws/intelliform_formDatas.wsdl']
   restartPolicy: Never
diff --git a/src/test/helm/deployment_63_chars_test.yaml b/src/test/helm/deployment_63_chars_test.yaml
deleted file mode 100644
index 0d8bd5424..000000000
--- a/src/test/helm/deployment_63_chars_test.yaml
+++ /dev/null
@@ -1,62 +0,0 @@
-#
-# Copyright (C) 2022 Das Land Schleswig-Holstein vertreten durch den
-# Ministerpräsidenten des Landes Schleswig-Holstein
-# Staatskanzlei
-# Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
-#
-# Lizenziert unter der EUPL, Version 1.2 oder - sobald
-# diese von der Europäischen Kommission genehmigt wurden -
-# Folgeversionen der EUPL ("Lizenz");
-# Sie dürfen dieses Werk ausschließlich gemäß
-# dieser Lizenz nutzen.
-# Eine Kopie der Lizenz finden Sie hier:
-#
-# https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
-#
-# Sofern nicht durch anwendbare Rechtsvorschriften
-# gefordert oder in schriftlicher Form vereinbart, wird
-# die unter der Lizenz verbreitete Software "so wie sie
-# ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
-# ausdrücklich oder stillschweigend - verbreitet.
-# Die sprachspezifischen Genehmigungen und Beschränkungen
-# unter der Lizenz sind dem Lizenztext zu entnehmen.
-#
-
-suite: test deyploment less than 63 chars
-release:
-  name: eingang-manager
-  namespace: sh-helm-test
-  
-chart:
-  name: eingang-manager
-
-templates:
-  - templates/deployment.yaml
-set:
-  ozgcloud:
-    environment: test
-    bundesland: sh
-    bezeichner: helm
-  sso:
-    serverUrl: https://sso.company.local
-  baseUrl: test.company.local
-
-tests:
-  - it: should fail on .Release.Namespace length longer than 63 characters
-    release:
-      namespace: test1234567890123123456789012345678901234567890123456789012345678901234567890123456789012345678904567890
-    asserts:
-      - failedTemplate:
-          errorMessage: .Release.Namespace test1234567890123123456789012345678901234567890123456789012345678901234567890123456789012345678904567890 ist zu lang (max. 63 Zeichen)
-  - it: should not fail on .Release.Namespace length less than 63 characters
-    asserts:
-      - notFailedTemplate: {}
-  - it: should fail on .Chart.Name-.Chart.Version length longer than 63 characters
-    chart:
-      version: 1.0-test1234567890123123456789012345678901234567890123456789012345678901234567890123456789012345678904567890
-    asserts:
-      - failedTemplate:
-          errorMessage: .Chart.Name-.Chart.Version eingang-manager-1.0-test1234567890123123456789012345678901234567890123456789012345678901234567890123456789012345678904567890 ist zu lang (max. 63 Zeichen)
-  - it: should not fail on .Chart.Name-.Chart.Version length less than 63 characters
-    asserts:
-      - notFailedTemplate: {}
\ No newline at end of file
diff --git a/xta-adapter/src/main/helm/templates/_helpers.tpl b/xta-adapter/src/main/helm/templates/_helpers.tpl
index d504d14d5..203aab813 100644
--- a/xta-adapter/src/main/helm/templates/_helpers.tpl
+++ b/xta-adapter/src/main/helm/templates/_helpers.tpl
@@ -28,9 +28,9 @@
 
 {{/* Default Labels: Helm recommended best-practice labels https://helm.sh/docs/chart_best_practices/labels/ */}}
 {{- define "app.defaultLabels" }}
-app.kubernetes.io/instance: {{ .Release.Name }}
+app.kubernetes.io/instance: {{ include "app.name" . }}
 app.kubernetes.io/managed-by: {{ include "app.managedBy" . }}
-app.kubernetes.io/name: {{ .Release.Name }}
+app.kubernetes.io/name: {{ include "app.name" . }}
 app.kubernetes.io/part-of: ozgcloud
 app.kubernetes.io/version: {{ include "app.version" . }}
 app.kubernetes.io/namespace: {{ include "app.namespace" . }}
diff --git a/xta-adapter/src/main/helm/templates/image-pull-secret.yaml b/xta-adapter/src/main/helm/templates/image-pull-secret.yaml
index 327b37f8b..9ac29e90a 100644
--- a/xta-adapter/src/main/helm/templates/image-pull-secret.yaml
+++ b/xta-adapter/src/main/helm/templates/image-pull-secret.yaml
@@ -26,7 +26,7 @@
 apiVersion: v1
 kind: Secret
 metadata:
-  name: {{ .Release.Name }}-image-pull-secret
+  name: {{ include "app.name" . }}-image-pull-secret
   namespace: {{ include "app.namespace" . }}
 type: kubernetes.io/dockerconfigjson
 data:
diff --git a/xta-adapter/src/main/helm/templates/xta_adapter_cronjob.yaml b/xta-adapter/src/main/helm/templates/xta_adapter_cronjob.yaml
index ce8935ad1..2fa6f19d1 100644
--- a/xta-adapter/src/main/helm/templates/xta_adapter_cronjob.yaml
+++ b/xta-adapter/src/main/helm/templates/xta_adapter_cronjob.yaml
@@ -25,7 +25,7 @@
 apiVersion: batch/v1
 kind: CronJob
 metadata:
-  name: {{ .Release.Name }}
+  name: {{ include "app.name" . }}
   namespace: {{ include "app.namespace" . }}
   labels:
     {{- include "app.defaultLabels" . | indent 4 }}
@@ -162,5 +162,5 @@ spec:
             {{- if .Values.imagePullSecret }}
             - name: {{ .Values.imagePullSecret }}
             {{ else }}
-            - name: {{ .Release.Name }}-image-pull-secret
+            - name: {{ include "app.name" . }}-image-pull-secret
             {{- end }}
\ No newline at end of file
-- 
GitLab