Skip to content

Commit 621b1d0

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 621b1d0

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

pkg/lintcontext/context.go

+3
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+
"k8s.io/apimachinery/pkg/runtime"
56
)
67

78
// ObjectMetadata is metadata about an object.
@@ -31,6 +32,8 @@ type LintContext interface {
3132
type lintContextImpl struct {
3233
objects []Object
3334
invalidObjects []InvalidObject
35+
36+
customDecoder runtime.Decoder
3437
}
3538

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

pkg/lintcontext/create_contexts.go

+12
Original file line numberDiff line numberDiff line change
@@ -9,19 +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+
}
2431

32+
func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintContext, error) {
2533
contextsByDir := make(map[string]*lintContextImpl)
2634
for _, fileOrDir := range filesOrDirs {
2735
// Stdin
@@ -30,6 +38,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
3038
continue
3139
}
3240
ctx := new()
41+
ctx.customDecoder = options.customDecoder
3342
if err := ctx.loadObjectsFromReader("<standard input>", os.Stdin); err != nil {
3443
return nil, err
3544
}
@@ -49,6 +58,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
4958
if !info.IsDir() {
5059
if strings.HasSuffix(strings.ToLower(currentPath), ".tgz") {
5160
ctx := new()
61+
ctx.customDecoder = options.customDecoder
5262
if err := ctx.loadObjectsFromTgzHelmChart(currentPath); err != nil {
5363
return err
5464
}
@@ -63,6 +73,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
6373
ctx := contextsByDir[dirName]
6474
if ctx == nil {
6575
ctx = new()
76+
ctx.customDecoder = options.customDecoder
6677
contextsByDir[dirName] = ctx
6778
}
6879
if err := ctx.loadObjectsFromYAMLFile(currentPath, info); err != nil {
@@ -77,6 +88,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
7788
return nil
7889
}
7990
ctx := new()
91+
ctx.customDecoder = options.customDecoder
8092
contextsByDir[currentPath] = ctx
8193
if err := ctx.loadObjectsFromHelmChart(currentPath); err != nil {
8294
return err

pkg/lintcontext/parse_yaml.go

+8-4
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
}
@@ -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, l.customDecoder)
201205
if err != nil {
202206
l.addInvalidObjects(InvalidObject{
203207
Metadata: metadata,

0 commit comments

Comments
 (0)