Skip to content

Commit 6cc5e58

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

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

pkg/lintcontext/context.go

Lines changed: 3 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,30 @@ 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+
// Options represent values that can be provided to modify how objects are parsed to create lint contexts
20+
type Options struct {
21+
// CustomDecoder allows users to supply a non-default decoder to parse k8s objects. This can be used
22+
// to allow the linter to create contexts for k8s custom resources
23+
CustomDecoder runtime.Decoder
24+
}
25+
1826
// CreateContexts creates a context. Each context contains a set of files that should be linted
1927
// as a group.
2028
// Currently, each directory of Kube YAML files (or Helm charts) are treated as a separate context.
2129
// TODO: Figure out if it's useful to allow people to specify that files spanning different directories
2230
// should be treated as being in the same context.
2331
func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
32+
return CreateContextsWithOptions(Options{}, filesOrDirs...)
33+
}
2434

35+
func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintContext, error) {
2536
contextsByDir := make(map[string]*lintContextImpl)
2637
for _, fileOrDir := range filesOrDirs {
2738
// Stdin
@@ -30,6 +41,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
3041
continue
3142
}
3243
ctx := new()
44+
ctx.customDecoder = options.CustomDecoder
3345
if err := ctx.loadObjectsFromReader("<standard input>", os.Stdin); err != nil {
3446
return nil, err
3547
}
@@ -49,6 +61,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
4961
if !info.IsDir() {
5062
if strings.HasSuffix(strings.ToLower(currentPath), ".tgz") {
5163
ctx := new()
64+
ctx.customDecoder = options.CustomDecoder
5265
if err := ctx.loadObjectsFromTgzHelmChart(currentPath); err != nil {
5366
return err
5467
}
@@ -63,6 +76,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
6376
ctx := contextsByDir[dirName]
6477
if ctx == nil {
6578
ctx = new()
79+
ctx.customDecoder = options.CustomDecoder
6680
contextsByDir[dirName] = ctx
6781
}
6882
if err := ctx.loadObjectsFromYAMLFile(currentPath, info); err != nil {
@@ -77,6 +91,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
7791
return nil
7892
}
7993
ctx := new()
94+
ctx.customDecoder = options.CustomDecoder
8095
contextsByDir[currentPath] = ctx
8196
if err := ctx.loadObjectsFromHelmChart(currentPath); err != nil {
8297
return err

pkg/lintcontext/parse_yaml.go

Lines changed: 8 additions & 4 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
}
@@ -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)