-
Notifications
You must be signed in to change notification settings - Fork 7
Helm_Lab_08 | SubCharts
The Purpose of this lab is to give the understanding SubCharts (sometimes called a partial or a subtemplate).
To get going, let's take a quick look at SubChartsTemplate
To this point we have been working only with one chart. But charts can have dependencies, called subcharts, that also have their own values and templates
In this section we will see Sharing Templates with Subcharts.
Parent charts and subcharts can share templates. Any defined block in any chart is available to other charts.
For example, we can define a simple template like this:
{{- define "labels" }}from: mychart{{ end }}
Thus, the labels chart can be included from any other chart.
While chart developers have a choice between include and template, one advantage of using include is that include can dynamically reference templates:
{{ include $mytemplate }}
Using template function in helm
- Update the gowebapp/templates/deployment.yaml
{{- define "gowebappchart.labels" }}
app.kubernetes.io/name: app
app.kubernetes.io/instance: goweb-app
{{- end }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: goweb-app
labels:
{{- template "gowebappchart.labels" }}
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: app
app.kubernetes.io/instance: goweb-app
template:
metadata:
labels:
app.kubernetes.io/name: app
app.kubernetes.io/instance: goweb-app
spec:
containers:
- name: {{ .Values.containers.name }}
image: "{{ .Values.containers.image }}:{{ .Values.containers.tag }}"
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
protocol: TCP
env:
{{- range $key, $value := .Values.containers.envVar }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
{{- if .Values.containers.livenessProbe }}
livenessProbe:
{{- with .Values.containers.livenessProbe }}
{{- toYaml . | nindent 12 }}
{{- end }}
{{- end }}
{{- if .Values.containers.readinessProbe }}
readinessProbe:
{{- with .Values.containers.readinessProbe }}
{{- toYaml . | nindent 12 }}
{{- end }}
{{- end }}
So you can validate the output by:-
helm template goweb-app ./gowebapp
Using include function in helm
- Update the gowebapp/templates/deployment.yaml
{{- define "gowebappchart.labels" }}
app.kubernetes.io/name: app
app.kubernetes.io/instance: goweb-app
{{- end }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: goweb-app
labels:
{{- include "gowebappchart.labels" . | nindent 4 }}
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: app
app.kubernetes.io/instance: goweb-app
template:
metadata:
labels:
app.kubernetes.io/name: app
app.kubernetes.io/instance: goweb-app
spec:
containers:
- name: {{ .Values.containers.name }}
image: "{{ .Values.containers.image }}:{{ .Values.containers.tag }}"
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
protocol: TCP
env:
{{- range $key, $value := .Values.containers.envVar }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
{{- if .Values.containers.livenessProbe }}
livenessProbe:
{{- with .Values.containers.livenessProbe }}
{{- toYaml . | nindent 12 }}
{{- end }}
{{- end }}
{{- if .Values.containers.readinessProbe }}
readinessProbe:
{{- with .Values.containers.readinessProbe }}
{{- toYaml . | nindent 12 }}
{{- end }}
{{- end }}
So you can validate the output by:-
helm template goweb-app ./gowebapp