@@ -91,34 +91,18 @@ func (g Generator) Generate(ctx context.Context, out io.Writer) (manifest.Manife
91
91
var manifests manifest.ManifestList
92
92
93
93
// Generate kustomize Manifests
94
- _ , endTrace := instrumentation .StartTrace (ctx , "Render_expandGlobKustomizeManifests" )
95
- kustomizePaths , err := resolveRemoteAndLocal (g .config .Kustomize , g .workingDir )
96
- if err != nil {
97
- event .DeployInfoEvent (fmt .Errorf ("could not expand the glob kustomize manifests: %w" , err ))
98
- return nil , err
99
- }
100
- endTrace ()
101
- kustomizePathMap := make (map [string ]bool )
102
- for _ , path := range kustomizePaths {
103
- if dir , ok := isKustomizeDir (path ); ok {
104
- kustomizePathMap [dir ] = true
105
- }
106
- }
107
- for kPath := range kustomizePathMap {
108
- // TODO: kustomize kpt-fn not available yet. See https://github.com/GoogleContainerTools/kpt/issues/1447
109
- cmd := exec .CommandContext (ctx , "kustomize" , "build" , kPath )
110
- out , err := util .RunCmdOut (ctx , cmd )
94
+ if g .config .Kustomize != nil && len (g .config .Kustomize .Paths ) != 0 {
95
+ kustomizeManifests , err := g .generateKustomizeManifests (ctx )
111
96
if err != nil {
112
97
return nil , err
113
98
}
114
- if len ( out ) == 0 {
115
- continue
99
+ for _ , m := range kustomizeManifests {
100
+ manifests . Append ( m )
116
101
}
117
- manifests .Append (out )
118
102
}
119
103
120
104
// Generate in-place hydrated kpt Manifests
121
- _ , endTrace = instrumentation .StartTrace (ctx , "Render_expandGlobKptManifests" )
105
+ _ , endTrace : = instrumentation .StartTrace (ctx , "Render_expandGlobKptManifests" )
122
106
kptPaths , err := resolveRemoteAndLocal (g .config .Kpt , g .workingDir )
123
107
if err != nil {
124
108
event .DeployInfoEvent (fmt .Errorf ("could not expand the glob kpt manifests: %w" , err ))
@@ -174,6 +158,38 @@ func (g Generator) Generate(ctx context.Context, out io.Writer) (manifest.Manife
174
158
return manifests , nil
175
159
}
176
160
161
+ func (g Generator ) generateKustomizeManifests (ctx context.Context ) ([][]byte , error ) {
162
+ var manifests [][]byte
163
+
164
+ _ , endTrace := instrumentation .StartTrace (ctx , "Render_expandGlobKustomizeManifests" )
165
+ kustomizePaths , err := resolveRemoteAndLocal (g .config .Kustomize .Paths , g .workingDir )
166
+ if err != nil {
167
+ event .DeployInfoEvent (fmt .Errorf ("could not expand the glob kustomize manifests: %w" , err ))
168
+ return nil , err
169
+ }
170
+ endTrace ()
171
+ kustomizePathMap := make (map [string ]bool )
172
+ for _ , path := range kustomizePaths {
173
+ if dir , ok := isKustomizeDir (path ); ok {
174
+ kustomizePathMap [dir ] = true
175
+ }
176
+ }
177
+ for kPath := range kustomizePathMap {
178
+ // TODO: kustomize kpt-fn not available yet. See https://github.com/GoogleContainerTools/kpt/issues/1447
179
+ cmd := exec .CommandContext (ctx , "kustomize" , append ([]string {"build" }, kustomizeBuildArgs (g .config .Kustomize .BuildArgs , kPath )... )... )
180
+ out , err := util .RunCmdOut (ctx , cmd )
181
+ if err != nil {
182
+ return nil , err
183
+ }
184
+ if len (out ) == 0 {
185
+ continue
186
+ }
187
+ manifests = append (manifests , out )
188
+ }
189
+
190
+ return manifests , nil
191
+ }
192
+
177
193
// isKustomizeDir checks if the path is managed by kustomize. A more reliable approach is parsing the kustomize content
178
194
// resources, bases, overlays. However, this switches the manifests parsing from kustomize/kpt to skaffold. To avoid
179
195
// skaffold render.generate mis-use, we expect the users do not place non-kustomize manifests under the kustomization.yaml directory, so as the kpt manifests.
@@ -200,6 +216,24 @@ func isKustomizeDir(path string) (string, bool) {
200
216
return "" , false
201
217
}
202
218
219
+ // kustomizeBuildArgs returns a list of build args to be passed to kustomize build.
220
+ func kustomizeBuildArgs (buildArgs []string , kustomizePath string ) []string {
221
+ var args []string
222
+
223
+ if len (buildArgs ) > 0 {
224
+ for _ , v := range buildArgs {
225
+ parts := strings .Split (v , " " )
226
+ args = append (args , parts ... )
227
+ }
228
+ }
229
+
230
+ if len (kustomizePath ) > 0 {
231
+ args = append (args , kustomizePath )
232
+ }
233
+
234
+ return args
235
+ }
236
+
203
237
func isKptDir (path string ) (string , bool ) {
204
238
fileInfo , err := os .Stat (path )
205
239
if err != nil {
@@ -220,16 +254,19 @@ func isKptDir(path string) (string, bool) {
220
254
221
255
// walkManifests finds out all the manifests from the `.manifests.generate`, so they can be registered in the file watcher.
222
256
// Note: the logic about manifest dependencies shall separate from the "Generate" function, which requires "context" and
223
- // only be called when a renderig action is needed (normally happens after the file watcher registration).
257
+ // only be called when a rendering action is needed (normally happens after the file watcher registration).
224
258
func (g Generator ) walkManifests () ([]string , error ) {
225
259
var dependencyPaths []string
260
+
226
261
// Generate kustomize Manifests
227
- kustomizePaths , err := resolveRemoteAndLocal (g .config .Kustomize , g .workingDir )
228
- if err != nil {
229
- event .DeployInfoEvent (fmt .Errorf ("could not expand the glob kustomize manifests: %w" , err ))
230
- return nil , err
262
+ if g .config .Kustomize != nil {
263
+ kustomizePaths , err := resolveRemoteAndLocal (g .config .Kustomize .Paths , g .workingDir )
264
+ if err != nil {
265
+ event .DeployInfoEvent (fmt .Errorf ("could not expand the glob kustomize manifests: %w" , err ))
266
+ return nil , err
267
+ }
268
+ dependencyPaths = append (dependencyPaths , kustomizePaths ... )
231
269
}
232
- dependencyPaths = append (dependencyPaths , kustomizePaths ... )
233
270
234
271
// Generate in-place hydrated kpt Manifests
235
272
kptPaths , err := resolveRemoteAndLocal (g .config .Kpt , g .workingDir )
0 commit comments