slok / go-helm-template

Simple go library to run Helm template without executing Helm
Apache License 2.0
16 stars 2 forks source link
go golang helm helm-chart k8s kubernetes lib library template

go-helm-template

CI Go Report Card Apache 2 licensed GitHub release (latest SemVer)

Simple, fast and easy to use Go library to run helm template without the need of a Helm binary nor its execution as an external command.

Features

Getting started

package main

import (
    "context"
    "fmt"
    "testing/fstest"

    "github.com/slok/go-helm-template/helm"
)

// Chart data in memory.
const (
    chart = `
apiVersion: v2
name: example-memory
version: 0.1.0
`
    configmap = `
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ printf "%s-%s" .Chart.Name .Release.Name | trunc 63 | trimSuffix "-" }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- with .Values.labels -}}
    {{ toYaml . | nindent 4 }}
    {{- end }}
data:
  something: something
`
)

func main() {
    ctx := context.Background()

    // Create chart in memory.
    chartFS := make(fstest.MapFS)
    chartFS["Chart.yaml"] = &fstest.MapFile{Data: []byte(chart)}
    chartFS["templates/configmap.yaml"] = &fstest.MapFile{Data: []byte(configmap)}

    // Load chart.
    chart, err := helm.LoadChart(ctx, chartFS)
    if err != nil {
        panic(err)
    }

    // Execute helm template.
    result, err := helm.Template(ctx, helm.TemplateConfig{
        Chart:       chart,
        ReleaseName: "test",
        Namespace:   "no-kube-system",
        Values: map[string]interface{}{
            "labels": map[string]string{
                "example-from": "go-helm-template",
            },
        },
    })
    if err != nil {
        panic(err)
    }

    fmt.Println(result)
}

Examples

Tradeoffs

This library doesn't support anything apart from simple helm template. dependencies, hooks... and similar fancy features, are not supported.

Why

One of the Helm's most powerful feature (if not the most) is its template system, lots of users only use Helm for this usage.

Not depending on helm as a system dependency, nor requiring to execute an external command, improves the portability and performance of applications that use Helm internally.

Some use cases