mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 00:52:55 +00:00
Merge pull request #1589 from monopole/moreTestsAroundKustFileName
improve tests for alternative kustomization file names
This commit is contained in:
@@ -67,8 +67,9 @@ func NewCmdBuild(
|
||||
pl := plugins.NewLoader(pluginConfig, rf)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "build {path}",
|
||||
Short: "Print configuration per contents of " + pgmconfig.KustomizationFileName0,
|
||||
Use: "build {path}",
|
||||
Short: "Print configuration per contents of " +
|
||||
pgmconfig.DefaultKustomizationFileName(),
|
||||
Example: examples,
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
@@ -96,7 +97,8 @@ func NewCmdBuild(
|
||||
func (o *Options) Validate(args []string) (err error) {
|
||||
if len(args) > 1 {
|
||||
return errors.New(
|
||||
"specify one path to " + pgmconfig.KustomizationFileName0)
|
||||
"specify one path to " +
|
||||
pgmconfig.DefaultKustomizationFileName())
|
||||
}
|
||||
if len(args) == 0 {
|
||||
o.kustomizationPath = loader.CWD
|
||||
|
||||
@@ -26,7 +26,9 @@ 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 " + pgmconfig.KustomizationFileName0},
|
||||
"",
|
||||
"specify one path to " +
|
||||
pgmconfig.DefaultKustomizationFileName()},
|
||||
}
|
||||
for _, mycase := range cases {
|
||||
opts := Options{}
|
||||
|
||||
@@ -162,7 +162,7 @@ func detectResources(fSys fs.FileSystem, uf ifc.KunstructuredFactory, base strin
|
||||
}
|
||||
// If a sub-directory contains an existing kustomization file add the
|
||||
// directory as a resource and do not decend into it.
|
||||
for _, kfilename := range pgmconfig.KustomizationFileNames() {
|
||||
for _, kfilename := range pgmconfig.RecognizedKustomizationFileNames() {
|
||||
if fSys.Exists(filepath.Join(path, kfilename)) {
|
||||
paths = append(paths, path)
|
||||
return filepath.SkipDir
|
||||
|
||||
@@ -46,8 +46,9 @@ func newCmdAddAnnotation(fSys fs.FileSystem, v func(map[string]string) error) *c
|
||||
o.kind = annotation
|
||||
o.mapValidator = v
|
||||
cmd := &cobra.Command{
|
||||
Use: "annotation",
|
||||
Short: "Adds one or more commonAnnotations to " + pgmconfig.KustomizationFileName0,
|
||||
Use: "annotation",
|
||||
Short: "Adds one or more commonAnnotations to " +
|
||||
pgmconfig.DefaultKustomizationFileName(),
|
||||
Example: `
|
||||
add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
@@ -66,8 +67,9 @@ func newCmdAddLabel(fSys fs.FileSystem, v func(map[string]string) error) *cobra.
|
||||
o.kind = label
|
||||
o.mapValidator = v
|
||||
cmd := &cobra.Command{
|
||||
Use: "label",
|
||||
Short: "Adds one or more commonLabels to " + pgmconfig.KustomizationFileName0,
|
||||
Use: "label",
|
||||
Short: "Adds one or more commonLabels to " +
|
||||
pgmconfig.DefaultKustomizationFileName(),
|
||||
Example: `
|
||||
add label {labelKey1:labelValue1},{labelKey2:labelValue2}`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
@@ -47,7 +47,8 @@ func newCmdRemoveAnnotation(fSys fs.FileSystem, v func([]string) error) *cobra.C
|
||||
o.arrayValidator = v
|
||||
cmd := &cobra.Command{
|
||||
Use: "annotation",
|
||||
Short: "Removes one or more commonAnnotations from " + pgmconfig.KustomizationFileName0,
|
||||
Short: "Removes one or more commonAnnotations from " +
|
||||
pgmconfig.DefaultKustomizationFileName(),
|
||||
Example: `
|
||||
remove annotation {annotationKey1},{annotationKey2}`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
@@ -67,7 +68,8 @@ func newCmdRemoveLabel(fSys fs.FileSystem, v func([]string) error) *cobra.Comman
|
||||
o.arrayValidator = v
|
||||
cmd := &cobra.Command{
|
||||
Use: "label",
|
||||
Short: "Removes one or more commonLabels from " + pgmconfig.KustomizationFileName0,
|
||||
Short: "Removes one or more commonLabels from " +
|
||||
pgmconfig.DefaultKustomizationFileName(),
|
||||
Example: `
|
||||
remove label {labelKey1},{labelKey2}`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
@@ -25,7 +25,8 @@ func newCmdRemovePatch(fsys fs.FileSystem) *cobra.Command {
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "patch",
|
||||
Short: "Removes one or more patches from " + pgmconfig.KustomizationFileName0,
|
||||
Short: "Removes one or more patches from " +
|
||||
pgmconfig.DefaultKustomizationFileName(),
|
||||
Example: `
|
||||
remove patch {filepath}`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
@@ -23,7 +23,8 @@ func newCmdRemoveResource(fsys fs.FileSystem) *cobra.Command {
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "resource",
|
||||
Short: "Removes one or more resource file paths from " + pgmconfig.KustomizationFileName0,
|
||||
Short: "Removes one or more resource file paths from " +
|
||||
pgmconfig.DefaultKustomizationFileName(),
|
||||
Example: `
|
||||
remove resource my-resource.yml
|
||||
remove resource resource1.yml resource2.yml resource3.yml
|
||||
|
||||
@@ -119,7 +119,7 @@ func NewKustomizationFile(fSys fs.FileSystem) (*kustomizationFile, error) { // n
|
||||
func (mf *kustomizationFile) validate() error {
|
||||
match := 0
|
||||
var path []string
|
||||
for _, kfilename := range pgmconfig.KustomizationFileNames() {
|
||||
for _, kfilename := range pgmconfig.RecognizedKustomizationFileNames() {
|
||||
if mf.fSys.Exists(kfilename) {
|
||||
match += 1
|
||||
path = append(path, kfilename)
|
||||
@@ -128,7 +128,9 @@ func (mf *kustomizationFile) validate() error {
|
||||
|
||||
switch match {
|
||||
case 0:
|
||||
return fmt.Errorf("Missing kustomization file '%s'.\n", pgmconfig.KustomizationFileName0)
|
||||
return fmt.Errorf(
|
||||
"Missing kustomization file '%s'.\n",
|
||||
pgmconfig.DefaultKustomizationFileName())
|
||||
case 1:
|
||||
mf.path = path[0]
|
||||
default:
|
||||
@@ -228,7 +230,6 @@ func (mf *kustomizationFile) marshal(kustomization *types.Kustomization) ([]byte
|
||||
return content, nil
|
||||
}
|
||||
output = append(output, content...)
|
||||
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ func TestNewNotExist(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecondarySuffix(t *testing.T) {
|
||||
func TestAllKustomizationFileNames(t *testing.T) {
|
||||
kcontent := `
|
||||
configMapGenerator:
|
||||
- literals:
|
||||
@@ -105,14 +105,16 @@ configMapGenerator:
|
||||
- baz=qux
|
||||
name: my-configmap
|
||||
`
|
||||
fSys := fs.MakeFsInMemory()
|
||||
fSys.WriteFile(pgmconfig.KustomizationFileName1, []byte(kcontent))
|
||||
k, err := NewKustomizationFile(fSys)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
if k.path != pgmconfig.KustomizationFileName1 {
|
||||
t.Fatalf("Load incorrect file path %s", k.path)
|
||||
for _, n := range pgmconfig.RecognizedKustomizationFileNames() {
|
||||
fSys := fs.MakeFsInMemory()
|
||||
fSys.WriteFile(n, []byte(kcontent))
|
||||
k, err := NewKustomizationFile(fSys)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
if k.path != n {
|
||||
t.Fatalf("Load incorrect file path %s", k.path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,10 +38,10 @@ func WriteTestKustomization(fSys fs.FileSystem) {
|
||||
|
||||
// WriteTestKustomizationWith writes content to a well known file name.
|
||||
func WriteTestKustomizationWith(fSys fs.FileSystem, bytes []byte) {
|
||||
fSys.WriteFile(pgmconfig.KustomizationFileName0, bytes)
|
||||
fSys.WriteFile(pgmconfig.DefaultKustomizationFileName(), bytes)
|
||||
}
|
||||
|
||||
// ReadTestKustomization reads content from a well known file name.
|
||||
func ReadTestKustomization(fSys fs.FileSystem) ([]byte, error) {
|
||||
return fSys.ReadFile(pgmconfig.KustomizationFileName0)
|
||||
return fSys.ReadFile(pgmconfig.DefaultKustomizationFileName())
|
||||
}
|
||||
|
||||
@@ -74,7 +74,10 @@ func (th *KustTestHarness) WriteF(dir string, content string) {
|
||||
}
|
||||
|
||||
func (th *KustTestHarness) WriteK(dir string, content string) {
|
||||
th.WriteF(filepath.Join(dir, pgmconfig.KustomizationFileName0), `
|
||||
th.WriteF(
|
||||
filepath.Join(
|
||||
dir,
|
||||
pgmconfig.DefaultKustomizationFileName()), `
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
`+content)
|
||||
|
||||
@@ -389,7 +389,8 @@ func TestNewLoaderAtGitClone(t *testing.T) {
|
||||
fSys.MkdirAll(coRoot)
|
||||
fSys.MkdirAll(coRoot + "/" + pathInRepo)
|
||||
fSys.WriteFile(
|
||||
coRoot+"/"+pathInRepo+"/"+pgmconfig.KustomizationFileName0,
|
||||
coRoot+"/"+pathInRepo+"/"+
|
||||
pgmconfig.DefaultKustomizationFileName(),
|
||||
[]byte(`
|
||||
whatever
|
||||
`))
|
||||
|
||||
@@ -4,22 +4,23 @@
|
||||
// Package pgmconfig holds global constants for the kustomize tool.
|
||||
package pgmconfig
|
||||
|
||||
// KustomizationFileNames is a list of filenames
|
||||
// RecognizedKustomizationFileNames is a list of file names
|
||||
// that kustomize recognizes.
|
||||
// To avoid ambiguity, a directory cannot contain
|
||||
// more than one match to this list.
|
||||
func KustomizationFileNames() []string {
|
||||
// To avoid ambiguity, a kustomization directory may not
|
||||
// contain more than one match to this list.
|
||||
func RecognizedKustomizationFileNames() []string {
|
||||
return []string{
|
||||
KustomizationFileName0,
|
||||
KustomizationFileName1,
|
||||
KustomizationFileName2}
|
||||
"kustomization.yaml",
|
||||
"kustomization.yml",
|
||||
"Kustomization",
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultKustomizationFileName() string {
|
||||
return RecognizedKustomizationFileNames()[0]
|
||||
}
|
||||
|
||||
const (
|
||||
KustomizationFileName0 = "kustomization.yaml"
|
||||
KustomizationFileName1 = "kustomization.yml"
|
||||
KustomizationFileName2 = "Kustomization"
|
||||
|
||||
// An environment variable to consult for kustomization
|
||||
// configuration data. See:
|
||||
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"testing"
|
||||
|
||||
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
|
||||
"sigs.k8s.io/kustomize/v3/pkg/plugins/testenv"
|
||||
"sigs.k8s.io/kustomize/v3/pkg/plugins/testenv"
|
||||
)
|
||||
|
||||
// This is an example of using a helm chart as a base,
|
||||
|
||||
@@ -81,7 +81,7 @@ func commaOr(q []string) string {
|
||||
func loadKustFile(ldr ifc.Loader) ([]byte, error) {
|
||||
var content []byte
|
||||
match := 0
|
||||
for _, kf := range pgmconfig.KustomizationFileNames() {
|
||||
for _, kf := range pgmconfig.RecognizedKustomizationFileNames() {
|
||||
c, err := ldr.Load(kf)
|
||||
if err == nil {
|
||||
match += 1
|
||||
@@ -92,7 +92,7 @@ func loadKustFile(ldr ifc.Loader) ([]byte, error) {
|
||||
case 0:
|
||||
return nil, fmt.Errorf(
|
||||
"unable to find one of %v in directory '%s'",
|
||||
commaOr(quoted(pgmconfig.KustomizationFileNames())),
|
||||
commaOr(quoted(pgmconfig.RecognizedKustomizationFileNames())),
|
||||
ldr.Root())
|
||||
case 1:
|
||||
return content, nil
|
||||
|
||||
Reference in New Issue
Block a user