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