refactor: use a helper function for printing out both labels and annotations

This commit is contained in:
Jose Diaz-Gonzalez
2024-02-28 12:42:40 -05:00
parent 065d4ef828
commit 7a5bd7ee0f
12 changed files with 82 additions and 105 deletions

View File

@@ -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 }}

View File

@@ -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 }}
- {{ . }}

View File

@@ -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:

View File

@@ -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:

View File

@@ -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:

View File

@@ -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:

View File

@@ -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:

View File

@@ -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:

View File

@@ -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 }}

View File

@@ -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 }}"

View File

@@ -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:

View File

@@ -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"),