Support publishing starlark functions from urls

This commit is contained in:
Phillip Wittrock
2020-05-13 11:36:19 -07:00
parent 0a8d367633
commit 6ae53cb732
6 changed files with 142 additions and 30 deletions

View File

@@ -65,6 +65,9 @@ type StarlarkSpec struct {
// Path specifies a path to a starlark script
Path string `json:"path,omitempty" yaml:"path,omitempty"`
// URL specifies a url containing a starlark script
URL string `json:"url,omitempty" yaml:"url,omitempty"`
}
// StorageMount represents a container's mounted storage option(s)

View File

@@ -50,9 +50,9 @@ func (sf *Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
}
func (sf *Filter) setup() error {
if sf.URL != "" && sf.Path != "" ||
sf.URL != "" && sf.Program != "" ||
sf.Path != "" && sf.Program != "" {
if (sf.URL != "" && sf.Path != "") ||
(sf.URL != "" && sf.Program != "") ||
(sf.Path != "" && sf.Program != "") {
return errors.Errorf("Filter Path, Program and URL are mutually exclusive")
}

View File

@@ -356,25 +356,29 @@ func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode) (kio.Filter
cf.Exec.DeferFailure = spec.DeferFailure
return cf, nil
}
if r.EnableStarlark && spec.Starlark.Path != "" {
if r.EnableStarlark && (spec.Starlark.Path != "" || spec.Starlark.URL != "") {
// the script path is relative to the function config file
m, err := api.GetMeta()
if err != nil {
return nil, errors.Wrap(err)
}
p := m.Annotations[kioutil.PathAnnotation]
spec.Starlark.Path = path.Clean(spec.Starlark.Path)
if path.IsAbs(spec.Starlark.Path) {
return nil, errors.Errorf(
"absolute function path %s not allowed", spec.Starlark.Path)
}
if strings.HasPrefix(spec.Starlark.Path, "..") {
return nil, errors.Errorf(
"function path %s not allowed to start with ../", spec.Starlark.Path)
}
p = path.Join(r.Path, path.Dir(p), spec.Starlark.Path)
sf := &starlark.Filter{Name: spec.Starlark.Name, Path: p}
var p string
if spec.Starlark.Path != "" {
p = m.Annotations[kioutil.PathAnnotation]
spec.Starlark.Path = path.Clean(spec.Starlark.Path)
if path.IsAbs(spec.Starlark.Path) {
return nil, errors.Errorf(
"absolute function path %s not allowed", spec.Starlark.Path)
}
if strings.HasPrefix(spec.Starlark.Path, "..") {
return nil, errors.Errorf(
"function path %s not allowed to start with ../", spec.Starlark.Path)
}
p = path.Join(r.Path, path.Dir(p), spec.Starlark.Path)
}
sf := &starlark.Filter{Name: spec.Starlark.Name, Path: p, URL: spec.Starlark.URL}
sf.FunctionConfig = api
sf.GlobalScope = r.GlobalScope