Skip to content

Commit

Permalink
Allow HelmValuesOptions in lintcontext Options
Browse files Browse the repository at this point in the history
Signed-off-by: Arvind Iyengar <arvind.iyengar@rancher.com>
  • Loading branch information
Arvind Iyengar committed Mar 27, 2021
1 parent 244a173 commit 58ffbd1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
4 changes: 3 additions & 1 deletion pkg/lintcontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lintcontext

import (
"golang.stackrox.io/kube-linter/internal/k8sutil"
"helm.sh/helm/v3/pkg/cli/values"
"k8s.io/apimachinery/pkg/runtime"
)

Expand Down Expand Up @@ -33,7 +34,8 @@ type lintContextImpl struct {
objects []Object
invalidObjects []InvalidObject

customDecoder runtime.Decoder
customDecoder runtime.Decoder
helmValuesOptions values.Options
}

// Objects returns the (valid) objects loaded from this LintContext.
Expand Down
7 changes: 7 additions & 0 deletions pkg/lintcontext/create_contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"golang.stackrox.io/kube-linter/internal/set"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli/values"
"k8s.io/apimachinery/pkg/runtime"
)

Expand All @@ -21,6 +22,10 @@ type Options struct {
// CustomDecoder allows users to supply a non-default decoder to parse k8s objects. This can be used
// to allow the linter to create contexts for k8s custom resources
CustomDecoder runtime.Decoder

// HelmValuesOptions provide options for additional values.yamls that can be provided to Helm on loading a chart
// These will be ignored for contexts that are not Helm-based
HelmValuesOptions values.Options
}

// CreateContexts creates a context. Each context contains a set of files that should be linted
Expand Down Expand Up @@ -63,6 +68,7 @@ func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintCo
if strings.HasSuffix(strings.ToLower(currentPath), ".tgz") {
ctx := new()
ctx.customDecoder = options.CustomDecoder
ctx.helmValuesOptions = options.HelmValuesOptions
if err := ctx.loadObjectsFromTgzHelmChart(currentPath); err != nil {
return err
}
Expand Down Expand Up @@ -93,6 +99,7 @@ func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintCo
}
ctx := new()
ctx.customDecoder = options.CustomDecoder
ctx.helmValuesOptions = options.HelmValuesOptions
contextsByDir[currentPath] = ctx
if err := ctx.loadObjectsFromHelmChart(currentPath); err != nil {
return err
Expand Down
34 changes: 4 additions & 30 deletions pkg/lintcontext/parse_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (
"path/filepath"
"strings"

y "github.com/ghodss/yaml"
"github.com/pkg/errors"
"golang.stackrox.io/kube-linter/internal/k8sutil"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/engine"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -84,10 +82,9 @@ 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")}}
values, err := valOpts.MergeValues(nil)
values, err := l.helmValuesOptions.MergeValues(nil)
if err != nil {
return nil, errors.Wrap(err, "loading values.yaml file")
return nil, errors.Wrap(err, "loading provided Helm value options")
}
return l.renderValues(chrt, values)
}
Expand Down Expand Up @@ -155,37 +152,14 @@ func (l *lintContextImpl) renderTgzHelmChart(tgzFile string) (map[string]string,
return nil, err
}

valuesIndex := -1
for i, f := range chrt.Raw {
if f.Name == "values.yaml" {
valuesIndex = i
break
}
}

indexName := filepath.Join(tgzFile, "values.yaml")
if valuesIndex == -1 {
return nil, errors.Errorf("%s not found", indexName)
}

values, err := l.parseValues(indexName, chrt.Raw[valuesIndex].Data)
values, err := l.helmValuesOptions.MergeValues(nil)
if err != nil {
return nil, errors.Wrap(err, "loading values.yaml file")
return nil, errors.Wrap(err, "loading provided Helm value options")
}

return l.renderValues(chrt, values)
}

func (l *lintContextImpl) parseValues(filePath string, bytes []byte) (map[string]interface{}, error) {
currentMap := map[string]interface{}{}

if err := y.Unmarshal(bytes, &currentMap); err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", filePath)
}

return currentMap, nil
}

func (l *lintContextImpl) loadObjectFromYAMLReader(filePath string, r *yaml.YAMLReader) error {
doc, err := r.Read()
if err != nil {
Expand Down

0 comments on commit 58ffbd1

Please sign in to comment.