Skip to content

Commit

Permalink
use a field instead of an annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Aug 19, 2021
1 parent eb3493c commit 122d708
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 38 deletions.
1 change: 1 addition & 0 deletions api/internal/plugins/fnplugin/fnplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 14 additions & 12 deletions api/internal/plugins/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion api/internal/plugins/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 2 additions & 13 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions api/resmap/resmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions api/testutils/kusttest/harnessenhanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions api/types/pluginrestrictions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 0 additions & 4 deletions kyaml/kio/kioutil/kioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 8 additions & 6 deletions kyaml/runfn/runfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}

Expand All @@ -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()
}

Expand Down

0 comments on commit 122d708

Please sign in to comment.