helm

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Template

func Template(ctx context.Context, config TemplateConfig) (string, error)

Template will runhelm template in the provided chart and values without the need of the Helm binary and without executing an external command.

Example (Memory)

Template shows a basic example of how you would use helm template by using a fake chart created in memory. To load a chart from disk you could use `os.DirFS`.

package main

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

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

func main() {
	// Chart data in memory.
	const (
		chartData = `
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
`
	)

	ctx := context.Background()

	// Create chart in memory.
	// We could use `os.DirFS("./some-chart")` if the chart is in the disk.
	chartFS := make(fstest.MapFS)
	chartFS["Chart.yaml"] = &fstest.MapFile{Data: []byte(chartData)}
	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)

}
Output:

---
# Source: example-memory/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-memory-test
  namespace: no-kube-system
  labels:
    example-from: go-helm-template
data:
  something: something

Types

type Chart

type Chart struct {
	// contains filtered or unexported fields
}

Chart represents a loaded Helm chart.

func LoadChart

func LoadChart(ctx context.Context, f fs.FS) (*Chart, error)

LoadChart loads a chart from a fs.FS system. There chart files must be at the root of the provided fs.FS. e.g: ./Chart.yaml, ./values.yaml ./templates/deployment.yaml...

You can use `fs.Sub` as a helper tool to get the root chart.

func MustLoadChart

func MustLoadChart(ctx context.Context, f fs.FS) *Chart

MustLoadChart is the same as LoadChart but panics if there is any error while loading the chart.

type TemplateConfig

type TemplateConfig struct {
	// ReleaseName is the name of the release.
	ReleaseName string
	// Chart is the loaded chart. Use `LoadChart`.
	Chart *Chart
	// Values are the custom values to be used on the chart template..
	Values map[string]interface{}
	// IncludeCRDs when enabled will template/render the CRDs.
	IncludeCRDs bool
	// Namespace is the namespace used to render the chart.
	Namespace string
	// ShowFiles is a list of files that can be used to only template the provided files,
	// by default it will render all.
	// This can be handy on specific use cases like unit tests for charts.
	ShowFiles []string
}

TemplateConfig is the configuration for Helm Template rendering.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL