Skip to content

Commit 58ffbd1

Browse files
author
Arvind Iyengar
committed
Allow HelmValuesOptions in lintcontext Options
Signed-off-by: Arvind Iyengar <[email protected]>
1 parent 244a173 commit 58ffbd1

File tree

3 files changed

+14
-31
lines changed

3 files changed

+14
-31
lines changed

pkg/lintcontext/context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lintcontext
22

33
import (
44
"golang.stackrox.io/kube-linter/internal/k8sutil"
5+
"helm.sh/helm/v3/pkg/cli/values"
56
"k8s.io/apimachinery/pkg/runtime"
67
)
78

@@ -33,7 +34,8 @@ type lintContextImpl struct {
3334
objects []Object
3435
invalidObjects []InvalidObject
3536

36-
customDecoder runtime.Decoder
37+
customDecoder runtime.Decoder
38+
helmValuesOptions values.Options
3739
}
3840

3941
// Objects returns the (valid) objects loaded from this LintContext.

pkg/lintcontext/create_contexts.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/pkg/errors"
1010
"golang.stackrox.io/kube-linter/internal/set"
1111
"helm.sh/helm/v3/pkg/chartutil"
12+
"helm.sh/helm/v3/pkg/cli/values"
1213
"k8s.io/apimachinery/pkg/runtime"
1314
)
1415

@@ -21,6 +22,10 @@ type Options struct {
2122
// CustomDecoder allows users to supply a non-default decoder to parse k8s objects. This can be used
2223
// to allow the linter to create contexts for k8s custom resources
2324
CustomDecoder runtime.Decoder
25+
26+
// HelmValuesOptions provide options for additional values.yamls that can be provided to Helm on loading a chart
27+
// These will be ignored for contexts that are not Helm-based
28+
HelmValuesOptions values.Options
2429
}
2530

2631
// CreateContexts creates a context. Each context contains a set of files that should be linted
@@ -63,6 +68,7 @@ func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintCo
6368
if strings.HasSuffix(strings.ToLower(currentPath), ".tgz") {
6469
ctx := new()
6570
ctx.customDecoder = options.CustomDecoder
71+
ctx.helmValuesOptions = options.HelmValuesOptions
6672
if err := ctx.loadObjectsFromTgzHelmChart(currentPath); err != nil {
6773
return err
6874
}
@@ -93,6 +99,7 @@ func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintCo
9399
}
94100
ctx := new()
95101
ctx.customDecoder = options.CustomDecoder
102+
ctx.helmValuesOptions = options.HelmValuesOptions
96103
contextsByDir[currentPath] = ctx
97104
if err := ctx.loadObjectsFromHelmChart(currentPath); err != nil {
98105
return err

pkg/lintcontext/parse_yaml.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import (
99
"path/filepath"
1010
"strings"
1111

12-
y "github.com/ghodss/yaml"
1312
"github.com/pkg/errors"
1413
"golang.stackrox.io/kube-linter/internal/k8sutil"
1514
"helm.sh/helm/v3/pkg/chart"
1615
"helm.sh/helm/v3/pkg/chart/loader"
1716
"helm.sh/helm/v3/pkg/chartutil"
18-
"helm.sh/helm/v3/pkg/cli/values"
1917
"helm.sh/helm/v3/pkg/engine"
2018
v1 "k8s.io/api/core/v1"
2119
"k8s.io/apimachinery/pkg/runtime"
@@ -84,10 +82,9 @@ func (l *lintContextImpl) renderHelmChart(dir string) (map[string]string, error)
8482
if err := chrt.Validate(); err != nil {
8583
return nil, err
8684
}
87-
valOpts := &values.Options{ValueFiles: []string{filepath.Join(dir, "values.yaml")}}
88-
values, err := valOpts.MergeValues(nil)
85+
values, err := l.helmValuesOptions.MergeValues(nil)
8986
if err != nil {
90-
return nil, errors.Wrap(err, "loading values.yaml file")
87+
return nil, errors.Wrap(err, "loading provided Helm value options")
9188
}
9289
return l.renderValues(chrt, values)
9390
}
@@ -155,37 +152,14 @@ func (l *lintContextImpl) renderTgzHelmChart(tgzFile string) (map[string]string,
155152
return nil, err
156153
}
157154

158-
valuesIndex := -1
159-
for i, f := range chrt.Raw {
160-
if f.Name == "values.yaml" {
161-
valuesIndex = i
162-
break
163-
}
164-
}
165-
166-
indexName := filepath.Join(tgzFile, "values.yaml")
167-
if valuesIndex == -1 {
168-
return nil, errors.Errorf("%s not found", indexName)
169-
}
170-
171-
values, err := l.parseValues(indexName, chrt.Raw[valuesIndex].Data)
155+
values, err := l.helmValuesOptions.MergeValues(nil)
172156
if err != nil {
173-
return nil, errors.Wrap(err, "loading values.yaml file")
157+
return nil, errors.Wrap(err, "loading provided Helm value options")
174158
}
175159

176160
return l.renderValues(chrt, values)
177161
}
178162

179-
func (l *lintContextImpl) parseValues(filePath string, bytes []byte) (map[string]interface{}, error) {
180-
currentMap := map[string]interface{}{}
181-
182-
if err := y.Unmarshal(bytes, &currentMap); err != nil {
183-
return nil, errors.Wrapf(err, "failed to parse %s", filePath)
184-
}
185-
186-
return currentMap, nil
187-
}
188-
189163
func (l *lintContextImpl) loadObjectFromYAMLReader(filePath string, r *yaml.YAMLReader) error {
190164
doc, err := r.Read()
191165
if err != nil {

0 commit comments

Comments
 (0)