Skip to content

Commit 4034e36

Browse files
authored
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
1 parent c3872ce commit 4034e36

File tree

8 files changed

+83
-2
lines changed

8 files changed

+83
-2
lines changed

api/internal/builtins/HelmChartInflationGenerator.go

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/types/helmchartargs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ type HelmChart struct {
9696

9797
// SkipTests skips tests from templated output.
9898
SkipTests bool `json:"skipTests,omitempty" yaml:"skipTests,omitempty"`
99+
100+
// debug enables debug output from the Helm chart inflator generator.
101+
Debug bool `json:"debug,omitempty" yaml:"debug,omitempty"`
99102
}
100103

101104
// HelmChartArgs contains arguments to helm.
@@ -188,5 +191,8 @@ func (h HelmChart) AsHelmArgs(absChartHome string) []string {
188191
if h.SkipHooks {
189192
args = append(args, "--no-hooks")
190193
}
194+
if h.Debug {
195+
args = append(args, "--debug")
196+
}
191197
return args
192198
}

api/types/helmchartargs_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,21 @@ func TestAsHelmArgs(t *testing.T) {
6060
"-f", "values1", "-f", "values2",
6161
"--api-versions", "foo", "--api-versions", "bar"})
6262
})
63+
64+
t.Run("use helm-debug", func(t *testing.T) {
65+
p := types.HelmChart{
66+
Name: "chart-name",
67+
Version: "1.0.0",
68+
Repo: "https://helm.releases.hashicorp.com",
69+
ValuesFile: "values",
70+
AdditionalValuesFiles: []string{"values1", "values2"},
71+
Debug: true,
72+
}
73+
require.Equal(t, p.AsHelmArgs("/home/charts"),
74+
[]string{"template", "--generate-name", "/home/charts/chart-name",
75+
"-f", "values",
76+
"-f", "values1",
77+
"-f", "values2",
78+
"--debug"})
79+
})
6380
}

api/types/pluginconfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type HelmConfig struct {
88
Command string
99
ApiVersions []string
1010
KubeVersion string
11+
Debug bool
1112
}
1213

1314
// PluginConfig holds plugin configuration.

kustomize/commands/build/build.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var theFlags struct {
3030
helmCommand string
3131
helmApiVersions []string
3232
helmKubeVersion string
33+
helmDebug bool
3334
loadRestrictor string
3435
reorderOutput string
3536
fnOptions types.FnPluginLoadingOptions
@@ -163,6 +164,7 @@ func HonorKustomizeFlags(kOpts *krusty.Options, flags *flag.FlagSet) *krusty.Opt
163164
kOpts.PluginConfig.HelmConfig.Command = theFlags.helmCommand
164165
kOpts.PluginConfig.HelmConfig.ApiVersions = theFlags.helmApiVersions
165166
kOpts.PluginConfig.HelmConfig.KubeVersion = theFlags.helmKubeVersion
167+
kOpts.PluginConfig.HelmConfig.Debug = theFlags.helmDebug
166168
kOpts.AddManagedbyLabel = isManagedByLabelEnabled()
167169
return kOpts
168170
}

kustomize/commands/build/flagenablehelm.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ func AddFlagEnableHelm(set *pflag.FlagSet) {
3131
"helm-kube-version",
3232
"", // default
3333
"Kubernetes version used by Helm for Capabilities.KubeVersion")
34+
set.BoolVar(
35+
&theFlags.helmDebug,
36+
"helm-debug",
37+
false,
38+
"Enable debug output from the Helm chart inflator generator.")
3439
}

plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os/exec"
1515
"path/filepath"
1616
"regexp"
17+
"slices"
1718
"strings"
1819

1920
"sigs.k8s.io/kustomize/api/resmap"
@@ -68,6 +69,9 @@ func (p *plugin) Config(
6869
if len(h.GeneralConfig().HelmConfig.ApiVersions) != 0 {
6970
p.HelmChart.ApiVersions = h.GeneralConfig().HelmConfig.ApiVersions
7071
}
72+
if h.GeneralConfig().HelmConfig.Debug {
73+
p.HelmChart.Debug = h.GeneralConfig().HelmConfig.Debug
74+
}
7175

7276
p.h = h
7377
if err = yaml.Unmarshal(config, p); err != nil {
@@ -174,13 +178,17 @@ func (p *plugin) runHelmCommand(
174178
fmt.Sprintf("HELM_DATA_HOME=%s/.data", p.ConfigHome)}
175179
cmd.Env = append(os.Environ(), env...)
176180
err := cmd.Run()
181+
errorOutput := stderr.String()
182+
if slices.Contains(args, "--debug") {
183+
errorOutput = " Helm stack trace:\n" + errorOutput + "\nHelm template:\n" + stdout.String() + "\n"
184+
}
177185
if err != nil {
178186
helm := p.h.GeneralConfig().HelmConfig.Command
179187
err = errors.WrapPrefixf(
180188
fmt.Errorf(
181189
"unable to run: '%s %s' with env=%s (is '%s' installed?): %w",
182190
helm, strings.Join(args, " "), env, helm, err),
183-
stderr.String(),
191+
errorOutput,
184192
)
185193
}
186194
return stdout.Bytes(), err

plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,3 +959,37 @@ chartHome: ./charts
959959
assert.Contains(t, string(chartYamlContent), "name: test-chart")
960960
assert.Contains(t, string(chartYamlContent), "version: 1.0.0")
961961
}
962+
963+
func TestHelmChartInflationGeneratorWithDebug(t *testing.T) {
964+
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
965+
PrepBuiltin("HelmChartInflationGenerator")
966+
defer th.Reset()
967+
if err := th.ErrIfNoHelm(); err != nil {
968+
t.Skip("skipping: " + err.Error())
969+
}
970+
copyTestChartsIntoHarness(t, th)
971+
972+
rm := th.LoadAndRunGenerator(`
973+
apiVersion: builtin
974+
kind: HelmChartInflationGenerator
975+
metadata:
976+
name: test-chart
977+
name: test-chart
978+
version: 1.0.0
979+
releaseName: test
980+
chartHome: ./charts
981+
debug: true
982+
`)
983+
984+
cm, err := rm.Resources()[0].GetFieldValue("metadata.name")
985+
require.NoError(t, err)
986+
assert.Equal(t, "bar", cm)
987+
988+
chartDir := filepath.Join(th.GetRoot(), "charts/test-chart")
989+
assert.True(t, th.GetFSys().Exists(chartDir))
990+
991+
chartYamlContent, err := th.GetFSys().ReadFile(filepath.Join(chartDir, "Chart.yaml"))
992+
require.NoError(t, err)
993+
assert.Contains(t, string(chartYamlContent), "name: test-chart")
994+
assert.Contains(t, string(chartYamlContent), "version: 1.0.0")
995+
}

0 commit comments

Comments
 (0)