Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for kustomization resources (#2416) #2420

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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