mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-17 09:49:13 +00:00
Load legacy kustomization fields for localize (#4918)
* Load legacy kustomization * Expose loadKustFile in kusttarget
This commit is contained in:
@@ -9,9 +9,7 @@ import (
|
|||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/internal/generators"
|
"sigs.k8s.io/kustomize/api/internal/generators"
|
||||||
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
|
||||||
"sigs.k8s.io/kustomize/api/internal/target"
|
"sigs.k8s.io/kustomize/api/internal/target"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
|
||||||
"sigs.k8s.io/kustomize/api/loader"
|
"sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
@@ -26,17 +24,14 @@ import (
|
|||||||
type localizer struct {
|
type localizer struct {
|
||||||
fSys filesys.FileSystem
|
fSys filesys.FileSystem
|
||||||
|
|
||||||
// kusttarget fields
|
|
||||||
validator ifc.Validator
|
|
||||||
rFactory *resmap.Factory
|
|
||||||
pLdr *pLdr.Loader
|
|
||||||
|
|
||||||
// underlying type is Loader
|
// underlying type is Loader
|
||||||
ldr ifc.Loader
|
ldr ifc.Loader
|
||||||
|
|
||||||
// root is at ldr.Root()
|
// root is at ldr.Root()
|
||||||
root filesys.ConfirmedDir
|
root filesys.ConfirmedDir
|
||||||
|
|
||||||
|
rFactory *resmap.Factory
|
||||||
|
|
||||||
// destination directory in newDir that mirrors root
|
// destination directory in newDir that mirrors root
|
||||||
dst string
|
dst string
|
||||||
}
|
}
|
||||||
@@ -58,19 +53,12 @@ func Run(target string, scope string, newDir string, fSys filesys.FileSystem) er
|
|||||||
return errors.WrapPrefixf(err, "unable to create directory in localize destination")
|
return errors.WrapPrefixf(err, "unable to create directory in localize destination")
|
||||||
}
|
}
|
||||||
|
|
||||||
depProvider := provider.NewDepProvider()
|
|
||||||
rFactory := resmap.NewFactory(depProvider.GetResourceFactory())
|
|
||||||
// As of alpha, only built-in plugins, using kustomize's built-in definitions of them,
|
|
||||||
// are potentially localized.
|
|
||||||
plgnsLdr := pLdr.NewLoader(types.DisabledPluginConfig(), rFactory, filesys.MakeFsOnDisk())
|
|
||||||
err = (&localizer{
|
err = (&localizer{
|
||||||
fSys: fSys,
|
fSys: fSys,
|
||||||
validator: depProvider.GetFieldValidator(),
|
ldr: ldr,
|
||||||
rFactory: rFactory,
|
root: args.Target,
|
||||||
pLdr: plgnsLdr,
|
rFactory: resmap.NewFactory(provider.NewDepProvider().GetResourceFactory()),
|
||||||
ldr: ldr,
|
dst: dst,
|
||||||
root: args.Target,
|
|
||||||
dst: dst,
|
|
||||||
}).localize()
|
}).localize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errCleanup := fSys.RemoveAll(args.NewDir.String())
|
errCleanup := fSys.RemoveAll(args.NewDir.String())
|
||||||
@@ -84,32 +72,53 @@ func Run(target string, scope string, newDir string, fSys filesys.FileSystem) er
|
|||||||
|
|
||||||
// localize localizes the root that lc is at
|
// localize localizes the root that lc is at
|
||||||
func (lc *localizer) localize() error {
|
func (lc *localizer) localize() error {
|
||||||
kt := target.NewKustTarget(lc.ldr, lc.validator, lc.rFactory, lc.pLdr)
|
kustomization, kustFileName, err := lc.load()
|
||||||
err := kt.Load()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
kustomization := kt.Kustomization()
|
|
||||||
err = lc.localizeNativeFields(&kustomization)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = lc.localizeBuiltinPlugins(&kustomization)
|
err = lc.localizeNativeFields(kustomization)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = lc.localizeBuiltinPlugins(kustomization)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := yaml.Marshal(&kustomization)
|
content, err := yaml.Marshal(kustomization)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WrapPrefixf(err, "unable to serialize localized kustomization file")
|
return errors.WrapPrefixf(err, "unable to serialize localized kustomization file")
|
||||||
}
|
}
|
||||||
if err = lc.fSys.WriteFile(filepath.Join(lc.dst, konfig.DefaultKustomizationFileName()), content); err != nil {
|
if err = lc.fSys.WriteFile(filepath.Join(lc.dst, kustFileName), content); err != nil {
|
||||||
return errors.WrapPrefixf(err, "unable to write localized kustomization file")
|
return errors.WrapPrefixf(err, "unable to write localized kustomization file")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// load returns the kustomization at lc.root and the file name under which it was found
|
||||||
|
func (lc *localizer) load() (*types.Kustomization, string, error) {
|
||||||
|
content, kustFileName, err := target.LoadKustFile(lc.ldr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", errors.Wrap(err)
|
||||||
|
}
|
||||||
|
content, err = types.FixKustomizationPreUnmarshalling(content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", errors.WrapPrefixf(err, "invalid kustomization")
|
||||||
|
}
|
||||||
|
var kust types.Kustomization
|
||||||
|
err = (&kust).Unmarshal(content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", errors.WrapPrefixf(err, "invalid kustomization")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Localize intentionally does not replace legacy fields to return a localized kustomization
|
||||||
|
// with as much resemblance to the original as possible.
|
||||||
|
// Localize also intentionally does not enforce fields, as localize does not wish to unnecessarily
|
||||||
|
// repeat the responsibilities of kustomize build.
|
||||||
|
|
||||||
|
return &kust, kustFileName, nil
|
||||||
|
}
|
||||||
|
|
||||||
// localizeNativeFields localizes paths on kustomize-native fields, like configMapGenerator, that kustomize has a
|
// localizeNativeFields localizes paths on kustomize-native fields, like configMapGenerator, that kustomize has a
|
||||||
// built-in understanding of. This excludes helm-related fields, such as `helmGlobals` and `helmCharts`.
|
// built-in understanding of. This excludes helm-related fields, such as `helmGlobals` and `helmCharts`.
|
||||||
func (lc *localizer) localizeNativeFields(kust *types.Kustomization) error {
|
func (lc *localizer) localizeNativeFields(kust *types.Kustomization) error {
|
||||||
@@ -163,7 +172,8 @@ func (lc *localizer) localizeNativeFields(kust *types.Kustomization) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(annasong): localize all other kustomization fields: resources, bases, crds, configurations, openapi
|
// TODO(annasong): localize all other kustomization fields: resources, bases, crds, configurations,
|
||||||
|
// openapi, configMapGenerator.env, secretGenerator.env
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,13 +283,11 @@ func (lc *localizer) localizeDir(path string) (string, error) {
|
|||||||
return "", errors.WrapPrefixf(err, "unable to create root %q in localize destination", path)
|
return "", errors.WrapPrefixf(err, "unable to create root %q in localize destination", path)
|
||||||
}
|
}
|
||||||
err = (&localizer{
|
err = (&localizer{
|
||||||
fSys: lc.fSys,
|
fSys: lc.fSys,
|
||||||
validator: lc.validator,
|
ldr: ldr,
|
||||||
rFactory: lc.rFactory,
|
root: root,
|
||||||
pLdr: lc.pLdr,
|
rFactory: lc.rFactory,
|
||||||
ldr: ldr,
|
dst: newDst,
|
||||||
root: root,
|
|
||||||
dst: newDst,
|
|
||||||
}).localize()
|
}).localize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.WrapPrefixf(err, "unable to localize root %q", path)
|
return "", errors.WrapPrefixf(err, "unable to localize root %q", path)
|
||||||
|
|||||||
@@ -50,6 +50,17 @@ func addFiles(t *testing.T, fSys filesys.FileSystem, parentDir string, files map
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeFileSystems(t *testing.T, target string, files map[string]string) (expected filesys.FileSystem, actual filesys.FileSystem) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
copies := make([]filesys.FileSystem, 2)
|
||||||
|
for i := range copies {
|
||||||
|
copies[i] = makeMemoryFs(t)
|
||||||
|
addFiles(t, copies[i], target, files)
|
||||||
|
}
|
||||||
|
return copies[0], copies[1]
|
||||||
|
}
|
||||||
|
|
||||||
func checkFSys(t *testing.T, fSysExpected filesys.FileSystem, fSysActual filesys.FileSystem) {
|
func checkFSys(t *testing.T, fSysExpected filesys.FileSystem, fSysActual filesys.FileSystem) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
@@ -95,25 +106,22 @@ func reportFSysDiff(t *testing.T, fSysExpected filesys.FileSystem, fSysActual fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTargetIsScope(t *testing.T) {
|
func TestTargetIsScope(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustomization := map[string]string{
|
kustomization := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
namePrefix: my-
|
namePrefix: my-
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a", kustomization)
|
fSysExpected, fSysActual := makeFileSystems(t, "/a", kustomization)
|
||||||
err := Run("/a", "", "/a/b/dst", fSys)
|
|
||||||
|
err := Run("/a", "", "/a/b/dst", fSysActual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
|
||||||
addFiles(t, fSysExpected, "/a", kustomization)
|
|
||||||
addFiles(t, fSysExpected, "/a/b/dst", kustomization)
|
addFiles(t, fSysExpected, "/a/b/dst", kustomization)
|
||||||
checkFSys(t, fSysExpected, fSys)
|
checkFSys(t, fSysExpected, fSysActual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTargetNestedInScope(t *testing.T) {
|
func TestTargetNestedInScope(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustomization := map[string]string{
|
kustomization := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -127,18 +135,16 @@ patches:
|
|||||||
labelSelector: env=dev
|
labelSelector: env=dev
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a/b", kustomization)
|
fSysExpected, fSysActual := makeFileSystems(t, "/a/b", kustomization)
|
||||||
err := Run("/a/b", "/", "/a/b/dst", fSys)
|
|
||||||
|
err := Run("/a/b", "/", "/a/b/dst", fSysActual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
|
||||||
addFiles(t, fSysExpected, "/a/b", kustomization)
|
|
||||||
addFiles(t, fSysExpected, "/a/b/dst/a/b", kustomization)
|
addFiles(t, fSysExpected, "/a/b/dst/a/b", kustomization)
|
||||||
checkFSys(t, fSysExpected, fSys)
|
checkFSys(t, fSysExpected, fSysActual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeKustomizationName(t *testing.T) {
|
func TestLoadKustomizationName(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustomization := map[string]string{
|
kustomization := map[string]string{
|
||||||
"Kustomization": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"Kustomization": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
commonLabels:
|
commonLabels:
|
||||||
@@ -147,17 +153,83 @@ commonLabels:
|
|||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a", kustomization)
|
fSysExpected, fSysActual := makeFileSystems(t, "/a", kustomization)
|
||||||
|
|
||||||
err := Run("/a", "/", "/dst", fSys)
|
err := Run("/a", "/", "/dst", fSysActual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, fSysExpected, "/dst/a", kustomization)
|
||||||
addFiles(t, fSysExpected, "/a", kustomization)
|
checkFSys(t, fSysExpected, fSysActual)
|
||||||
addFiles(t, fSysExpected, "/dst/a", map[string]string{
|
}
|
||||||
"kustomization.yaml": kustomization["Kustomization"],
|
|
||||||
|
func TestLoadGVKNN(t *testing.T) {
|
||||||
|
for name, kustomization := range map[string]string{
|
||||||
|
"missing": `namePrefix: my-
|
||||||
|
`,
|
||||||
|
"wrong": `kind: NotChecked
|
||||||
|
`,
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
files := map[string]string{
|
||||||
|
"kustomization.yaml": kustomization,
|
||||||
|
}
|
||||||
|
fSysExpected, fSysActual := makeFileSystems(t, "/a", files)
|
||||||
|
|
||||||
|
err := Run("/a", "/a", "/dst", fSysActual)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
addFiles(t, fSysExpected, "/dst", files)
|
||||||
|
checkFSys(t, fSysExpected, fSysActual)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadLegacyFields(t *testing.T) {
|
||||||
|
// TODO(annasong): add referenced files when implement legacy field localization
|
||||||
|
kustomization := map[string]string{
|
||||||
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
|
bases:
|
||||||
|
- beta
|
||||||
|
configMapGenerator:
|
||||||
|
- env: env.properties
|
||||||
|
imageTags:
|
||||||
|
- name: postgres
|
||||||
|
newName: my-registry/my-postgres
|
||||||
|
newTag: v1
|
||||||
|
kind: Kustomization
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
fSysExpected, fSysActual := makeFileSystems(t, "/alpha", kustomization)
|
||||||
|
|
||||||
|
err := Run("/alpha", "/alpha", "/beta", fSysActual)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
addFiles(t, fSysExpected, "/beta", map[string]string{
|
||||||
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
|
bases:
|
||||||
|
- beta
|
||||||
|
configMapGenerator:
|
||||||
|
- env: env.properties
|
||||||
|
images:
|
||||||
|
- name: postgres
|
||||||
|
newName: my-registry/my-postgres
|
||||||
|
newTag: v1
|
||||||
|
kind: Kustomization
|
||||||
|
`,
|
||||||
})
|
})
|
||||||
checkFSys(t, fSysExpected, fSys)
|
checkFSys(t, fSysExpected, fSysActual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadUnknownKustFields(t *testing.T) {
|
||||||
|
fSysExpected, fSysTest := makeFileSystems(t, "/a", map[string]string{
|
||||||
|
"kustomization.yaml": `namePrefix: valid
|
||||||
|
suffix: invalid`,
|
||||||
|
})
|
||||||
|
|
||||||
|
err := Run("/a", "", "", fSysTest)
|
||||||
|
require.EqualError(t, err, `unable to localize target "/a": invalid kustomization: json: unknown field "suffix"`)
|
||||||
|
|
||||||
|
checkFSys(t, fSysExpected, fSysTest)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeFileName(t *testing.T) {
|
func TestLocalizeFileName(t *testing.T) {
|
||||||
@@ -169,7 +241,6 @@ func TestLocalizeFileName(t *testing.T) {
|
|||||||
"kustomization_name": "a/kustomization.yaml",
|
"kustomization_name": "a/kustomization.yaml",
|
||||||
} {
|
} {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndPatch := map[string]string{
|
kustAndPatch := map[string]string{
|
||||||
"kustomization.yaml": fmt.Sprintf(`apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": fmt.Sprintf(`apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -178,21 +249,18 @@ patches:
|
|||||||
`, path),
|
`, path),
|
||||||
path: podConfiguration,
|
path: podConfiguration,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a", kustAndPatch)
|
expected, actual := makeFileSystems(t, "/a", kustAndPatch)
|
||||||
|
|
||||||
err := Run("/a", "/", "/a/dst", fSys)
|
err := Run("/a", "/", "/a/dst", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/a/dst/a", kustAndPatch)
|
||||||
addFiles(t, fSysExpected, "/a", kustAndPatch)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/a/dst/a", kustAndPatch)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeFileCleaned(t *testing.T) {
|
func TestLocalizeFileCleaned(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndPatch := map[string]string{
|
kustAndPatch := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -201,14 +269,12 @@ patches:
|
|||||||
`,
|
`,
|
||||||
"patch.yaml": podConfiguration,
|
"patch.yaml": podConfiguration,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/alpha/beta/gamma", kustAndPatch)
|
expected, actual := makeFileSystems(t, "/alpha/beta/gamma", kustAndPatch)
|
||||||
|
|
||||||
err := Run("/alpha/beta/gamma", "/", "", fSys)
|
err := Run("/alpha/beta/gamma", "/", "", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/localized-gamma/alpha/beta/gamma", map[string]string{
|
||||||
addFiles(t, fSysExpected, "/alpha/beta/gamma", kustAndPatch)
|
|
||||||
addFiles(t, fSysExpected, "/localized-gamma/alpha/beta/gamma", map[string]string{
|
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
patches:
|
patches:
|
||||||
@@ -216,11 +282,10 @@ patches:
|
|||||||
`,
|
`,
|
||||||
"patch.yaml": podConfiguration,
|
"patch.yaml": podConfiguration,
|
||||||
})
|
})
|
||||||
checkFSys(t, fSysExpected, fSys)
|
checkFSys(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeUnreferencedIgnored(t *testing.T) {
|
func TestLocalizeUnreferencedIgnored(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
targetAndUnreferenced := map[string]string{
|
targetAndUnreferenced := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
configMapGenerator:
|
configMapGenerator:
|
||||||
@@ -233,22 +298,19 @@ kind: Kustomization
|
|||||||
"env.properties": "USERNAME=password",
|
"env.properties": "USERNAME=password",
|
||||||
"resource.yaml": podConfiguration,
|
"resource.yaml": podConfiguration,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/alpha/beta", targetAndUnreferenced)
|
expected, actual := makeFileSystems(t, "/alpha/beta", targetAndUnreferenced)
|
||||||
|
|
||||||
err := Run("/alpha/beta", "/alpha", "/beta", fSys)
|
err := Run("/alpha/beta", "/alpha", "/beta", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/beta/beta", map[string]string{
|
||||||
addFiles(t, fSysExpected, "/alpha/beta", targetAndUnreferenced)
|
|
||||||
addFiles(t, fSysExpected, "/beta/beta", map[string]string{
|
|
||||||
"kustomization.yaml": targetAndUnreferenced["kustomization.yaml"],
|
"kustomization.yaml": targetAndUnreferenced["kustomization.yaml"],
|
||||||
"env": targetAndUnreferenced["env"],
|
"env": targetAndUnreferenced["env"],
|
||||||
})
|
})
|
||||||
checkFSys(t, fSysExpected, fSys)
|
checkFSys(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizePatches(t *testing.T) {
|
func TestLocalizePatches(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndPatch := map[string]string{
|
kustAndPatch := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -278,19 +340,16 @@ spec:
|
|||||||
image: nginx:1.21.0
|
image: nginx:1.21.0
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/", kustAndPatch)
|
expected, actual := makeFileSystems(t, "/", kustAndPatch)
|
||||||
|
|
||||||
err := Run("/", "", "", fSys)
|
err := Run("/", "", "", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/localized", kustAndPatch)
|
||||||
addFiles(t, fSysExpected, "/", kustAndPatch)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/localized", kustAndPatch)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizePatchesJson(t *testing.T) {
|
func TestLocalizePatchesJson(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndPatches := map[string]string{
|
kustAndPatches := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -319,19 +378,16 @@ patchesJson6902:
|
|||||||
{"op": "remove", "path": "/some/existing/path"},
|
{"op": "remove", "path": "/some/existing/path"},
|
||||||
]`,
|
]`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/alpha/beta", kustAndPatches)
|
expected, actual := makeFileSystems(t, "/alpha/beta", kustAndPatches)
|
||||||
|
|
||||||
err := Run("/alpha/beta", "/", "/beta", fSys)
|
err := Run("/alpha/beta", "/", "/beta", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/beta/alpha/beta", kustAndPatches)
|
||||||
addFiles(t, fSysExpected, "/alpha/beta", kustAndPatches)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/beta/alpha/beta", kustAndPatches)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizePatchesSM(t *testing.T) {
|
func TestLocalizePatchesSM(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndPatches := map[string]string{
|
kustAndPatches := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -347,19 +403,16 @@ patchesStrategicMerge:
|
|||||||
`,
|
`,
|
||||||
"patch.yaml": podConfiguration,
|
"patch.yaml": podConfiguration,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a", kustAndPatches)
|
expected, actual := makeFileSystems(t, "/a", kustAndPatches)
|
||||||
|
|
||||||
err := Run("/a", "", "/dst", fSys)
|
err := Run("/a", "", "/dst", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/dst", kustAndPatches)
|
||||||
addFiles(t, fSysExpected, "/a", kustAndPatches)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/dst", kustAndPatches)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeReplacements(t *testing.T) {
|
func TestLocalizeReplacements(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndReplacement := map[string]string{
|
kustAndReplacement := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -389,19 +442,16 @@ targets:
|
|||||||
select:
|
select:
|
||||||
namespace: my`,
|
namespace: my`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a", kustAndReplacement)
|
expected, actual := makeFileSystems(t, "/a", kustAndReplacement)
|
||||||
|
|
||||||
err := Run("/a", "/", "/dst", fSys)
|
err := Run("/a", "/", "/dst", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/dst/a", kustAndReplacement)
|
||||||
addFiles(t, fSysExpected, "/a", kustAndReplacement)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/dst/a", kustAndReplacement)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeConfigMapGenerator(t *testing.T) {
|
func TestLocalizeConfigMapGenerator(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndData := map[string]string{
|
kustAndData := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
configMapGenerator:
|
configMapGenerator:
|
||||||
@@ -423,19 +473,16 @@ metadata:
|
|||||||
IS_GLOBAL=true`,
|
IS_GLOBAL=true`,
|
||||||
"key.properties": "value",
|
"key.properties": "value",
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a/b", kustAndData)
|
expected, actual := makeFileSystems(t, "/a/b", kustAndData)
|
||||||
|
|
||||||
err := Run("/a/b", "", "", fSys)
|
err := Run("/a/b", "", "", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/localized-b", kustAndData)
|
||||||
addFiles(t, fSysExpected, "/a/b", kustAndData)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/localized-b", kustAndData)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeSecretGenerator(t *testing.T) {
|
func TestLocalizeSecretGenerator(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndData := map[string]string{
|
kustAndData := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -459,19 +506,16 @@ secretGenerator:
|
|||||||
"b/value.properties": "dmFsdWU=",
|
"b/value.properties": "dmFsdWU=",
|
||||||
"b/value": "dmFsdWU=",
|
"b/value": "dmFsdWU=",
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a", kustAndData)
|
expected, actual := makeFileSystems(t, "/a", kustAndData)
|
||||||
|
|
||||||
err := Run("/a", "/", "/localized-a", fSys)
|
err := Run("/a", "/", "/localized-a", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/localized-a/a", kustAndData)
|
||||||
addFiles(t, fSysExpected, "/a", kustAndData)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/localized-a/a", kustAndData)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeFileNoFile(t *testing.T) {
|
func TestLocalizeFileNoFile(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndPatch := map[string]string{
|
kustAndPatch := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -479,18 +523,15 @@ patches:
|
|||||||
- path: name-DNE.yaml
|
- path: name-DNE.yaml
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a/b", kustAndPatch)
|
expected, actual := makeFileSystems(t, "/a/b", kustAndPatch)
|
||||||
|
|
||||||
err := Run("/a/b", "", "/dst", fSys)
|
err := Run("/a/b", "", "/dst", actual)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/a/b", kustAndPatch)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeGenerators(t *testing.T) {
|
func TestLocalizeGenerators(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndPlugins := map[string]string{
|
kustAndPlugins := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
generators:
|
generators:
|
||||||
@@ -520,21 +561,18 @@ metadata:
|
|||||||
name: map
|
name: map
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a", kustAndPlugins)
|
expected, actual := makeFileSystems(t, "/a", kustAndPlugins)
|
||||||
|
|
||||||
err := Run("/a", "", "/alpha/dst", fSys)
|
err := Run("/a", "", "/alpha/dst", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/alpha/dst", map[string]string{
|
||||||
addFiles(t, fSysExpected, "/a", kustAndPlugins)
|
|
||||||
addFiles(t, fSysExpected, "/alpha/dst", map[string]string{
|
|
||||||
"kustomization.yaml": kustAndPlugins["kustomization.yaml"],
|
"kustomization.yaml": kustAndPlugins["kustomization.yaml"],
|
||||||
})
|
})
|
||||||
checkFSys(t, fSysExpected, fSys)
|
checkFSys(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeTransformers(t *testing.T) {
|
func TestLocalizeTransformers(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndPlugins := map[string]string{
|
kustAndPlugins := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -571,21 +609,18 @@ paths:
|
|||||||
- pod.yaml
|
- pod.yaml
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/a", kustAndPlugins)
|
expected, actual := makeFileSystems(t, "/a", kustAndPlugins)
|
||||||
|
|
||||||
err := Run("/a", "", "/dst", fSys)
|
err := Run("/a", "", "/dst", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/dst", map[string]string{
|
||||||
addFiles(t, fSysExpected, "/a", kustAndPlugins)
|
|
||||||
addFiles(t, fSysExpected, "/dst", map[string]string{
|
|
||||||
"kustomization.yaml": kustAndPlugins["kustomization.yaml"],
|
"kustomization.yaml": kustAndPlugins["kustomization.yaml"],
|
||||||
})
|
})
|
||||||
checkFSys(t, fSysExpected, fSys)
|
checkFSys(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeValidators(t *testing.T) {
|
func TestLocalizeValidators(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndPlugin := map[string]string{
|
kustAndPlugin := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
@@ -622,16 +657,15 @@ replacements:
|
|||||||
namespace: test
|
namespace: test
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/", kustAndPlugin)
|
expected, actual := makeFileSystems(t, "/", kustAndPlugin)
|
||||||
err := Run("/", "", "/dst", fSys)
|
|
||||||
|
err := Run("/", "", "/dst", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/dst", map[string]string{
|
||||||
addFiles(t, fSysExpected, "/", kustAndPlugin)
|
|
||||||
addFiles(t, fSysExpected, "/dst", map[string]string{
|
|
||||||
"kustomization.yaml": kustAndPlugin["kustomization.yaml"],
|
"kustomization.yaml": kustAndPlugin["kustomization.yaml"],
|
||||||
})
|
})
|
||||||
checkFSys(t, fSysExpected, fSys)
|
checkFSys(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeBuiltinPluginsNotResource(t *testing.T) {
|
func TestLocalizeBuiltinPluginsNotResource(t *testing.T) {
|
||||||
@@ -679,9 +713,9 @@ metadata:
|
|||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
expected, actual := makeFileSystems(t, "/", test.files)
|
||||||
addFiles(t, fSys, "/", test.files)
|
|
||||||
err := Run("/", "", "/dst", fSys)
|
err := Run("/", "", "/dst", actual)
|
||||||
|
|
||||||
var actualErr ResourceLoadError
|
var actualErr ResourceLoadError
|
||||||
require.ErrorAs(t, err, &actualErr)
|
require.ErrorAs(t, err, &actualErr)
|
||||||
@@ -691,9 +725,7 @@ metadata:
|
|||||||
require.EqualError(t, err, fmt.Sprintf(`unable to localize target "/": %s: when parsing as inline received error: %s
|
require.EqualError(t, err, fmt.Sprintf(`unable to localize target "/": %s: when parsing as inline received error: %s
|
||||||
when parsing as filepath received error: %s`, test.errPrefix, test.inlineErrMsg, test.fileErrMsg))
|
when parsing as filepath received error: %s`, test.errPrefix, test.inlineErrMsg, test.fileErrMsg))
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/", test.files)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -789,22 +821,18 @@ namespace: kustomize-namespace
|
|||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
expected, actual := makeFileSystems(t, "/alpha/beta/gamma", tc.files)
|
||||||
addFiles(t, fSys, "/alpha/beta/gamma", tc.files)
|
|
||||||
|
|
||||||
err := Run("/alpha/beta/gamma", "/alpha/beta", "/dst", fSys)
|
err := Run("/alpha/beta/gamma", "/alpha/beta", "/dst", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/dst/gamma", tc.files)
|
||||||
addFiles(t, fSysExpected, "/alpha/beta/gamma", tc.files)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/dst/gamma", tc.files)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeDirCleanedSibling(t *testing.T) {
|
func TestLocalizeDirCleanedSibling(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndComponents := map[string]string{
|
kustAndComponents := map[string]string{
|
||||||
// This test checks that winding paths that might traverse through directories
|
// This test checks that winding paths that might traverse through directories
|
||||||
// outside of scope, which will not be present at destination, are cleaned.
|
// outside of scope, which will not be present at destination, are cleaned.
|
||||||
@@ -817,13 +845,11 @@ kind: Component
|
|||||||
namespace: kustomize-namespace
|
namespace: kustomize-namespace
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/alpha", kustAndComponents)
|
expected, actual := makeFileSystems(t, "/alpha", kustAndComponents)
|
||||||
|
|
||||||
err := Run("/alpha/beta/gamma", "/alpha", "/alpha/beta/dst", fSys)
|
err := Run("/alpha/beta/gamma", "/alpha", "/alpha/beta/dst", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
|
||||||
addFiles(t, fSysExpected, "/alpha", kustAndComponents)
|
|
||||||
cleanedFiles := map[string]string{
|
cleanedFiles := map[string]string{
|
||||||
"beta/gamma/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"beta/gamma/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
components:
|
components:
|
||||||
@@ -832,12 +858,11 @@ kind: Kustomization
|
|||||||
`,
|
`,
|
||||||
"beta/sibling/kustomization.yaml": kustAndComponents["beta/sibling/kustomization.yaml"],
|
"beta/sibling/kustomization.yaml": kustAndComponents["beta/sibling/kustomization.yaml"],
|
||||||
}
|
}
|
||||||
addFiles(t, fSysExpected, "/alpha/beta/dst", cleanedFiles)
|
addFiles(t, expected, "/alpha/beta/dst", cleanedFiles)
|
||||||
checkFSys(t, fSysExpected, fSys)
|
checkFSys(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalizeComponents(t *testing.T) {
|
func TestLocalizeComponents(t *testing.T) {
|
||||||
fSys := makeMemoryFs(t)
|
|
||||||
kustAndComponents := map[string]string{
|
kustAndComponents := map[string]string{
|
||||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
components:
|
components:
|
||||||
@@ -854,13 +879,11 @@ kind: Component
|
|||||||
nameSuffix: -test
|
nameSuffix: -test
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
addFiles(t, fSys, "/", kustAndComponents)
|
expected, actual := makeFileSystems(t, "/", kustAndComponents)
|
||||||
|
|
||||||
err := Run("/", "", "", fSys)
|
err := Run("/", "", "", actual)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fSysExpected := makeMemoryFs(t)
|
addFiles(t, expected, "/localized", kustAndComponents)
|
||||||
addFiles(t, fSysExpected, "/", kustAndComponents)
|
checkFSys(t, expected, actual)
|
||||||
addFiles(t, fSysExpected, "/localized", kustAndComponents)
|
|
||||||
checkFSys(t, fSysExpected, fSys)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/internal/accumulator"
|
"sigs.k8s.io/kustomize/api/internal/accumulator"
|
||||||
"sigs.k8s.io/kustomize/api/internal/builtins"
|
"sigs.k8s.io/kustomize/api/internal/builtins"
|
||||||
@@ -55,7 +54,7 @@ func NewKustTarget(
|
|||||||
|
|
||||||
// Load attempts to load the target's kustomization file.
|
// Load attempts to load the target's kustomization file.
|
||||||
func (kt *KustTarget) Load() error {
|
func (kt *KustTarget) Load() error {
|
||||||
content, kustFileName, err := loadKustFile(kt.ldr)
|
content, kustFileName, err := LoadKustFile(kt.ldr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -97,7 +96,7 @@ func (kt *KustTarget) Kustomization() types.Kustomization {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadKustFile(ldr ifc.Loader) ([]byte, string, error) {
|
func LoadKustFile(ldr ifc.Loader) ([]byte, string, error) {
|
||||||
var content []byte
|
var content []byte
|
||||||
match := 0
|
match := 0
|
||||||
var kustFileName string
|
var kustFileName string
|
||||||
|
|||||||
@@ -5,12 +5,15 @@ package target_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
|
. "sigs.k8s.io/kustomize/api/internal/target"
|
||||||
|
"sigs.k8s.io/kustomize/api/loader"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
@@ -21,6 +24,44 @@ import (
|
|||||||
// KustTarget is primarily tested in the krusty package with
|
// KustTarget is primarily tested in the krusty package with
|
||||||
// high level tests.
|
// high level tests.
|
||||||
|
|
||||||
|
func TestLoadKustFile(t *testing.T) {
|
||||||
|
for name, test := range map[string]struct {
|
||||||
|
fileNames []string
|
||||||
|
kustFileName, errMsg string
|
||||||
|
}{
|
||||||
|
"missing": {
|
||||||
|
fileNames: []string{"kustomization"},
|
||||||
|
errMsg: `unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization' in directory '/'`,
|
||||||
|
},
|
||||||
|
"multiple": {
|
||||||
|
fileNames: []string{"kustomization.yaml", "Kustomization"},
|
||||||
|
errMsg: `Found multiple kustomization files under: /
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
"valid": {
|
||||||
|
fileNames: []string{"kustomization.yml", "kust"},
|
||||||
|
kustFileName: "kustomization.yml",
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
fSys := th.GetFSys()
|
||||||
|
for _, file := range test.fileNames {
|
||||||
|
require.NoError(t, fSys.WriteFile(file, []byte(fmt.Sprintf("namePrefix: test-%s", file))))
|
||||||
|
}
|
||||||
|
|
||||||
|
content, fileName, err := LoadKustFile(loader.NewFileLoaderAtCwd(fSys))
|
||||||
|
if test.kustFileName != "" {
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, fmt.Sprintf("namePrefix: test-%s", test.kustFileName), string(content))
|
||||||
|
require.Equal(t, test.kustFileName, fileName)
|
||||||
|
} else {
|
||||||
|
require.EqualError(t, err, test.errMsg)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoad(t *testing.T) {
|
func TestLoad(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
expectedTypeMeta := types.TypeMeta{
|
expectedTypeMeta := types.TypeMeta{
|
||||||
|
|||||||
Reference in New Issue
Block a user