From c409e201b324fc6b5c8ff473e164fd862d94ebdb Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Fri, 4 Sep 2026 15:26:17 +0200 Subject: [PATCH] refactor(deployment): extract annotation and label rendering into helpers The Deployment metadata inlined the annotation and label logic with nested `if` blocks, which duplicated the fallback handling and made the empty-value cases hard to follow. Moving the rendering into `gitea.deployment.annotations` and `gitea.deployment.labels` keeps the manifest declarative and allows other resources to reuse the same merge semantics later on. The helpers are consumed through `with (include ... | fromYaml)` so that an empty result never emits a dangling `annotations:` key. Labels always render because `gitea.labels` is never empty, which keeps Argo CD from reporting drift. Inside the label helper the user labels are appended with an untrimmed newline, otherwise they would be concatenated onto the last line of `gitea.labels` and `fromYaml` would silently return an `Error` map instead of failing the render. The metadata attributes are additionally sorted alphabetically to follow the chart conventions. Unit tests now cover the previously untested `deployment.annotations` value and assert that the base labels keep rendering despite the new `with` guard. Co-authored-by: Copilot --- templates/gitea/_deployments.tpl | 16 ++++++++++++++++ templates/gitea/deployment.yaml | 17 ++++++++--------- unittests/helm/deployment/basic.yaml | 25 +++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 templates/gitea/_deployments.tpl diff --git a/templates/gitea/_deployments.tpl b/templates/gitea/_deployments.tpl new file mode 100644 index 0000000..5089518 --- /dev/null +++ b/templates/gitea/_deployments.tpl @@ -0,0 +1,16 @@ +{{/* annotations */}} + +{{- define "gitea.deployment.annotations" -}} +{{- with .Values.deployment.annotations }} +{{- toYaml . -}} +{{- end }} +{{- end }} + +{{/* labels */}} + +{{- define "gitea.deployment.labels" -}} +{{ include "gitea.labels" . }} +{{- with .Values.deployment.labels }} +{{ toYaml . }} +{{- end }} +{{- end }} \ No newline at end of file diff --git a/templates/gitea/deployment.yaml b/templates/gitea/deployment.yaml index 58af957..5a04098 100644 --- a/templates/gitea/deployment.yaml +++ b/templates/gitea/deployment.yaml @@ -2,17 +2,16 @@ apiVersion: apps/v1 kind: Deployment metadata: + {{- with (include "gitea.deployment.annotations" . | fromYaml) }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with (include "gitea.deployment.labels" . | fromYaml) }} + labels: + {{- toYaml . | nindent 4 }} + {{- end }} name: {{ include "gitea.fullname" . }} namespace: {{ .Values.namespace | default .Release.Namespace }} - annotations: - {{- if .Values.deployment.annotations }} - {{- toYaml .Values.deployment.annotations | nindent 4 }} - {{- end }} - labels: - {{- include "gitea.labels" . | nindent 4 }} - {{- if .Values.deployment.labels }} - {{- toYaml .Values.deployment.labels | nindent 4 }} - {{- end }} spec: replicas: {{ .Values.deployment.replicas }} strategy: diff --git a/unittests/helm/deployment/basic.yaml b/unittests/helm/deployment/basic.yaml index 76cc0bf..c1a28dc 100644 --- a/unittests/helm/deployment/basic.yaml +++ b/unittests/helm/deployment/basic.yaml @@ -57,6 +57,31 @@ tests: value: app.kubernetes.io/name: gitea app.kubernetes.io/instance: gitea-unittests + - it: deployment labels are always rendered + template: templates/gitea/deployment.yaml + asserts: + - isSubset: + path: metadata.labels + content: + app: gitea + app.kubernetes.io/name: gitea + app.kubernetes.io/instance: gitea-unittests + app.kubernetes.io/managed-by: Helm + - it: deployment annotations are undefined + template: templates/gitea/deployment.yaml + asserts: + - notExists: + path: metadata.annotations + - it: deployment annotations are set + template: templates/gitea/deployment.yaml + set: + deployment.annotations: + hello: world + asserts: + - equal: + path: metadata.annotations + value: + hello: world - it: nodeSelector is undefined asserts: - notExists: