@@ -9,19 +9,30 @@ import (
9
9
"github.com/pkg/errors"
10
10
"golang.stackrox.io/kube-linter/internal/set"
11
11
"helm.sh/helm/v3/pkg/chartutil"
12
+ "k8s.io/apimachinery/pkg/runtime"
12
13
)
13
14
14
15
var (
15
16
knownYAMLExtensions = set .NewFrozenStringSet (".yaml" , ".yml" )
16
17
)
17
18
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
+
18
26
// CreateContexts creates a context. Each context contains a set of files that should be linted
19
27
// as a group.
20
28
// Currently, each directory of Kube YAML files (or Helm charts) are treated as a separate context.
21
29
// TODO: Figure out if it's useful to allow people to specify that files spanning different directories
22
30
// should be treated as being in the same context.
23
31
func CreateContexts (filesOrDirs ... string ) ([]LintContext , error ) {
32
+ return CreateContextsWithOptions (Options {}, filesOrDirs ... )
33
+ }
24
34
35
+ func CreateContextsWithOptions (options Options , filesOrDirs ... string ) ([]LintContext , error ) {
25
36
contextsByDir := make (map [string ]* lintContextImpl )
26
37
for _ , fileOrDir := range filesOrDirs {
27
38
// Stdin
@@ -30,6 +41,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
30
41
continue
31
42
}
32
43
ctx := new ()
44
+ ctx .customDecoder = options .CustomDecoder
33
45
if err := ctx .loadObjectsFromReader ("<standard input>" , os .Stdin ); err != nil {
34
46
return nil , err
35
47
}
@@ -49,6 +61,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
49
61
if ! info .IsDir () {
50
62
if strings .HasSuffix (strings .ToLower (currentPath ), ".tgz" ) {
51
63
ctx := new ()
64
+ ctx .customDecoder = options .CustomDecoder
52
65
if err := ctx .loadObjectsFromTgzHelmChart (currentPath ); err != nil {
53
66
return err
54
67
}
@@ -63,6 +76,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
63
76
ctx := contextsByDir [dirName ]
64
77
if ctx == nil {
65
78
ctx = new ()
79
+ ctx .customDecoder = options .CustomDecoder
66
80
contextsByDir [dirName ] = ctx
67
81
}
68
82
if err := ctx .loadObjectsFromYAMLFile (currentPath , info ); err != nil {
@@ -77,6 +91,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
77
91
return nil
78
92
}
79
93
ctx := new ()
94
+ ctx .customDecoder = options .CustomDecoder
80
95
contextsByDir [currentPath ] = ctx
81
96
if err := ctx .loadObjectsFromHelmChart (currentPath ); err != nil {
82
97
return err
0 commit comments