From 5fb7898e1b116fbe63f77a94f03f2a6b6b84741d Mon Sep 17 00:00:00 2001 From: Chi-En Wu Date: Fri, 17 Apr 2020 11:19:01 +0800 Subject: [PATCH 1/9] feat: allow `common.metadata` to be extended --- templates/_configmap.yaml | 3 ++- templates/_deployment.yaml | 3 ++- templates/_hpa.yaml | 3 ++- templates/_ingress.yaml | 14 +++++++++----- templates/_metadata.tpl | 11 +++++++---- templates/_pdb.yaml | 3 ++- templates/_secret.yaml | 3 ++- templates/_service.yaml | 3 ++- templates/_serviceaccount.yaml | 16 +++++++++------- 9 files changed, 37 insertions(+), 22 deletions(-) diff --git a/templates/_configmap.yaml b/templates/_configmap.yaml index 0a603dd..05f70f0 100644 --- a/templates/_configmap.yaml +++ b/templates/_configmap.yaml @@ -3,7 +3,8 @@ {{- define "common.configMap.tpl" -}} apiVersion: v1 kind: ConfigMap -{{ include "common.metadata" . }} +metadata: + {{ include "common.metadata" . | nindent 2 }} data: {} {{- end -}} diff --git a/templates/_deployment.yaml b/templates/_deployment.yaml index e577e0b..16efd93 100644 --- a/templates/_deployment.yaml +++ b/templates/_deployment.yaml @@ -4,7 +4,8 @@ {{- $autoscaling := .Values.autoscaling | default (dict) -}} apiVersion: apps/v1 kind: Deployment -{{ include "common.metadata" . }} +metadata: + {{ include "common.metadata" . | nindent 2 }} spec: {{- if not $autoscaling.enabled }} replicas: {{ .Values.replicaCount | default 1 }} diff --git a/templates/_hpa.yaml b/templates/_hpa.yaml index b3b332a..c1a2baa 100644 --- a/templates/_hpa.yaml +++ b/templates/_hpa.yaml @@ -3,7 +3,8 @@ {{- define "common.hpa.tpl" -}} apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler -{{ include "common.metadata" . }} +metadata: + {{ include "common.metadata" . | nindent 2 }} spec: scaleTargetRef: apiVersion: apps/v1 diff --git a/templates/_ingress.yaml b/templates/_ingress.yaml index 70ffc91..d8638a1 100644 --- a/templates/_ingress.yaml +++ b/templates/_ingress.yaml @@ -1,5 +1,12 @@ {{/* vim: set filetype=mustache: */}} +{{- define "common.ingress.metadata" -}} +{{- with .Values.ingress.annotations }} +annotations: + {{- toYaml . | nindent 2 }} +{{- end }} +{{- end -}} + {{- define "common.ingress.tpl" -}} {{- $fullName := include "common.fullname" . -}} {{- $svcPort := .Values.service.port -}} @@ -9,11 +16,8 @@ apiVersion: networking.k8s.io/v1beta1 apiVersion: extensions/v1beta1 {{- end }} kind: Ingress -{{ include "common.metadata" . }} - {{- with .Values.ingress.annotations }} - annotations: - {{- toYaml . | nindent 4 }} - {{- end }} +metadata: + {{ include "common.metadata" (list . "common.ingress.metadata") | nindent 2 }} spec: {{- if .Values.ingress.tls }} tls: diff --git a/templates/_metadata.tpl b/templates/_metadata.tpl index 15daf64..e20880b 100644 --- a/templates/_metadata.tpl +++ b/templates/_metadata.tpl @@ -20,12 +20,15 @@ app.kubernetes.io/name: {{ include "common.name" . }} app.kubernetes.io/instance: {{ .Release.Name }} {{- end -}} +{{ define "common.metadata.tpl" -}} +name: {{ include "common.fullname" . }} +labels: + {{- include "common.labels" . | nindent 2 -}} +{{- end -}} + {{- /* Create a standard metadata header */ -}} {{ define "common.metadata" -}} -metadata: - name: {{ include "common.fullname" . }} - labels: - {{- include "common.labels" . | nindent 4 -}} +{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.metadata.tpl") -}} {{- end -}} diff --git a/templates/_pdb.yaml b/templates/_pdb.yaml index 5e6382a..21699fe 100644 --- a/templates/_pdb.yaml +++ b/templates/_pdb.yaml @@ -3,7 +3,8 @@ {{- define "common.pdb.tpl" -}} apiVersion: policy/v1beta1 kind: PodDisruptionBudget -{{ include "common.metadata" . }} +metadata: + {{ include "common.metadata" . | nindent 2 }} spec: selector: matchLabels: diff --git a/templates/_secret.yaml b/templates/_secret.yaml index 0d25ed9..37b19f9 100644 --- a/templates/_secret.yaml +++ b/templates/_secret.yaml @@ -3,7 +3,8 @@ {{- define "common.secret.tpl" -}} apiVersion: v1 kind: Secret -{{ include "common.metadata" . }} +metadata: + {{ include "common.metadata" . | nindent 2 }} type: Opaque data: {} {{- end -}} diff --git a/templates/_service.yaml b/templates/_service.yaml index 082ff27..eb3d299 100644 --- a/templates/_service.yaml +++ b/templates/_service.yaml @@ -3,7 +3,8 @@ {{- define "common.service.tpl" -}} apiVersion: v1 kind: Service -{{ include "common.metadata" . }} +metadata: + {{ include "common.metadata" . | nindent 2 }} spec: type: {{ .Values.service.type }} ports: diff --git a/templates/_serviceaccount.yaml b/templates/_serviceaccount.yaml index 2837ac8..8209b09 100644 --- a/templates/_serviceaccount.yaml +++ b/templates/_serviceaccount.yaml @@ -1,16 +1,18 @@ {{/* vim: set filetype=mustache: */}} +{{- define "common.serviceAccount.metadata" -}} +name: {{ include "common.serviceAccountName" . }} +{{- with .Values.serviceAccount.annotations }} +annotations: + {{- toYaml . | nindent 2 }} +{{- end }} +{{- end -}} + {{- define "common.serviceAccount.tpl" -}} apiVersion: v1 kind: ServiceAccount metadata: - name: {{ include "common.serviceAccountName" . }} - labels: - {{- include "common.labels" . | nindent 4 }} - {{- with .Values.serviceAccount.annotations }} - annotations: - {{- toYaml . | nindent 4 }} - {{- end }} + {{ include "common.metadata" (list . "common.serviceAccount.metadata") | nindent 2 }} {{- end -}} {{- define "common.serviceAccount.if" -}} From 923cf384cd1b6cdbf36d2c923886ebeced0949a6 Mon Sep 17 00:00:00 2001 From: Chi-En Wu Date: Mon, 20 Apr 2020 11:10:52 +0800 Subject: [PATCH 2/9] feat: add template `common.serviceMonitor` --- templates/_servicemonitor.yaml | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 templates/_servicemonitor.yaml diff --git a/templates/_servicemonitor.yaml b/templates/_servicemonitor.yaml new file mode 100644 index 0000000..91156b1 --- /dev/null +++ b/templates/_servicemonitor.yaml @@ -0,0 +1,55 @@ +{{/* vim: set filetype=mustache: */}} + +{{- define "common.serviceMonitor.metadata" -}} +{{- with .Values.serviceMonitor.namespace }} +namespace: {{ . }} +{{- end }} +{{- end -}} + +{{- define "common.serviceMonitor.tpl" -}} +apiVersion: monitoring.coreos.com/v1 +kind: ServiceMonitor +metadata: + {{ include "common.metadata" (list . "common.serviceMonitor.metadata") | nindent 2 }} +spec: + selector: + matchLabels: + {{- include "common.selectorLabels" . | nindent 6 }} + namespaceSelector: + matchNames: + - {{ .Release.Namespace | quote }} + endpoints: + - port: {{ .Values.serviceMonitor.port | default .Values.service.port }} + {{- with .Values.serviceMonitor.path }} + path: {{ . }} + {{- end }} + {{- with .Values.serviceMonitor.interval }} + interval: {{ . }} + {{- end }} + {{- with .Values.serviceMonitor.scrapeTimeout }} + scrapeTimeout: {{ . }} + {{- end }} + {{- $basicAuth := .Values.serviceMonitor.basicAuth | default (dict) -}} + {{- $name := $basicAuth.secretName | default (include "common.fullname" .) -}} + {{- if $basicAuth.enabled }} + basicAuth: + username: + name: {{ $name }} + key: {{ $basicAuth.usernameKey | default "username" }} + password: + name: {{ $name }} + key: {{ $basicAuth.passwordKey | default "password" }} + {{- end }} +{{- end -}} + +{{- define "common.serviceMonitor.if" -}} +{{- $top := first . -}} +{{- $serviceMonitor := $top.Values.serviceMonitor | default (dict) -}} +{{- if $serviceMonitor.enabled -}} + {{- include "common.utils.merge" (append . "common.serviceMonitor.tpl") -}} +{{- end -}} +{{- end -}} + +{{- define "common.serviceMonitor" -}} +{{- include "common.utils.flattenCall" (list "common.serviceMonitor.if" .) -}} +{{- end -}} From 2228f4a49e54481119ac3bdaa1917a5308027149 Mon Sep 17 00:00:00 2001 From: Chi-En Wu Date: Mon, 20 Apr 2020 11:12:29 +0800 Subject: [PATCH 3/9] feat: add template `common.serviceMonitor.secret` --- templates/_servicemonitor-secret.yaml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 templates/_servicemonitor-secret.yaml diff --git a/templates/_servicemonitor-secret.yaml b/templates/_servicemonitor-secret.yaml new file mode 100644 index 0000000..ec6283e --- /dev/null +++ b/templates/_servicemonitor-secret.yaml @@ -0,0 +1,27 @@ +{{/* vim: set filetype=mustache: */}} + +{{- define "common.serviceMonitor.secret.tpl" -}} +{{- $basicAuth := .Values.serviceMonitor.basicAuth | default (dict) -}} +metadata: + name: {{ $basicAuth.secretName | default (include "common.fullname" .) }} + {{- with .Values.serviceMonitor.namespace }} + namespace: {{ . }} + {{- end }} +{{- if $basicAuth.enabled }} +data: + {{ $basicAuth.usernameKey | default "username" }}: {{ $basicAuth.username | toString | b64enc | quote }} + {{ $basicAuth.passwordKey | default "password" }}: {{ $basicAuth.password | toString | b64enc | quote }} +{{- end }} +{{- end -}} + +{{- define "common.serviceMonitor.secret.if" -}} +{{- $top := first . -}} +{{- $serviceMonitor := $top.Values.serviceMonitor | default (dict) -}} +{{- if $serviceMonitor.enabled -}} + {{- include "common.secret" (append . "common.serviceMonitor.secret.tpl") -}} +{{- end -}} +{{- end -}} + +{{- define "common.serviceMonitor.secret" -}} +{{- include "common.utils.flattenCall" (list "common.serviceMonitor.secret.if" .) -}} +{{- end -}} From 6fbe88593795c1da40a6ab5f42062b631e64c761 Mon Sep 17 00:00:00 2001 From: Chi-En Wu Date: Mon, 20 Apr 2020 16:24:08 +0800 Subject: [PATCH 4/9] feat: extract template `common.pod-template` --- templates/_deployment.yaml | 26 +------------------------- templates/_pod.tpl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 templates/_pod.tpl diff --git a/templates/_deployment.yaml b/templates/_deployment.yaml index 16efd93..5f4e448 100644 --- a/templates/_deployment.yaml +++ b/templates/_deployment.yaml @@ -14,31 +14,7 @@ spec: matchLabels: {{- include "common.selectorLabels" . | nindent 6 }} template: - metadata: - labels: - {{- include "common.selectorLabels" . | nindent 8 }} - spec: - {{- with .Values.imagePullSecrets }} - imagePullSecrets: - {{- toYaml . | nindent 8 }} - {{- end }} - serviceAccountName: {{ include "common.serviceAccountName" . }} - securityContext: - {{- toYaml .Values.podSecurityContext | nindent 8 }} - containers: - - {{- include "common.container" . | nindent 10 }} - {{- with .Values.nodeSelector }} - nodeSelector: - {{- toYaml . | nindent 8 }} - {{- end }} - {{- with .Values.affinity }} - affinity: - {{- toYaml . | nindent 8 }} - {{- end }} - {{- with .Values.tolerations }} - tolerations: - {{- toYaml . | nindent 8 }} - {{- end }} + {{- include "common.pod-template" . | nindent 4 }} {{- end -}} {{- define "common.deployment" -}} diff --git a/templates/_pod.tpl b/templates/_pod.tpl new file mode 100644 index 0000000..ca2b38d --- /dev/null +++ b/templates/_pod.tpl @@ -0,0 +1,33 @@ +{{/* vim: set filetype=mustache: */}} + +{{- define "common.pod-template.tpl" -}} +metadata: + labels: + {{- include "common.selectorLabels" . | nindent 4 }} +spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "common.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 4 }} + containers: + - {{- include "common.container" . | nindent 6 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end -}} + +{{- define "common.pod-template" -}} +{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.pod-template.tpl") -}} +{{- end -}} From bddaab694250aaa63ae994443777ab44149c45a4 Mon Sep 17 00:00:00 2001 From: Chi-En Wu Date: Mon, 20 Apr 2020 16:38:09 +0800 Subject: [PATCH 5/9] feat: add template `common.cronJob` --- templates/_cronjob.yaml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 templates/_cronjob.yaml diff --git a/templates/_cronjob.yaml b/templates/_cronjob.yaml new file mode 100644 index 0000000..c5c6651 --- /dev/null +++ b/templates/_cronjob.yaml @@ -0,0 +1,34 @@ +{{/* vim: set filetype=mustache: */}} + +{{- define "common.cronJob.pod" -}} +spec: + restartPolicy: OnFailure +{{- end -}} + +{{- define "common.cronJob.tpl" -}} +apiVersion: batch/v1beta1 +kind: CronJob +metadata: + {{ include "common.metadata" . | nindent 2 }} +spec: + schedule: "{{ .Values.cronJob.schedule }}" + {{- with .Values.cronJob.concurrencyPolicy }} + concurrencyPolicy: {{ . }} + {{- end }} + {{- with .Values.cronJob.failedJobsHistoryLimit }} + failedJobsHistoryLimit: {{ . }} + {{- end }} + {{- with .Values.cronJob.successfulJobsHistoryLimit }} + successfulJobsHistoryLimit: {{ . }} + {{- end }} + jobTemplate: + metadata: + {{ include "common.metadata" . | nindent 6 }} + spec: + template: + {{ include "common.pod-template" (list . "common.cronJob.pod") | nindent 8 }} +{{- end -}} + +{{- define "common.cronJob" -}} +{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.cronJob.tpl") -}} +{{- end -}} From 9aecdacf39e829a635e3fbe6ce61001d0fd569ee Mon Sep 17 00:00:00 2001 From: Chi-En Wu Date: Wed, 22 Apr 2020 10:26:17 +0800 Subject: [PATCH 6/9] feat: allow arguments to be passed to templates --- templates/_configmap.yaml | 5 ++-- templates/_container.yaml | 15 ++++++----- templates/_cronjob.yaml | 19 ++++++++------ templates/_deployment.yaml | 14 +++++----- templates/_hpa.yaml | 23 +++++++--------- templates/_ingress.yaml | 30 ++++++++++----------- templates/_metadata.tpl | 7 ++--- templates/_pdb.yaml | 31 ++++++++++------------ templates/_pod.tpl | 18 +++++++------ templates/_secret.yaml | 5 ++-- templates/_service.yaml | 12 +++++---- templates/_serviceaccount.yaml | 16 +++++------ templates/_servicemonitor-secret.yaml | 16 +++++------ templates/_servicemonitor.yaml | 31 +++++++++++----------- templates/_utils.tpl | 38 ++++++++++----------------- 15 files changed, 137 insertions(+), 143 deletions(-) diff --git a/templates/_configmap.yaml b/templates/_configmap.yaml index 05f70f0..d509ada 100644 --- a/templates/_configmap.yaml +++ b/templates/_configmap.yaml @@ -1,13 +1,14 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.configMap.tpl" -}} +{{- $top := first . -}} apiVersion: v1 kind: ConfigMap metadata: - {{ include "common.metadata" . | nindent 2 }} + {{ include "common.metadata" (list $top) | nindent 2 }} data: {} {{- end -}} {{- define "common.configMap" -}} -{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.configMap.tpl") -}} +{{- include "common.utils.merge" (append . "common.configMap.tpl") -}} {{- end -}} diff --git a/templates/_container.yaml b/templates/_container.yaml index 32e1fe0..a012156 100644 --- a/templates/_container.yaml +++ b/templates/_container.yaml @@ -1,15 +1,18 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.container.tpl" -}} -name: {{ .Chart.Name }} +{{- $top := first . -}} +{{- $container := index . 1 -}} +{{- $image := $container.image | default (dict) -}} +name: {{ $top.Chart.Name }} securityContext: - {{- toYaml .Values.securityContext | nindent 4 }} -image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" -imagePullPolicy: {{ .Values.image.pullPolicy }} + {{- toYaml $container.securityContext | nindent 4 }} +image: "{{ $image.repository }}:{{ $image.tag | default $top.Chart.AppVersion }}" +imagePullPolicy: {{ $container.image.pullPolicy }} resources: - {{- toYaml .Values.resources | nindent 4 }} + {{- toYaml $container.resources | nindent 4 }} {{- end -}} {{- define "common.container" -}} -{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.container.tpl") -}} +{{- include "common.utils.merge" (append . "common.container.tpl") -}} {{- end -}} diff --git a/templates/_cronjob.yaml b/templates/_cronjob.yaml index c5c6651..2c6eb32 100644 --- a/templates/_cronjob.yaml +++ b/templates/_cronjob.yaml @@ -6,29 +6,32 @@ spec: {{- end -}} {{- define "common.cronJob.tpl" -}} +{{- $top := first . -}} +{{- $cronJob := index . 1 -}} +{{- $pod := index . 2 -}} apiVersion: batch/v1beta1 kind: CronJob metadata: - {{ include "common.metadata" . | nindent 2 }} + {{ include "common.metadata" (list $top) | nindent 2 }} spec: - schedule: "{{ .Values.cronJob.schedule }}" - {{- with .Values.cronJob.concurrencyPolicy }} + schedule: "{{ $cronJob.schedule }}" + {{- with $cronJob.concurrencyPolicy }} concurrencyPolicy: {{ . }} {{- end }} - {{- with .Values.cronJob.failedJobsHistoryLimit }} + {{- with $cronJob.failedJobsHistoryLimit }} failedJobsHistoryLimit: {{ . }} {{- end }} - {{- with .Values.cronJob.successfulJobsHistoryLimit }} + {{- with $cronJob.successfulJobsHistoryLimit }} successfulJobsHistoryLimit: {{ . }} {{- end }} jobTemplate: metadata: - {{ include "common.metadata" . | nindent 6 }} + {{ include "common.metadata" (list $top) | nindent 6 }} spec: template: - {{ include "common.pod-template" (list . "common.cronJob.pod") | nindent 8 }} + {{ include "common.pod-template" (list $top $pod "common.cronJob.pod") | nindent 8 }} {{- end -}} {{- define "common.cronJob" -}} -{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.cronJob.tpl") -}} +{{- include "common.utils.merge" (append . "common.cronJob.tpl") -}} {{- end -}} diff --git a/templates/_deployment.yaml b/templates/_deployment.yaml index 5f4e448..bcc58a1 100644 --- a/templates/_deployment.yaml +++ b/templates/_deployment.yaml @@ -1,22 +1,24 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.deployment.tpl" -}} -{{- $autoscaling := .Values.autoscaling | default (dict) -}} +{{- $top := first . -}} +{{- $deployment := index . 1 -}} +{{- $autoscaling := index . 2 -}} apiVersion: apps/v1 kind: Deployment metadata: - {{ include "common.metadata" . | nindent 2 }} + {{ include "common.metadata" (list $top) | nindent 2 }} spec: {{- if not $autoscaling.enabled }} - replicas: {{ .Values.replicaCount | default 1 }} + replicas: {{ $deployment.replicaCount | default 1 }} {{- end }} selector: matchLabels: - {{- include "common.selectorLabels" . | nindent 6 }} + {{ include "common.selectorLabels" $top | nindent 6 }} template: - {{- include "common.pod-template" . | nindent 4 }} + {{ include "common.pod-template" . | nindent 4 }} {{- end -}} {{- define "common.deployment" -}} -{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.deployment.tpl") -}} +{{- include "common.utils.merge" (append . "common.deployment.tpl") -}} {{- end -}} diff --git a/templates/_hpa.yaml b/templates/_hpa.yaml index c1a2baa..4253f6b 100644 --- a/templates/_hpa.yaml +++ b/templates/_hpa.yaml @@ -1,19 +1,21 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.hpa.tpl" -}} +{{- $top := first . -}} +{{- $autoscaling := index . 1 -}} apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: - {{ include "common.metadata" . | nindent 2 }} + {{ include "common.metadata" (list $top) | nindent 2 }} spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment - name: {{ include "common.fullname" . }} - minReplicas: {{ .Values.autoscaling.minReplicas }} - maxReplicas: {{ .Values.autoscaling.maxReplicas }} + name: {{ include "common.fullname" $top }} + minReplicas: {{ $autoscaling.minReplicas }} + maxReplicas: {{ $autoscaling.maxReplicas }} metrics: - {{- with .Values.autoscaling.cpuUtilizationPercentage }} + {{- with $autoscaling.cpuUtilizationPercentage }} - type: Resource resource: name: cpu @@ -21,7 +23,7 @@ spec: type: Utilization averageUtilization: {{ . }} {{- end }} - {{- with .Values.autoscaling.memoryUtilizationPercentage }} + {{- with $autoscaling.memoryUtilizationPercentage }} - type: Resource resource: name: memory @@ -31,14 +33,9 @@ spec: {{- end -}} {{- end -}} -{{- define "common.hpa.if" -}} -{{- $top := first . -}} -{{- $autoscaling := $top.Values.autoscaling | default (dict) -}} +{{- define "common.hpa" -}} +{{- $autoscaling := index . 1 -}} {{- if $autoscaling.enabled -}} {{- include "common.utils.merge" (append . "common.hpa.tpl") -}} {{- end -}} {{- end -}} - -{{- define "common.hpa" -}} -{{- include "common.utils.flattenCall" (list "common.hpa.if" .) -}} -{{- end -}} diff --git a/templates/_ingress.yaml b/templates/_ingress.yaml index d8638a1..731b60a 100644 --- a/templates/_ingress.yaml +++ b/templates/_ingress.yaml @@ -1,27 +1,31 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.ingress.metadata" -}} -{{- with .Values.ingress.annotations }} +{{- $ingress := index . 1 -}} +{{- with $ingress.annotations }} annotations: {{- toYaml . | nindent 2 }} {{- end }} {{- end -}} {{- define "common.ingress.tpl" -}} -{{- $fullName := include "common.fullname" . -}} -{{- $svcPort := .Values.service.port -}} -{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +{{- $top := first . -}} +{{- $ingress := index . 1 -}} +{{- $service := index . 2 -}} +{{- $fullName := include "common.fullname" $top -}} +{{- $svcPort := $service.port -}} +{{- if semverCompare ">=1.14-0" $top.Capabilities.KubeVersion.GitVersion -}} apiVersion: networking.k8s.io/v1beta1 {{- else -}} apiVersion: extensions/v1beta1 {{- end }} kind: Ingress metadata: - {{ include "common.metadata" (list . "common.ingress.metadata") | nindent 2 }} + {{ include "common.metadata" (append . "common.ingress.metadata") | nindent 2 }} spec: -{{- if .Values.ingress.tls }} +{{- if $ingress.tls }} tls: - {{- range .Values.ingress.tls }} + {{- range $ingress.tls }} - hosts: {{- range .hosts }} - {{ . | quote }} @@ -30,7 +34,7 @@ spec: {{- end }} {{- end }} rules: - {{- range .Values.ingress.hosts }} + {{- range $ingress.hosts }} - host: {{ .host | quote }} http: paths: @@ -43,13 +47,9 @@ spec: {{- end }} {{- end -}} -{{- define "common.ingress.if" -}} -{{- $top := first . -}} -{{- if $top.Values.ingress.enabled -}} +{{- define "common.ingress" -}} +{{- $ingress := index . 1 -}} +{{- if $ingress.enabled -}} {{- include "common.utils.merge" (append . "common.ingress.tpl") -}} {{- end -}} {{- end -}} - -{{- define "common.ingress" -}} -{{- include "common.utils.flattenCall" (list "common.ingress.if" .) -}} -{{- end -}} diff --git a/templates/_metadata.tpl b/templates/_metadata.tpl index e20880b..3efe2e8 100644 --- a/templates/_metadata.tpl +++ b/templates/_metadata.tpl @@ -21,14 +21,15 @@ app.kubernetes.io/instance: {{ .Release.Name }} {{- end -}} {{ define "common.metadata.tpl" -}} -name: {{ include "common.fullname" . }} +{{- $top := first . -}} +name: {{ include "common.fullname" $top }} labels: - {{- include "common.labels" . | nindent 2 -}} + {{- include "common.labels" $top | nindent 2 -}} {{- end -}} {{- /* Create a standard metadata header */ -}} {{ define "common.metadata" -}} -{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.metadata.tpl") -}} +{{- include "common.utils.merge" (append . "common.metadata.tpl") -}} {{- end -}} diff --git a/templates/_pdb.yaml b/templates/_pdb.yaml index 21699fe..ee88c8f 100644 --- a/templates/_pdb.yaml +++ b/templates/_pdb.yaml @@ -1,35 +1,32 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.pdb.tpl" -}} +{{- $top := first . -}} +{{- $pdb := index . 1 -}} apiVersion: policy/v1beta1 kind: PodDisruptionBudget metadata: - {{ include "common.metadata" . | nindent 2 }} + {{ include "common.metadata" (list $top) | nindent 2 }} spec: selector: matchLabels: - {{- include "common.selectorLabels" . | nindent 6 }} - {{- with .Values.podDisruptionBudget }} - {{- if not (or (empty .minAvailable) (empty .maxUnavailable)) -}} - {{- fail "podDisruptionBudget.minAvailable and podDisruptionBudget.maxUnavailable can not be set together" }} + {{- include "common.selectorLabels" $top | nindent 6 }} + {{- if not (or (empty $pdb.minAvailable) (empty $pdb.maxUnavailable)) -}} + {{- fail "minAvailable and maxUnavailable can not be set together" }} {{- end -}} - {{- with .minAvailable }} + {{- with $pdb.minAvailable }} minAvailable: {{ . }} {{- end }} - {{- with .maxUnavailable }} + {{- with $pdb.maxUnavailable }} maxUnavailable: {{ . }} {{- end }} - {{- end }} -{{- end -}} - -{{- define "common.pdb.if" -}} -{{- $top := first . -}} -{{- $autoscaling := $top.Values.autoscaling | default (dict) -}} -{{- if or (and $autoscaling.enabled (gt ($autoscaling.minReplicas | int) 1)) (gt ($top.Values.replicaCount | int) 1) }} - {{- include "common.utils.merge" (append . "common.pdb.tpl") -}} -{{- end -}} {{- end -}} {{- define "common.pdb" -}} -{{- include "common.utils.flattenCall" (list "common.pdb.if" .) -}} +{{- $top := first . -}} +{{- $pod := index . 2 -}} +{{- $autoscaling := index . 3 -}} +{{- if or (and $autoscaling.enabled (gt ($autoscaling.minReplicas | int) 1)) (gt ($pod.replicaCount | int) 1) }} + {{- include "common.utils.merge" (append . "common.pdb.tpl") -}} +{{- end -}} {{- end -}} diff --git a/templates/_pod.tpl b/templates/_pod.tpl index ca2b38d..224b1e3 100644 --- a/templates/_pod.tpl +++ b/templates/_pod.tpl @@ -1,33 +1,35 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.pod-template.tpl" -}} +{{- $top := first . -}} +{{- $values := index . 1 -}} metadata: labels: - {{- include "common.selectorLabels" . | nindent 4 }} + {{- include "common.selectorLabels" $top | nindent 4 }} spec: - {{- with .Values.imagePullSecrets }} + {{- with $values.imagePullSecrets }} imagePullSecrets: {{- toYaml . | nindent 8 }} {{- end }} - serviceAccountName: {{ include "common.serviceAccountName" . }} + serviceAccountName: {{ include "common.serviceAccountName" $top }} securityContext: - {{- toYaml .Values.podSecurityContext | nindent 4 }} + {{- toYaml $values.podSecurityContext | nindent 4 }} containers: - {{- include "common.container" . | nindent 6 }} - {{- with .Values.nodeSelector }} + {{- with $values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 4 }} {{- end }} - {{- with .Values.affinity }} + {{- with $values.affinity }} affinity: {{- toYaml . | nindent 4 }} {{- end }} - {{- with .Values.tolerations }} + {{- with $values.tolerations }} tolerations: {{- toYaml . | nindent 4 }} {{- end }} {{- end -}} {{- define "common.pod-template" -}} -{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.pod-template.tpl") -}} +{{- include "common.utils.merge" (append . "common.pod-template.tpl") -}} {{- end -}} diff --git a/templates/_secret.yaml b/templates/_secret.yaml index 37b19f9..0bcbc66 100644 --- a/templates/_secret.yaml +++ b/templates/_secret.yaml @@ -1,14 +1,15 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.secret.tpl" -}} +{{- $top := first . -}} apiVersion: v1 kind: Secret metadata: - {{ include "common.metadata" . | nindent 2 }} + {{ include "common.metadata" (list $top) | nindent 2 }} type: Opaque data: {} {{- end -}} {{- define "common.secret" -}} -{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.secret.tpl") -}} +{{- include "common.utils.merge" (append . "common.secret.tpl") -}} {{- end -}} diff --git a/templates/_service.yaml b/templates/_service.yaml index eb3d299..3f151b8 100644 --- a/templates/_service.yaml +++ b/templates/_service.yaml @@ -1,21 +1,23 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.service.tpl" -}} +{{- $top := first . -}} +{{- $service := index . 1 -}} apiVersion: v1 kind: Service metadata: - {{ include "common.metadata" . | nindent 2 }} + {{ include "common.metadata" (list $top) | nindent 2 }} spec: - type: {{ .Values.service.type }} + type: {{ $service.type }} ports: - - port: {{ .Values.service.port }} + - port: {{ $service.port }} targetPort: http protocol: TCP name: http selector: - {{- include "common.selectorLabels" . | nindent 4 }} + {{- include "common.selectorLabels" $top | nindent 4 }} {{- end -}} {{- define "common.service" -}} -{{- include "common.utils.flattenCall" (list "common.utils.merge" . "common.service.tpl") -}} +{{- include "common.utils.merge" (append . "common.service.tpl") -}} {{- end -}} diff --git a/templates/_serviceaccount.yaml b/templates/_serviceaccount.yaml index 8209b09..bd425f0 100644 --- a/templates/_serviceaccount.yaml +++ b/templates/_serviceaccount.yaml @@ -1,8 +1,10 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.serviceAccount.metadata" -}} -name: {{ include "common.serviceAccountName" . }} -{{- with .Values.serviceAccount.annotations }} +{{- $top := first . -}} +{{- $serviceAccount := index . 1 -}} +name: {{ include "common.serviceAccountName" $top }} +{{- with $serviceAccount.annotations }} annotations: {{- toYaml . | nindent 2 }} {{- end }} @@ -12,17 +14,13 @@ annotations: apiVersion: v1 kind: ServiceAccount metadata: - {{ include "common.metadata" (list . "common.serviceAccount.metadata") | nindent 2 }} + {{ include "common.metadata" (append . "common.serviceAccount.metadata") | nindent 2 }} {{- end -}} -{{- define "common.serviceAccount.if" -}} +{{- define "common.serviceAccount" -}} {{- $top := first . -}} -{{- $serviceAccount := $top.Values.serviceAccount | default (dict) -}} +{{- $serviceAccount := index . 1 -}} {{- if $serviceAccount.create -}} {{- include "common.utils.merge" (append . "common.serviceAccount.tpl") -}} {{- end -}} {{- end -}} - -{{- define "common.serviceAccount" -}} -{{- include "common.utils.flattenCall" (list "common.serviceAccount.if" .) -}} -{{- end -}} diff --git a/templates/_servicemonitor-secret.yaml b/templates/_servicemonitor-secret.yaml index ec6283e..21f60c6 100644 --- a/templates/_servicemonitor-secret.yaml +++ b/templates/_servicemonitor-secret.yaml @@ -1,10 +1,12 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.serviceMonitor.secret.tpl" -}} -{{- $basicAuth := .Values.serviceMonitor.basicAuth | default (dict) -}} +{{- $top := first . -}} +{{- $serviceMonitor := index . 1 -}} +{{- $basicAuth := $serviceMonitor.basicAuth | default (dict) -}} metadata: - name: {{ $basicAuth.secretName | default (include "common.fullname" .) }} - {{- with .Values.serviceMonitor.namespace }} + name: {{ $basicAuth.secretName | default (include "common.fullname" $top) }} + {{- with $serviceMonitor.namespace }} namespace: {{ . }} {{- end }} {{- if $basicAuth.enabled }} @@ -14,14 +16,10 @@ data: {{- end }} {{- end -}} -{{- define "common.serviceMonitor.secret.if" -}} +{{- define "common.serviceMonitor.secret" -}} {{- $top := first . -}} -{{- $serviceMonitor := $top.Values.serviceMonitor | default (dict) -}} +{{- $serviceMonitor := index . 1 -}} {{- if $serviceMonitor.enabled -}} {{- include "common.secret" (append . "common.serviceMonitor.secret.tpl") -}} {{- end -}} {{- end -}} - -{{- define "common.serviceMonitor.secret" -}} -{{- include "common.utils.flattenCall" (list "common.serviceMonitor.secret.if" .) -}} -{{- end -}} diff --git a/templates/_servicemonitor.yaml b/templates/_servicemonitor.yaml index 91156b1..965a17b 100644 --- a/templates/_servicemonitor.yaml +++ b/templates/_servicemonitor.yaml @@ -1,36 +1,39 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.serviceMonitor.metadata" -}} -{{- with .Values.serviceMonitor.namespace }} +{{- $serviceMonitor := index . 1 -}} +{{- with $serviceMonitor.namespace }} namespace: {{ . }} {{- end }} {{- end -}} {{- define "common.serviceMonitor.tpl" -}} +{{- $top := first . -}} +{{- $serviceMonitor := index . 1 -}} apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: - {{ include "common.metadata" (list . "common.serviceMonitor.metadata") | nindent 2 }} + {{ include "common.metadata" (append . "common.serviceMonitor.metadata") | nindent 2 }} spec: selector: matchLabels: - {{- include "common.selectorLabels" . | nindent 6 }} + {{- include "common.selectorLabels" $top | nindent 6 }} namespaceSelector: matchNames: - - {{ .Release.Namespace | quote }} + - {{ $top.Release.Namespace | quote }} endpoints: - - port: {{ .Values.serviceMonitor.port | default .Values.service.port }} - {{- with .Values.serviceMonitor.path }} + - port: {{ $serviceMonitor.port }} + {{- with $serviceMonitor.path }} path: {{ . }} {{- end }} - {{- with .Values.serviceMonitor.interval }} + {{- with $serviceMonitor.interval }} interval: {{ . }} {{- end }} - {{- with .Values.serviceMonitor.scrapeTimeout }} + {{- with $serviceMonitor.scrapeTimeout }} scrapeTimeout: {{ . }} {{- end }} - {{- $basicAuth := .Values.serviceMonitor.basicAuth | default (dict) -}} - {{- $name := $basicAuth.secretName | default (include "common.fullname" .) -}} + {{- $basicAuth := $serviceMonitor.basicAuth | default (dict) -}} + {{- $name := $basicAuth.secretName | default (include "common.fullname" $top) -}} {{- if $basicAuth.enabled }} basicAuth: username: @@ -42,14 +45,10 @@ spec: {{- end }} {{- end -}} -{{- define "common.serviceMonitor.if" -}} +{{- define "common.serviceMonitor" -}} {{- $top := first . -}} -{{- $serviceMonitor := $top.Values.serviceMonitor | default (dict) -}} +{{- $serviceMonitor := index . 1 -}} {{- if $serviceMonitor.enabled -}} {{- include "common.utils.merge" (append . "common.serviceMonitor.tpl") -}} {{- end -}} {{- end -}} - -{{- define "common.serviceMonitor" -}} -{{- include "common.utils.flattenCall" (list "common.serviceMonitor.if" .) -}} -{{- end -}} diff --git a/templates/_utils.tpl b/templates/_utils.tpl index f0c087a..834979c 100644 --- a/templates/_utils.tpl +++ b/templates/_utils.tpl @@ -3,32 +3,22 @@ {{- /* Merge one or more YAML templates and output the result. This takes an list of values: -- the first item is the top context -- the rest items are template names of the templates, the former one will override the latter. +- the top context +- [optional] zero or more template args +- [optional] the template name of the overrides (destination) +- the template name of the base (source) */ -}} {{- define "common.utils.merge" -}} {{- $top := first . -}} -{{- $dest := dict -}} -{{- range (rest .) -}} - {{- $src := fromYaml (include . $top) | default (dict) -}} - {{- $dest = merge $dest $src -}} +{{- $tplName := last . -}} +{{- $args := initial . -}} +{{- if typeIs "string" (last $args) -}} + {{- $overridesName := last $args -}} + {{- $args = initial $args -}} + {{- $tpl := fromYaml (include $tplName $args) | default (dict) -}} + {{- $overrides := fromYaml (include $overridesName $args) | default (dict) -}} + {{- toYaml (merge $overrides $tpl) -}} +{{- else -}} + {{- include $tplName $args -}} {{- end -}} -{{- toYaml $dest -}} -{{- end -}} - -{{- /* -Flatten list of arguments before rendering the given template. -This takes an list of values: -- the first item is the template name to be rendered -- the second item is either an list of arguments or a single argument -- the rest items are the appended arguments -*/ -}} -{{- define "common.utils.flattenCall" -}} -{{- $tpl := first . -}} -{{- $args := index . 1 -}} -{{- if not (typeIs "[]interface {}" $args) -}} - {{- $args = list $args -}} -{{- end -}} -{{- $args = concat $args (slice . 2) -}} -{{- include $tpl $args -}} {{- end -}} From 96ba1fecbb8c432b906e221575a005e4ff503cea Mon Sep 17 00:00:00 2001 From: Chi-En Wu Date: Wed, 22 Apr 2020 10:51:27 +0800 Subject: [PATCH 7/9] refactor: remove `CronJob.spec.jobTemplate.metadata.name` --- templates/_cronjob.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/templates/_cronjob.yaml b/templates/_cronjob.yaml index 2c6eb32..f78cffc 100644 --- a/templates/_cronjob.yaml +++ b/templates/_cronjob.yaml @@ -26,7 +26,8 @@ spec: {{- end }} jobTemplate: metadata: - {{ include "common.metadata" (list $top) | nindent 6 }} + labels: + {{ include "common.selectorLabels" $top | nindent 8 }} spec: template: {{ include "common.pod-template" (list $top $pod "common.cronJob.pod") | nindent 8 }} From bae66c7c9ca6d194f26506018a87ce6e70b10024 Mon Sep 17 00:00:00 2001 From: Chi-En Wu Date: Wed, 22 Apr 2020 10:55:16 +0800 Subject: [PATCH 8/9] refactor: `common.pod-template` => `common.pod.template` --- templates/_cronjob.yaml | 2 +- templates/_deployment.yaml | 2 +- templates/_pod.tpl | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/templates/_cronjob.yaml b/templates/_cronjob.yaml index f78cffc..4dc54a0 100644 --- a/templates/_cronjob.yaml +++ b/templates/_cronjob.yaml @@ -30,7 +30,7 @@ spec: {{ include "common.selectorLabels" $top | nindent 8 }} spec: template: - {{ include "common.pod-template" (list $top $pod "common.cronJob.pod") | nindent 8 }} + {{ include "common.pod.template" (list $top $pod "common.cronJob.pod") | nindent 8 }} {{- end -}} {{- define "common.cronJob" -}} diff --git a/templates/_deployment.yaml b/templates/_deployment.yaml index bcc58a1..fcec8fc 100644 --- a/templates/_deployment.yaml +++ b/templates/_deployment.yaml @@ -16,7 +16,7 @@ spec: matchLabels: {{ include "common.selectorLabels" $top | nindent 6 }} template: - {{ include "common.pod-template" . | nindent 4 }} + {{ include "common.pod.template" . | nindent 4 }} {{- end -}} {{- define "common.deployment" -}} diff --git a/templates/_pod.tpl b/templates/_pod.tpl index 224b1e3..9f9c555 100644 --- a/templates/_pod.tpl +++ b/templates/_pod.tpl @@ -1,6 +1,6 @@ {{/* vim: set filetype=mustache: */}} -{{- define "common.pod-template.tpl" -}} +{{- define "common.pod.template.tpl" -}} {{- $top := first . -}} {{- $values := index . 1 -}} metadata: @@ -30,6 +30,6 @@ spec: {{- end }} {{- end -}} -{{- define "common.pod-template" -}} -{{- include "common.utils.merge" (append . "common.pod-template.tpl") -}} +{{- define "common.pod.template" -}} +{{- include "common.utils.merge" (append . "common.pod.template.tpl") -}} {{- end -}} From cf0679333cc1ef18f0235172c8dd92cb5b1f8e11 Mon Sep 17 00:00:00 2001 From: Chi-En Wu Date: Wed, 22 Apr 2020 11:42:25 +0800 Subject: [PATCH 9/9] chore: bump to version 0.2.0 --- Chart.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chart.yaml b/Chart.yaml index 004fe53..be62a9f 100644 --- a/Chart.yaml +++ b/Chart.yaml @@ -14,4 +14,4 @@ type: library # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 0.1.0 +version: 0.2.0