Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add --values flag to pass extra values files to helm #203

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/command/lint/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/flags.go

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

8 changes: 4 additions & 4 deletions pkg/lintcontext/create_contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions pkg/lintcontext/parse_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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")
Expand All @@ -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
Expand Down