Skip to content

Commit

Permalink
Add support for kustomization resources (#2416) (#2420)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jmcclell authored and dgageot committed Jul 9, 2019
1 parent 6e6af45 commit 71f5809
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
33 changes: 20 additions & 13 deletions pkg/skaffold/deploy/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,30 @@ func dependenciesForKustomization(dir string) ([]string, error) {
return nil, err
}

for _, base := range content.Bases {
deps = append(deps, path)

candidates := append(content.Bases, content.Resources...)

for _, candidate := range candidates {
// If the file doesn't exist locally, we can assume it's a remote file and
// skip it, since we can't monitor remote files. Kustomize itself will
// handle invalid/missing files.
if !fileExistsLocally(base, dir) {
local, mode := pathExistsLocally(candidate, dir)
if !local {
continue
}
baseDeps, err := dependenciesForKustomization(filepath.Join(dir, base))
if err != nil {
return nil, err
}

deps = append(deps, baseDeps...)
if mode.IsDir() {
candidateDeps, err := dependenciesForKustomization(filepath.Join(dir, candidate))
if err != nil {
return nil, err
}
deps = append(deps, candidateDeps...)
} else {
deps = append(deps, filepath.Join(dir, candidate))
}
}

deps = append(deps, path)
deps = append(deps, joinPaths(dir, content.Resources)...)
deps = append(deps, joinPaths(dir, content.Patches)...)
deps = append(deps, joinPaths(dir, content.PatchesStrategicMerge)...)
deps = append(deps, joinPaths(dir, content.CRDs)...)
Expand All @@ -210,15 +217,15 @@ func joinPaths(root string, paths []string) []string {
return list
}

func fileExistsLocally(filename string, workingDir string) bool {
func pathExistsLocally(filename string, workingDir string) (bool, os.FileMode) {
path := filename
if !filepath.IsAbs(filename) {
path = filepath.Join(workingDir, filename)
}
if _, err := os.Stat(path); err == nil {
return true
if f, err := os.Stat(path); err == nil {
return true, f.Mode()
}
return false
return false, 0
}

// Dependencies lists all the files that can change what needs to be deployed.
Expand Down
35 changes: 34 additions & 1 deletion pkg/skaffold/deploy/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ func TestDependenciesForKustomization(t *testing.T) {
description: "resources",
yaml: `resources: [pod1.yaml, path/pod2.yaml]`,
expected: []string{"kustomization.yaml", "pod1.yaml", "path/pod2.yaml"},
createFiles: map[string]string{
"pod1.yaml": "",
"path/pod2.yaml": "",
},
},
{
description: "paches",
Expand Down Expand Up @@ -207,16 +211,45 @@ func TestDependenciesForKustomization(t *testing.T) {
{
description: "base exists locally",
yaml: `bases: [base]`,
expected: []string{"base/kustomization.yaml", "base/app.yaml", "kustomization.yaml"},
expected: []string{"kustomization.yaml", "base/kustomization.yaml", "base/app.yaml"},
createFiles: map[string]string{
"base/kustomization.yaml": `resources: [app.yaml]`,
"base/app.yaml": "",
},
},
{
description: "missing base locally",
yaml: `bases: [missing-or-remote-base]`,
expected: []string{"kustomization.yaml"},
},
{
description: "local kustomization resource",
yaml: `resources: [app.yaml, base]`,
expected: []string{"kustomization.yaml", "app.yaml", "base/kustomization.yaml", "base/app.yaml"},
createFiles: map[string]string{
"app.yaml": "",
"base/kustomization.yaml": `resources: [app.yaml]`,
"base/app.yaml": "",
},
},
{
description: "missing local kustomization resource",
yaml: `resources: [app.yaml, missing-or-remote-base]`,
expected: []string{"kustomization.yaml", "app.yaml"},
createFiles: map[string]string{
"app.yaml": "",
},
},
{
description: "mixed resource types",
yaml: `resources: [app.yaml, missing-or-remote-base, base]`,
expected: []string{"kustomization.yaml", "app.yaml", "base/kustomization.yaml", "base/app.yaml"},
createFiles: map[string]string{
"app.yaml": "",
"base/kustomization.yaml": `resources: [app.yaml]`,
"base/app.yaml": "",
},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
Expand Down

0 comments on commit 71f5809

Please sign in to comment.