feat: allow arguments to be passed to templates

This commit is contained in:
Chi-En Wu
2020-04-22 10:26:17 +08:00
parent bddaab6942
commit 9aecdacf39
15 changed files with 137 additions and 143 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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