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:
Isar_NS
2024-09-13 01:43:12 +03:00
committed by GitHub
parent c3872ce3d9
commit 4034e36ee1
8 changed files with 83 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"slices"
"strings"
"sigs.k8s.io/kustomize/api/resmap"
@@ -68,6 +69,9 @@ func (p *plugin) Config(
if len(h.GeneralConfig().HelmConfig.ApiVersions) != 0 {
p.HelmChart.ApiVersions = h.GeneralConfig().HelmConfig.ApiVersions
}
if h.GeneralConfig().HelmConfig.Debug {
p.HelmChart.Debug = h.GeneralConfig().HelmConfig.Debug
}
p.h = h
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)}
cmd.Env = append(os.Environ(), env...)
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 {
helm := p.h.GeneralConfig().HelmConfig.Command
err = errors.WrapPrefixf(
fmt.Errorf(
"unable to run: '%s %s' with env=%s (is '%s' installed?): %w",
helm, strings.Join(args, " "), env, helm, err),
stderr.String(),
errorOutput,
)
}
return stdout.Bytes(), err

View File

@@ -959,3 +959,37 @@ chartHome: ./charts
assert.Contains(t, string(chartYamlContent), "name: test-chart")
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")
}