From 7a5bd7ee0f325a76aeb28a3136522ed5aee1ec9d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 28 Feb 2024 12:42:40 -0500 Subject: [PATCH] refactor: use a helper function for printing out both labels and annotations --- .../templates/chart/_helpers.tpl | 17 ++++++++++ .../templates/chart/certificate.yaml | 16 ++++------ .../templates/chart/cron-job.yaml | 32 +++++++------------ .../templates/chart/deployment.yaml | 30 ++++++----------- .../chart/https-redirect-middleware.yaml | 14 +++----- .../templates/chart/image-pull-secret.yaml | 7 ++-- .../templates/chart/ingress-route.yaml | 14 +++----- .../templates/chart/ingress.yaml | 14 +++----- .../scheduler-k3s/templates/chart/secret.yaml | 7 ++-- .../templates/chart/service-account.yaml | 7 ++-- .../templates/chart/service.yaml | 14 +++----- plugins/scheduler-k3s/triggers.go | 15 +++++++++ 12 files changed, 82 insertions(+), 105 deletions(-) create mode 100644 plugins/scheduler-k3s/templates/chart/_helpers.tpl diff --git a/plugins/scheduler-k3s/templates/chart/_helpers.tpl b/plugins/scheduler-k3s/templates/chart/_helpers.tpl new file mode 100644 index 000000000..3f340c56c --- /dev/null +++ b/plugins/scheduler-k3s/templates/chart/_helpers.tpl @@ -0,0 +1,17 @@ +{{- define "print.annotations" }} +{{- if and .config.annotations (hasKey .config.annotations .key) }} +{{- $annotations := get .config.annotations .key }} +{{- range $k, $v := $annotations }} +{{ $k }}: {{ $v | quote }} +{{- end }} +{{- end }} +{{- end }} + +{{- define "print.labels" }} +{{- if and .config.labels (hasKey .config.labels .key) }} +{{- $labels := get .config.labels .key }} +{{- range $k, $v := $labels }} +{{ $k }}: {{ $v | quote }} +{{- end }} +{{- end }} +{{- end }} diff --git a/plugins/scheduler-k3s/templates/chart/certificate.yaml b/plugins/scheduler-k3s/templates/chart/certificate.yaml index 0ee993927..1ffc42a92 100644 --- a/plugins/scheduler-k3s/templates/chart/certificate.yaml +++ b/plugins/scheduler-k3s/templates/chart/certificate.yaml @@ -7,19 +7,13 @@ kind: Certificate metadata: annotations: dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.certificate }} - {{- range $k, $v := $.Values.global.annotations.certificate }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} - {{- if and $config.annotations $config.annotations.certificate }} - {{- range $k, $v := $config.annotations.certificate }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "certificate") | indent 4 }} + {{ include "print.annotations" (dict "config" $config "key" "certificate") | indent 4 }} labels: app.kubernetes.io/name: {{ $.Values.global.app_name }}-{{ $processName }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "certificate") | indent 4 }} + {{ include "print.labels" (dict "config" $config "key" "certificate") | indent 4 }} name: {{ $.Values.global.app_name }}-{{ $processName }} namespace: {{ $.Values.global.namespace }} spec: @@ -33,6 +27,8 @@ spec: labels: app.kubernetes.io/name: {{ $.Values.global.app_name }}-{{ $processName }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "secret") | indent 6 }} + {{ include "print.labels" (dict "config" $config "key" "secret") | indent 6 }} dnsNames: {{- range $config.web.domains }} - {{ . }} diff --git a/plugins/scheduler-k3s/templates/chart/cron-job.yaml b/plugins/scheduler-k3s/templates/chart/cron-job.yaml index ddd5cbefb..9363322f7 100644 --- a/plugins/scheduler-k3s/templates/chart/cron-job.yaml +++ b/plugins/scheduler-k3s/templates/chart/cron-job.yaml @@ -11,21 +11,15 @@ metadata: dokku.com/job-suffix: {{ $config.cron.suffix }} dokku.com/managed: "true" kubectl.kubernetes.io/default-container: {{ $.Values.global.app_name }}-cron - {{- if and $.Values.global.annotations $.Values.global.annotations.cronjob }} - {{- range $k, $v := $.Values.global.annotations.cronjob }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} - {{- if and $config.annotations $config.annotations.cronjob }} - {{- range $k, $v := $config.annotations.cronjob }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "cronjob") | indent 4 }} + {{ include "print.annotations" (dict "config" $config "key" "cronjob") | indent 4 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-cron-{{ $config.cron.suffix }} app.kubernetes.io/name: cron app.kubernetes.io/part-of: {{ $.Values.global.app_name }} dokku.com/cron-id: {{ $config.cron.id }} + {{ include "print.labels" (dict "config" $.Values.global "key" "cronjob") | indent 4 }} + {{ include "print.labels" (dict "config" $config "key" "cronjob") | indent 4 }} name: {{ $.Values.global.app_name }}-cron-{{ $config.cron.suffix }} namespace: {{ $.Values.global.namespace }} spec: @@ -40,21 +34,15 @@ spec: dokku.com/job-suffix: {{ $config.cron.suffix }} dokku.com/managed: "true" kubectl.kubernetes.io/default-container: {{ $.Values.global.app_name }}-cron - {{- if and $.Values.global.annotations $.Values.global.annotations.pod }} - {{- range $k, $v := $.Values.global.annotations.pod }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} - {{- if and $config.annotations $config.annotations.pod }} - {{- range $k, $v := $config.annotations.pod }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "job") | indent 8 }} + {{ include "print.annotations" (dict "config" $config "key" "job") | indent 8 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-cron-{{ $config.cron.suffix }} app.kubernetes.io/name: cron app.kubernetes.io/part-of: {{ $.Values.global.app_name }} dokku.com/cron-id: {{ $config.cron.id }} + {{ include "print.labels" (dict "config" $.Values.global "key" "job") | indent 8 }} + {{ include "print.labels" (dict "config" $config "key" "job") | indent 8 }} spec: backoffLimit: 0 podReplacementPolicy: Failed @@ -68,11 +56,15 @@ spec: dokku.com/job-suffix: {{ $config.cron.suffix }} dokku.com/managed: "true" kubectl.kubernetes.io/default-container: {{ $.Values.global.app_name }}-cron + {{ include "print.annotations" (dict "config" $.Values.global "key" "pod") | indent 12 }} + {{ include "print.annotations" (dict "config" $config "key" "pod") | indent 12 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-cron-{{ $config.cron.suffix }} app.kubernetes.io/name: cron app.kubernetes.io/part-of: {{ $.Values.global.app_name }} dokku.com/cron-id: {{ $config.cron.id }} + {{ include "print.labels" (dict "config" $.Values.global "key" "pod") | indent 12 }} + {{ include "print.labels" (dict "config" $config "key" "pod") | indent 12 }} spec: containers: - args: diff --git a/plugins/scheduler-k3s/templates/chart/deployment.yaml b/plugins/scheduler-k3s/templates/chart/deployment.yaml index b2688edc9..40f32c6af 100644 --- a/plugins/scheduler-k3s/templates/chart/deployment.yaml +++ b/plugins/scheduler-k3s/templates/chart/deployment.yaml @@ -15,20 +15,14 @@ metadata: dokku.com/builder-type: {{ $.Values.global.image.type }} dokku.com/managed: "true" kubectl.kubernetes.io/default-container: {{ $.Values.global.app_name }}-{{ $processName }} - {{- if and $.Values.global.annotations $.Values.global.annotations.deployment }} - {{- range $k, $v := $.Values.global.annotations.deployment }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} - {{- if and $config.annotations $config.annotations.deployment }} - {{- range $k, $v := $config.annotations.deployment }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "deployment") | indent 4 }} + {{ include "print.annotations" (dict "config" $config "key" "deployment") | indent 4 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $processName }} app.kubernetes.io/name: {{ $processName }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "deployment") | indent 4 }} + {{ include "print.labels" (dict "config" $config "key" "deployment") | indent 4 }} name: {{ $.Values.global.app_name }}-{{ $processName }} namespace: {{ $.Values.global.namespace }} spec: @@ -39,6 +33,8 @@ spec: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $processName }} app.kubernetes.io/name: {{ $processName }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "pod") | indent 6 }} + {{ include "print.labels" (dict "config" $config "key" "pod") | indent 6 }} strategy: {} template: metadata: @@ -47,20 +43,14 @@ spec: dokku.com/builder-type: {{ $.Values.global.image.type }} dokku.com/managed: "true" kubectl.kubernetes.io/default-container: {{ $.Values.global.app_name }}-{{ $processName }} - {{- if and $.Values.global.annotations $.Values.global.annotations.pod }} - {{- range $k, $v := $.Values.global.annotations.pod }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} - {{- if and $config.annotations $config.annotations.pod }} - {{- range $k, $v := $config.annotations.pod }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "pod") | indent 8 }} + {{ include "print.annotations" (dict "config" $config "key" "pod") | indent 8 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $processName }} app.kubernetes.io/name: {{ $processName }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "pod") | indent 8 }} + {{ include "print.labels" (dict "config" $config "key" "pod") | indent 8 }} spec: containers: - args: diff --git a/plugins/scheduler-k3s/templates/chart/https-redirect-middleware.yaml b/plugins/scheduler-k3s/templates/chart/https-redirect-middleware.yaml index fef18ded1..20a3d4429 100644 --- a/plugins/scheduler-k3s/templates/chart/https-redirect-middleware.yaml +++ b/plugins/scheduler-k3s/templates/chart/https-redirect-middleware.yaml @@ -7,20 +7,14 @@ kind: Middleware metadata: annotations: dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.traefik_middleware }} - {{- range $k, $v := $.Values.global.annotations.traefik_middleware }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} - {{- if and $config.annotations $config.annotations.traefik_middleware }} - {{- range $k, $v := $config.annotations.traefik_middleware }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "traefik_middleware") | indent 4 }} + {{ include "print.annotations" (dict "config" $config "key" "traefik_middleware") | indent 4 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $processName }}-redirect-to-https app.kubernetes.io/name: {{ $processName }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "traefik_middleware") | indent 4 }} + {{ include "print.labels" (dict "config" $config "key" "traefik_middleware") | indent 4 }} name: {{ $.Values.global.app_name}}-{{ $processName }}-redirect-to-https namespace: {{ $.Values.global.namespace }} spec: diff --git a/plugins/scheduler-k3s/templates/chart/image-pull-secret.yaml b/plugins/scheduler-k3s/templates/chart/image-pull-secret.yaml index 934c15886..ac5d44cbe 100644 --- a/plugins/scheduler-k3s/templates/chart/image-pull-secret.yaml +++ b/plugins/scheduler-k3s/templates/chart/image-pull-secret.yaml @@ -6,15 +6,12 @@ metadata: annotations: app.kubernetes.io/version: {{ $.Values.global.deploment_id | quote }} dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.secret }} - {{- range $k, $v := $.Values.global.annotations.secret }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "secret") | indent 4 }} labels: app.kubernetes.io/instance: ims-{{ $.Values.global.app_name }}.{{ $.Values.global.deploment_id }} app.kubernetes.io/name: ims-{{ $.Values.global.app_name }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "secret") | indent 4 }} name: ims-{{ $.Values.global.app_name }}.{{ $.Values.global.deploment_id }} namespace: {{ $.Values.global.namespace }} data: diff --git a/plugins/scheduler-k3s/templates/chart/ingress-route.yaml b/plugins/scheduler-k3s/templates/chart/ingress-route.yaml index 578fa7b3c..e61c48063 100644 --- a/plugins/scheduler-k3s/templates/chart/ingress-route.yaml +++ b/plugins/scheduler-k3s/templates/chart/ingress-route.yaml @@ -15,20 +15,14 @@ kind: IngressRoute metadata: annotations: dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.traefik_ingressroute }} - {{- range $k, $v := $.Values.global.annotations.traefik_ingressroute }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} - {{- if and $config.annotations $config.annotations.traefik_ingressroute }} - {{- range $k, $v := $config.annotations.traefik_ingressroute }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "traefik_ingressroute") | indent 4 }} + {{ include "print.annotations" (dict "config" $config "key" "traefik_ingressroute") | indent 4 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $processName }} app.kubernetes.io/name: {{ $processName }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "traefik_ingressroute") | indent 4 }} + {{ include "print.labels" (dict "config" $config "key" "traefik_ingressroute") | indent 4 }} name: {{ $.Values.global.app_name }}-{{ $processName }}-{{ $port_map.name }} namespace: {{ $.Values.global.namespace }} spec: diff --git a/plugins/scheduler-k3s/templates/chart/ingress.yaml b/plugins/scheduler-k3s/templates/chart/ingress.yaml index f78290cdd..cfbf519df 100644 --- a/plugins/scheduler-k3s/templates/chart/ingress.yaml +++ b/plugins/scheduler-k3s/templates/chart/ingress.yaml @@ -15,23 +15,17 @@ kind: Ingress metadata: annotations: dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.ingress }} - {{- range $k, $v := $.Values.global.annotations.ingress }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} - {{- if and $config.annotations $config.annotations.ingress }} - {{- range $k, $v := $config.annotations.ingress }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} {{- if $config.web.tls.enabled }} nginx.ingress.kubernetes.io/force-ssl-redirect: "true" {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "ingress") | indent 4 }} + {{ include "print.annotations" (dict "config" $config "key" "ingress") | indent 4 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $processName }} app.kubernetes.io/name: {{ $processName }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "ingress") | indent 4 }} + {{ include "print.labels" (dict "config" $config "key" "ingress") | indent 4 }} name: {{ $.Values.global.app_name }}-{{ $processName }}-{{ $port_map.name }} namespace: {{ $.Values.global.namespace }} spec: diff --git a/plugins/scheduler-k3s/templates/chart/secret.yaml b/plugins/scheduler-k3s/templates/chart/secret.yaml index f8ac42d21..47dd3adee 100644 --- a/plugins/scheduler-k3s/templates/chart/secret.yaml +++ b/plugins/scheduler-k3s/templates/chart/secret.yaml @@ -5,15 +5,12 @@ metadata: annotations: app.kubernetes.io/version: {{ $.Values.global.deploment_id | quote }} dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.secret }} - {{- range $k, $v := $.Values.global.annotations.secret }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "secret") | indent 4 }} labels: app.kubernetes.io/instance: env-{{ $.Values.global.app_name }}.{{ $.Values.global.deploment_id }} app.kubernetes.io/name: env-{{ $.Values.global.app_name }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "secret") | indent 4 }} name: env-{{ $.Values.global.app_name }}.{{ $.Values.global.deploment_id }} namespace: {{ $.Values.global.namespace }} {{- with .Values.global.secrets }} diff --git a/plugins/scheduler-k3s/templates/chart/service-account.yaml b/plugins/scheduler-k3s/templates/chart/service-account.yaml index eaf142e84..55f669b45 100644 --- a/plugins/scheduler-k3s/templates/chart/service-account.yaml +++ b/plugins/scheduler-k3s/templates/chart/service-account.yaml @@ -3,14 +3,11 @@ kind: ServiceAccount metadata: annotations: dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.serviceaccount }} - {{- range $k, $v := $.Values.global.annotations.serviceaccount }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "serviceaccount") | indent 4 }} labels: app.kubernetes.io/instance: service-account app.kubernetes.io/name: service-account app.kubernetes.io/part-of: "{{ $.Values.global.app_name }}" + {{ include "print.labels" (dict "config" $.Values.global "key" "serviceaccount") | indent 4 }} name: "{{ $.Values.global.app_name }}" namespace: "{{ $.Values.global.namespace }}" diff --git a/plugins/scheduler-k3s/templates/chart/service.yaml b/plugins/scheduler-k3s/templates/chart/service.yaml index d6b812762..bf5d3d34b 100644 --- a/plugins/scheduler-k3s/templates/chart/service.yaml +++ b/plugins/scheduler-k3s/templates/chart/service.yaml @@ -6,20 +6,14 @@ kind: Service metadata: annotations: dokku.com/managed: "true" - {{- if and $.Values.global.annotations $.Values.global.annotations.service }} - {{- range $k, $v := $.Values.global.annotations.service }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} - {{- if and $config.annotations $config.annotations.service }} - {{- range $k, $v := $config.annotations.service }} - {{ $k }}: {{ $v | quote }} - {{- end }} - {{- end }} + {{ include "print.annotations" (dict "config" $.Values.global "key" "service") | indent 4 }} + {{ include "print.annotations" (dict "config" $config "key" "service") | indent 4 }} labels: app.kubernetes.io/instance: {{ $.Values.global.app_name }}-{{ $processName }} app.kubernetes.io/name: {{ $processName }} app.kubernetes.io/part-of: {{ $.Values.global.app_name }} + {{ include "print.labels" (dict "config" $.Values.global "key" "service") | indent 4 }} + {{ include "print.labels" (dict "config" $config "key" "service") | indent 4 }} name: {{ $.Values.global.app_name }}-{{ $processName }} namespace: {{ $.Values.global.namespace }} spec: diff --git a/plugins/scheduler-k3s/triggers.go b/plugins/scheduler-k3s/triggers.go index e4c2d597e..46518c1d6 100644 --- a/plugins/scheduler-k3s/triggers.go +++ b/plugins/scheduler-k3s/triggers.go @@ -493,6 +493,21 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e values.Global.Secrets[key] = base64.StdEncoding.EncodeToString([]byte(value)) } + b, err := templates.ReadFile("templates/chart/_helpers.tpl") + if err != nil { + return fmt.Errorf("Error reading _helpers template: %w", err) + } + + helpersFile := filepath.Join(chartDir, "templates", "_helpers.tpl") + err = os.WriteFile(helpersFile, b, os.FileMode(0644)) + if err != nil { + return fmt.Errorf("Error writing _helpers template: %w", err) + } + + if os.Getenv("DOKKU_TRACE") == "1" { + common.CatFile(helpersFile) + } + err = writeYaml(WriteYamlInput{ Object: values, Path: filepath.Join(chartDir, "values.yaml"),