From f7a59178a81e3888b9c3eb075debde8611d4e7bb Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Thu, 24 Jan 2019 12:26:59 -0800 Subject: [PATCH] support different filenames for kustomization file --- pkg/commands/build/build.go | 4 ++-- pkg/commands/build/build_test.go | 2 +- pkg/commands/edit/add/addmetadata.go | 4 ++-- pkg/commands/kustfile/kustomizationfile.go | 22 +++++++++++++----- .../kustfile/kustomizationfile_test.go | 4 ++-- pkg/constants/constants.go | 21 ++++++++--------- pkg/fs/fakefs.go | 4 ++-- pkg/loader/gitcloner_test.go | 2 +- pkg/target/kusttarget.go | 23 +++++++++++-------- pkg/target/utils_for_test.go | 2 +- 10 files changed, 50 insertions(+), 38 deletions(-) diff --git a/pkg/commands/build/build.go b/pkg/commands/build/build.go index 9a04da800..0c3d1e425 100644 --- a/pkg/commands/build/build.go +++ b/pkg/commands/build/build.go @@ -67,7 +67,7 @@ func NewCmdBuild( cmd := &cobra.Command{ Use: "build [path]", - Short: "Print current configuration per contents of " + constants.KustomizationFileName, + Short: "Print current configuration per contents of " + constants.KustomizationFileNames[0], Example: examples, SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { @@ -88,7 +88,7 @@ func NewCmdBuild( // Validate validates build command. func (o *BuildOptions) Validate(args []string) error { if len(args) > 1 { - return errors.New("specify one path to " + constants.KustomizationFileName) + return errors.New("specify one path to " + constants.KustomizationFileNames[0]) } if len(args) == 0 { o.kustomizationPath = "./" diff --git a/pkg/commands/build/build_test.go b/pkg/commands/build/build_test.go index e9f1d3ec8..95859e529 100644 --- a/pkg/commands/build/build_test.go +++ b/pkg/commands/build/build_test.go @@ -33,7 +33,7 @@ func TestBuildValidate(t *testing.T) { {"file", []string{"beans"}, "beans", ""}, {"path", []string{"a/b/c"}, "a/b/c", ""}, {"path", []string{"too", "many"}, - "", "specify one path to " + constants.KustomizationFileName}, + "", "specify one path to " + constants.KustomizationFileNames[0]}, } for _, mycase := range cases { opts := BuildOptions{} diff --git a/pkg/commands/edit/add/addmetadata.go b/pkg/commands/edit/add/addmetadata.go index 00daaa617..633c7566a 100644 --- a/pkg/commands/edit/add/addmetadata.go +++ b/pkg/commands/edit/add/addmetadata.go @@ -59,7 +59,7 @@ func newCmdAddAnnotation(fSys fs.FileSystem, v func(map[string]string) error) *c o.mapValidator = v cmd := &cobra.Command{ Use: "annotation", - Short: "Adds one or more commonAnnotations to " + constants.KustomizationFileName, + Short: "Adds one or more commonAnnotations to " + constants.KustomizationFileNames[0], Example: ` add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`, RunE: func(cmd *cobra.Command, args []string) error { @@ -76,7 +76,7 @@ func newCmdAddLabel(fSys fs.FileSystem, v func(map[string]string) error) *cobra. o.mapValidator = v cmd := &cobra.Command{ Use: "label", - Short: "Adds one or more commonLabels to " + constants.KustomizationFileName, + Short: "Adds one or more commonLabels to " + constants.KustomizationFileNames[0], Example: ` add label {labelKey1:labelValue1},{labelKey2:labelValue2}`, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/pkg/commands/kustfile/kustomizationfile.go b/pkg/commands/kustfile/kustomizationfile.go index 177ec604f..537a07c09 100644 --- a/pkg/commands/kustfile/kustomizationfile.go +++ b/pkg/commands/kustfile/kustomizationfile.go @@ -129,12 +129,22 @@ func NewKustomizationFile(fSys fs.FileSystem) (*kustomizationFile, error) { // n } func (mf *kustomizationFile) validate() error { - if mf.fSys.Exists(constants.KustomizationFileName) { - mf.path = constants.KustomizationFileName - } else if mf.fSys.Exists(constants.SecondaryKustomizationFileName) { - mf.path = constants.SecondaryKustomizationFileName - } else { - return fmt.Errorf("Missing kustomization file '%s'.\n", constants.KustomizationFileName) + match := 0 + var path []string + for _, kfilename := range constants.KustomizationFileNames { + if mf.fSys.Exists(kfilename) { + match += 1 + path = append(path, kfilename) + } + } + + switch match { + case 0: + return fmt.Errorf("Missing kustomization file '%s'.\n", constants.KustomizationFileNames[0]) + case 1: + mf.path = path[0] + default: + return fmt.Errorf("Found multiple kustomization file: %v\n", path) } if mf.fSys.IsDir(mf.path) { diff --git a/pkg/commands/kustfile/kustomizationfile_test.go b/pkg/commands/kustfile/kustomizationfile_test.go index b1b1cc42a..b50f68ffd 100644 --- a/pkg/commands/kustfile/kustomizationfile_test.go +++ b/pkg/commands/kustfile/kustomizationfile_test.go @@ -159,12 +159,12 @@ configMapGenerator: name: my-configmap ` fakeFS := fs.MakeFakeFS() - fakeFS.WriteFile(constants.SecondaryKustomizationFileName, []byte(kcontent)) + fakeFS.WriteFile(constants.KustomizationFileNames[1], []byte(kcontent)) k, err := NewKustomizationFile(fakeFS) if err != nil { t.Fatalf("Unexpected Error: %v", err) } - if k.path != constants.SecondaryKustomizationFileName { + if k.path != constants.KustomizationFileNames[1] { t.Fatalf("Load incorrect file path %s", k.path) } } diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index d81024afa..0938f1567 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -17,15 +17,12 @@ limitations under the License. // Package constants holds global constants for the kustomize tool. package constants -// KustomizationFileSuffix is expected suffix for KustomizationFileName. -const KustomizationFileSuffix = ".yaml" - -// SecondaryKustomizationFileSuffix is the second expected suffix when KustomizationFileSuffix is not found -const SecondaryKustomizationFileSuffix = ".yml" - -// KustomizationFileName is the Well-Known File Name for a kustomize configuration file. -const KustomizationFileName = "kustomization" + KustomizationFileSuffix - -// SecondaryKustomizationFileName is the secondary File Name for a kustomize configuration file when -// KustomizationFileName is not found -const SecondaryKustomizationFileName = "kustomization" + SecondaryKustomizationFileSuffix +// KustomizationFileNames is a list of filenames that can be recognized and consumbed +// by Kustomize. +// In each directory, Kustomize searches for file with the name in this list. +// Only one match is allowed. +var KustomizationFileNames = [3]string{ + "kustomization.yaml", + "kustomization.yml", + "Kustomization", +} diff --git a/pkg/fs/fakefs.go b/pkg/fs/fakefs.go index f38260185..edb19bc5e 100644 --- a/pkg/fs/fakefs.go +++ b/pkg/fs/fakefs.go @@ -151,7 +151,7 @@ func (fs *fakeFs) ReadFile(name string) ([]byte, error) { } func (fs *fakeFs) ReadTestKustomization() ([]byte, error) { - return fs.ReadFile(constants.KustomizationFileName) + return fs.ReadFile(constants.KustomizationFileNames[0]) } // WriteFile always succeeds and does nothing. @@ -169,7 +169,7 @@ func (fs *fakeFs) WriteTestKustomization() { // WriteTestKustomizationWith writes a standard test file. func (fs *fakeFs) WriteTestKustomizationWith(bytes []byte) { - fs.WriteFile(constants.KustomizationFileName, bytes) + fs.WriteFile(constants.KustomizationFileNames[0], bytes) } func (fs *fakeFs) pathMatch(path, pattern string) bool { diff --git a/pkg/loader/gitcloner_test.go b/pkg/loader/gitcloner_test.go index 4b0b1f386..2c5df4474 100644 --- a/pkg/loader/gitcloner_test.go +++ b/pkg/loader/gitcloner_test.go @@ -162,7 +162,7 @@ func TestGitLoader(t *testing.T) { fSys.MkdirAll(coRoot) fSys.MkdirAll(coRoot + "/" + pathInRepo) fSys.WriteFile( - coRoot+"/"+pathInRepo+"/"+constants.KustomizationFileName, + coRoot+"/"+pathInRepo+"/"+constants.KustomizationFileNames[0], []byte(` whatever `)) diff --git a/pkg/target/kusttarget.go b/pkg/target/kusttarget.go index 2dd0b2f02..3c6bcb0a1 100644 --- a/pkg/target/kusttarget.go +++ b/pkg/target/kusttarget.go @@ -82,18 +82,23 @@ func NewKustTarget( } func loadKustFile(ldr ifc.Loader) ([]byte, error) { - for _, kf := range []string{ - constants.KustomizationFileName, - constants.SecondaryKustomizationFileName} { - content, err := ldr.Load(kf) + var content []byte + match := 0 + for _, kf := range constants.KustomizationFileNames { + c, err := ldr.Load(kf) if err == nil { - return content, nil - } - if !strings.Contains(err.Error(), "no such file or directory") { - return nil, err + match += 1 + content = c } } - return nil, fmt.Errorf("no kustomization.yaml file under %s", ldr.Root()) + switch match { + case 0: + return nil, fmt.Errorf("no kustomization.yaml file under %s", ldr.Root()) + case 1: + return content, nil + default: + return nil, fmt.Errorf("Found multiple kustomization file under: %s\n", ldr.Root()) + } } func unmarshal(y []byte, o interface{}) error { diff --git a/pkg/target/utils_for_test.go b/pkg/target/utils_for_test.go index 97f17b5e3..e3541e6a7 100644 --- a/pkg/target/utils_for_test.go +++ b/pkg/target/utils_for_test.go @@ -74,7 +74,7 @@ func (th *KustTestHarness) writeF(dir string, content string) { } func (th *KustTestHarness) writeK(dir string, content string) { - th.writeF(filepath.Join(dir, constants.KustomizationFileName), ` + th.writeF(filepath.Join(dir, constants.KustomizationFileNames[0]), ` apiVersion: v1beta1 kind: Kustomization `+content)