The year is 2021, and I need to write a Windows Batch Script to get Helm Post-Renderer working on Windows so I can deploy my application to Kubernetes.
What is Helm?
From the Helm website: “Helm helps you manage Kubernetes applications”.
If you use Kubernetes, there’s a good chance you will be using at least some portion of Helm. Helm has many features, and is most well known for Helm Charts. You can (and should) also use Helm to deploy your application to Kubernetes.
Windows Batch Script with Helm Post-Renderer and WSL
I want my Kubernetes deployment (using Helm and Kustomize) to be able to work on my laptop, and also in a GitLab CI/CD pipeline. And because my Windows laptop has WSL (no WSL2!) I can do a few cool Linux things with it, like directly run Helm commands (Maybe WSL2 also has this problem). If only I could use a real Linux laptop at work, but anyways..
The Helm Post-Renderer gives an error when running a Linux Bash script in WSL. So much to my amazement, in 2021 I needed to write a Windows Batch Script!
Error message in WSL
Running a Helm install/upgrade gives the error:
helm upgrade --install release123 my-charts/app123-charts --version=0.1.0 --namespace=dev1 --values=../values.yaml --post-renderer ./kustomize.sh --debug --dry-run ## Error: error while running post render on files: error while running command \\C\Users\username\helm\my-charts\app123-charts\app-kustomize\kustomize-post-render\kustomize.sh. error output: ## : fork/exec \\C\Users\username\helm\my-charts\app123-charts\app-kustomize\kustomize-post-render\kustomize.sh: %1 is not a valid Win32 application.
Just the error:
%1 is not a valid Win32 application.
Okay, that’s interesting. Running a helm command gives me a Windows related error. So now we need to do it the Windows way.
Helm Post-Renderer Windows Batch Script
This Windows Batch Script will work from Windows (kustomize.bat):
@echo off REM fix mapped drive in TS: https://stackoverflow.com/questions/9013941/how-to-run-batch-file-from-network-share-without-unc-path-are-not-supported-me/34182234#34182234 @pushd %~dp0 REM findstr is pure windows command: https://superuser.com/questions/853580/real-windows-equivalent-to-cat-stdin/1425671#1425671 FINDSTR . > all.yaml kustomize build . && del all.yaml || exit 1 @popd
The above Batch Script also works from a Windows Terminal Server that is mapping a drive.
Helm Post-Renderer Linux Bash script
For reference, the same in Linux would be (kustomize.sh):
#!/bin/bash cat <&0 > all.yaml kustomize build . && rm all.yaml
Above Linux bash script source: Taylor Thomas.
With this Windows Batch Script, you can run Helm install/upgrade commands directly on your Windows laptop so you can deploy to your local or remote Kubernetes cluster. This can help to debug your true pipeline deployments, if using something like GitLab CI/CD since it’s now possible to deploy using the same method.