mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
support for more helm template args (#4926)
* support for more helm template args * move templateArgs and unit tests to api/types * undo package name change * use our own simple helm chart instead of forking one * add argument to AsHelmArgs * code review * lint errors
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
|
||||
package types
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
const HelmDefaultHome = "charts"
|
||||
|
||||
type HelmGlobals struct {
|
||||
@@ -57,7 +59,11 @@ type HelmChart struct {
|
||||
// in the helm template
|
||||
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
|
||||
|
||||
// ValuesFile is local file path to a values file to use _instead of_
|
||||
// AdditionalValuesFiles are local file paths to values files to be used in
|
||||
// addition to either the default values file or the values specified in ValuesFile.
|
||||
AdditionalValuesFiles []string `json:"additionalValuesFiles,omitempty" yaml:"additionalValuesFiles,omitempty"`
|
||||
|
||||
// ValuesFile is a local file path to a values file to use _instead of_
|
||||
// the default values that accompanied the chart.
|
||||
// The default values are in '{ChartHome}/{Name}/values.yaml'.
|
||||
ValuesFile string `json:"valuesFile,omitempty" yaml:"valuesFile,omitempty"`
|
||||
@@ -78,6 +84,15 @@ type HelmChart struct {
|
||||
// SkipHooks sets the --no-hooks flag when calling helm template. This prevents
|
||||
// helm from erroneously rendering test templates.
|
||||
SkipHooks bool `json:"skipHooks,omitempty" yaml:"skipHooks,omitempty"`
|
||||
|
||||
// ApiVersions is the kubernetes apiversions used for Capabilities.APIVersions
|
||||
ApiVersions []string `json:"apiVersions,omitempty" yaml:"apiVersions,omitempty"`
|
||||
|
||||
// NameTemplate is for specifying the name template used to name the release.
|
||||
NameTemplate string `json:"nameTemplate,omitempty" yaml:"nameTemplate,omitempty"`
|
||||
|
||||
// SkipTests skips tests from templated output.
|
||||
SkipTests bool `json:"skipTests,omitempty" yaml:"skipTests,omitempty"`
|
||||
}
|
||||
|
||||
// HelmChartArgs contains arguments to helm.
|
||||
@@ -126,3 +141,45 @@ func makeHelmChartFromHca(old *HelmChartArgs) (c HelmChart) {
|
||||
c.ReleaseName = old.ReleaseName
|
||||
return
|
||||
}
|
||||
|
||||
func (h HelmChart) AsHelmArgs(absChartHome string) []string {
|
||||
args := []string{"template"}
|
||||
if h.ReleaseName != "" {
|
||||
args = append(args, h.ReleaseName)
|
||||
} else {
|
||||
// AFAICT, this doesn't work as intended due to a bug in helm.
|
||||
// See https://github.com/helm/helm/issues/6019
|
||||
// I've tried placing the flag before and after the name argument.
|
||||
args = append(args, "--generate-name")
|
||||
}
|
||||
if h.Name != "" {
|
||||
args = append(args, filepath.Join(absChartHome, h.Name))
|
||||
}
|
||||
if h.Namespace != "" {
|
||||
args = append(args, "--namespace", h.Namespace)
|
||||
}
|
||||
if h.NameTemplate != "" {
|
||||
args = append(args, "--name-template", h.NameTemplate)
|
||||
}
|
||||
|
||||
if h.ValuesFile != "" {
|
||||
args = append(args, "-f", h.ValuesFile)
|
||||
}
|
||||
for _, valuesFile := range h.AdditionalValuesFiles {
|
||||
args = append(args, "-f", valuesFile)
|
||||
}
|
||||
|
||||
for _, apiVer := range h.ApiVersions {
|
||||
args = append(args, "--api-versions", apiVer)
|
||||
}
|
||||
if h.IncludeCRDs {
|
||||
args = append(args, "--include-crds")
|
||||
}
|
||||
if h.SkipTests {
|
||||
args = append(args, "--skip-tests")
|
||||
}
|
||||
if h.SkipHooks {
|
||||
args = append(args, "--no-hooks")
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
61
api/types/helmchartargs_test.go
Normal file
61
api/types/helmchartargs_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package types_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
)
|
||||
|
||||
func TestAsHelmArgs(t *testing.T) {
|
||||
t.Run("use generate-name", func(t *testing.T) {
|
||||
p := types.HelmChart{
|
||||
Name: "chart-name",
|
||||
Version: "1.0.0",
|
||||
Repo: "https://helm.releases.hashicorp.com",
|
||||
ApiVersions: []string{"foo", "bar"},
|
||||
NameTemplate: "template",
|
||||
SkipTests: true,
|
||||
IncludeCRDs: true,
|
||||
SkipHooks: true,
|
||||
ValuesFile: "values",
|
||||
AdditionalValuesFiles: []string{"values1", "values2"},
|
||||
Namespace: "my-ns",
|
||||
}
|
||||
require.Equal(t, p.AsHelmArgs("/home/charts"),
|
||||
[]string{"template", "--generate-name",
|
||||
"/home/charts/chart-name",
|
||||
"--namespace", "my-ns",
|
||||
"--name-template", "template",
|
||||
"-f", "values",
|
||||
"-f", "values1", "-f", "values2",
|
||||
"--api-versions", "foo", "--api-versions", "bar",
|
||||
"--include-crds",
|
||||
"--skip-tests",
|
||||
"--no-hooks"})
|
||||
})
|
||||
|
||||
t.Run("use release-name", func(t *testing.T) {
|
||||
p := types.HelmChart{
|
||||
Name: "chart-name",
|
||||
Version: "1.0.0",
|
||||
Repo: "https://helm.releases.hashicorp.com",
|
||||
ApiVersions: []string{"foo", "bar"},
|
||||
NameTemplate: "template",
|
||||
ValuesFile: "values",
|
||||
AdditionalValuesFiles: []string{"values1", "values2"},
|
||||
Namespace: "my-ns",
|
||||
ReleaseName: "test",
|
||||
}
|
||||
require.Equal(t, p.AsHelmArgs("/home/charts"),
|
||||
[]string{"template", "test", "/home/charts/chart-name",
|
||||
"--namespace", "my-ns",
|
||||
"--name-template", "template",
|
||||
"-f", "values",
|
||||
"-f", "values1", "-f", "values2",
|
||||
"--api-versions", "foo", "--api-versions", "bar"})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user