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 16482c9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 24 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
5 changes: 5 additions & 0 deletions api/internal/plugins/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func (l *Loader) Config() *types.PluginConfig {
return l.pc
}

// SetWorkDir sets the working directory for this loader's plugins
func (l *Loader) SetWorkDir(wd string) {
l.pc.FnpLoadingOptions.WorkingDir = wd
}

func (l *Loader) LoadGenerators(
ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap) ([]resmap.Generator, error) {
var result []resmap.Generator
Expand Down
19 changes: 5 additions & 14 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 All @@ -41,11 +40,13 @@ func NewKustTarget(
validator ifc.Validator,
rFactory *resmap.Factory,
pLdr *loader.Loader) *KustTarget {
pLdrCopy := *pLdr
pLdrCopy.SetWorkDir(ldr.Root())
return &KustTarget{
ldr: ldr,
validator: validator,
rFactory: rFactory,
pLdr: pLdr,
pLdr: &pLdrCopy,
}
}

Expand Down Expand Up @@ -263,12 +264,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())
}

func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error {
Expand Down Expand Up @@ -305,12 +301,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())
}

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
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 16482c9

Please sign in to comment.