@@ -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,56 @@ 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
+ type HelmOptions struct {
131
+ Options
123
132
124
- return []LintContext {ctx }, nil
133
+ // HelmValuesOptions provide options for additional values.yamls that can be provided to Helm on loading a chart
134
+ // These will be ignored for contexts that are not Helm-based
135
+ HelmValuesOptions []values.Options
136
+
137
+ // Whether to treat this as a Helm chart directory
138
+ FromDir bool
139
+ // Whether to treat this as a Helm chart archive (tgz).
140
+ FromArchive bool
141
+ // FromReader is used if isDir and isArchive are both false
142
+ FromReader io.Reader
143
+ }
144
+
145
+ func CreateHelmContextsWithOptions (options HelmOptions , chartDir string ) ([]LintContext , error ) {
146
+ if isHelm , _ := chartutil .IsChartDir (chartDir ); ! isHelm {
147
+ return nil , errors .New ("cannot generate helm context from non-helm dir " + chartDir )
148
+ }
149
+ contextsByHelmValues := []LintContext {}
150
+ for _ , helmValueOptions := range options .HelmValuesOptions {
151
+ ctx := newHelmCtx (options .Options , helmValueOptions )
152
+ if err := ctx .loadObjectsFromHelmChart (chartDir , options ); err != nil {
153
+ return nil , err
154
+ }
155
+ contextsByHelmValues = append (contextsByHelmValues , ctx )
156
+ }
157
+ return contextsByHelmValues , nil
125
158
}
0 commit comments