@@ -10,6 +10,7 @@ import (
10
10
"github.com/pkg/errors"
11
11
"golang.stackrox.io/kube-linter/internal/set"
12
12
"helm.sh/helm/v3/pkg/chartutil"
13
+ "helm.sh/helm/v3/pkg/cli/values"
13
14
"k8s.io/apimachinery/pkg/runtime"
14
15
)
15
16
@@ -36,6 +37,7 @@ func CreateContexts(filesOrDirs ...string) ([]LintContext, error) {
36
37
// CreateContextsWithOptions creates a context with additional Options
37
38
func CreateContextsWithOptions (options Options , filesOrDirs ... string ) ([]LintContext , error ) {
38
39
contextsByDir := make (map [string ]* lintContextImpl )
40
+ contextsByChartDir := make (map [string ][]LintContext )
39
41
for _ , fileOrDir := range filesOrDirs {
40
42
// Stdin
41
43
if fileOrDir == "-" {
@@ -59,14 +61,17 @@ func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintCo
59
61
return nil
60
62
}
61
63
64
+ if _ , exists := contextsByChartDir [currentPath ]; exists {
65
+ return nil
66
+ }
67
+
62
68
if ! info .IsDir () {
63
69
if strings .HasSuffix (strings .ToLower (currentPath ), ".tgz" ) {
64
- ctx := newCtx ( options )
65
- if err := ctx . loadObjectsFromTgzHelmChart ( currentPath ); err != nil {
70
+ lintCtxs , err := CreateHelmContextsWithOptions ( HelmOptions { Options : options , FromArchive : true }, currentPath )
71
+ if err != nil {
66
72
return err
67
73
}
68
-
69
- contextsByDir [currentPath ] = ctx
74
+ contextsByChartDir [currentPath ] = lintCtxs
70
75
return nil
71
76
}
72
77
@@ -85,15 +90,11 @@ func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintCo
85
90
return nil
86
91
}
87
92
if isHelm , _ := chartutil .IsChartDir (currentPath ); isHelm {
88
- // Path has already been loaded, possibly through another argument. Skip.
89
- if _ , alreadyExists := contextsByDir [currentPath ]; alreadyExists {
90
- return nil
91
- }
92
- ctx := newCtx (options )
93
- contextsByDir [currentPath ] = ctx
94
- if err := ctx .loadObjectsFromHelmChart (currentPath ); err != nil {
93
+ lintCtxs , err := CreateHelmContextsWithOptions (HelmOptions {Options : options , FromDir : true }, currentPath )
94
+ if err != nil {
95
95
return err
96
96
}
97
+ contextsByChartDir [currentPath ] = lintCtxs
97
98
return filepath .SkipDir
98
99
}
99
100
return nil
@@ -102,24 +103,55 @@ func CreateContextsWithOptions(options Options, filesOrDirs ...string) ([]LintCo
102
103
return nil , errors .Wrapf (err , "loading from path %q" , fileOrDir )
103
104
}
104
105
}
105
- dirs := make ([]string , 0 , len (contextsByDir ))
106
+ dirs := make ([]string , 0 , len (contextsByDir )+ len ( contextsByChartDir ) )
106
107
for dir := range contextsByDir {
107
108
dirs = append (dirs , dir )
108
109
}
110
+ for dir := range contextsByChartDir {
111
+ dirs = append (dirs , dir )
112
+ }
109
113
sort .Strings (dirs )
110
114
var contexts []LintContext
111
115
for _ , dir := range dirs {
116
+ if helmCtxs , ok := contextsByChartDir [dir ]; ok {
117
+ contexts = append (contexts , helmCtxs ... )
118
+ continue
119
+ }
112
120
contexts = append (contexts , contextsByDir [dir ])
113
121
}
114
122
return contexts , nil
115
123
}
116
124
117
- // CreateContextsFromHelmArchive creates a context from TGZ reader of Helm Chart.
125
+ // CreateContextsFromHelmArchive creates a context from a tgz file based on a provided tgzReader
118
126
func CreateContextsFromHelmArchive (fileName string , tgzReader io.Reader ) ([]LintContext , error ) {
119
- ctx := newCtx (Options {})
120
- if err := ctx .readObjectsFromTgzHelmChart (fileName , tgzReader ); err != nil {
121
- return nil , err
122
- }
127
+ return CreateHelmContextsWithOptions (HelmOptions {FromReader : tgzReader }, fileName )
128
+ }
129
+
130
+ // HelmOptions represent Helm-specific values that can be provided to modify how objects are parsed to create lint contexts
131
+ type HelmOptions struct {
132
+ Options
133
+
134
+ // HelmValuesOptions provide options for additional values.yamls that can be provided to Helm on loading a chart
135
+ // These will be ignored for contexts that are not Helm-based
136
+ HelmValuesOptions []values.Options
123
137
124
- return []LintContext {ctx }, nil
138
+ // Whether to treat this as a Helm chart directory
139
+ FromDir bool
140
+ // Whether to treat this as a Helm chart archive (tgz).
141
+ FromArchive bool
142
+ // FromReader is used if isDir and isArchive are both false
143
+ FromReader io.Reader
144
+ }
145
+
146
+ // CreateContextsFromHelmArchive creates a context based on provided options
147
+ func CreateHelmContextsWithOptions (options HelmOptions , fileOrDir string ) ([]LintContext , error ) {
148
+ contextsByHelmValues := []LintContext {}
149
+ for _ , helmValueOptions := range options .HelmValuesOptions {
150
+ ctx := newHelmCtx (options .Options , helmValueOptions )
151
+ if err := ctx .loadObjectsFromHelmChart (fileOrDir , options ); err != nil {
152
+ return nil , err
153
+ }
154
+ contextsByHelmValues = append (contextsByHelmValues , ctx )
155
+ }
156
+ return contextsByHelmValues , nil
125
157
}
0 commit comments