Skip to content

Commit 71f5809

Browse files
jmcclelldgageot
authored andcommitted
Add support for kustomization resources (#2416) (#2420)
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 6e6af45 commit 71f5809

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-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

+34-1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ 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",
@@ -207,16 +211,45 @@ func TestDependenciesForKustomization(t *testing.T) {
207211
{
208212
description: "base exists locally",
209213
yaml: `bases: [base]`,
210-
expected: []string{"base/kustomization.yaml", "base/app.yaml", "kustomization.yaml"},
214+
expected: []string{"kustomization.yaml", "base/kustomization.yaml", "base/app.yaml"},
211215
createFiles: map[string]string{
212216
"base/kustomization.yaml": `resources: [app.yaml]`,
217+
"base/app.yaml": "",
213218
},
214219
},
215220
{
216221
description: "missing base locally",
217222
yaml: `bases: [missing-or-remote-base]`,
218223
expected: []string{"kustomization.yaml"},
219224
},
225+
{
226+
description: "local kustomization resource",
227+
yaml: `resources: [app.yaml, base]`,
228+
expected: []string{"kustomization.yaml", "app.yaml", "base/kustomization.yaml", "base/app.yaml"},
229+
createFiles: map[string]string{
230+
"app.yaml": "",
231+
"base/kustomization.yaml": `resources: [app.yaml]`,
232+
"base/app.yaml": "",
233+
},
234+
},
235+
{
236+
description: "missing local kustomization resource",
237+
yaml: `resources: [app.yaml, missing-or-remote-base]`,
238+
expected: []string{"kustomization.yaml", "app.yaml"},
239+
createFiles: map[string]string{
240+
"app.yaml": "",
241+
},
242+
},
243+
{
244+
description: "mixed resource types",
245+
yaml: `resources: [app.yaml, missing-or-remote-base, base]`,
246+
expected: []string{"kustomization.yaml", "app.yaml", "base/kustomization.yaml", "base/app.yaml"},
247+
createFiles: map[string]string{
248+
"app.yaml": "",
249+
"base/kustomization.yaml": `resources: [app.yaml]`,
250+
"base/app.yaml": "",
251+
},
252+
},
220253
}
221254
for _, test := range tests {
222255
testutil.Run(t, test.description, func(t *testutil.T) {

0 commit comments

Comments
 (0)