Skip to content

Commit

Permalink
Add --helm-debug Flag to Kustomize for Enhanced Helm Debugging (#5751)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
isarns authored Sep 12, 2024
1 parent c3872ce commit 4034e36
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 2 deletions.
10 changes: 9 additions & 1 deletion api/internal/builtins/HelmChartInflationGenerator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions api/types/helmchartargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type HelmChart struct {

// SkipTests skips tests from templated output.
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.
Expand Down Expand Up @@ -188,5 +191,8 @@ func (h HelmChart) AsHelmArgs(absChartHome string) []string {
if h.SkipHooks {
args = append(args, "--no-hooks")
}
if h.Debug {
args = append(args, "--debug")
}
return args
}
17 changes: 17 additions & 0 deletions api/types/helmchartargs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,21 @@ func TestAsHelmArgs(t *testing.T) {
"-f", "values1", "-f", "values2",
"--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"})
})
}
1 change: 1 addition & 0 deletions api/types/pluginconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type HelmConfig struct {
Command string
ApiVersions []string
KubeVersion string
Debug bool
}

// PluginConfig holds plugin configuration.
Expand Down
2 changes: 2 additions & 0 deletions kustomize/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var theFlags struct {
helmCommand string
helmApiVersions []string
helmKubeVersion string
helmDebug bool
loadRestrictor string
reorderOutput string
fnOptions types.FnPluginLoadingOptions
Expand Down Expand Up @@ -163,6 +164,7 @@ func HonorKustomizeFlags(kOpts *krusty.Options, flags *flag.FlagSet) *krusty.Opt
kOpts.PluginConfig.HelmConfig.Command = theFlags.helmCommand
kOpts.PluginConfig.HelmConfig.ApiVersions = theFlags.helmApiVersions
kOpts.PluginConfig.HelmConfig.KubeVersion = theFlags.helmKubeVersion
kOpts.PluginConfig.HelmConfig.Debug = theFlags.helmDebug
kOpts.AddManagedbyLabel = isManagedByLabelEnabled()
return kOpts
}
5 changes: 5 additions & 0 deletions kustomize/commands/build/flagenablehelm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ func AddFlagEnableHelm(set *pflag.FlagSet) {
"helm-kube-version",
"", // default
"Kubernetes version used by Helm for Capabilities.KubeVersion")
set.BoolVar(
&theFlags.helmDebug,
"helm-debug",
false,
"Enable debug output from the Helm chart inflator generator.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"slices"
"strings"

"sigs.k8s.io/kustomize/api/resmap"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 4034e36

Please # to comment.