Allow functions to enable the starlark filter (off by default)

This commit is contained in:
Phillip Wittrock
2020-03-24 20:12:39 -07:00
parent 85e9779bd6
commit 7164e55831
7 changed files with 215 additions and 37 deletions

View File

@@ -25,6 +25,9 @@ type FunctionSpec struct {
// Container is the spec for running a function as a container
Container ContainerSpec `json:"container,omitempty" yaml:"container,omitempty"`
// Starlark is the spec for running a function as a starlark script
Starlark StarlarkSpec `json:"starlark,omitempty" yaml:"starlark,omitempty"`
}
// ContainerSpec defines a spec for running a function as a container
@@ -42,6 +45,14 @@ type ContainerNetwork struct {
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
}
// StarlarkSpec defines how to run a function as a starlark program
type StarlarkSpec struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// Path specifies a path to a starlark script
Path string `json:"path,omitempty" yaml:"path,omitempty"`
}
// GetFunctionSpec returns the FunctionSpec for a resource. Returns
// nil if the resource does not have a FunctionSpec.
//

View File

@@ -15,6 +15,7 @@ import (
"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"
)
@@ -57,6 +58,12 @@ type RunFns struct {
// and only use explicit sources
NoFunctionsFromInput *bool
// EnableStarlark will enable functions run as starlark scripts
EnableStarlark bool
// DisableContainers will disable functions run as containers
DisableContainers bool
// functionFilterProvider provides a filter to perform the function.
// this is a variable so it can be mocked in tests
functionFilterProvider func(
@@ -208,8 +215,10 @@ func (r RunFns) getFunctionFilters(global bool, fns ...*yaml.RNode) (
}
spec.Network = r.NetworkName
}
c := r.functionFilterProvider(*spec, api)
if c == nil {
continue
}
cf, ok := c.(*filters.ContainerFilter)
if global && ok {
cf.GlobalScope = true
@@ -291,7 +300,7 @@ func (r *RunFns) init() {
// ffp provides function filters
func (r *RunFns) ffp(spec filters.FunctionSpec, api *yaml.RNode) kio.Filter {
if spec.Container.Image != "" {
if !r.DisableContainers && spec.Container.Image != "" {
return &filters.ContainerFilter{
Image: spec.Container.Image,
Config: api,
@@ -300,9 +309,12 @@ func (r *RunFns) ffp(spec filters.FunctionSpec, api *yaml.RNode) kio.Filter {
GlobalScope: r.GlobalScope,
}
}
return noOpFilter
if r.EnableStarlark && spec.Starlark.Path != "" {
return &starlark.Filter{
Name: spec.Starlark.Name,
Path: spec.Starlark.Path,
FunctionConfig: api,
}
}
return nil
}
var noOpFilter = kio.FilterFunc(func(in []*yaml.RNode) ([]*yaml.RNode, error) {
return in, nil
})

View File

@@ -190,6 +190,10 @@ func TestRunFns_getFilters(t *testing.T) {
name string
// value to set for NoFunctionsFromInput
noFunctionsFromInput *bool
enableStarlark bool
disableContainers bool
}{
// Test
//
@@ -213,6 +217,26 @@ metadata:
out: []string{"gcr.io/example.com/image:v1.0.0"},
},
{name: "disable containers",
in: []f{
{
path: filepath.Join("foo", "bar.yaml"),
value: `
apiVersion: example.com/v1alpha1
kind: ExampleFunction
metadata:
annotations:
config.kubernetes.io/function: |
container:
image: gcr.io/example.com/image:v1.0.0
config.kubernetes.io/local-config: "true"
`,
},
},
out: nil,
disableContainers: true,
},
// Test
//
//
@@ -432,6 +456,45 @@ metadata:
},
out: []string{"b", "a", "c"},
},
// Test
//
//
{name: "starlark-function",
in: []f{
{
path: filepath.Join("foo", "bar.yaml"),
value: `
apiVersion: example.com/v1alpha1
kind: ExampleFunction
metadata:
annotations:
config.kubernetes.io/function: |
starlark:
path: a/b/c
`,
},
},
enableStarlark: true,
out: []string{"name: path: a/b/c url: program:"},
},
{name: "starlark-function-disabled",
in: []f{
{
path: filepath.Join("foo", "bar.yaml"),
value: `
apiVersion: example.com/v1alpha1
kind: ExampleFunction
metadata:
annotations:
config.kubernetes.io/function: |
starlark:
path: a/b/c
`,
},
},
},
}
for i := range tests {
@@ -484,6 +547,8 @@ metadata:
// init the instance
r := &RunFns{
EnableStarlark: tt.enableStarlark,
DisableContainers: tt.disableContainers,
FunctionPaths: fnPaths,
Functions: parsedFns,
Path: d,

View File

@@ -35,6 +35,10 @@ type Filter struct {
FunctionConfig *yaml.RNode
}
func (sf *Filter) String() string {
return fmt.Sprintf("name: %v path: %v url: %v program: %v", sf.Name, sf.Path, sf.URL, sf.Program)
}
func (sf *Filter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
if sf.URL != "" && sf.Path != "" ||
sf.URL != "" && sf.Program != "" ||