mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Refactor container function runtime into its own package
This commit is contained in:
@@ -14,8 +14,9 @@ import (
|
||||
"sync/atomic"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/runtime/container"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/filters"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/starlark"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
@@ -24,7 +25,7 @@ import (
|
||||
// RunFns runs the set of configuration functions in a local directory against
|
||||
// the Resources in that directory
|
||||
type RunFns struct {
|
||||
StorageMounts []filters.StorageMount
|
||||
StorageMounts []runtimeutil.StorageMount
|
||||
|
||||
// Path is the path to the directory containing functions
|
||||
Path string
|
||||
@@ -75,7 +76,7 @@ type RunFns struct {
|
||||
// functionFilterProvider provides a filter to perform the function.
|
||||
// this is a variable so it can be mocked in tests
|
||||
functionFilterProvider func(
|
||||
filter filters.FunctionSpec, api *yaml.RNode) (kio.Filter, error)
|
||||
filter runtimeutil.FunctionSpec, api *yaml.RNode) (kio.Filter, error)
|
||||
}
|
||||
|
||||
// Execute runs the command
|
||||
@@ -173,7 +174,7 @@ func (r RunFns) runFunctions(
|
||||
// check for deferred function errors
|
||||
var errs []string
|
||||
for i := range fltrs {
|
||||
cf, ok := fltrs[i].(filters.DeferFailureFunction)
|
||||
cf, ok := fltrs[i].(runtimeutil.DeferFailureFunction)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
@@ -196,7 +197,7 @@ func (r RunFns) getFunctionsFromInput(nodes []*yaml.RNode) ([]kio.Filter, error)
|
||||
buff := &kio.PackageBuffer{}
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{&kio.PackageBuffer{Nodes: nodes}},
|
||||
Filters: []kio.Filter{&filters.IsReconcilerFilter{}},
|
||||
Filters: []kio.Filter{&runtimeutil.IsReconcilerFilter{}},
|
||||
Outputs: []kio.Writer{buff},
|
||||
}.Execute()
|
||||
if err != nil {
|
||||
@@ -235,7 +236,7 @@ func (r RunFns) getFunctionFilters(global bool, fns ...*yaml.RNode) (
|
||||
var fltrs []kio.Filter
|
||||
for i := range fns {
|
||||
api := fns[i]
|
||||
spec := filters.GetFunctionSpec(api)
|
||||
spec := runtimeutil.GetFunctionSpec(api)
|
||||
if spec.Container.Network.Required {
|
||||
if !r.Network {
|
||||
// TODO(eddiezane): Provide error info about which function needs the network
|
||||
@@ -251,9 +252,9 @@ func (r RunFns) getFunctionFilters(global bool, fns ...*yaml.RNode) (
|
||||
if c == nil {
|
||||
continue
|
||||
}
|
||||
cf, ok := c.(*filters.ContainerFilter)
|
||||
cf, ok := c.(*container.Filter)
|
||||
if global && ok {
|
||||
cf.GlobalScope = true
|
||||
cf.Exec.GlobalScope = true
|
||||
}
|
||||
fltrs = append(fltrs, c)
|
||||
}
|
||||
@@ -331,7 +332,7 @@ func (r *RunFns) init() {
|
||||
}
|
||||
|
||||
// ffp provides function filters
|
||||
func (r *RunFns) ffp(spec filters.FunctionSpec, api *yaml.RNode) (kio.Filter, error) {
|
||||
func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode) (kio.Filter, error) {
|
||||
if !r.DisableContainers && spec.Container.Image != "" {
|
||||
var resultsFile string
|
||||
// TODO: Add a test for this behavior
|
||||
@@ -341,15 +342,17 @@ func (r *RunFns) ffp(spec filters.FunctionSpec, api *yaml.RNode) (kio.Filter, er
|
||||
"results-%v.yaml", r.resultsCount))
|
||||
atomic.AddUint32(&r.resultsCount, 1)
|
||||
}
|
||||
return &filters.ContainerFilter{
|
||||
|
||||
cf := &container.Filter{
|
||||
Image: spec.Container.Image,
|
||||
Config: api,
|
||||
Network: spec.Network,
|
||||
StorageMounts: r.StorageMounts,
|
||||
GlobalScope: r.GlobalScope,
|
||||
ResultsFile: resultsFile,
|
||||
DeferFailure: spec.DeferFailure,
|
||||
}, nil
|
||||
}
|
||||
cf.Exec.FunctionConfig = api
|
||||
cf.Exec.GlobalScope = r.GlobalScope
|
||||
cf.Exec.ResultsFile = resultsFile
|
||||
cf.Exec.DeferFailure = spec.DeferFailure
|
||||
return cf, nil
|
||||
}
|
||||
if r.EnableStarlark && spec.Starlark.Path != "" {
|
||||
// the script path is relative to the function config file
|
||||
|
||||
@@ -16,6 +16,8 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"sigs.k8s.io/kustomize/kyaml/copyutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/runtime/container"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/filters"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
@@ -48,8 +50,8 @@ func TestRunFns_init(t *testing.T) {
|
||||
api, err := yaml.Parse(`apiVersion: apps/v1
|
||||
kind:
|
||||
`)
|
||||
spec := filters.FunctionSpec{
|
||||
Container: filters.ContainerSpec{
|
||||
spec := runtimeutil.FunctionSpec{
|
||||
Container: runtimeutil.ContainerSpec{
|
||||
Image: "example.com:version",
|
||||
},
|
||||
}
|
||||
@@ -57,7 +59,9 @@ kind:
|
||||
return
|
||||
}
|
||||
filter, _ := instance.functionFilterProvider(spec, api)
|
||||
assert.Equal(t, &filters.ContainerFilter{Image: "example.com:version", Config: api}, filter)
|
||||
cf := &container.Filter{Image: "example.com:version"}
|
||||
cf.Exec.FunctionConfig = api
|
||||
assert.Equal(t, cf, filter)
|
||||
}
|
||||
|
||||
func TestRunFns_Execute__initGlobalScope(t *testing.T) {
|
||||
@@ -76,8 +80,8 @@ kind:
|
||||
return
|
||||
}
|
||||
|
||||
spec := filters.FunctionSpec{
|
||||
Container: filters.ContainerSpec{
|
||||
spec := runtimeutil.FunctionSpec{
|
||||
Container: runtimeutil.ContainerSpec{
|
||||
Image: "example.com:version",
|
||||
},
|
||||
}
|
||||
@@ -85,8 +89,10 @@ kind:
|
||||
return
|
||||
}
|
||||
filter, _ := instance.functionFilterProvider(spec, api)
|
||||
assert.Equal(t, &filters.ContainerFilter{
|
||||
Image: "example.com:version", Config: api, GlobalScope: true}, filter)
|
||||
cf := &container.Filter{Image: "example.com:version"}
|
||||
cf.Exec.FunctionConfig = api
|
||||
cf.Exec.GlobalScope = true
|
||||
assert.Equal(t, cf, filter)
|
||||
}
|
||||
|
||||
func TestRunFns_Execute__initDefault(t *testing.T) {
|
||||
@@ -143,12 +149,12 @@ func TestRunFns_Execute__initDefault(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "explicit directories in mounts",
|
||||
instance: RunFns{StorageMounts: []filters.StorageMount{{MountType: "volume", Src: "myvol", DstPath: "/local/"}}},
|
||||
instance: RunFns{StorageMounts: []runtimeutil.StorageMount{{MountType: "volume", Src: "myvol", DstPath: "/local/"}}},
|
||||
expected: RunFns{
|
||||
Output: os.Stdout,
|
||||
Input: os.Stdin,
|
||||
NoFunctionsFromInput: getFalse(),
|
||||
StorageMounts: []filters.StorageMount{{MountType: "volume", Src: "myvol", DstPath: "/local/"}},
|
||||
StorageMounts: []runtimeutil.StorageMount{{MountType: "volume", Src: "myvol", DstPath: "/local/"}},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -751,7 +757,7 @@ replace: StatefulSet
|
||||
var fltrs []*TestFilter
|
||||
instance := RunFns{
|
||||
Path: dir,
|
||||
functionFilterProvider: func(f filters.FunctionSpec, node *yaml.RNode) (kio.Filter, error) {
|
||||
functionFilterProvider: func(f runtimeutil.FunctionSpec, node *yaml.RNode) (kio.Filter, error) {
|
||||
tf := &TestFilter{
|
||||
Exit: errors.Errorf("message: %s", f.Container.Image),
|
||||
}
|
||||
@@ -927,8 +933,8 @@ func setupTest(t *testing.T) string {
|
||||
// getFilterProvider fakes the creation of a filter, replacing the ContainerFiler with
|
||||
// a filter to s/kind: Deployment/kind: StatefulSet/g.
|
||||
// this can be used to simulate running a filter.
|
||||
func getFilterProvider(t *testing.T) func(filters.FunctionSpec, *yaml.RNode) (kio.Filter, error) {
|
||||
return func(f filters.FunctionSpec, node *yaml.RNode) (kio.Filter, error) {
|
||||
func getFilterProvider(t *testing.T) func(runtimeutil.FunctionSpec, *yaml.RNode) (kio.Filter, error) {
|
||||
return func(f runtimeutil.FunctionSpec, node *yaml.RNode) (kio.Filter, error) {
|
||||
// parse the filter from the input
|
||||
filter := yaml.YFilter{}
|
||||
b := &bytes.Buffer{}
|
||||
|
||||
Reference in New Issue
Block a user