From d96bd2f1499cb6de1b8e03811c6cfcf97e69533a Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Tue, 27 Feb 2024 13:59:44 +0100 Subject: [PATCH] OZG-4971-refactor trunc 63 chars in helm helper --- 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 | 54 +++++++++++++++++++ .../src/main/helm/templates/_helpers.tpl | 31 ++++++----- .../helm/templates/image-pull-secret.yaml | 2 +- .../helm/templates/xta_adapter_cronjob.yaml | 4 +- .../test/helm/xta_adapter_63_chars_test.yaml | 54 +++++++++++++++++++ 14 files changed, 154 insertions(+), 48 deletions(-) create mode 100644 src/test/helm/deployment_63_chars_test.yaml create mode 100644 xta-adapter/src/test/helm/xta_adapter_63_chars_test.yaml diff --git a/src/main/helm/templates/_helpers.tpl b/src/main/helm/templates/_helpers.tpl index 410348cb1..0915f0f23 100644 --- a/src/main/helm/templates/_helpers.tpl +++ b/src/main/helm/templates/_helpers.tpl @@ -1,45 +1,44 @@ {{/* vim: set filetype=mustache: */}} -{{/* 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 -}} - +{{/* error check 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec) */}} {{/* Namespace */}} {{- define "app.namespace" -}} -{{- default .Release.Namespace | toString | trunc 63 | trimSuffix "-" -}} +{{- if gt (len (.Release.Namespace)) 63 -}} +{{- fail (printf ".Release.Namespace %s ist zu lang (max. 63 Zeichen)" .Release.Namespace) -}} {{- end -}} - -{{/* Version */}} -{{- define "app.version" -}} -{{- default .Chart.Version | toString | trunc 63 | trimSuffix "-" -}} +{{ printf "%s" .Release.Namespace }} {{- end -}} {{/* Chart: Name + Version */}} {{- define "app.chart" -}} -{{ printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- 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 }} {{- end -}} {{/* Managed-by -> On Helm, this value is always Helm */}} {{- define "app.managedBy" -}} -{{- default .Release.Service | toString | trunc 63 | trimSuffix "-" -}} +{{- if gt (len (.Release.Service)) 63 -}} +{{- fail (printf ".Release.Service %s ist zu lang (max. 63 Zeichen)" .Release.Service) -}} +{{- end -}} +{{ printf "%s" .Release.Service }} {{- 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: {{ include "app.name" . }} +app.kubernetes.io/name: {{ .Release.Name }} app.kubernetes.io/part-of: ozgcloud -app.kubernetes.io/version: {{ include "app.version" . }} +app.kubernetes.io/version: {{ .Chart.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: {{ include "app.name" . }} +app.kubernetes.io/name: {{ .Release.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 5074738e2..83555f367 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: {{ include "app.name" . }} + name: {{ .Release.Name }} namespace: {{ include "app.namespace" . }} labels: {{- include "app.defaultLabels" . | indent 4 }} @@ -56,7 +56,7 @@ spec: whenUnsatisfiable: DoNotSchedule labelSelector: matchLabels: - app.kubernetes.io/name: {{ include "app.name" . }} + app.kubernetes.io/name: {{ .Release.Name }} containers: - env: {{- range (.Values.env).grpc }} @@ -144,7 +144,7 @@ spec: {{- if .Values.imagePullSecret }} - name: {{ .Values.imagePullSecret }} {{ else }} - - name: {{ include "app.name" . }}-image-pull-secret + - name: {{ .Release.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 9ac29e90a..327b37f8b 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: {{ include "app.name" . }}-image-pull-secret + name: {{ .Release.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 fe85cf3ea..d04a5339b 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: {{ include "app.name" . }} + name: {{ .Release.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: {{ include "app.name" . }} + name: {{ .Release.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 }}-{{ include "app.name" . }}-tls + secretName: {{ .Values.ozgcloud.bezeichner }}-{{ .Release.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 929afd7c8..2d70106bb 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-{{ include "app.name" .}} + name: network-policy-{{ .Release.Name}} namespace: {{ .Release.Namespace }} spec: podSelector: diff --git a/src/main/helm/templates/service.yaml b/src/main/helm/templates/service.yaml index 541a2679c..93574b29b 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: {{ include "app.name" . }} + name: {{ .Release.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 ae25136b8..053a25810 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: {{ include "app.name" . }} + name: {{ .Release.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 13551e9ef..6d33a4e54 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: "{{ include "app.name" . }}-test-ingress" + name: "{{ .Release.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 9bc4d39f4..62dda4f5e 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: "{{ include "app.name" . }}-test-connection" + name: "{{ .Release.Name }}-test-connection" labels: {{- include "app.matchLabels" . | nindent 4 }} annotations: @@ -35,5 +35,5 @@ spec: - name: wget image: busybox command: ['wget'] - args: ['{{ include "app.name" . }}:8080/ws/intelliform_formDatas.wsdl'] + args: ['{{ .Release.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 new file mode 100644 index 000000000..68d8afffe --- /dev/null +++ b/src/test/helm/deployment_63_chars_test.yaml @@ -0,0 +1,54 @@ +# +# 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 + +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 Intelliform-Adapter-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 203aab813..afcc6d669 100644 --- a/xta-adapter/src/main/helm/templates/_helpers.tpl +++ b/xta-adapter/src/main/helm/templates/_helpers.tpl @@ -1,38 +1,37 @@ {{/* vim: set filetype=mustache: */}} -{{/* 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 -}} - +{{/* error check 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec) */}} {{/* Namespace */}} {{- define "app.namespace" -}} -{{- default .Release.Namespace | toString | trunc 63 | trimSuffix "-" -}} +{{- if gt (len (.Release.Namespace)) 63 -}} +{{- fail (printf ".Release.Namespace %s ist zu lang (max. 63 Zeichen)" .Release.Namespace) -}} {{- end -}} - -{{/* Version */}} -{{- define "app.version" -}} -{{- default .Chart.Version | toString | trunc 63 | trimSuffix "-" -}} +{{ printf "%s" .Release.Namespace }} {{- end -}} {{/* Chart: Name + Version */}} {{- define "app.chart" -}} -{{ printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- 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 }} {{- end -}} {{/* Managed-by -> On Helm, this value is always Helm */}} {{- define "app.managedBy" -}} -{{- default .Release.Service | toString | trunc 63 | trimSuffix "-" -}} +{{- if gt (len (.Release.Service)) 63 -}} +{{- fail (printf ".Release.Service %s ist zu lang (max. 63 Zeichen)" .Release.Service) -}} +{{- end -}} +{{ printf "%s" .Release.Service }} {{- end -}} {{/* Default Labels: Helm recommended best-practice labels https://helm.sh/docs/chart_best_practices/labels/ */}} {{- define "app.defaultLabels" }} -app.kubernetes.io/instance: {{ include "app.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} app.kubernetes.io/managed-by: {{ include "app.managedBy" . }} -app.kubernetes.io/name: {{ include "app.name" . }} +app.kubernetes.io/name: {{ .Release.Name }} app.kubernetes.io/part-of: ozgcloud -app.kubernetes.io/version: {{ include "app.version" . }} +app.kubernetes.io/version: {{ .Chart.Version }} app.kubernetes.io/namespace: {{ include "app.namespace" . }} helm.sh/chart: {{ include "app.chart" . }} {{- end -}} 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 9ac29e90a..327b37f8b 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: {{ include "app.name" . }}-image-pull-secret + name: {{ .Release.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 2fa6f19d1..ce8935ad1 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: {{ include "app.name" . }} + name: {{ .Release.Name }} namespace: {{ include "app.namespace" . }} labels: {{- include "app.defaultLabels" . | indent 4 }} @@ -162,5 +162,5 @@ spec: {{- if .Values.imagePullSecret }} - name: {{ .Values.imagePullSecret }} {{ else }} - - name: {{ include "app.name" . }}-image-pull-secret + - name: {{ .Release.Name }}-image-pull-secret {{- end }} \ No newline at end of file diff --git a/xta-adapter/src/test/helm/xta_adapter_63_chars_test.yaml b/xta-adapter/src/test/helm/xta_adapter_63_chars_test.yaml new file mode 100644 index 000000000..396d0971e --- /dev/null +++ b/xta-adapter/src/test/helm/xta_adapter_63_chars_test.yaml @@ -0,0 +1,54 @@ +# +# 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 less than 63 chars +release: + name: xta-adapter + namespace: sh-helm-test + +chart: + name: xta-adapter + +templates: + - templates/xta_adapter_cronjob.yaml + +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 xta-adapter-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 -- GitLab