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{
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 = "./"

View File

@@ -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{}

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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)
}
}

View File

@@ -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",
}

View File

@@ -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 {

View File

@@ -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
`))

View File

@@ -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 {

View File

@@ -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)