Read values for templates


Read values for templates

In this tutorial, we are going to discuss about how to read values for templates in Kubernetes. Let us understand how to create our own values and use it within the template.

Within our sample chart, Let me get in to 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 which 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 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.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 and that’s going to 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 }}

Now when I’m deploying this particular chart as a release, it’s going to substitute the values and generate the right YAML file and deploy them.

dry run

In case if I don’t want to do the installation, say I don’t want to make a release I just 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 file or using some functions and calculate the value. Simple example is 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 same 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 is no releases. In case, if I wanted to do the release, I can do it using helm install. Helm install 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 and that will be replaced within the template and template would get deployed. Now I should have those 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. In case if I wanted to check the values, what got deployed I can use manifest command. So I want to get the values that was 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 are able to read a key value pair from the values file and substitute that within the templates and make use of that as a part of the installation.

Along with that, we learned about how to do the dry run so that we can verify the computed value and how the YAML file will be generated for the test purpose.

Read values for templates


Scroll to top