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 diff --git a/templates/_configmap.yaml b/templates/_configmap.yaml index 0a603dd..d509ada 100644 --- a/templates/_configmap.yaml +++ b/templates/_configmap.yaml @@ -1,12 +1,14 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.configMap.tpl" -}} +{{- $top := first . -}} apiVersion: v1 kind: ConfigMap -{{ include "common.metadata" . }} +metadata: + {{ 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 new file mode 100644 index 0000000..4dc54a0 --- /dev/null +++ b/templates/_cronjob.yaml @@ -0,0 +1,38 @@ +{{/* vim: set filetype=mustache: */}} + +{{- define "common.cronJob.pod" -}} +spec: + restartPolicy: OnFailure +{{- end -}} + +{{- define "common.cronJob.tpl" -}} +{{- $top := first . -}} +{{- $cronJob := index . 1 -}} +{{- $pod := index . 2 -}} +apiVersion: batch/v1beta1 +kind: CronJob +metadata: + {{ include "common.metadata" (list $top) | nindent 2 }} +spec: + schedule: "{{ $cronJob.schedule }}" + {{- with $cronJob.concurrencyPolicy }} + concurrencyPolicy: {{ . }} + {{- end }} + {{- with $cronJob.failedJobsHistoryLimit }} + failedJobsHistoryLimit: {{ . }} + {{- end }} + {{- with $cronJob.successfulJobsHistoryLimit }} + successfulJobsHistoryLimit: {{ . }} + {{- end }} + jobTemplate: + metadata: + labels: + {{ include "common.selectorLabels" $top | nindent 8 }} + spec: + template: + {{ include "common.pod.template" (list $top $pod "common.cronJob.pod") | nindent 8 }} +{{- end -}} + +{{- define "common.cronJob" -}} +{{- include "common.utils.merge" (append . "common.cronJob.tpl") -}} +{{- end -}} diff --git a/templates/_deployment.yaml b/templates/_deployment.yaml index e577e0b..fcec8fc 100644 --- a/templates/_deployment.yaml +++ b/templates/_deployment.yaml @@ -1,45 +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 -{{ include "common.metadata" . }} +metadata: + {{ 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: - 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" -}} -{{- 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 b3b332a..4253f6b 100644 --- a/templates/_hpa.yaml +++ b/templates/_hpa.yaml @@ -1,18 +1,21 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.hpa.tpl" -}} +{{- $top := first . -}} +{{- $autoscaling := index . 1 -}} apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler -{{ include "common.metadata" . }} +metadata: + {{ 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 @@ -20,7 +23,7 @@ spec: type: Utilization averageUtilization: {{ . }} {{- end }} - {{- with .Values.autoscaling.memoryUtilizationPercentage }} + {{- with $autoscaling.memoryUtilizationPercentage }} - type: Resource resource: name: memory @@ -30,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 70ffc91..731b60a 100644 --- a/templates/_ingress.yaml +++ b/templates/_ingress.yaml @@ -1,23 +1,31 @@ {{/* vim: set filetype=mustache: */}} +{{- define "common.ingress.metadata" -}} +{{- $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 -{{ include "common.metadata" . }} - {{- with .Values.ingress.annotations }} - annotations: - {{- toYaml . | nindent 4 }} - {{- end }} +metadata: + {{ 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 }} @@ -26,7 +34,7 @@ spec: {{- end }} {{- end }} rules: - {{- range .Values.ingress.hosts }} + {{- range $ingress.hosts }} - host: {{ .host | quote }} http: paths: @@ -39,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 15daf64..3efe2e8 100644 --- a/templates/_metadata.tpl +++ b/templates/_metadata.tpl @@ -20,12 +20,16 @@ app.kubernetes.io/name: {{ include "common.name" . }} app.kubernetes.io/instance: {{ .Release.Name }} {{- end -}} +{{ define "common.metadata.tpl" -}} +{{- $top := first . -}} +name: {{ include "common.fullname" $top }} +labels: + {{- include "common.labels" $top | 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.merge" (append . "common.metadata.tpl") -}} {{- end -}} diff --git a/templates/_pdb.yaml b/templates/_pdb.yaml index 5e6382a..ee88c8f 100644 --- a/templates/_pdb.yaml +++ b/templates/_pdb.yaml @@ -1,34 +1,32 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.pdb.tpl" -}} +{{- $top := first . -}} +{{- $pdb := index . 1 -}} apiVersion: policy/v1beta1 kind: PodDisruptionBudget -{{ include "common.metadata" . }} +metadata: + {{ 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 new file mode 100644 index 0000000..9f9c555 --- /dev/null +++ b/templates/_pod.tpl @@ -0,0 +1,35 @@ +{{/* vim: set filetype=mustache: */}} + +{{- define "common.pod.template.tpl" -}} +{{- $top := first . -}} +{{- $values := index . 1 -}} +metadata: + labels: + {{- include "common.selectorLabels" $top | nindent 4 }} +spec: + {{- with $values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "common.serviceAccountName" $top }} + 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.merge" (append . "common.pod.template.tpl") -}} +{{- end -}} diff --git a/templates/_secret.yaml b/templates/_secret.yaml index 0d25ed9..0bcbc66 100644 --- a/templates/_secret.yaml +++ b/templates/_secret.yaml @@ -1,13 +1,15 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.secret.tpl" -}} +{{- $top := first . -}} apiVersion: v1 kind: Secret -{{ include "common.metadata" . }} +metadata: + {{ 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 082ff27..3f151b8 100644 --- a/templates/_service.yaml +++ b/templates/_service.yaml @@ -1,20 +1,23 @@ {{/* vim: set filetype=mustache: */}} {{- define "common.service.tpl" -}} +{{- $top := first . -}} +{{- $service := index . 1 -}} apiVersion: v1 kind: Service -{{ include "common.metadata" . }} +metadata: + {{ 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 2837ac8..bd425f0 100644 --- a/templates/_serviceaccount.yaml +++ b/templates/_serviceaccount.yaml @@ -1,26 +1,26 @@ {{/* vim: set filetype=mustache: */}} +{{- define "common.serviceAccount.metadata" -}} +{{- $top := first . -}} +{{- $serviceAccount := index . 1 -}} +name: {{ include "common.serviceAccountName" $top }} +{{- with $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" (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 new file mode 100644 index 0000000..21f60c6 --- /dev/null +++ b/templates/_servicemonitor-secret.yaml @@ -0,0 +1,25 @@ +{{/* vim: set filetype=mustache: */}} + +{{- define "common.serviceMonitor.secret.tpl" -}} +{{- $top := first . -}} +{{- $serviceMonitor := index . 1 -}} +{{- $basicAuth := $serviceMonitor.basicAuth | default (dict) -}} +metadata: + name: {{ $basicAuth.secretName | default (include "common.fullname" $top) }} + {{- with $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" -}} +{{- $top := first . -}} +{{- $serviceMonitor := index . 1 -}} +{{- if $serviceMonitor.enabled -}} + {{- include "common.secret" (append . "common.serviceMonitor.secret.tpl") -}} +{{- end -}} +{{- end -}} diff --git a/templates/_servicemonitor.yaml b/templates/_servicemonitor.yaml new file mode 100644 index 0000000..965a17b --- /dev/null +++ b/templates/_servicemonitor.yaml @@ -0,0 +1,54 @@ +{{/* vim: set filetype=mustache: */}} + +{{- define "common.serviceMonitor.metadata" -}} +{{- $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" (append . "common.serviceMonitor.metadata") | nindent 2 }} +spec: + selector: + matchLabels: + {{- include "common.selectorLabels" $top | nindent 6 }} + namespaceSelector: + matchNames: + - {{ $top.Release.Namespace | quote }} + endpoints: + - port: {{ $serviceMonitor.port }} + {{- with $serviceMonitor.path }} + path: {{ . }} + {{- end }} + {{- with $serviceMonitor.interval }} + interval: {{ . }} + {{- end }} + {{- with $serviceMonitor.scrapeTimeout }} + scrapeTimeout: {{ . }} + {{- end }} + {{- $basicAuth := $serviceMonitor.basicAuth | default (dict) -}} + {{- $name := $basicAuth.secretName | default (include "common.fullname" $top) -}} + {{- 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" -}} +{{- $top := first . -}} +{{- $serviceMonitor := index . 1 -}} +{{- if $serviceMonitor.enabled -}} + {{- include "common.utils.merge" (append . "common.serviceMonitor.tpl") -}} +{{- end -}} +{{- 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 -}}