Debugging and Testing Helm Charts Using VS Code

Introduction

Helm charts are useful to manage, install and upgrade Kubernetes applications. Helm is a developer friendly templating Kubernetes resource files based on GO templates and YAML. The templates are written using any notepads and deployed using Helm. Writing helm commands on notepad is not user friendly. Using Visual studio code we can setup a workaround that will enable to code faster and see the generated Kubernetes YAML files.  The following document has the details of necessary steps and guide that will help you to debug and test your helm charts before directly installing in the Kubernetes cluster using Visual Studio Code.

Download and configure Helm in windows

Download the Helm.exe for windows from the link https://github.com/helm/helm/releases or https://get.helm.sh/helm-v3.5.0-windows-amd64.zip.

Unzip and copy the contents to some directory like 'C:\Users\<username>\Tools\Helm'.

Configure the above directory URL to environment variable 'PATH'. This enable to run the helm commands in command line terminal without mentioning the actual location of the binary.

Setup Visual Studio Code

Install the following helm extensions in VS code. The helm extensions are optional and not necessary to debug or test.

·       YAML by Red Hat

·       Kubernetes by Microsoft

·       docs-yaml by Microsoft

Create a folder in you local machine where you want to keep all you helm charts. For example "C:\Users\<username>\helm".

Open the folder in VS Code by navigating to 'File→Open folder'. The folder is now your workspace.

Working with Charts

Let us see how we can work with a chart. From the VS Code open a new terminal my navigating to 'Terminal→New Terminal'. This will open the command line terminal within VS Code. This will also avoid switching to multiple windows if you are using windows command line.

From the terminal enter the following command,

helm create mychart

The above command will create a new chart called 'mychart' with basic templates in you helm folder. 

Let us check how this basic templates generates into Kubernetes manifest. To check run the following command,

helm template .\mychart > mychart.template.yaml --debug

From the above listing 'template' command generates your chart into Kubernetes manifest and writes the generated output into 'mychart.template.yaml' file.

The '--debug' command will give us the detail error information. We will explore that in a moment.

Open the mychart.template.yaml file into VS Code and split the windows so that your VS Code will be as shown below,


This will help you to write your code and view the output simultaneously. Your left windows will be used to write code and right window will serves as your output.

Let us see how it works. Create a new file 'configmap.yaml' in 'mychart\template' directory and insert the following code.

apiVersion: v1
kind: ConfigMap
metadata:
  name: mychart-configmap
data:
  myvalue: "Hello World"

Once ready re run the template command again. If all goes well you can see the configmap.yaml section if your generated template file. This was we can add a template or edit an existing template and view your changes immediately without having to install the charts in Kubernetes cluster.

Debugging the code

Till now we have seen how to setup the environment. But what happen if there are any syntax error with the chart templates? The '--debug' command list down all the errors in the terminal window.

To demonstrate, open the 'serviceaccount.yaml' file and in the last line you will see this command {{- end }}. Remove the spaces within the brackets(remember yaml is space sensitive) and re-run the template command again. You should see the bellow error,


The above screen shot shows the detail error message which will help us to identify the problem and debug faster.

Conclusion

This is a simple work around to debug the helm charts using VS Code on windows. The approach will be better improved if we have access to either kubernetes cluster or minikube cluster. There are few commands in helm (refer https://helm.sh/docs/chart_template_guide/debugging/) which debug the templates in better way even without actually installing the charts. 

Comments

Popular posts from this blog

Handle Multipart Contents in Asp.Net Core

Validate appsettings in ASP.net Core using FluentValidation