add support .yml extension for kusotmization file

This commit is contained in:
Jingfang Liu
2018-10-24 10:22:39 -07:00
parent 18a2321ddd
commit ecbf3c5f51
4 changed files with 54 additions and 17 deletions

View File

@@ -22,7 +22,6 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"path"
"reflect" "reflect"
"regexp" "regexp"
"strings" "strings"
@@ -118,7 +117,7 @@ type kustomizationFile struct {
// NewKustomizationFile returns a new instance. // NewKustomizationFile returns a new instance.
func NewKustomizationFile(fSys fs.FileSystem) (*kustomizationFile, error) { // nolint func NewKustomizationFile(fSys fs.FileSystem) (*kustomizationFile, error) { // nolint
mf := &kustomizationFile{path: constants.KustomizationFileName, fSys: fSys} mf := &kustomizationFile{fSys: fSys}
err := mf.validate() err := mf.validate()
if err != nil { if err != nil {
return nil, err return nil, err
@@ -127,19 +126,16 @@ func NewKustomizationFile(fSys fs.FileSystem) (*kustomizationFile, error) { // n
} }
func (mf *kustomizationFile) validate() error { func (mf *kustomizationFile) validate() error {
if !mf.fSys.Exists(mf.path) { if mf.fSys.Exists(constants.KustomizationFileName) {
return fmt.Errorf("Missing kustomization file '%s'.\n", mf.path) mf.path = constants.KustomizationFileName
} } else if mf.fSys.Exists(constants.SecondaryKustomizationFileName) {
if mf.fSys.IsDir(mf.path) { mf.path = constants.SecondaryKustomizationFileName
mf.path = path.Join(mf.path, constants.KustomizationFileName)
if !mf.fSys.Exists(mf.path) {
return fmt.Errorf("Missing kustomization file '%s'.\n", mf.path)
}
} else { } else {
if !strings.HasSuffix(mf.path, constants.KustomizationFileName) { return fmt.Errorf("Missing kustomization file '%s'.\n", constants.KustomizationFileName)
return fmt.Errorf("Kustomization file path (%s) should have %s suffix\n", }
mf.path, constants.KustomizationFileSuffix)
} if mf.fSys.IsDir(mf.path) {
return fmt.Errorf("%s should be a file", mf.path)
} }
return nil return nil
} }

View File

@@ -21,6 +21,7 @@ import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/kustomize/pkg/types"
) )
@@ -141,6 +142,25 @@ func TestNewNotExist(t *testing.T) {
} }
} }
func TestSecondarySuffix(t *testing.T) {
kcontent := `
configMapGenerator:
- literals:
- foo=bar
- baz=qux
name: my-configmap
`
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.SecondaryKustomizationFileName, []byte(kcontent))
k, err := NewKustomizationFile(fakeFS)
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
if k.path != constants.SecondaryKustomizationFileName {
t.Fatalf("Load incorrect file path %s", k.path)
}
}
func TestPreserveComments(t *testing.T) { func TestPreserveComments(t *testing.T) {
kustomizationContentWithComments := []byte( kustomizationContentWithComments := []byte(
`# shem qing some comments `# shem qing some comments

View File

@@ -20,5 +20,12 @@ package constants
// KustomizationFileSuffix is expected suffix for KustomizationFileName. // KustomizationFileSuffix is expected suffix for KustomizationFileName.
const KustomizationFileSuffix = ".yaml" 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. // KustomizationFileName is the Well-Known File Name for a kustomize configuration file.
const KustomizationFileName = "kustomization" + KustomizationFileSuffix const KustomizationFileName = "kustomization" + KustomizationFileSuffix
// SecondaryKustomizationFileName is the secondary File Name for a kustomize configuration file when
// KustomizationFileName is not found
const SecondaryKustomizationFileName = "kustomization" + SecondaryKustomizationFileSuffix

View File

@@ -21,17 +21,18 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"sigs.k8s.io/kustomize/pkg/ifc" "strings"
"sigs.k8s.io/kustomize/pkg/resid"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/pkg/errors" "github.com/pkg/errors"
"sigs.k8s.io/kustomize/pkg/constants" "sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/ifc/transformer" "sigs.k8s.io/kustomize/pkg/ifc/transformer"
interror "sigs.k8s.io/kustomize/pkg/internal/error" interror "sigs.k8s.io/kustomize/pkg/internal/error"
patchtransformer "sigs.k8s.io/kustomize/pkg/patch/transformer" patchtransformer "sigs.k8s.io/kustomize/pkg/patch/transformer"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformers" "sigs.k8s.io/kustomize/pkg/transformers"
@@ -55,7 +56,7 @@ func NewKustTarget(
rf *resmap.Factory, rf *resmap.Factory,
ptf transformer.Factory, ptf transformer.Factory,
tcfg *config.TransformerConfig) (*KustTarget, error) { tcfg *config.TransformerConfig) (*KustTarget, error) {
content, err := ldr.Load(constants.KustomizationFileName) content, err := loadKustFile(ldr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -347,3 +348,16 @@ func (kt *KustTarget) getAllVars() ([]types.Var, error) {
} }
return result, nil return result, nil
} }
func loadKustFile(ldr ifc.Loader) ([]byte, error) {
for _, kf := range []string{constants.KustomizationFileName, constants.SecondaryKustomizationFileName} {
content, err := ldr.Load(kf)
if err == nil {
return content, nil
}
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())
}