Files
kustomize/kyaml/fn/runtime/exec/exec.go
Natasha Sarkar 1e1b9b484a exec function working dir is the kustomization that referenced it (#4125)
* exec function working dir is the kustomization that referenced it

* suggested changes

* more code review

* use a field instead of an annotation

* more code review
2021-08-19 20:15:24 -07:00

54 lines
1.2 KiB
Go

// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package exec
import (
"io"
"os"
"os/exec"
"path/filepath"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
type Filter struct {
// Path is the path to the executable to run
Path string `yaml:"path,omitempty"`
// Args are the arguments to the executable
Args []string `yaml:"args,omitempty"`
// WorkingDir is the working directory that the executable
// should run in
WorkingDir string
runtimeutil.FunctionFilter
}
func (c *Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
c.FunctionFilter.Run = c.Run
return c.FunctionFilter.Filter(nodes)
}
func (c *Filter) Run(reader io.Reader, writer io.Writer) error {
cmd := exec.Command(c.Path, c.Args...)
cmd.Stdin = reader
cmd.Stdout = writer
cmd.Stderr = os.Stderr
if c.WorkingDir != "" {
if !filepath.IsAbs(c.WorkingDir) {
return errors.Errorf(
"relative working directory %s not allowed", c.WorkingDir)
}
if c.WorkingDir == "/" {
return errors.Errorf(
"root working directory '/' not allowed")
}
cmd.Dir = c.WorkingDir
}
return cmd.Run()
}