mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-16 17:33:14 +00:00
Add --helm-debug Flag to Kustomize for Enhanced Helm Debugging (#5751)
* feat: add helm-debug flag * revert: go.work.sum * test: add helm chart args helm-debug test * test: helm debug flag * refactor: helm debug output * style: linting * revert: go.work.sum
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
@@ -62,6 +63,9 @@ func (p *HelmChartInflationGeneratorPlugin) Config(
|
|||||||
if len(h.GeneralConfig().HelmConfig.ApiVersions) != 0 {
|
if len(h.GeneralConfig().HelmConfig.ApiVersions) != 0 {
|
||||||
p.HelmChart.ApiVersions = h.GeneralConfig().HelmConfig.ApiVersions
|
p.HelmChart.ApiVersions = h.GeneralConfig().HelmConfig.ApiVersions
|
||||||
}
|
}
|
||||||
|
if h.GeneralConfig().HelmConfig.Debug {
|
||||||
|
p.HelmChart.Debug = h.GeneralConfig().HelmConfig.Debug
|
||||||
|
}
|
||||||
|
|
||||||
p.h = h
|
p.h = h
|
||||||
if err = yaml.Unmarshal(config, p); err != nil {
|
if err = yaml.Unmarshal(config, p); err != nil {
|
||||||
@@ -168,13 +172,17 @@ func (p *HelmChartInflationGeneratorPlugin) runHelmCommand(
|
|||||||
fmt.Sprintf("HELM_DATA_HOME=%s/.data", p.ConfigHome)}
|
fmt.Sprintf("HELM_DATA_HOME=%s/.data", p.ConfigHome)}
|
||||||
cmd.Env = append(os.Environ(), env...)
|
cmd.Env = append(os.Environ(), env...)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
|
errorOutput := stderr.String()
|
||||||
|
if slices.Contains(args, "--debug") {
|
||||||
|
errorOutput = " Helm stack trace:\n" + errorOutput + "\nHelm template:\n" + stdout.String() + "\n"
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helm := p.h.GeneralConfig().HelmConfig.Command
|
helm := p.h.GeneralConfig().HelmConfig.Command
|
||||||
err = errors.WrapPrefixf(
|
err = errors.WrapPrefixf(
|
||||||
fmt.Errorf(
|
fmt.Errorf(
|
||||||
"unable to run: '%s %s' with env=%s (is '%s' installed?): %w",
|
"unable to run: '%s %s' with env=%s (is '%s' installed?): %w",
|
||||||
helm, strings.Join(args, " "), env, helm, err),
|
helm, strings.Join(args, " "), env, helm, err),
|
||||||
stderr.String(),
|
errorOutput,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return stdout.Bytes(), err
|
return stdout.Bytes(), err
|
||||||
|
|||||||
@@ -96,6 +96,9 @@ type HelmChart struct {
|
|||||||
|
|
||||||
// SkipTests skips tests from templated output.
|
// SkipTests skips tests from templated output.
|
||||||
SkipTests bool `json:"skipTests,omitempty" yaml:"skipTests,omitempty"`
|
SkipTests bool `json:"skipTests,omitempty" yaml:"skipTests,omitempty"`
|
||||||
|
|
||||||
|
// debug enables debug output from the Helm chart inflator generator.
|
||||||
|
Debug bool `json:"debug,omitempty" yaml:"debug,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// HelmChartArgs contains arguments to helm.
|
// HelmChartArgs contains arguments to helm.
|
||||||
@@ -188,5 +191,8 @@ func (h HelmChart) AsHelmArgs(absChartHome string) []string {
|
|||||||
if h.SkipHooks {
|
if h.SkipHooks {
|
||||||
args = append(args, "--no-hooks")
|
args = append(args, "--no-hooks")
|
||||||
}
|
}
|
||||||
|
if h.Debug {
|
||||||
|
args = append(args, "--debug")
|
||||||
|
}
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,4 +60,21 @@ func TestAsHelmArgs(t *testing.T) {
|
|||||||
"-f", "values1", "-f", "values2",
|
"-f", "values1", "-f", "values2",
|
||||||
"--api-versions", "foo", "--api-versions", "bar"})
|
"--api-versions", "foo", "--api-versions", "bar"})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("use helm-debug", func(t *testing.T) {
|
||||||
|
p := types.HelmChart{
|
||||||
|
Name: "chart-name",
|
||||||
|
Version: "1.0.0",
|
||||||
|
Repo: "https://helm.releases.hashicorp.com",
|
||||||
|
ValuesFile: "values",
|
||||||
|
AdditionalValuesFiles: []string{"values1", "values2"},
|
||||||
|
Debug: true,
|
||||||
|
}
|
||||||
|
require.Equal(t, p.AsHelmArgs("/home/charts"),
|
||||||
|
[]string{"template", "--generate-name", "/home/charts/chart-name",
|
||||||
|
"-f", "values",
|
||||||
|
"-f", "values1",
|
||||||
|
"-f", "values2",
|
||||||
|
"--debug"})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ type HelmConfig struct {
|
|||||||
Command string
|
Command string
|
||||||
ApiVersions []string
|
ApiVersions []string
|
||||||
KubeVersion string
|
KubeVersion string
|
||||||
|
Debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// PluginConfig holds plugin configuration.
|
// PluginConfig holds plugin configuration.
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ var theFlags struct {
|
|||||||
helmCommand string
|
helmCommand string
|
||||||
helmApiVersions []string
|
helmApiVersions []string
|
||||||
helmKubeVersion string
|
helmKubeVersion string
|
||||||
|
helmDebug bool
|
||||||
loadRestrictor string
|
loadRestrictor string
|
||||||
reorderOutput string
|
reorderOutput string
|
||||||
fnOptions types.FnPluginLoadingOptions
|
fnOptions types.FnPluginLoadingOptions
|
||||||
@@ -163,6 +164,7 @@ func HonorKustomizeFlags(kOpts *krusty.Options, flags *flag.FlagSet) *krusty.Opt
|
|||||||
kOpts.PluginConfig.HelmConfig.Command = theFlags.helmCommand
|
kOpts.PluginConfig.HelmConfig.Command = theFlags.helmCommand
|
||||||
kOpts.PluginConfig.HelmConfig.ApiVersions = theFlags.helmApiVersions
|
kOpts.PluginConfig.HelmConfig.ApiVersions = theFlags.helmApiVersions
|
||||||
kOpts.PluginConfig.HelmConfig.KubeVersion = theFlags.helmKubeVersion
|
kOpts.PluginConfig.HelmConfig.KubeVersion = theFlags.helmKubeVersion
|
||||||
|
kOpts.PluginConfig.HelmConfig.Debug = theFlags.helmDebug
|
||||||
kOpts.AddManagedbyLabel = isManagedByLabelEnabled()
|
kOpts.AddManagedbyLabel = isManagedByLabelEnabled()
|
||||||
return kOpts
|
return kOpts
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,4 +31,9 @@ func AddFlagEnableHelm(set *pflag.FlagSet) {
|
|||||||
"helm-kube-version",
|
"helm-kube-version",
|
||||||
"", // default
|
"", // default
|
||||||
"Kubernetes version used by Helm for Capabilities.KubeVersion")
|
"Kubernetes version used by Helm for Capabilities.KubeVersion")
|
||||||
|
set.BoolVar(
|
||||||
|
&theFlags.helmDebug,
|
||||||
|
"helm-debug",
|
||||||
|
false,
|
||||||
|
"Enable debug output from the Helm chart inflator generator.")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
@@ -68,6 +69,9 @@ func (p *plugin) Config(
|
|||||||
if len(h.GeneralConfig().HelmConfig.ApiVersions) != 0 {
|
if len(h.GeneralConfig().HelmConfig.ApiVersions) != 0 {
|
||||||
p.HelmChart.ApiVersions = h.GeneralConfig().HelmConfig.ApiVersions
|
p.HelmChart.ApiVersions = h.GeneralConfig().HelmConfig.ApiVersions
|
||||||
}
|
}
|
||||||
|
if h.GeneralConfig().HelmConfig.Debug {
|
||||||
|
p.HelmChart.Debug = h.GeneralConfig().HelmConfig.Debug
|
||||||
|
}
|
||||||
|
|
||||||
p.h = h
|
p.h = h
|
||||||
if err = yaml.Unmarshal(config, p); err != nil {
|
if err = yaml.Unmarshal(config, p); err != nil {
|
||||||
@@ -174,13 +178,17 @@ func (p *plugin) runHelmCommand(
|
|||||||
fmt.Sprintf("HELM_DATA_HOME=%s/.data", p.ConfigHome)}
|
fmt.Sprintf("HELM_DATA_HOME=%s/.data", p.ConfigHome)}
|
||||||
cmd.Env = append(os.Environ(), env...)
|
cmd.Env = append(os.Environ(), env...)
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
|
errorOutput := stderr.String()
|
||||||
|
if slices.Contains(args, "--debug") {
|
||||||
|
errorOutput = " Helm stack trace:\n" + errorOutput + "\nHelm template:\n" + stdout.String() + "\n"
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helm := p.h.GeneralConfig().HelmConfig.Command
|
helm := p.h.GeneralConfig().HelmConfig.Command
|
||||||
err = errors.WrapPrefixf(
|
err = errors.WrapPrefixf(
|
||||||
fmt.Errorf(
|
fmt.Errorf(
|
||||||
"unable to run: '%s %s' with env=%s (is '%s' installed?): %w",
|
"unable to run: '%s %s' with env=%s (is '%s' installed?): %w",
|
||||||
helm, strings.Join(args, " "), env, helm, err),
|
helm, strings.Join(args, " "), env, helm, err),
|
||||||
stderr.String(),
|
errorOutput,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return stdout.Bytes(), err
|
return stdout.Bytes(), err
|
||||||
|
|||||||
@@ -959,3 +959,37 @@ chartHome: ./charts
|
|||||||
assert.Contains(t, string(chartYamlContent), "name: test-chart")
|
assert.Contains(t, string(chartYamlContent), "name: test-chart")
|
||||||
assert.Contains(t, string(chartYamlContent), "version: 1.0.0")
|
assert.Contains(t, string(chartYamlContent), "version: 1.0.0")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHelmChartInflationGeneratorWithDebug(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
|
||||||
|
PrepBuiltin("HelmChartInflationGenerator")
|
||||||
|
defer th.Reset()
|
||||||
|
if err := th.ErrIfNoHelm(); err != nil {
|
||||||
|
t.Skip("skipping: " + err.Error())
|
||||||
|
}
|
||||||
|
copyTestChartsIntoHarness(t, th)
|
||||||
|
|
||||||
|
rm := th.LoadAndRunGenerator(`
|
||||||
|
apiVersion: builtin
|
||||||
|
kind: HelmChartInflationGenerator
|
||||||
|
metadata:
|
||||||
|
name: test-chart
|
||||||
|
name: test-chart
|
||||||
|
version: 1.0.0
|
||||||
|
releaseName: test
|
||||||
|
chartHome: ./charts
|
||||||
|
debug: true
|
||||||
|
`)
|
||||||
|
|
||||||
|
cm, err := rm.Resources()[0].GetFieldValue("metadata.name")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "bar", cm)
|
||||||
|
|
||||||
|
chartDir := filepath.Join(th.GetRoot(), "charts/test-chart")
|
||||||
|
assert.True(t, th.GetFSys().Exists(chartDir))
|
||||||
|
|
||||||
|
chartYamlContent, err := th.GetFSys().ReadFile(filepath.Join(chartDir, "Chart.yaml"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Contains(t, string(chartYamlContent), "name: test-chart")
|
||||||
|
assert.Contains(t, string(chartYamlContent), "version: 1.0.0")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user