From e9e3b86549ad078b9db6686e0984a76d3cc10271 Mon Sep 17 00:00:00 2001 From: Timofey Kirillov Date: Mon, 25 Apr 2022 22:17:50 +0300 Subject: [PATCH] feat: support --show-only|-s helm-style render option + export-values chaining * Pass either `-s templates/*ingress*.yaml` path (helm compatible) or use `-s .helm/templates/*ingress*.yaml` with full path in local filesystem (with bash completion convenience). * `export-values` in subcharts and sub-subcharts (and sub-sub-subcharts etc.) are processed now as expected. Refs https://github.com/werf/3p-helm/pull/162 Refs https://github.com/werf/3p-helm/pull/161 Signed-off-by: Timofey Kirillov --- cmd/werf/render/render.go | 34 ++++++++++++++++++++++++++++++++-- go.mod | 2 +- go.sum | 2 ++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/cmd/werf/render/render.go b/cmd/werf/render/render.go index d05565411b..5113234daf 100644 --- a/cmd/werf/render/render.go +++ b/cmd/werf/render/render.go @@ -6,6 +6,7 @@ import ( "io" "os" "path/filepath" + "strings" "github.com/spf13/cobra" helm_v3 "helm.sh/helm/v3/cmd/helm" @@ -31,6 +32,7 @@ import ( "github.com/werf/werf/pkg/storage/manager" "github.com/werf/werf/pkg/tmp_manager" "github.com/werf/werf/pkg/true_git" + "github.com/werf/werf/pkg/util" "github.com/werf/werf/pkg/werf" "github.com/werf/werf/pkg/werf/global_warnings" ) @@ -39,6 +41,7 @@ var cmdData struct { RenderOutput string Validate bool IncludeCRDs bool + ShowOnly []string } var commonCmdData common.CmdData @@ -127,10 +130,15 @@ func NewCmd() *cobra.Command { cmd.Flags().BoolVarP(&cmdData.IncludeCRDs, "include-crds", "", common.GetBoolEnvironmentDefaultTrue("WERF_INCLUDE_CRDS"), "Include CRDs in the templated output (default $WERF_INCLUDE_CRDS)") cmd.Flags().StringVarP(&cmdData.RenderOutput, "output", "", os.Getenv("WERF_RENDER_OUTPUT"), "Write render output to the specified file instead of stdout ($WERF_RENDER_OUTPUT by default)") + cmd.Flags().StringArrayVarP(&cmdData.ShowOnly, "show-only", "s", []string{}, "only show manifests rendered from the given templates") return cmd } +func getShowOnly() []string { + return append(common.PredefinedValuesByEnvNamePrefix("WERF_SHOW_ONLY"), cmdData.ShowOnly...) +} + func runRender(ctx context.Context) error { if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil { return fmt.Errorf("initialization error: %w", err) @@ -387,7 +395,7 @@ func runRender(ctx context.Context) error { SubchartExtenderFactoryFunc: func() chart.ChartExtender { return chart_extender.NewWerfSubchart() }, } - helmTemplateCmd, _ := helm_v3.NewTemplateCmd(actionConfig, output, helm_v3.TemplateCmdOptions{ + templateOpts := helm_v3.TemplateCmdOptions{ ChainPostRenderer: wc.ChainPostRenderer, ValueOpts: &values.Options{ ValueFiles: common.GetValues(&commonCmdData), @@ -397,7 +405,29 @@ func runRender(ctx context.Context) error { }, Validate: &cmdData.Validate, IncludeCrds: &cmdData.IncludeCRDs, - }) + } + + fullChartDir := filepath.Join(giterminismManager.ProjectDir(), chartDir) + + if showOnly := getShowOnly(); len(showOnly) > 0 { + var showFiles []string + + for _, p := range showOnly { + pAbs := util.GetAbsoluteFilepath(p) + if strings.HasPrefix(pAbs, fullChartDir) { + tp := util.GetRelativeToBaseFilepath(fullChartDir, pAbs) + logboek.Context(ctx).Debug().LogF("Process show-only params: use path %q\n", tp) + showFiles = append(showFiles, tp) + } else { + logboek.Context(ctx).Debug().LogF("Process show-only params: use path %q\n", p) + showFiles = append(showFiles, p) + } + } + + templateOpts.ShowFiles = &showFiles + } + + helmTemplateCmd, _ := helm_v3.NewTemplateCmd(actionConfig, output, templateOpts) if err := helmTemplateCmd.RunE(helmTemplateCmd, []string{releaseName, filepath.Join(giterminismManager.ProjectDir(), chartDir)}); err != nil { return fmt.Errorf("helm templates rendering failed: %w", err) } diff --git a/go.mod b/go.mod index a2315b135d..6f828329a9 100644 --- a/go.mod +++ b/go.mod @@ -109,4 +109,4 @@ replace k8s.io/helm => github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f replace github.com/deislabs/oras => github.com/werf/third-party-oras v0.9.1-0.20210927171747-6d045506f4c8 -replace helm.sh/helm/v3 => github.com/werf/3p-helm/v3 v3.0.0-20220419131239-a2df9b725de3 +replace helm.sh/helm/v3 => github.com/werf/3p-helm/v3 v3.0.0-20220426084422-a22e7bc5d766 diff --git a/go.sum b/go.sum index 26ea5bdc11..74182f3295 100644 --- a/go.sum +++ b/go.sum @@ -2061,6 +2061,8 @@ github.com/weppos/publicsuffix-go v0.5.0 h1:rutRtjBJViU/YjcI5d80t4JAVvDltS6bciJg github.com/weppos/publicsuffix-go v0.5.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln16JPQ02lHAdn5k= github.com/werf/3p-helm/v3 v3.0.0-20220419131239-a2df9b725de3 h1:H3C8D/30GQ71laOCsLGJrIHZMAa4Joab48hkOfTH8KA= github.com/werf/3p-helm/v3 v3.0.0-20220419131239-a2df9b725de3/go.mod h1:Nm0Z2ciZFFvR9cRKpiRE2SMhJTgqY0b+ezT2cDcyqNw= +github.com/werf/3p-helm/v3 v3.0.0-20220426084422-a22e7bc5d766 h1:Z1vzNVuptwHZBzHcSZ1rrJ8MVVdh8Sh7P39onUS9a3k= +github.com/werf/3p-helm/v3 v3.0.0-20220426084422-a22e7bc5d766/go.mod h1:Nm0Z2ciZFFvR9cRKpiRE2SMhJTgqY0b+ezT2cDcyqNw= github.com/werf/copy-recurse v0.2.2 h1:OpBB+Ezsv7j+iQR02p7zUQXSefZ7UaKBtQPMg2dxi7M= github.com/werf/copy-recurse v0.2.2/go.mod h1:KVHSQ90p19xflWW0B7BJhLBwmSbEtuxIaBnjlUYRPhk= github.com/werf/copy-recurse v0.2.3 h1:bi43vHBDQiX2Qnq+Ci9IGqtm7YdzzpkBO+8MKjp6x8g=