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