mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +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/internal/generators"
|
||||
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
|
||||
"sigs.k8s.io/kustomize/api/internal/target"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/loader"
|
||||
"sigs.k8s.io/kustomize/api/provider"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
@@ -26,17 +24,14 @@ import (
|
||||
type localizer struct {
|
||||
fSys filesys.FileSystem
|
||||
|
||||
// kusttarget fields
|
||||
validator ifc.Validator
|
||||
rFactory *resmap.Factory
|
||||
pLdr *pLdr.Loader
|
||||
|
||||
// underlying type is Loader
|
||||
ldr ifc.Loader
|
||||
|
||||
// root is at ldr.Root()
|
||||
root filesys.ConfirmedDir
|
||||
|
||||
rFactory *resmap.Factory
|
||||
|
||||
// destination directory in newDir that mirrors root
|
||||
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")
|
||||
}
|
||||
|
||||
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{
|
||||
fSys: fSys,
|
||||
validator: depProvider.GetFieldValidator(),
|
||||
rFactory: rFactory,
|
||||
pLdr: plgnsLdr,
|
||||
ldr: ldr,
|
||||
root: args.Target,
|
||||
dst: dst,
|
||||
fSys: fSys,
|
||||
ldr: ldr,
|
||||
root: args.Target,
|
||||
rFactory: resmap.NewFactory(provider.NewDepProvider().GetResourceFactory()),
|
||||
dst: dst,
|
||||
}).localize()
|
||||
if err != nil {
|
||||
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
|
||||
func (lc *localizer) localize() error {
|
||||
kt := target.NewKustTarget(lc.ldr, lc.validator, lc.rFactory, lc.pLdr)
|
||||
err := kt.Load()
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
kustomization := kt.Kustomization()
|
||||
err = lc.localizeNativeFields(&kustomization)
|
||||
kustomization, kustFileName, err := lc.load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = lc.localizeBuiltinPlugins(&kustomization)
|
||||
err = lc.localizeNativeFields(kustomization)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = lc.localizeBuiltinPlugins(kustomization)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
content, err := yaml.Marshal(&kustomization)
|
||||
content, err := yaml.Marshal(kustomization)
|
||||
if err != nil {
|
||||
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 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
|
||||
// built-in understanding of. This excludes helm-related fields, such as `helmGlobals` and `helmCharts`.
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
err = (&localizer{
|
||||
fSys: lc.fSys,
|
||||
validator: lc.validator,
|
||||
rFactory: lc.rFactory,
|
||||
pLdr: lc.pLdr,
|
||||
ldr: ldr,
|
||||
root: root,
|
||||
dst: newDst,
|
||||
fSys: lc.fSys,
|
||||
ldr: ldr,
|
||||
root: root,
|
||||
rFactory: lc.rFactory,
|
||||
dst: newDst,
|
||||
}).localize()
|
||||
if err != nil {
|
||||
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) {
|
||||
t.Helper()
|
||||
|
||||
@@ -95,25 +106,22 @@ func reportFSysDiff(t *testing.T, fSysExpected filesys.FileSystem, fSysActual fi
|
||||
}
|
||||
|
||||
func TestTargetIsScope(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustomization := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
namePrefix: my-
|
||||
`,
|
||||
}
|
||||
addFiles(t, fSys, "/a", kustomization)
|
||||
err := Run("/a", "", "/a/b/dst", fSys)
|
||||
fSysExpected, fSysActual := makeFileSystems(t, "/a", kustomization)
|
||||
|
||||
err := Run("/a", "", "/a/b/dst", fSysActual)
|
||||
require.NoError(t, err)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/a", kustomization)
|
||||
addFiles(t, fSysExpected, "/a/b/dst", kustomization)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
checkFSys(t, fSysExpected, fSysActual)
|
||||
}
|
||||
|
||||
func TestTargetNestedInScope(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustomization := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -127,18 +135,16 @@ patches:
|
||||
labelSelector: env=dev
|
||||
`,
|
||||
}
|
||||
addFiles(t, fSys, "/a/b", kustomization)
|
||||
err := Run("/a/b", "/", "/a/b/dst", fSys)
|
||||
fSysExpected, fSysActual := makeFileSystems(t, "/a/b", kustomization)
|
||||
|
||||
err := Run("/a/b", "/", "/a/b/dst", fSysActual)
|
||||
require.NoError(t, err)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/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) {
|
||||
fSys := makeMemoryFs(t)
|
||||
func TestLoadKustomizationName(t *testing.T) {
|
||||
kustomization := map[string]string{
|
||||
"Kustomization": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
commonLabels:
|
||||
@@ -147,17 +153,83 @@ commonLabels:
|
||||
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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/a", kustomization)
|
||||
addFiles(t, fSysExpected, "/dst/a", map[string]string{
|
||||
"kustomization.yaml": kustomization["Kustomization"],
|
||||
addFiles(t, fSysExpected, "/dst/a", kustomization)
|
||||
checkFSys(t, fSysExpected, fSysActual)
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -169,7 +241,6 @@ func TestLocalizeFileName(t *testing.T) {
|
||||
"kustomization_name": "a/kustomization.yaml",
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndPatch := map[string]string{
|
||||
"kustomization.yaml": fmt.Sprintf(`apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -178,21 +249,18 @@ patches:
|
||||
`, path),
|
||||
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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/a", kustAndPatch)
|
||||
addFiles(t, fSysExpected, "/a/dst/a", kustAndPatch)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
addFiles(t, expected, "/a/dst/a", kustAndPatch)
|
||||
checkFSys(t, expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalizeFileCleaned(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndPatch := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -201,14 +269,12 @@ patches:
|
||||
`,
|
||||
"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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/alpha/beta/gamma", kustAndPatch)
|
||||
addFiles(t, fSysExpected, "/localized-gamma/alpha/beta/gamma", map[string]string{
|
||||
addFiles(t, expected, "/localized-gamma/alpha/beta/gamma", map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
patches:
|
||||
@@ -216,11 +282,10 @@ patches:
|
||||
`,
|
||||
"patch.yaml": podConfiguration,
|
||||
})
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizeUnreferencedIgnored(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
targetAndUnreferenced := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
configMapGenerator:
|
||||
@@ -233,22 +298,19 @@ kind: Kustomization
|
||||
"env.properties": "USERNAME=password",
|
||||
"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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/alpha/beta", targetAndUnreferenced)
|
||||
addFiles(t, fSysExpected, "/beta/beta", map[string]string{
|
||||
addFiles(t, expected, "/beta/beta", map[string]string{
|
||||
"kustomization.yaml": targetAndUnreferenced["kustomization.yaml"],
|
||||
"env": targetAndUnreferenced["env"],
|
||||
})
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizePatches(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndPatch := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -278,19 +340,16 @@ spec:
|
||||
image: nginx:1.21.0
|
||||
`,
|
||||
}
|
||||
addFiles(t, fSys, "/", kustAndPatch)
|
||||
expected, actual := makeFileSystems(t, "/", kustAndPatch)
|
||||
|
||||
err := Run("/", "", "", fSys)
|
||||
err := Run("/", "", "", actual)
|
||||
require.NoError(t, err)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/", kustAndPatch)
|
||||
addFiles(t, fSysExpected, "/localized", kustAndPatch)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
addFiles(t, expected, "/localized", kustAndPatch)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizePatchesJson(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndPatches := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -319,19 +378,16 @@ patchesJson6902:
|
||||
{"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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/alpha/beta", kustAndPatches)
|
||||
addFiles(t, fSysExpected, "/beta/alpha/beta", kustAndPatches)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
addFiles(t, expected, "/beta/alpha/beta", kustAndPatches)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizePatchesSM(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndPatches := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -347,19 +403,16 @@ patchesStrategicMerge:
|
||||
`,
|
||||
"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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/a", kustAndPatches)
|
||||
addFiles(t, fSysExpected, "/dst", kustAndPatches)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
addFiles(t, expected, "/dst", kustAndPatches)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizeReplacements(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndReplacement := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -389,19 +442,16 @@ targets:
|
||||
select:
|
||||
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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/a", kustAndReplacement)
|
||||
addFiles(t, fSysExpected, "/dst/a", kustAndReplacement)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
addFiles(t, expected, "/dst/a", kustAndReplacement)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizeConfigMapGenerator(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndData := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
configMapGenerator:
|
||||
@@ -423,19 +473,16 @@ metadata:
|
||||
IS_GLOBAL=true`,
|
||||
"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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/a/b", kustAndData)
|
||||
addFiles(t, fSysExpected, "/localized-b", kustAndData)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
addFiles(t, expected, "/localized-b", kustAndData)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizeSecretGenerator(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndData := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -459,19 +506,16 @@ secretGenerator:
|
||||
"b/value.properties": "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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/a", kustAndData)
|
||||
addFiles(t, fSysExpected, "/localized-a/a", kustAndData)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
addFiles(t, expected, "/localized-a/a", kustAndData)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizeFileNoFile(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndPatch := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -479,18 +523,15 @@ patches:
|
||||
- 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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/a/b", kustAndPatch)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizeGenerators(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndPlugins := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
generators:
|
||||
@@ -520,21 +561,18 @@ metadata:
|
||||
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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/a", kustAndPlugins)
|
||||
addFiles(t, fSysExpected, "/alpha/dst", map[string]string{
|
||||
addFiles(t, expected, "/alpha/dst", map[string]string{
|
||||
"kustomization.yaml": kustAndPlugins["kustomization.yaml"],
|
||||
})
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizeTransformers(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndPlugins := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -571,21 +609,18 @@ paths:
|
||||
- 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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/a", kustAndPlugins)
|
||||
addFiles(t, fSysExpected, "/dst", map[string]string{
|
||||
addFiles(t, expected, "/dst", map[string]string{
|
||||
"kustomization.yaml": kustAndPlugins["kustomization.yaml"],
|
||||
})
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizeValidators(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndPlugin := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
@@ -622,16 +657,15 @@ replacements:
|
||||
namespace: test
|
||||
`,
|
||||
}
|
||||
addFiles(t, fSys, "/", kustAndPlugin)
|
||||
err := Run("/", "", "/dst", fSys)
|
||||
expected, actual := makeFileSystems(t, "/", kustAndPlugin)
|
||||
|
||||
err := Run("/", "", "/dst", actual)
|
||||
require.NoError(t, err)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/", kustAndPlugin)
|
||||
addFiles(t, fSysExpected, "/dst", map[string]string{
|
||||
addFiles(t, expected, "/dst", map[string]string{
|
||||
"kustomization.yaml": kustAndPlugin["kustomization.yaml"],
|
||||
})
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizeBuiltinPluginsNotResource(t *testing.T) {
|
||||
@@ -679,9 +713,9 @@ metadata:
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
addFiles(t, fSys, "/", test.files)
|
||||
err := Run("/", "", "/dst", fSys)
|
||||
expected, actual := makeFileSystems(t, "/", test.files)
|
||||
|
||||
err := Run("/", "", "/dst", actual)
|
||||
|
||||
var actualErr ResourceLoadError
|
||||
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
|
||||
when parsing as filepath received error: %s`, test.errPrefix, test.inlineErrMsg, test.fileErrMsg))
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/", test.files)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
checkFSys(t, expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -789,22 +821,18 @@ namespace: kustomize-namespace
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
addFiles(t, fSys, "/alpha/beta/gamma", tc.files)
|
||||
expected, actual := makeFileSystems(t, "/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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/alpha/beta/gamma", tc.files)
|
||||
addFiles(t, fSysExpected, "/dst/gamma", tc.files)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
addFiles(t, expected, "/dst/gamma", tc.files)
|
||||
checkFSys(t, expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalizeDirCleanedSibling(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndComponents := map[string]string{
|
||||
// This test checks that winding paths that might traverse through directories
|
||||
// outside of scope, which will not be present at destination, are cleaned.
|
||||
@@ -817,13 +845,11 @@ kind: Component
|
||||
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)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/alpha", kustAndComponents)
|
||||
cleanedFiles := map[string]string{
|
||||
"beta/gamma/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
components:
|
||||
@@ -832,12 +858,11 @@ kind: Kustomization
|
||||
`,
|
||||
"beta/sibling/kustomization.yaml": kustAndComponents["beta/sibling/kustomization.yaml"],
|
||||
}
|
||||
addFiles(t, fSysExpected, "/alpha/beta/dst", cleanedFiles)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
addFiles(t, expected, "/alpha/beta/dst", cleanedFiles)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestLocalizeComponents(t *testing.T) {
|
||||
fSys := makeMemoryFs(t)
|
||||
kustAndComponents := map[string]string{
|
||||
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
components:
|
||||
@@ -854,13 +879,11 @@ kind: Component
|
||||
nameSuffix: -test
|
||||
`,
|
||||
}
|
||||
addFiles(t, fSys, "/", kustAndComponents)
|
||||
expected, actual := makeFileSystems(t, "/", kustAndComponents)
|
||||
|
||||
err := Run("/", "", "", fSys)
|
||||
err := Run("/", "", "", actual)
|
||||
require.NoError(t, err)
|
||||
|
||||
fSysExpected := makeMemoryFs(t)
|
||||
addFiles(t, fSysExpected, "/", kustAndComponents)
|
||||
addFiles(t, fSysExpected, "/localized", kustAndComponents)
|
||||
checkFSys(t, fSysExpected, fSys)
|
||||
addFiles(t, expected, "/localized", kustAndComponents)
|
||||
checkFSys(t, expected, actual)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/accumulator"
|
||||
"sigs.k8s.io/kustomize/api/internal/builtins"
|
||||
@@ -55,7 +54,7 @@ func NewKustTarget(
|
||||
|
||||
// Load attempts to load the target's kustomization file.
|
||||
func (kt *KustTarget) Load() error {
|
||||
content, kustFileName, err := loadKustFile(kt.ldr)
|
||||
content, kustFileName, err := LoadKustFile(kt.ldr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -97,7 +96,7 @@ func (kt *KustTarget) Kustomization() types.Kustomization {
|
||||
return result
|
||||
}
|
||||
|
||||
func loadKustFile(ldr ifc.Loader) ([]byte, string, error) {
|
||||
func LoadKustFile(ldr ifc.Loader) ([]byte, string, error) {
|
||||
var content []byte
|
||||
match := 0
|
||||
var kustFileName string
|
||||
|
||||
@@ -5,12 +5,15 @@ package target_test
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"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/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
@@ -21,6 +24,44 @@ import (
|
||||
// KustTarget is primarily tested in the krusty package with
|
||||
// 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) {
|
||||
th := kusttest_test.MakeHarness(t)
|
||||
expectedTypeMeta := types.TypeMeta{
|
||||
|
||||
Reference in New Issue
Block a user