examples
examples copied to clipboard
Example of generating multiple resources from a single template file
In the values.yaml file, we could specify several sites:
sites:
- name: foo
source: "foo.com"
- name: bar
source: "bar.com
- name: baz
source: "baz.com
Then the chart would generate three deployments and three services, one for each site.
Then we could add an ingress that includes each of the services:
spec:
rules:
- host: hello-world.com
http:
paths:
- backend:
service:
name: foo
port:
name: http
path: /foo
pathType: Prefix
- backend:
service:
name: bar
port:
name: http
path: /bar
pathType: Prefix
- backend:
service:
name: baz
port:
name: http
path: /baz
pathType: Prefix
Then each site would be accessible via the following URLs:
- hello-world.com/foo
- hello-world.com/bar
- hello-world.com/baz
I created a new chart:
helm create hello-world-multiple
cd hello-world-multiple
helm template hello-world .
Here is the modified template/deployment.yaml file:
{{- range $key, $value := .Values.repositories }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ $value.name }}
labels:
{{- include "hello-world-multiple.labels" $ | nindent 4 }}
spec:
{{- if not $.Values.autoscaling.enabled }}
replicas: {{ $.Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "hello-world-multiple.selectorLabels" $ | nindent 6 }}
template:
metadata:
{{- with $.Values.podAnnotations }}
annotations:
{{- toYaml $ | nindent 8 }}
{{- end }}
labels:
{{- include "hello-world-multiple.selectorLabels" $ | nindent 8 }}
spec:
{{- with $.Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml $ | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "hello-world-multiple.serviceAccountName" $ }}
securityContext:
{{- toYaml $.Values.podSecurityContext | nindent 8 }}
containers:
- name: hello-world
securityContext:
{{- toYaml $.Values.securityContext | nindent 12 }}
image: "{{ $.Values.image.repository }}:{{ $.Values.image.tag | default $.Chart.AppVersion }}"
imagePullPolicy: {{ $.Values.image.pullPolicy }}
{{- end }}
Reference(s):
- https://github.com/helm/helm/issues/3684
- https://stackoverflow.com/a/56225254/8507637