Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#5010 from annasong20/expose-locali…
Browse files Browse the repository at this point in the history
…ze-dst

Expose path to `localize` destination
  • Loading branch information
k8s-ci-robot authored Jan 31, 2023
2 parents 361154d + e2aff13 commit 00b0bd8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 76 deletions.
11 changes: 6 additions & 5 deletions api/internal/localizer/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ type localizer struct {
}

// Run attempts to localize the kustomization root at target with the given localize arguments
func Run(target string, scope string, newDir string, fSys filesys.FileSystem) error {
// and returns the path to the created newDir.
func Run(target, scope, newDir string, fSys filesys.FileSystem) (string, error) {
ldr, args, err := NewLoader(target, scope, newDir, fSys)
if err != nil {
return errors.Wrap(err)
return "", errors.Wrap(err)
}
defer func() { _ = ldr.Cleanup() }()

Expand All @@ -51,7 +52,7 @@ func Run(target string, scope string, newDir string, fSys filesys.FileSystem) er
}
dst := args.NewDir.Join(toDst)
if err = fSys.MkdirAll(dst); err != nil {
return errors.WrapPrefixf(err, "unable to create directory in localize destination")
return "", errors.WrapPrefixf(err, "unable to create directory in localize destination")
}

err = (&localizer{
Expand All @@ -66,9 +67,9 @@ func Run(target string, scope string, newDir string, fSys filesys.FileSystem) er
if errCleanup != nil {
log.Printf("unable to clean localize destination: %s", errCleanup)
}
return errors.WrapPrefixf(err, "unable to localize target %q", target)
return "", errors.WrapPrefixf(err, "unable to localize target %q", target)
}
return nil
return args.NewDir.String(), nil
}

// localize localizes the root that lc is at
Expand Down
68 changes: 26 additions & 42 deletions api/internal/localizer/localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ func addFiles(t *testing.T, fSys filesys.FileSystem, parentDir string, files map
}
}

func checkRun(t *testing.T, fSys filesys.FileSystem, target, scope, dst string) {
t.Helper()

actualDst, err := Run(target, scope, dst, fSys)
require.NoError(t, err)
require.Equal(t, dst, actualDst)
}

func makeFileSystems(t *testing.T, target string, files map[string]string) (expected filesys.FileSystem, actual filesys.FileSystem) {
t.Helper()

Expand Down Expand Up @@ -161,9 +169,7 @@ func checkLocalizeInTargetSuccess(t *testing.T, files map[string]string) {
fSys := makeMemoryFs(t)
addFiles(t, fSys, "/a", files)

err := Run("/a", "/", "dst", fSys)
require.NoError(t, err)

checkRun(t, fSys, "/a", "/", "/dst")
fSysExpected := makeMemoryFs(t)
addFiles(t, fSysExpected, "/a", files)
addFiles(t, fSysExpected, "/dst/a", files)
Expand All @@ -179,9 +185,7 @@ namePrefix: my-
}
fSysExpected, fSysActual := makeFileSystems(t, "/a", kustomization)

err := Run("/a", "", "/a/b/dst", fSysActual)
require.NoError(t, err)

checkRun(t, fSysActual, "/a", "/a", "/a/b/dst")
addFiles(t, fSysExpected, "/a/b/dst", kustomization)
checkFSys(t, fSysExpected, fSysActual)
}
Expand All @@ -202,9 +206,7 @@ patches:
}
fSysExpected, fSysActual := makeFileSystems(t, "/a/b", kustomization)

err := Run("/a/b", "/", "/a/b/dst", fSysActual)
require.NoError(t, err)

checkRun(t, fSysActual, "/a/b", "/", "/a/b/dst")
addFiles(t, fSysExpected, "/a/b/dst/a/b", kustomization)
checkFSys(t, fSysExpected, fSysActual)
}
Expand Down Expand Up @@ -259,7 +261,7 @@ func TestLoadUnknownKustFields(t *testing.T) {
suffix: invalid`,
})

err := Run("/a", "", "", fSysTest)
_, err := Run("/a", "", "", fSysTest)
require.EqualError(t, err,
`unable to localize target "/a": invalid Kustomization: error unmarshaling JSON: while decoding JSON: json: unknown field "suffix"`)

Expand Down Expand Up @@ -299,9 +301,7 @@ patches:
}
expected, actual := makeFileSystems(t, "/alpha/beta/gamma", kustAndPatch)

err := Run("/alpha/beta/gamma", "/", "", actual)
require.NoError(t, err)

checkRun(t, actual, "/alpha/beta/gamma", "/", "/localized-gamma")
addFiles(t, expected, "/localized-gamma/alpha/beta/gamma", map[string]string{
"kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
Expand All @@ -328,9 +328,7 @@ kind: Kustomization
}
expected, actual := makeFileSystems(t, "/alpha/beta", targetAndUnreferenced)

err := Run("/alpha/beta", "/alpha", "/beta", actual)
require.NoError(t, err)

checkRun(t, actual, "/alpha/beta", "/alpha", "/beta")
addFiles(t, expected, "/beta/beta", map[string]string{
"kustomization.yaml": targetAndUnreferenced["kustomization.yaml"],
"env": targetAndUnreferenced["env"],
Expand Down Expand Up @@ -586,7 +584,7 @@ patches:
}
expected, actual := makeFileSystems(t, "/a/b", kustAndPatch)

err := Run("/a/b", "", "/dst", actual)
_, err := Run("/a/b", "", "/dst", actual)
require.EqualError(t, err, `unable to localize target "/a/b": unable to localize patches: invalid file reference: '/a/b/name-DNE.yaml' doesn't exist`)

checkFSys(t, expected, actual)
Expand Down Expand Up @@ -710,9 +708,7 @@ transformers:
}
expected, actual := makeFileSystems(t, "/a", kustAndPlugins)

err := Run("/a", "", "/dst", actual)
require.NoError(t, err)

checkRun(t, actual, "/a", "/a", "/dst")
addFiles(t, expected, "/dst", map[string]string{
"kustomization.yaml": kustAndPlugins["kustomization.yaml"],
"patch.yaml": fmt.Sprintf(patchf, "patchSM.yaml"),
Expand Down Expand Up @@ -971,7 +967,7 @@ metadata:
t.Run(test.name, func(t *testing.T) {
expected, actual := makeFileSystems(t, "/", test.files)

err := Run("/", "", "/dst", actual)
_, err := Run("/", "", "/dst", actual)

var actualErr ResourceLoadError
require.ErrorAs(t, err, &actualErr)
Expand Down Expand Up @@ -1025,7 +1021,7 @@ func TestLocalizeBuiltinPlugins_Errors(t *testing.T) {
} {
t.Run(name, func(t *testing.T) {
expected, actual := makeFileSystems(t, "/a", test.files)
err := Run("/a", "", "/dst", actual)
_, err := Run("/a", "", "/dst", actual)
const errPrefix = `unable to localize target "/a"`
require.EqualError(t, err, fmt.Sprintf(
"%s: %s: %s", errPrefix, test.fieldSpecErr, test.locErr))
Expand Down Expand Up @@ -1145,9 +1141,7 @@ namespace: kustomize-namespace
}
expected, actual := makeFileSystems(t, "/alpha", kustAndComponents)

err := Run("/alpha/beta/gamma", "/alpha", "/alpha/beta/dst", actual)
require.NoError(t, err)

checkRun(t, actual, "/alpha/beta/gamma", "/alpha", "/alpha/beta/dst")
cleanedFiles := map[string]string{
"beta/gamma/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
components:
Expand Down Expand Up @@ -1210,9 +1204,7 @@ namePrefix: my-
}
expected, actual := makeFileSystems(t, "/a/b", kustAndResources)

err := Run("/a/b", "/", "", actual)
require.NoError(t, err)

checkRun(t, actual, "/a/b", "/", "/localized-b")
addFiles(t, expected, "/localized-b/a/b", kustAndResources)
checkFSys(t, expected, actual)
}
Expand All @@ -1227,7 +1219,7 @@ resources:
}
expected, actual := makeFileSystems(t, "/a", kustAndResources)

err := Run("/a", "/", "", actual)
_, err := Run("/a", "/", "", actual)

const expectedFileErr = `invalid file reference: '/a/b' must resolve to a file`
const expectedRootErr = `unable to localize root "b": unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization' in directory '/a/b'`
Expand Down Expand Up @@ -1324,9 +1316,7 @@ func TestLocalizeHelmChartsNoDefault(t *testing.T) {
}
expected, actual := makeFileSystems(t, "/a", files)

err := Run("/a", "", "/dst", actual)
require.NoError(t, err)

checkRun(t, actual, "/a", "/a", "/dst")
addFiles(t, expected, "/dst", map[string]string{
"kustomization.yaml": files["kustomization.yaml"],
"home/name/values.yaml": valuesFile,
Expand Down Expand Up @@ -1440,9 +1430,7 @@ helmGlobals:
t.Run(name, func(t *testing.T) {
expected, actual := makeFileSystems(t, "/a/b", test.files)

err := Run("/a/b", "/a/b", "/dst", actual)
require.NoError(t, err)

checkRun(t, actual, "/a/b", "/a/b", "/dst")
addFiles(t, expected, "/dst", test.copiedFiles)
checkFSys(t, expected, actual)
})
Expand All @@ -1459,9 +1447,7 @@ func TestCopyChartHomeEmpty(t *testing.T) {
require.NoError(t, actual.Mkdir("/a/home"))
require.NoError(t, expected.Mkdir("/a/home"))

err := Run("/a", "", "/dst", actual)
require.NoError(t, err)

checkRun(t, actual, "/a", "/a", "/dst")
addFiles(t, expected, "/dst", kustomization)
require.NoError(t, expected.Mkdir("/dst/home"))
checkFSys(t, expected, actual)
Expand Down Expand Up @@ -1503,7 +1489,7 @@ func TestCopyChartHomeError(t *testing.T) {
t.Run(name, func(t *testing.T) {
expected, actual := makeFileSystems(t, "/", test.files)

err := Run("/a/b", "/a", "/dst", actual)
_, err := Run("/a/b", "/a", "/dst", actual)
const prefix = `unable to localize target "/a/b"`
require.EqualError(t, err, fmt.Sprintf("%s: %s", prefix, test.err))

Expand Down Expand Up @@ -1558,9 +1544,7 @@ metadata:
}
expected, actual := makeFileSystems(t, "/a", files)

err := Run("/a", "", "/dst", actual)
require.NoError(t, err)

checkRun(t, actual, "/a", "/a", "/dst")
addFiles(t, expected, "/dst", map[string]string{
"kustomization.yaml": files["kustomization.yaml"],
"configMap.yaml": files["configMap.yaml"],
Expand Down
8 changes: 5 additions & 3 deletions api/krusty/localizer/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"sigs.k8s.io/kustomize/kyaml/filesys"
)

// Run `kustomize localize`s files referenced by kustomization target in scope to destination newDir on fSys.
func Run(fSys filesys.FileSystem, target, scope, newDir string) error {
return errors.Wrap(localizer.Run(target, scope, newDir, fSys))
// Run executes `kustomize localize` on fSys given the `localize` arguments and
// returns the path to the created newDir.
func Run(fSys filesys.FileSystem, target, scope, newDir string) (string, error) {
dst, err := localizer.Run(target, scope, newDir, fSys)
return dst, errors.Wrap(err)
}
Loading

0 comments on commit 00b0bd8

Please sign in to comment.