Read values for templates


Read values for templates

In this tutorial, we are going to discuss how to read values for templates in helm charts.

Read values for templates

Let us understand how to create our values and use them within the template. Within our sample chart, Let me get into the values YAML file.

[email protected]:~/mychart$ ls
Chart.yaml  charts  templates  values.yaml

I do have values.yaml file. This file was created while using the chart create command.

So I’m going to delete all the values and add my one value over here. So these are all the values that were added when the template was getting created. Let me delete all the values.

Now I’m going to add a simple line with a key and value. So I do have a key message, and a value corresponding to that key is “Welcome to waytoeasylearn.”

[email protected]:~/mychart$ cat values.yaml 
message: "Welcome to waytoeasylearn"

So we’re going to read this specific key within the template and substitute that and make use of that within the templates.

Let me go ahead and edit the template. I’m going to edit the configmap.yaml. Now I’m going to add another key-value pair as a part of the data.

So I’m going to add a key called message, which will have a value read from the values file using the message.

The Syntax is .Values.message (message is the key used within the values.yaml file.) and that will be enclosed within double flower bracket.

[email protected]:~/mychart$ cat templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Sample Config Map"
  message: {{ .Values.message }}

When I’m deploying this particular chart as a release, it will substitute the values and generate the suitable YAML file and deploy them.

dry run

If I don’t want to do the installation, say I don’t want to make a release, I want to see how the values are getting substituted; I can use a dry run option.

This dry run option is one of the commonly used command. So that I can test it out before doing the release. So I do have the mychart template. Now I’m going to do a dry run. The command is

[email protected]:~$ helm install --debug --dry-run firstdryrun ./mychart
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /home/ashok/mychart
NAME: firstdryrun
LAST DEPLOYED: Tue May 18 16:33:55 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}
COMPUTED VALUES:
message: Welcome to waytoeasylearn
HOOKS:
MANIFEST:
Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: firstdryrun-configmap
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn

So it’s going to substitute the values and generate the manifest file. And this is how the manifest file would get generated.

If you see the output, we have a template derivative for the message, and that’s read from the values file, and it got substituted. So “Welcome to waytoeasylearn” was the value that was computed.

The reason why we have the computed values within values.yaml file, we can have some computations to read data from other files or using some functions and calculate the value. A simple example is a date.

So those computed values it’s going to generate, and that value will get substituted. In this case, we hard-coded the value, and the exact value is substituted within the YAML file.

But there will not be any deployment for this particular operation. I can check using the helm ls.

[email protected]:~$ helm ls
NAME    NAMESPACE       REVISION        UPDATED STATUS  CHART   APP VERSION

There are no releases. In case if I wanted to do the release, I can do it using helm install. Helm install the name of the release and then the folder where we have the chart resources.

Install helm
[email protected]:~$ helm install firstvalue ./mychart/
NAME: firstvalue
LAST DEPLOYED: Tue May 18 16:58:52 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

So the values will be computed, which will replace within the template, and the template would get deployed. Now I should have that config file within the Kubernetes.

So “firstvalue” is the name of the release. Let me check the release.

[email protected]:~$ helm ls
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
firstvalue      default         1               2021-05-18 16:58:52.151010348 +0530 IST deployed        mychart-0.1.0   1.16.0     

Yes, it got deployed. If I wanted to check the values, what got deployed, I can use the manifest command. So I want to get the values that were used within the YAML file after doing the computation.

[email protected]:~$ helm get manifest firstvalue
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: firstvalue-configmap
data:
  myvalue: "Sample Config Map"
  message: Welcome to waytoeasylearn

Now I can get the details about this particular config map within the Kubernetes. Yes, I do have the following command.

[email protected]:~$ kubectl describe configmaps firstvalue-configmap
Name:         firstvalue-configmap
Namespace:    default
Labels:       app.kubernetes.io/managed-by=Helm
Annotations:  meta.helm.sh/release-name: firstvalue
              meta.helm.sh/release-namespace: default
Data
====
message:
----
Welcome to waytoeasylearn
myvalue:
----
Sample Config Map
Events:  

And here, I do have the key message and the value corresponding to that.

So successfully, we can read a key-value pair from the values file and substitute that within the templates and use that as a part of the installation.

Along with that, we learned how to do the dry run to verify the computed value and generate the YAML file for the test purpose.

Read values for templates

Scroll to top