From 6b86408500a89492f2f048088716860b775e3d2d Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Thu, 5 Nov 2020 10:13:25 +0900 Subject: [PATCH] feat: Add `helmfile template --include-crds` (#1568) This allows you to use helmfile-template output as a GitOps source, when the template output contains CRDs and you use Helm 3. Helm 3 by default removes CRDs from the template output. If you want to git-commit helmfile-template containing CRDs for GitOps and you use Helm 3 for templating, the only way is provide this newly added `--include-crds` flag. --- main.go | 8 ++++++++ pkg/app/app.go | 1 + pkg/app/app_test.go | 13 +++++++++---- pkg/app/config.go | 1 + pkg/state/state.go | 5 +++++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 938c4124..7fd386c6 100644 --- a/main.go +++ b/main.go @@ -248,6 +248,10 @@ func main() { Name: "validate", Usage: "validate your manifests against the Kubernetes cluster you are currently pointing at", }, + cli.BoolFlag{ + Name: "include-crds", + Usage: "include CRDs in the templated output", + }, cli.BoolFlag{ Name: "skip-deps", Usage: "skip running `helm repo update` and `helm dependency build`", @@ -717,6 +721,10 @@ func (c configImpl) EmbedValues() bool { return c.c.Bool("embed-values") } +func (c configImpl) IncludeCRDs() bool { + return c.c.Bool("include-crds") +} + func (c configImpl) Logger() *zap.SugaredLogger { return c.c.App.Metadata["logger"].(*zap.SugaredLogger) } diff --git a/pkg/app/app.go b/pkg/app/app.go index 8011e353..80a2b09c 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -1433,6 +1433,7 @@ func (a *App) template(r *Run, c TemplateConfigProvider) (bool, []error) { opts := &state.TemplateOpts{ Set: c.Set(), + IncludeCRDs: c.IncludeCRDs(), OutputDirTemplate: c.OutputDirTemplate(), } return subst.TemplateReleases(helm, c.OutputDir(), c.Values(), args, c.Concurrency(), c.Validate(), opts) diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 3987742d..8c126a2e 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -2237,10 +2237,11 @@ services: } type configImpl struct { - selectors []string - set []string - output string - skipDeps bool + selectors []string + set []string + output string + includeCRDs bool + skipDeps bool } func (a configImpl) Selectors() []string { @@ -2275,6 +2276,10 @@ func (c configImpl) OutputDirTemplate() string { return "" } +func (c configImpl) IncludeCRDs() bool { + return c.includeCRDs +} + func (c configImpl) Concurrency() int { return 1 } diff --git a/pkg/app/config.go b/pkg/app/config.go index 10cba2c1..5cc1d173 100644 --- a/pkg/app/config.go +++ b/pkg/app/config.go @@ -133,6 +133,7 @@ type TemplateConfigProvider interface { Validate() bool SkipDeps() bool OutputDir() string + IncludeCRDs() bool concurrencyConfig } diff --git a/pkg/state/state.go b/pkg/state/state.go index 2df11bd9..00f28c38 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -1125,6 +1125,7 @@ func (st *HelmState) runHelmDepBuilds(helm helmexec.Interface, concurrency int, type TemplateOpts struct { Set []string OutputDirTemplate string + IncludeCRDs bool } type TemplateOpt interface{ Apply(*TemplateOpts) } @@ -1197,6 +1198,10 @@ func (st *HelmState) TemplateReleases(helm helmexec.Interface, outputDir string, flags = append(flags, "--validate") } + if opts.IncludeCRDs { + flags = append(flags, "--include-crds") + } + if len(errs) == 0 { if err := helm.TemplateRelease(release.Name, release.Chart, flags...); err != nil { errs = append(errs, err)