support different filenames for kustomization file

This commit is contained in:
Jingfang Liu
2019-01-24 12:26:59 -08:00
parent 028724df08
commit f7a59178a8
10 changed files with 50 additions and 38 deletions

View File

@@ -67,7 +67,7 @@ func NewCmdBuild(
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "build [path]", Use: "build [path]",
Short: "Print current configuration per contents of " + constants.KustomizationFileName, Short: "Print current configuration per contents of " + constants.KustomizationFileNames[0],
Example: examples, Example: examples,
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@@ -88,7 +88,7 @@ func NewCmdBuild(
// Validate validates build command. // Validate validates build command.
func (o *BuildOptions) Validate(args []string) error { func (o *BuildOptions) Validate(args []string) error {
if len(args) > 1 { 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 { if len(args) == 0 {
o.kustomizationPath = "./" o.kustomizationPath = "./"

View File

@@ -33,7 +33,7 @@ func TestBuildValidate(t *testing.T) {
{"file", []string{"beans"}, "beans", ""}, {"file", []string{"beans"}, "beans", ""},
{"path", []string{"a/b/c"}, "a/b/c", ""}, {"path", []string{"a/b/c"}, "a/b/c", ""},
{"path", []string{"too", "many"}, {"path", []string{"too", "many"},
"", "specify one path to " + constants.KustomizationFileName}, "", "specify one path to " + constants.KustomizationFileNames[0]},
} }
for _, mycase := range cases { for _, mycase := range cases {
opts := BuildOptions{} opts := BuildOptions{}

View File

@@ -59,7 +59,7 @@ func newCmdAddAnnotation(fSys fs.FileSystem, v func(map[string]string) error) *c
o.mapValidator = v o.mapValidator = v
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "annotation", Use: "annotation",
Short: "Adds one or more commonAnnotations to " + constants.KustomizationFileName, Short: "Adds one or more commonAnnotations to " + constants.KustomizationFileNames[0],
Example: ` Example: `
add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`, add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`,
RunE: func(cmd *cobra.Command, args []string) error { 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 o.mapValidator = v
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "label", Use: "label",
Short: "Adds one or more commonLabels to " + constants.KustomizationFileName, Short: "Adds one or more commonLabels to " + constants.KustomizationFileNames[0],
Example: ` Example: `
add label {labelKey1:labelValue1},{labelKey2:labelValue2}`, add label {labelKey1:labelValue1},{labelKey2:labelValue2}`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {

View File

@@ -129,12 +129,22 @@ func NewKustomizationFile(fSys fs.FileSystem) (*kustomizationFile, error) { // n
} }
func (mf *kustomizationFile) validate() error { func (mf *kustomizationFile) validate() error {
if mf.fSys.Exists(constants.KustomizationFileName) { match := 0
mf.path = constants.KustomizationFileName var path []string
} else if mf.fSys.Exists(constants.SecondaryKustomizationFileName) { for _, kfilename := range constants.KustomizationFileNames {
mf.path = constants.SecondaryKustomizationFileName if mf.fSys.Exists(kfilename) {
} else { match += 1
return fmt.Errorf("Missing kustomization file '%s'.\n", constants.KustomizationFileName) 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) { if mf.fSys.IsDir(mf.path) {

View File

@@ -159,12 +159,12 @@ configMapGenerator:
name: my-configmap name: my-configmap
` `
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.SecondaryKustomizationFileName, []byte(kcontent)) fakeFS.WriteFile(constants.KustomizationFileNames[1], []byte(kcontent))
k, err := NewKustomizationFile(fakeFS) k, err := NewKustomizationFile(fakeFS)
if err != nil { if err != nil {
t.Fatalf("Unexpected Error: %v", err) 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) t.Fatalf("Load incorrect file path %s", k.path)
} }
} }

View File

@@ -17,15 +17,12 @@ limitations under the License.
// Package constants holds global constants for the kustomize tool. // Package constants holds global constants for the kustomize tool.
package constants package constants
// KustomizationFileSuffix is expected suffix for KustomizationFileName. // KustomizationFileNames is a list of filenames that can be recognized and consumbed
const KustomizationFileSuffix = ".yaml" // by Kustomize.
// In each directory, Kustomize searches for file with the name in this list.
// SecondaryKustomizationFileSuffix is the second expected suffix when KustomizationFileSuffix is not found // Only one match is allowed.
const SecondaryKustomizationFileSuffix = ".yml" var KustomizationFileNames = [3]string{
"kustomization.yaml",
// KustomizationFileName is the Well-Known File Name for a kustomize configuration file. "kustomization.yml",
const KustomizationFileName = "kustomization" + KustomizationFileSuffix "Kustomization",
}
// SecondaryKustomizationFileName is the secondary File Name for a kustomize configuration file when
// KustomizationFileName is not found
const SecondaryKustomizationFileName = "kustomization" + SecondaryKustomizationFileSuffix

View File

@@ -151,7 +151,7 @@ func (fs *fakeFs) ReadFile(name string) ([]byte, error) {
} }
func (fs *fakeFs) ReadTestKustomization() ([]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. // WriteFile always succeeds and does nothing.
@@ -169,7 +169,7 @@ func (fs *fakeFs) WriteTestKustomization() {
// WriteTestKustomizationWith writes a standard test file. // WriteTestKustomizationWith writes a standard test file.
func (fs *fakeFs) WriteTestKustomizationWith(bytes []byte) { 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 { func (fs *fakeFs) pathMatch(path, pattern string) bool {

View File

@@ -162,7 +162,7 @@ func TestGitLoader(t *testing.T) {
fSys.MkdirAll(coRoot) fSys.MkdirAll(coRoot)
fSys.MkdirAll(coRoot + "/" + pathInRepo) fSys.MkdirAll(coRoot + "/" + pathInRepo)
fSys.WriteFile( fSys.WriteFile(
coRoot+"/"+pathInRepo+"/"+constants.KustomizationFileName, coRoot+"/"+pathInRepo+"/"+constants.KustomizationFileNames[0],
[]byte(` []byte(`
whatever whatever
`)) `))

View File

@@ -82,18 +82,23 @@ func NewKustTarget(
} }
func loadKustFile(ldr ifc.Loader) ([]byte, error) { func loadKustFile(ldr ifc.Loader) ([]byte, error) {
for _, kf := range []string{ var content []byte
constants.KustomizationFileName, match := 0
constants.SecondaryKustomizationFileName} { for _, kf := range constants.KustomizationFileNames {
content, err := ldr.Load(kf) c, err := ldr.Load(kf)
if err == nil { if err == nil {
return content, nil match += 1
} content = c
if !strings.Contains(err.Error(), "no such file or directory") {
return nil, err
} }
} }
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 { func unmarshal(y []byte, o interface{}) error {

View File

@@ -74,7 +74,7 @@ func (th *KustTestHarness) writeF(dir string, content string) {
} }
func (th *KustTestHarness) writeK(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 apiVersion: v1beta1
kind: Kustomization kind: Kustomization
`+content) `+content)