From ceff32d5f8aa173549426625b608137a47981447 Mon Sep 17 00:00:00 2001 From: DongGang Date: Tue, 7 Jul 2020 23:44:22 +0800 Subject: [PATCH] fix(template):Issue:helm template with --output-dir (#8156) * fix(template):Issue:helm template with --output-dir doesn't write template with a hook to file Close #7836 Signed-off-by: Dong Gang * fix go file style Signed-off-by: Dong Gang * fix go file style Signed-off-by: Dong Gang --- cmd/helm/template.go | 6 ------ pkg/action/action.go | 20 +++++++++++++++++++- pkg/action/install.go | 2 +- pkg/action/install_test.go | 6 ++++++ pkg/action/upgrade.go | 2 +- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/cmd/helm/template.go b/cmd/helm/template.go index 9ea61b17a..5a8a25102 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -80,12 +80,6 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { var manifests bytes.Buffer fmt.Fprintln(&manifests, strings.TrimSpace(rel.Manifest)) - if !client.DisableHooks { - for _, m := range rel.Hooks { - fmt.Fprintf(&manifests, "---\n# Source: %s\n%s\n", m.Path, m.Manifest) - } - } - // if we have a list of files to render, then check that each of the // provided files exists in the chart. if len(showFiles) > 0 { diff --git a/pkg/action/action.go b/pkg/action/action.go index bb9ef5f71..698ebd23f 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -97,7 +97,7 @@ type Configuration struct { // renderResources renders the templates in a chart // // TODO: This function is badly in need of a refactor. -func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, pr postrender.PostRenderer, dryRun bool) ([]*release.Hook, *bytes.Buffer, string, error) { +func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, disableHooks bool, pr postrender.PostRenderer, dryRun bool) ([]*release.Hook, *bytes.Buffer, string, error) { hs := []*release.Hook{} b := bytes.NewBuffer(nil) @@ -210,6 +210,24 @@ func (c *Configuration) renderResources(ch *chart.Chart, values chartutil.Values } } + if !disableHooks && len(hs) > 0 { + for _, h := range hs { + if outputDir == "" { + fmt.Fprintf(b, "---\n# Source: %s\n%s\n", h.Path, h.Manifest) + } else { + newDir := outputDir + if useReleaseName { + newDir = filepath.Join(outputDir, releaseName) + } + err = writeToFile(newDir, h.Path, h.Manifest, fileWritten[h.Path]) + if err != nil { + return hs, b, "", err + } + fileWritten[h.Path] = true + } + } + } + if pr != nil { b, err = pr.Run(b) if err != nil { diff --git a/pkg/action/install.go b/pkg/action/install.go index 00fb208b0..48a3aeeca 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -236,7 +236,7 @@ func (i *Install) Run(chrt *chart.Chart, vals map[string]interface{}) (*release. rel := i.createRelease(chrt, vals) var manifestDoc *bytes.Buffer - rel.Hooks, manifestDoc, rel.Info.Notes, err = i.cfg.renderResources(chrt, valuesToRender, i.ReleaseName, i.OutputDir, i.SubNotes, i.UseReleaseName, i.IncludeCRDs, i.PostRenderer, i.DryRun) + rel.Hooks, manifestDoc, rel.Info.Notes, err = i.cfg.renderResources(chrt, valuesToRender, i.ReleaseName, i.OutputDir, i.SubNotes, i.UseReleaseName, i.IncludeCRDs, i.DisableHooks, i.PostRenderer, i.DryRun) // Even for errors, attach this if available if manifestDoc != nil { rel.Manifest = manifestDoc.String() diff --git a/pkg/action/install_test.go b/pkg/action/install_test.go index 6c4012cfd..4366889ce 100644 --- a/pkg/action/install_test.go +++ b/pkg/action/install_test.go @@ -499,6 +499,9 @@ func TestInstallReleaseOutputDir(t *testing.T) { _, err = os.Stat(filepath.Join(dir, "hello/templates/with-partials")) is.NoError(err) + _, err = os.Stat(filepath.Join(dir, "hello/templates/hooks")) + is.NoError(err) + _, err = os.Stat(filepath.Join(dir, "hello/templates/rbac")) is.NoError(err) @@ -539,6 +542,9 @@ func TestInstallOutputDirWithReleaseName(t *testing.T) { _, err = os.Stat(filepath.Join(newDir, "hello/templates/with-partials")) is.NoError(err) + _, err = os.Stat(filepath.Join(newDir, "hello/templates/hooks")) + is.NoError(err) + _, err = os.Stat(filepath.Join(newDir, "hello/templates/rbac")) is.NoError(err) diff --git a/pkg/action/upgrade.go b/pkg/action/upgrade.go index fc289dbab..921a0eba5 100644 --- a/pkg/action/upgrade.go +++ b/pkg/action/upgrade.go @@ -217,7 +217,7 @@ func (u *Upgrade) prepareUpgrade(name string, chart *chart.Chart, vals map[strin return nil, nil, err } - hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender, "", "", u.SubNotes, false, false, u.PostRenderer, u.DryRun) + hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender, "", "", u.SubNotes, false, false, false, u.PostRenderer, u.DryRun) if err != nil { return nil, nil, err }