Skip to content

Commit b5b4fcc

Browse files
committed
Add support for kustomization resources (GoogleContainerTools#2416)
As of version 2.1, Kustomize has deprecated bases in favor of combining the functionality with resources by enforcing merge order based on resource array order. This means resources now may act as bases do today instead of being simply files - they may point to kustomiziations (directories with a kustomization config in the root) or even to remote locations. The code to generate the list of dependencies for file watching has been updated accordingly.
1 parent 2f2a6f4 commit b5b4fcc

File tree

2 files changed

+80
-14
lines changed

2 files changed

+80
-14
lines changed

pkg/skaffold/deploy/kustomize.go

+20-13
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,30 @@ func dependenciesForKustomization(dir string) ([]string, error) {
167167
return nil, err
168168
}
169169

170-
for _, base := range content.Bases {
170+
deps = append(deps, path)
171+
172+
candidates := append(content.Bases, content.Resources...)
173+
174+
for _, candidate := range candidates {
171175
// If the file doesn't exist locally, we can assume it's a remote file and
172176
// skip it, since we can't monitor remote files. Kustomize itself will
173177
// handle invalid/missing files.
174-
if !fileExistsLocally(base, dir) {
178+
local, mode := pathExistsLocally(candidate, dir)
179+
if !local {
175180
continue
176181
}
177-
baseDeps, err := dependenciesForKustomization(filepath.Join(dir, base))
178-
if err != nil {
179-
return nil, err
180-
}
181182

182-
deps = append(deps, baseDeps...)
183+
if mode.IsDir() {
184+
candidateDeps, err := dependenciesForKustomization(filepath.Join(dir, candidate))
185+
if err != nil {
186+
return nil, err
187+
}
188+
deps = append(deps, candidateDeps...)
189+
} else {
190+
deps = append(deps, filepath.Join(dir, candidate))
191+
}
183192
}
184193

185-
deps = append(deps, path)
186-
deps = append(deps, joinPaths(dir, content.Resources)...)
187194
deps = append(deps, joinPaths(dir, content.Patches)...)
188195
deps = append(deps, joinPaths(dir, content.PatchesStrategicMerge)...)
189196
deps = append(deps, joinPaths(dir, content.CRDs)...)
@@ -210,15 +217,15 @@ func joinPaths(root string, paths []string) []string {
210217
return list
211218
}
212219

213-
func fileExistsLocally(filename string, workingDir string) bool {
220+
func pathExistsLocally(filename string, workingDir string) (bool, os.FileMode) {
214221
path := filename
215222
if !filepath.IsAbs(filename) {
216223
path = filepath.Join(workingDir, filename)
217224
}
218-
if _, err := os.Stat(path); err == nil {
219-
return true
225+
if f, err := os.Stat(path); err == nil {
226+
return true, f.Mode()
220227
}
221-
return false
228+
return false, 0
222229
}
223230

224231
// Dependencies lists all the files that can change what needs to be deployed.

pkg/skaffold/deploy/kustomize_test.go

+60-1
Original file line numberDiff line numberDiff line change
@@ -167,56 +167,115 @@ func TestDependenciesForKustomization(t *testing.T) {
167167
description: "resources",
168168
yaml: `resources: [pod1.yaml, path/pod2.yaml]`,
169169
expected: []string{"kustomization.yaml", "pod1.yaml", "path/pod2.yaml"},
170+
createFiles: map[string]string{
171+
"pod1.yaml": "",
172+
"path/pod2.yaml": "",
173+
},
170174
},
171175
{
172176
description: "paches",
173177
yaml: `patches: [patch1.yaml, path/patch2.yaml]`,
174178
expected: []string{"kustomization.yaml", "patch1.yaml", "path/patch2.yaml"},
179+
createFiles: map[string]string{
180+
"patch1.yaml": "",
181+
"path/patch2.yaml": "",
182+
},
175183
},
176184
{
177185
description: "patchesStrategicMerge",
178186
yaml: `patchesStrategicMerge: [patch1.yaml, path/patch2.yaml]`,
179187
expected: []string{"kustomization.yaml", "patch1.yaml", "path/patch2.yaml"},
188+
createFiles: map[string]string{
189+
"patch1.yaml": "",
190+
"path/patch2.yaml": "",
191+
},
180192
},
181193
{
182194
description: "crds",
183195
yaml: `patches: [crd1.yaml, path/crd2.yaml]`,
184196
expected: []string{"kustomization.yaml", "crd1.yaml", "path/crd2.yaml"},
197+
createFiles: map[string]string{
198+
"crd1.yaml": "",
199+
"path/crd2.yaml": "",
200+
},
185201
},
186202
{
187203
description: "patches json 6902",
188204
yaml: `patchesJson6902:
189205
- path: patch1.json
190206
- path: path/patch2.json`,
191207
expected: []string{"kustomization.yaml", "patch1.json", "path/patch2.json"},
208+
createFiles: map[string]string{
209+
"patch1.json": "",
210+
"path/patch2.json": "",
211+
},
192212
},
193213
{
194214
description: "configMapGenerator",
195215
yaml: `configMapGenerator:
196216
- files: [app1.properties]
197217
- files: [app2.properties, app3.properties]`,
198218
expected: []string{"kustomization.yaml", "app1.properties", "app2.properties", "app3.properties"},
219+
createFiles: map[string]string{
220+
"app1.properties": "",
221+
"app2.properties": "",
222+
"app3.properties": "",
223+
},
199224
},
200225
{
201226
description: "secretGenerator",
202227
yaml: `secretGenerator:
203228
- files: [secret1.file]
204229
- files: [secret2.file, secret3.file]`,
205230
expected: []string{"kustomization.yaml", "secret1.file", "secret2.file", "secret3.file"},
231+
createFiles: map[string]string{
232+
"secrite1.file": "",
233+
"secrite2.file": "",
234+
"secrite3.file": "",
235+
},
206236
},
207237
{
208238
description: "base exists locally",
209239
yaml: `bases: [base]`,
210-
expected: []string{"base/kustomization.yaml", "base/app.yaml", "kustomization.yaml"},
240+
expected: []string{"kustomization.yaml", "base/kustomization.yaml", "base/app.yaml"},
211241
createFiles: map[string]string{
212242
"base/kustomization.yaml": `resources: [app.yaml]`,
243+
"base/app.yaml": "",
213244
},
214245
},
215246
{
216247
description: "missing base locally",
217248
yaml: `bases: [missing-or-remote-base]`,
218249
expected: []string{"kustomization.yaml"},
219250
},
251+
{
252+
description: "local kustomization resource",
253+
yaml: `resources: [app.yaml, base]`,
254+
expected: []string{"kustomization.yaml", "app.yaml", "base/kustomization.yaml", "base/app.yaml"},
255+
createFiles: map[string]string{
256+
"app.yaml": "",
257+
"base/kustomization.yaml": `resources: [app.yaml]`,
258+
"base/app.yaml": "",
259+
},
260+
},
261+
{
262+
description: "missing local kustomization resource",
263+
yaml: `resources: [app.yaml, missing-or-remote-base]`,
264+
expected: []string{"kustomization.yaml", "app.yaml"},
265+
createFiles: map[string]string{
266+
"app.yaml": "",
267+
},
268+
},
269+
{
270+
description: "mixed resource types",
271+
yaml: `resources: [app.yaml, missing-or-remote-base, base]`,
272+
expected: []string{"kustomization.yaml", "app.yaml", "base/kustomization.yaml", "base/app.yaml"},
273+
createFiles: map[string]string{
274+
"app.yaml": "",
275+
"base/kustomization.yaml": `resources: [app.yaml]`,
276+
"base/app.yaml": "",
277+
},
278+
},
220279
}
221280
for _, test := range tests {
222281
testutil.Run(t, test.description, func(t *testutil.T) {

0 commit comments

Comments
 (0)