Skip to content

Commit

Permalink
Merge pull request #2439 from jmcclell/kustomize-configs
Browse files Browse the repository at this point in the history
Support alternative Kustomization config filenames (#2422)
  • Loading branch information
priyawadhwa authored Jul 11, 2019
2 parents f28beec + 511c77f commit 6b8cd3f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
19 changes: 18 additions & 1 deletion pkg/skaffold/deploy/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package deploy

import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -156,7 +157,11 @@ func (k *KustomizeDeployer) Cleanup(ctx context.Context, out io.Writer) error {
func dependenciesForKustomization(dir string) ([]string, error) {
var deps []string

path := filepath.Join(dir, "kustomization.yaml")
path, err := findKustomizationConfig(dir)
if err != nil {
return nil, err
}

buf, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -217,6 +222,18 @@ func joinPaths(root string, paths []string) []string {
return list
}

// A kustomization config must be at the root of the direectory. Kustomize will
// error if more than one of these files exists so order doesn't matter.
func findKustomizationConfig(dir string) (string, error) {
candidates := []string{"kustomization.yaml", "kustomization.yml", "Kustomization"}
for _, candidate := range candidates {
if local, _ := pathExistsLocally(candidate, dir); local {
return filepath.Join(dir, candidate), nil
}
}
return "", fmt.Errorf("no Kustomization configuration found in directory: %s", dir)
}

func pathExistsLocally(filename string, workingDir string) (bool, os.FileMode) {
path := filename
if !filepath.IsAbs(filename) {
Expand Down
46 changes: 45 additions & 1 deletion pkg/skaffold/deploy/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func TestDependenciesForKustomization(t *testing.T) {
expected []string
shouldErr bool
createFiles map[string]string
configName string
}{
{
description: "resources",
Expand Down Expand Up @@ -250,11 +251,54 @@ func TestDependenciesForKustomization(t *testing.T) {
"base/app.yaml": "",
},
},
{
description: "alt config name: kustomization.yml",
yaml: `resources: [app.yaml]`,
expected: []string{"kustomization.yml", "app.yaml"},
createFiles: map[string]string{
"app.yaml": "",
},
configName: "kustomization.yml",
},
{
description: "alt config name: Kustomization",
yaml: `resources: [app.yaml]`,
expected: []string{"Kustomization", "app.yaml"},
createFiles: map[string]string{
"app.yaml": "",
},
configName: "Kustomization",
},
{
description: "mixture of config names",
yaml: `resources: [app.yaml, base1, base2]`,
expected: []string{"Kustomization", "app.yaml", "base1/kustomization.yml", "base1/app.yaml", "base2/Kustomization", "base2/app.yaml"},
createFiles: map[string]string{
"app.yaml": "",
"base1/kustomization.yml": `resources: [app.yaml]`,
"base1/app.yaml": "",
"base2/Kustomization": `resources: [app.yaml]`,
"base2/app.yaml": "",
},
configName: "Kustomization",
},
{
description: "no kustomization config",
yaml: `resources: [foo]`,
shouldErr: true,
createFiles: map[string]string{
"foo/invalid-config-name": "",
},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
if test.configName == "" {
test.configName = "kustomization.yaml"
}

tmpDir := t.NewTempDir().
Write("kustomization.yaml", test.yaml)
Write(test.configName, test.yaml)

for path, contents := range test.createFiles {
tmpDir.Write(path, contents)
Expand Down

0 comments on commit 6b8cd3f

Please sign in to comment.