diff --git a/api/internal/plugins/fnplugin/fnplugin.go b/api/internal/plugins/fnplugin/fnplugin.go index 76be4b07606..5ee53a282a3 100644 --- a/api/internal/plugins/fnplugin/fnplugin.go +++ b/api/internal/plugins/fnplugin/fnplugin.go @@ -187,6 +187,7 @@ func (p *FnPlugin) invokePlugin(input []byte) ([]byte, error) { p.runFns.Input = bytes.NewReader(input) p.runFns.Functions = append(p.runFns.Functions, functionConfig) p.runFns.Output = &ouputBuffer + p.runFns.WorkDir = p.h.WorkingDir() err = p.runFns.Execute() if err != nil { diff --git a/api/internal/plugins/loader/loader.go b/api/internal/plugins/loader/loader.go index 7bcce431522..465d942fd4f 100644 --- a/api/internal/plugins/loader/loader.go +++ b/api/internal/plugins/loader/loader.go @@ -48,10 +48,10 @@ func (l *Loader) Config() *types.PluginConfig { } func (l *Loader) LoadGenerators( - ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap) ([]resmap.Generator, error) { + ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap, workDir string) ([]resmap.Generator, error) { var result []resmap.Generator for _, res := range rm.Resources() { - g, err := l.LoadGenerator(ldr, v, res) + g, err := l.LoadGenerator(ldr, v, res, workDir) if err != nil { return nil, err } @@ -61,8 +61,8 @@ func (l *Loader) LoadGenerators( } func (l *Loader) LoadGenerator( - ldr ifc.Loader, v ifc.Validator, res *resource.Resource) (resmap.Generator, error) { - c, err := l.loadAndConfigurePlugin(ldr, v, res) + ldr ifc.Loader, v ifc.Validator, res *resource.Resource, workDir string) (resmap.Generator, error) { + c, err := l.loadAndConfigurePlugin(ldr, v, res, workDir) if err != nil { return nil, err } @@ -74,10 +74,10 @@ func (l *Loader) LoadGenerator( } func (l *Loader) LoadTransformers( - ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap) ([]resmap.Transformer, error) { + ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap, workDir string) ([]resmap.Transformer, error) { var result []resmap.Transformer for _, res := range rm.Resources() { - t, err := l.LoadTransformer(ldr, v, res) + t, err := l.LoadTransformer(ldr, v, res, workDir) if err != nil { return nil, err } @@ -87,8 +87,8 @@ func (l *Loader) LoadTransformers( } func (l *Loader) LoadTransformer( - ldr ifc.Loader, v ifc.Validator, res *resource.Resource) (resmap.Transformer, error) { - c, err := l.loadAndConfigurePlugin(ldr, v, res) + ldr ifc.Loader, v ifc.Validator, res *resource.Resource, workDir string) (resmap.Transformer, error) { + c, err := l.loadAndConfigurePlugin(ldr, v, res, workDir) if err != nil { return nil, err } @@ -158,11 +158,12 @@ func isBuiltinPlugin(res *resource.Resource) bool { func (l *Loader) loadAndConfigurePlugin( ldr ifc.Loader, v ifc.Validator, - res *resource.Resource) (c resmap.Configurable, err error) { + res *resource.Resource, + workDir string) (c resmap.Configurable, err error) { if isBuiltinPlugin(res) { switch l.pc.BpLoadingOptions { case types.BploLoadFromFileSys: - c, err = l.loadPlugin(res) + c, err = l.loadPlugin(res, workDir) case types.BploUseStaticallyLinked: // Instead of looking for and loading a .so file, // instantiate the plugin from a generated factory @@ -177,7 +178,7 @@ func (l *Loader) loadAndConfigurePlugin( } else { switch l.pc.PluginRestrictions { case types.PluginRestrictionsNone: - c, err = l.loadPlugin(res) + c, err = l.loadPlugin(res, workDir) case types.PluginRestrictionsBuiltinsOnly: err = types.NewErrOnlyBuiltinPluginsAllowed(res.OrgId().Kind) default: @@ -212,9 +213,10 @@ func (l *Loader) makeBuiltinPlugin(r resid.Gvk) (resmap.Configurable, error) { return nil, errors.Errorf("unable to load builtin %s", r) } -func (l *Loader) loadPlugin(res *resource.Resource) (resmap.Configurable, error) { +func (l *Loader) loadPlugin(res *resource.Resource, workDir string) (resmap.Configurable, error) { spec := fnplugin.GetFunctionSpec(res) if spec != nil { + l.pc.FnpLoadingOptions.WorkingDir = workDir return fnplugin.NewFnPlugin(&l.pc.FnpLoadingOptions), nil } return l.loadExecOrGoPlugin(res.OrgId()) diff --git a/api/internal/plugins/loader/loader_test.go b/api/internal/plugins/loader/loader_test.go index c5e30b808bb..363d79699c0 100644 --- a/api/internal/plugins/loader/loader_test.go +++ b/api/internal/plugins/loader/loader_test.go @@ -72,7 +72,7 @@ func TestLoader(t *testing.T) { t.Fatal("expect non-nil loader") } _, err = pLdr.LoadGenerators( - fLdr, valtest_test.MakeFakeValidator(), generatorConfigs) + fLdr, valtest_test.MakeFakeValidator(), generatorConfigs, "") if err != nil { t.Fatal(err) } diff --git a/api/internal/target/kusttarget.go b/api/internal/target/kusttarget.go index f601b42530b..6aa8e4def06 100644 --- a/api/internal/target/kusttarget.go +++ b/api/internal/target/kusttarget.go @@ -21,7 +21,6 @@ import ( "sigs.k8s.io/kustomize/api/resmap" "sigs.k8s.io/kustomize/api/resource" "sigs.k8s.io/kustomize/api/types" - "sigs.k8s.io/kustomize/kyaml/kio/kioutil" "sigs.k8s.io/kustomize/kyaml/openapi" "sigs.k8s.io/yaml" ) @@ -263,12 +262,7 @@ func (kt *KustTarget) configureExternalGenerators() ([]resmap.Generator, error) if err != nil { return nil, err } - m := ra.ResMap() - err = m.AnnotateAll(kioutil.WorkingDirAnnotation, kt.ldr.Root()) - if err != nil { - return nil, err - } - return kt.pLdr.LoadGenerators(kt.ldr, kt.validator, m) + return kt.pLdr.LoadGenerators(kt.ldr, kt.validator, ra.ResMap(), kt.ldr.Root()) } func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error { @@ -305,12 +299,7 @@ func (kt *KustTarget) configureExternalTransformers(transformers []string) ([]re if err != nil { return nil, err } - m := ra.ResMap() - err = m.AnnotateAll(kioutil.WorkingDirAnnotation, kt.ldr.Root()) - if err != nil { - return nil, err - } - return kt.pLdr.LoadTransformers(kt.ldr, kt.validator, m) + return kt.pLdr.LoadTransformers(kt.ldr, kt.validator, ra.ResMap(), kt.ldr.Root()) } func (kt *KustTarget) runValidators(ra *accumulator.ResAccumulator) error { diff --git a/api/resmap/resmap.go b/api/resmap/resmap.go index 7c0c2ea8021..8327543ae2b 100644 --- a/api/resmap/resmap.go +++ b/api/resmap/resmap.go @@ -66,6 +66,10 @@ func (c *PluginHelpers) Validator() ifc.Validator { return c.v } +func (c *PluginHelpers) WorkingDir() string { + return c.pc.FnpLoadingOptions.WorkingDir +} + type GeneratorPlugin interface { Generator Configurable diff --git a/api/testutils/kusttest/harnessenhanced.go b/api/testutils/kusttest/harnessenhanced.go index fa33f5db84c..4f1eec487af 100644 --- a/api/testutils/kusttest/harnessenhanced.go +++ b/api/testutils/kusttest/harnessenhanced.go @@ -161,7 +161,7 @@ func (th *HarnessEnhanced) LoadAndRunGeneratorWithBuildAnnotations( th.t.Fatalf("Err: %v", err) } g, err := th.pl.LoadGenerator( - th.ldr, valtest_test.MakeFakeValidator(), res) + th.ldr, valtest_test.MakeFakeValidator(), res, "") if err != nil { th.t.Fatalf("Err: %v", err) } @@ -218,7 +218,7 @@ func (th *HarnessEnhanced) RunTransformerFromResMap( th.t.Fatalf("Err: %v", err) } g, err := th.pl.LoadTransformer( - th.ldr, valtest_test.MakeFakeValidator(), transConfig) + th.ldr, valtest_test.MakeFakeValidator(), transConfig, "") if err != nil { return nil, err } diff --git a/api/types/pluginrestrictions.go b/api/types/pluginrestrictions.go index b1ab2221f69..88b03b3f5bd 100644 --- a/api/types/pluginrestrictions.go +++ b/api/types/pluginrestrictions.go @@ -57,4 +57,6 @@ type FnPluginLoadingOptions struct { Env []string // Run as uid and gid of the command executor AsCurrentUser bool + // Run in this working directory + WorkingDir string } diff --git a/kyaml/kio/kioutil/kioutil.go b/kyaml/kio/kioutil/kioutil.go index 16155e4b266..993cdfd848a 100644 --- a/kyaml/kio/kioutil/kioutil.go +++ b/kyaml/kio/kioutil/kioutil.go @@ -25,10 +25,6 @@ const ( // SeqIndentAnnotation records the sequence nodes indentation of the input resource SeqIndentAnnotation AnnotationKey = "internal.config.kubernetes.io/seqindent" - - // WorkingDirAnnotation records the directory of the kustomization that - // refers to the resource - WorkingDirAnnotation AnnotationKey = "internal.config.kubernetes.io/working-dir" ) func GetFileAnnotations(rn *yaml.RNode) (string, string, error) { diff --git a/kyaml/runfn/runfn.go b/kyaml/runfn/runfn.go index 7904bf434f1..f85788dfb9e 100644 --- a/kyaml/runfn/runfn.go +++ b/kyaml/runfn/runfn.go @@ -101,6 +101,10 @@ type RunFns struct { // If it is true, the empty result will be provided as input to the next // function in the list. ContinueOnEmptyResult bool + + // WorkDir specifies which working directory an exec function should run in. + // If this is empty, fall back to the current working directory. + WorkDir string } // Execute runs the command @@ -507,13 +511,13 @@ func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode, currentUser } if r.EnableExec && spec.Exec.Path != "" { - wd, err := getWorkingDirectory(api) + wd, err := getWorkingDirectory(r.WorkDir) if err != nil { return nil, err } ef := &exec.Filter{ - Path: spec.Exec.Path, + Path: spec.Exec.Path, WorkingDir: wd, } @@ -527,10 +531,8 @@ func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode, currentUser return nil, nil } -func getWorkingDirectory(api *yaml.RNode) (string, error) { - annotations := api.GetAnnotations() - wd, ok := annotations[kioutil.WorkingDirAnnotation] - if !ok { +func getWorkingDirectory(wd string) (string, error) { + if wd == "" { return os.Getwd() }