From 509d38527fd883b50bd3838ea21bc61cdd91652b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Lehmann?= Date: Thu, 19 Aug 2021 07:57:13 +0200 Subject: [PATCH] add --values flag to pass extra values files to helm --- pkg/command/lint/command.go | 2 +- pkg/config/config.go | 3 +++ pkg/config/flags.go | 4 ++++ pkg/lintcontext/create_contexts.go | 8 ++++---- pkg/lintcontext/parse_yaml.go | 17 +++++++++++++---- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/pkg/command/lint/command.go b/pkg/command/lint/command.go index f52737746..b81be709e 100644 --- a/pkg/command/lint/command.go +++ b/pkg/command/lint/command.go @@ -76,7 +76,7 @@ func Command() *cobra.Command { fmt.Fprintln(os.Stderr, "Warning: no checks enabled.") return nil } - lintCtxs, err := lintcontext.CreateContexts(args...) + lintCtxs, err := lintcontext.CreateContexts(cfg.Checks.Values, args...) if err != nil { return err } diff --git a/pkg/config/config.go b/pkg/config/config.go index 2c65663fa..53a5e160b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -26,6 +26,9 @@ type ChecksConfig struct { // Exclude wins. // +flagName=include Include []string `json:"include"` + // A list of helm values files. + // +flagName=values + Values []string `json:"values"` } // Config represents the config file format. diff --git a/pkg/config/flags.go b/pkg/config/flags.go index be1e66311..2857039ed 100644 --- a/pkg/config/flags.go +++ b/pkg/config/flags.go @@ -27,4 +27,8 @@ func AddFlags(c *cobra.Command, v *viper.Viper) { if err := v.BindPFlag("checks.include", c.Flags().Lookup("include")); err != nil { panic(err) } + c.Flags().StringSlice("values", nil, "A list of helm values files.") + if err := v.BindPFlag("checks.values", c.Flags().Lookup("values")); err != nil { + panic(err) + } } diff --git a/pkg/lintcontext/create_contexts.go b/pkg/lintcontext/create_contexts.go index 5aa5b58fb..0ae562ef1 100644 --- a/pkg/lintcontext/create_contexts.go +++ b/pkg/lintcontext/create_contexts.go @@ -29,12 +29,12 @@ type Options struct { // Currently, each directory of Kube YAML files (or Helm charts) are treated as a separate context. // TODO: Figure out if it's useful to allow people to specify that files spanning different directories // should be treated as being in the same context. -func CreateContexts(filesOrDirs ...string) ([]LintContext, error) { - return CreateContextsWithOptions(Options{}, filesOrDirs...) +func CreateContexts(valuesFiles []string, filesOrDirs ...string) ([]LintContext, error) { + return CreateContextsWithOptions(Options{}, valuesFiles, filesOrDirs...) } // CreateContextsWithOptions creates a context with additional Options -func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintContext, error) { +func CreateContextsWithOptions(options Options, valuesFiles []string, filesOrDirs ...string) ([]LintContext, error) { contextsByDir := make(map[string]*lintContextImpl) for _, fileOrDir := range filesOrDirs { // Stdin @@ -91,7 +91,7 @@ func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintCo } ctx := newCtx(options) contextsByDir[currentPath] = ctx - if err := ctx.loadObjectsFromHelmChart(currentPath); err != nil { + if err := ctx.loadObjectsFromHelmChart(currentPath, valuesFiles); err != nil { return err } return filepath.SkipDir diff --git a/pkg/lintcontext/parse_yaml.go b/pkg/lintcontext/parse_yaml.go index ae05228f9..71d307070 100644 --- a/pkg/lintcontext/parse_yaml.go +++ b/pkg/lintcontext/parse_yaml.go @@ -84,7 +84,7 @@ func (w nopWriter) Write(p []byte) (n int, err error) { return len(p), nil } -func (l *lintContextImpl) renderHelmChart(dir string) (map[string]string, error) { +func (l *lintContextImpl) renderHelmChart(dir string, valuesFiles []string) (map[string]string, error) { // Helm doesn't have great logging behaviour, and can spam stderr, so silence their logging. // TODO: capture these logs. log.SetOutput(nopWriter{}) @@ -96,7 +96,16 @@ func (l *lintContextImpl) renderHelmChart(dir string) (map[string]string, error) if err := chrt.Validate(); err != nil { return nil, err } - valOpts := &values.Options{ValueFiles: []string{filepath.Join(dir, "values.yaml")}} + var fullPathValues = []string{filepath.Join(dir, "values.yaml")} + for _, valuesFile := range valuesFiles { + fullPath, err := filepath.Abs(valuesFile) + if err != nil { + return nil, err + } + fullPathValues = append(fullPathValues, fullPath) + } + valOpts := &values.Options{ValueFiles: fullPathValues} + values, err := valOpts.MergeValues(nil) if err != nil { return nil, errors.Wrap(err, "loading values.yaml file") @@ -119,9 +128,9 @@ func (l *lintContextImpl) renderValues(chrt *chart.Chart, values map[string]inte return rendered, nil } -func (l *lintContextImpl) loadObjectsFromHelmChart(dir string) error { +func (l *lintContextImpl) loadObjectsFromHelmChart(dir string, valuesFiles []string) error { metadata := ObjectMetadata{FilePath: dir} - renderedFiles, err := l.renderHelmChart(dir) + renderedFiles, err := l.renderHelmChart(dir, valuesFiles) if err != nil { l.addInvalidObjects(InvalidObject{Metadata: metadata, LoadErr: err}) return nil