Helm Lint
In this tutorial, we are going to discuss about helm lint. Till now, we have seen how to create the template and deploy the template into the Kubernetes cluster.
There are n number of stages where we need to do the testing. For example, immediately after making the templates or creating the templates, I need to do the testing, whether it is adhering to the standard or not.
As well as after the entities are deployed into the Kubernetes cluster, we need to verify whether the deployment happened properly, as expected or not. For that, we need to do some testing.
So for that helm provide 2 options one is lint. where it can verify the template whether it is adhering to the standard or not. This is like lint option available in any other language and the command is very simple. All I need to do is invoke Helm lint and then the chart folder location.
Example
For example, I do have n number of charts. Let me go ahead and run lint.
ashok@waytoeasylearn:~$ helm lint dependencytest/
==> Linting dependencytest/
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, 0 chart(s) failed
So that’s going to get into the charts and verify whether it is adhering to the standard or not. In this particular case it provided only one one info where icon is recommended for chart.yaml file. Other than that there are no error messages.
Check errors
In case, if I wanted to verify how it’s going to give the error messages, I can inject the error. Let me get into the charts, modify the template. Let me go ahead and change the template for test purpose.
ashok@waytoeasylearn:~$ cat dependencytest/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "dependencytest.fullname" . }}
labels:
{{- include "dependencytest.labels" . | nindent 4 - }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "dependencytest.selectorLabels" . | nindent 6 }}
.................................
.................................
.................................
.................................
So I changed
{{- include "dependencytest.labels" . | nindent 4 }}
To
{{- include "dependencytest.labels" . | nindent 4 - }}
So that it will become an invalid yaml file. And in the early tutorial, we discussed about what this particular dash going to do. It’s going to remove the new line and append this particular content as a part of the labels.
So this will fail to validate to be a valid yaml file. After making the changes Let me go ahead and run the lint command.
ashok@waytoeasylearn:~$ helm lint dependencytest/
==> Linting dependencytest/
[INFO] Chart.yaml: icon is recommended
[ERROR] templates/deployment.yaml: unable to parse YAML: error converting YAML to JSON: yaml: line 10: mapping values are not allowed in this context
Error: 1 chart(s) linted, 1 chart(s) failed
And it identified the error as it’s not able to convert the yaml file because it’s not a valid yaml file. So what the value that we had provided is not a valid one.
So this will help us in a big way while doing the development or constructing the templates. Let me go ahead and correct the same.
ashok@waytoeasylearn:~$ cat dependencytest/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "dependencytest.fullname" . }}
labels:
{{- include "dependencytest.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "dependencytest.selectorLabels" . | nindent 6 }}
.................................
.................................
.................................
.................................
Since it’s going to provide the warning message of front, we should be in a position to correct and get the valid yaml file as a part of the templates and able to get a valid yaml file.
I’m correcting making it as a valid yaml file. Now I can go ahead and run the lint command.
ashok@waytoeasylearn:~$ helm lint dependencytest/
==> Linting dependencytest/
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, 0 chart(s) failed
And this lint can be integrated with other CI/CD tools, so that the lint run can be automated and whenever it is getting a new source as a part of GitHub repository or even into the source control system automatically.
This lint can get invoked and validate whether the valid templates are getting checked into the repository or the source control system.
And there is another option to test whether the entities are deployed properly or not. That will be achieved using test that we will be seeing it in the next tutorial.
Summary
So in a quick summary we have seen how to use the lint and how it’s going to give the error messages after validating the templates and the charts.