Skip to content

Commit 6637217

Browse files
author
Arvind Iyengar
committed
Add CreateContextWithOptions for lintcontext
Allows users to provide options for creating lint context. Related Issue: stackrox#141 Signed-off-by: Arvind Iyengar <[email protected]>
1 parent 241e80d commit 6637217

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

pkg/lintcontext/create_contexts.go

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

1415
var (
1516
knownYAMLExtensions = set.NewFrozenStringSet(".yaml", ".yml")
1617
)
1718

19+
type Options struct {
20+
customDecoder runtime.Decoder
21+
}
22+
1823
// CreateContexts creates a context. Each context contains a set of files that should be linted
1924
// as a group.
2025
// Currently, each directory of Kube YAML files (or Helm charts) are treated as a separate context.
2126
// TODO: Figure out if it's useful to allow people to specify that files spanning different directories
2227
// should be treated as being in the same context.
2328
func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
29+
return CreateContextsWithOptions(Options{}, filesOrDirs...)
30+
}
31+
32+
func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintContext, error) {
2433

2534
contextsByDir := make(map[string]*lintContextImpl)
2635
for _, fileOrDir := range filesOrDirs {
@@ -30,7 +39,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
3039
continue
3140
}
3241
ctx := new()
33-
if err := ctx.loadObjectsFromReader("<standard input>", os.Stdin); err != nil {
42+
if err := ctx.loadObjectsFromReader("<standard input>", os.Stdin, options.customDecoder); err != nil {
3443
return nil, err
3544
}
3645
contextsByDir["-"] = ctx
@@ -49,7 +58,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
4958
if !info.IsDir() {
5059
if strings.HasSuffix(strings.ToLower(currentPath), ".tgz") {
5160
ctx := new()
52-
if err := ctx.loadObjectsFromTgzHelmChart(currentPath); err != nil {
61+
if err := ctx.loadObjectsFromTgzHelmChart(currentPath, options.customDecoder); err != nil {
5362
return err
5463
}
5564

@@ -65,7 +74,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
6574
ctx = new()
6675
contextsByDir[dirName] = ctx
6776
}
68-
if err := ctx.loadObjectsFromYAMLFile(currentPath, info); err != nil {
77+
if err := ctx.loadObjectsFromYAMLFile(currentPath, info, options.customDecoder); err != nil {
6978
return err
7079
}
7180
}
@@ -78,7 +87,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
7887
}
7988
ctx := new()
8089
contextsByDir[currentPath] = ctx
81-
if err := ctx.loadObjectsFromHelmChart(currentPath); err != nil {
90+
if err := ctx.loadObjectsFromHelmChart(currentPath, options.customDecoder); err != nil {
8291
return err
8392
}
8493
return filepath.SkipDir

pkg/lintcontext/parse_yaml.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"helm.sh/helm/v3/pkg/cli/values"
1919
"helm.sh/helm/v3/pkg/engine"
2020
v1 "k8s.io/api/core/v1"
21+
"k8s.io/apimachinery/pkg/runtime"
2122
"k8s.io/apimachinery/pkg/runtime/serializer"
2223
"k8s.io/apimachinery/pkg/util/yaml"
2324
"k8s.io/client-go/kubernetes/scheme"
@@ -34,15 +35,18 @@ var (
3435
decoder = serializer.NewCodecFactory(clientSchema).UniversalDeserializer()
3536
)
3637

37-
func parseObjects(data []byte) ([]k8sutil.Object, error) {
38-
obj, _, err := decoder.Decode(data, nil, nil)
38+
func parseObjects(data []byte, d runtime.Decoder) ([]k8sutil.Object, error) {
39+
if d == nil {
40+
d = decoder
41+
}
42+
obj, _, err := d.Decode(data, nil, nil)
3943
if err != nil {
4044
return nil, errors.Wrap(err, "failed to decode")
4145
}
4246
if list, ok := obj.(*v1.List); ok {
4347
objs := make([]k8sutil.Object, 0, len(list.Items))
4448
for i, item := range list.Items {
45-
obj, _, err := decoder.Decode(item.Raw, nil, nil)
49+
obj, _, err := d.Decode(item.Raw, nil, nil)
4650
if err != nil {
4751
return nil, errors.Wrapf(err, "decoding item %d in the list", i)
4852
}
@@ -103,7 +107,7 @@ func (l *lintContextImpl) renderValues(chrt *chart.Chart, values map[string]inte
103107
return rendered, nil
104108
}
105109

106-
func (l *lintContextImpl) loadObjectsFromHelmChart(dir string) error {
110+
func (l *lintContextImpl) loadObjectsFromHelmChart(dir string, d runtime.Decoder) error {
107111
metadata := ObjectMetadata{FilePath: dir}
108112
renderedFiles, err := l.renderHelmChart(dir)
109113
if err != nil {
@@ -114,14 +118,14 @@ func (l *lintContextImpl) loadObjectsFromHelmChart(dir string) error {
114118
// The first element of path will be the same as the last element of dir, because
115119
// Helm duplicates it.
116120
pathToTemplate := filepath.Join(filepath.Dir(dir), path)
117-
if err := l.loadObjectsFromReader(pathToTemplate, strings.NewReader(contents)); err != nil {
121+
if err := l.loadObjectsFromReader(pathToTemplate, strings.NewReader(contents), d); err != nil {
118122
return errors.Wrapf(err, "loading objects from rendered helm chart %s/%s", dir, pathToTemplate)
119123
}
120124
}
121125
return nil
122126
}
123127

124-
func (l *lintContextImpl) loadObjectsFromTgzHelmChart(tgzFile string) error {
128+
func (l *lintContextImpl) loadObjectsFromTgzHelmChart(tgzFile string, d runtime.Decoder) error {
125129
metadata := ObjectMetadata{FilePath: tgzFile}
126130
renderedFiles, err := l.renderTgzHelmChart(tgzFile)
127131
if err != nil {
@@ -132,7 +136,7 @@ func (l *lintContextImpl) loadObjectsFromTgzHelmChart(tgzFile string) error {
132136
// The first element of path will be the same as the last element of tgzFile, because
133137
// Helm duplicates it.
134138
pathToTemplate := filepath.Join(filepath.Dir(tgzFile), path)
135-
if err := l.loadObjectsFromReader(pathToTemplate, strings.NewReader(contents)); err != nil {
139+
if err := l.loadObjectsFromReader(pathToTemplate, strings.NewReader(contents), d); err != nil {
136140
return errors.Wrapf(err, "loading objects from rendered helm chart %s/%s", tgzFile, pathToTemplate)
137141
}
138142
}
@@ -182,7 +186,7 @@ func (l *lintContextImpl) parseValues(filePath string, bytes []byte) (map[string
182186
return currentMap, nil
183187
}
184188

185-
func (l *lintContextImpl) loadObjectFromYAMLReader(filePath string, r *yaml.YAMLReader) error {
189+
func (l *lintContextImpl) loadObjectFromYAMLReader(filePath string, r *yaml.YAMLReader, d runtime.Decoder) error {
186190
doc, err := r.Read()
187191
if err != nil {
188192
return err
@@ -197,7 +201,7 @@ func (l *lintContextImpl) loadObjectFromYAMLReader(filePath string, r *yaml.YAML
197201
Raw: doc,
198202
}
199203

200-
objs, err := parseObjects(doc)
204+
objs, err := parseObjects(doc, d)
201205
if err != nil {
202206
l.addInvalidObjects(InvalidObject{
203207
Metadata: metadata,
@@ -214,7 +218,7 @@ func (l *lintContextImpl) loadObjectFromYAMLReader(filePath string, r *yaml.YAML
214218
return nil
215219
}
216220

217-
func (l *lintContextImpl) loadObjectsFromYAMLFile(filePath string, info os.FileInfo) error {
221+
func (l *lintContextImpl) loadObjectsFromYAMLFile(filePath string, info os.FileInfo, d runtime.Decoder) error {
218222
if info.Size() > maxFileSizeBytes {
219223
return nil
220224
}
@@ -226,13 +230,13 @@ func (l *lintContextImpl) loadObjectsFromYAMLFile(filePath string, info os.FileI
226230
_ = file.Close()
227231
}()
228232

229-
return l.loadObjectsFromReader(filePath, file)
233+
return l.loadObjectsFromReader(filePath, file, d)
230234
}
231235

232-
func (l *lintContextImpl) loadObjectsFromReader(filePath string, reader io.Reader) error {
236+
func (l *lintContextImpl) loadObjectsFromReader(filePath string, reader io.Reader, d runtime.Decoder) error {
233237
yamlReader := yaml.NewYAMLReader(bufio.NewReader(reader))
234238
for {
235-
if err := l.loadObjectFromYAMLReader(filePath, yamlReader); err != nil {
239+
if err := l.loadObjectFromYAMLReader(filePath, yamlReader, d); err != nil {
236240
if err == io.EOF {
237241
return nil
238242
}

0 commit comments

Comments
 (0)