Harness Local Go-Templating

Overview

As declarative approaches to configuring technical solutions increases (manifests, IaC, etc.) the desire to leverage the configuration code on a local box for testing purposes grows. This can become difficult depending on the solution that consumes the configuration code and how the individual user can replicate the production-level processes.

The configuration code at Harness is no different. Certain pieces of the code are very specific to Harness and how Harness parses and uses the values in the code. However, other portions of the configuration code are not specific to Harness but can be difficult to run locally without the proper tools or processes.

This document will walk through how to set up the Configuration Code in a way where you can replicate the process locally without the need for a delegate on the local machine or in a local version of Minikube.

Setup

The first and main step required is to set up the Configuration as Code sync from Harness to the desired git repository (the doc for this setup is here). If you are using a poly-repo, you will need to set up the git sync for a Harness Application to the desired git repository. However, you can sync all of the Harness Applications down to the same git repository, if that is desired.

After that is complete, you will see a folder hierarchy where the Application breaks down into Services, Environments, Workflows, Pipelines, Triggers, and Provisioners. The main part to look into is the Services folder, where you will see the name of every Harness Service in the application. Then, you will want to traverse into a Kubernetes-based Service and you will see a folder called Manifests with a sub-folder called Files. This folder contains the Values file and the Templates files that leverage go-templating, which we will use in the next steps.

The last step will require the conversion of the values file from Harness to a local values file that only has hardcoded key:value pairs in it.

Tools

Now that the git sync is set up and the core files are found, the main tool that you will need to get is the go-templating engine from Harness. This can be downloaded from:

Mac:

curl -LO https://app.harness.io/storage/harness-download/snapshot-go-template/release/v0.4/bin/darwin/amd64/go-template chmod +x go-template

Linux:

curl -LO https://app.harness.io/storage/harness-download/snapshot-go-template/release/v0.4/bin/linux/amd64/go-template chmod +x go-template

Windows:

curl -LO https://app.harness.io/storage/harness-download/snapshot-go-template/release/v0.4/bin/windows/amd64/go-template.exe

In Practice

Now that you have the git sync and the go-templating engine set up, the last thing to do is configure the local environment.

Start by cloning the desired set of files from git to your machine. Then, in your terminal, execute the following command:

go-template -t <template file> -f <values files> -o <output folder>

The -t flag indicates the folder where the template files exist (i.e. deployment.yaml, service.yaml, etc.). The -f1 flag indicates the path to the values.yamlfile (if you have multiple values files, you can use additional-fflags at the end for each file). When the command is run, you will get amanifest.yaml` file output, similar to the one that you would see in the Harness Execution Context.

Here is an example of this process using one of the default set-ups in Harness:

Example

Service in Harness

Service YAML on a local box with local values.yaml file and hardcoded key:value pairs

Make an output folder to put the final documents into

Run the command above on a designated file

The final deployment.yaml file

One last piece to mention is since the go-templating engine only does one file at a time, you may want to run through all of the files in a folder. This script runs a basic loop over the files in a folder and runs the go-templating on that:

for file in $(ls manifests/*.yaml); do ./go-template -t $file -f values.yaml -o output; done

Don’t forget to Comment/Like/Share!

5 Likes

Hi!
How can i transform a value to base64 to create a secret?

I Try:
{{.Values.var | b64enc }}
But don’t work.

Hi,
I have 2 questions -

  • How can I enable verbose logging in go-template?
  • I have a bunch of files in a directory. I want to create a configmap using all the files. Helm supports that with below syntax -
data:
{{ (.Files.Glob "foo/*").AsConfig | indent 2 }}

My configMap looks like below, but I am getting an error like below -
Error: template: tpl:8:52: executing "tpl" at <2>: invalid value; expected string which doesn’t give me any clue.

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.name }}-alerts-config
  namespace: {{ .Values.namespace }}
data:
  {{ (.Files.Glob "alertrules/*").AsConfig | indent 2 }}