This commit is contained in:
Donny Xia
2020-06-29 11:12:20 -07:00
parent 94a55210e1
commit 8d543d8483
8 changed files with 364 additions and 478 deletions

View File

@@ -1,23 +1,38 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package fnplugin
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
"sigs.k8s.io/kustomize/kyaml/runfn"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// FnPlugin holds the information about thie function
type FnPlugin struct {
// Function runner
runFns runfn.RunFns
// Plugin configuration data.
cfg []byte
// Plugin name cache for error output
pluginName string
// PluginHelpers
h *resmap.PluginHelpers
// Function runner
RunFns runfn.RunFns
}
func bytesToRNode(yml []byte) (*yaml.RNode, error) {
@@ -37,7 +52,7 @@ func resourceToRNode(res *resource.Resource) (*yaml.RNode, error) {
return bytesToRNode(yml)
}
func getFunctionSpec(res *resource.Resource) *runtimeutil.FunctionSpec {
func GetFunctionSpec(res *resource.Resource) *runtimeutil.FunctionSpec {
rnode, err := resourceToRNode(res)
if err != nil {
return nil
@@ -46,46 +61,153 @@ func getFunctionSpec(res *resource.Resource) *runtimeutil.FunctionSpec {
return runtimeutil.GetFunctionSpec(rnode)
}
// IsFnPlugin returns a resource is a function plugin spec or not
func IsFnPlugin(res *resource.Resource) bool {
return getFunctionSpec(res) != nil
func toStorageMounts(mounts []string) []runtimeutil.StorageMount {
var sms []runtimeutil.StorageMount
for _, mount := range mounts {
sms = append(sms, runtimeutil.StringToStorageMount(mount))
}
return sms
}
// NewFnPlugin returns a FnPlugin struct
func NewFnPlugin(res *resource.Resource) *FnPlugin {
return &FnPlugin{}
func NewFnPlugin(o *types.FnPluginLoadingOptions) *FnPlugin {
return &FnPlugin{
runFns: runfn.RunFns{
Functions: []*yaml.RNode{},
Network: o.Network,
NetworkName: o.NetworkName,
EnableStarlark: o.EnableStar,
EnableExec: o.EnableExec,
StorageMounts: toStorageMounts(o.Mounts),
},
}
}
// Config accepts the plugin helper and plugin config
func (f *FnPlugin) Config(h *resmap.PluginHelpers, config []byte) error {
f.h = h
f.cfg = config
// config is the content of the config file for the functions.
// If there are multiple functions in on config file, they will
// be passed in one by one.
fn, err := bytesToRNode(config)
func (p *FnPlugin) Cfg() []byte {
return p.cfg
}
func (p *FnPlugin) Config(h *resmap.PluginHelpers, config []byte) error {
p.h = h
p.cfg = config
fn, err := bytesToRNode(p.cfg)
if err != nil {
return err
}
f.RunFns.Functions = append(f.RunFns.Functions, fn)
meta, err := fn.GetMeta()
if err != nil {
return err
}
p.pluginName = fmt.Sprintf("api: %s, kind: %s, name: %s",
meta.APIVersion, meta.Kind, meta.Name)
return nil
}
// Transform does the transformation when the plugin is a transformer
func (f *FnPlugin) Transform(rm resmap.ResMap) error {
// convert input to ResourceList
// add functionConfig
// invoke function
// convert back to ResMap
return nil
func (p *FnPlugin) Generate() (resmap.ResMap, error) {
output, err := p.invokePlugin(nil)
if err != nil {
return nil, err
}
rm, err := p.h.ResmapFactory().NewResMapFromBytes(output)
if err != nil {
return nil, err
}
return utils.UpdateResourceOptions(rm)
}
// invoke call the actual function and send the input to it. It captures
// and returns the output
func (f *FnPlugin) invoke(input []byte) ([]byte, error) {
// setup input and output
var output []byte
return output, nil
func (p *FnPlugin) Transform(rm resmap.ResMap) error {
// add ResIds as annotations to all objects so that we can add them back
inputRM, err := utils.GetResMapWithIDAnnotation(rm)
if err != nil {
return err
}
// encode the ResMap so it can be fed to the plugin
resources, err := inputRM.AsYaml()
if err != nil {
return err
}
// invoke the plugin with resources as the input
output, err := p.invokePlugin(resources)
if err != nil {
return fmt.Errorf("%v %s", err, string(output))
}
// update the original ResMap based on the output
return utils.UpdateResMapValues(p.pluginName, p.h, output, rm)
}
// invokePlugin uses Function runner to run function as plugin
func (p *FnPlugin) invokePlugin(input []byte) ([]byte, error) {
// get config rnode
rnode, err := bytesToRNode(p.cfg)
if err != nil {
return nil, err
}
err = rnode.PipeE(yaml.SetAnnotation("config.kubernetes.io/local-config", "true"))
if err != nil {
return nil, err
}
// we need to add config as input for generators. Some of them don't work with FunctionConfig
// and in addition kio.Pipeline won't create anything if there are no objects
// see https://github.com/kubernetes-sigs/kustomize/blob/master/kyaml/kio/kio.go#L93
if input == nil {
yaml, err := rnode.String()
if err != nil {
return nil, err
}
input = []byte(yaml)
}
// Transform to ResourceList
var inOut bytes.Buffer
inIn := bytes.NewReader(input)
err = kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: inIn}},
Outputs: []kio.Writer{kio.ByteWriter{
Writer: &inOut,
WrappingKind: kio.ResourceListKind,
WrappingAPIVersion: kio.ResourceListAPIVersion}},
}.Execute()
if err != nil {
return nil, errors.Wrap(
err, "couldn't transform to ResourceList")
}
//log.Printf("converted to:\n%s\n", inOut.String())
// Configure and Execute Fn
var runFnsOut bytes.Buffer
p.runFns.Input = bytes.NewReader(inOut.Bytes())
p.runFns.Functions = append(p.runFns.Functions, rnode)
p.runFns.Output = &runFnsOut
err = p.runFns.Execute()
if err != nil {
return nil, errors.Wrap(
err, "couldn't execute function")
}
//log.Printf("fn returned:\n%s\n", runFnsOut.String())
// Convert back to a single multi-yaml doc
var outOut bytes.Buffer
outIn := bytes.NewReader(runFnsOut.Bytes())
err = kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: outIn}},
Outputs: []kio.Writer{kio.ByteWriter{Writer: &outOut}},
}.Execute()
if err != nil {
return nil, errors.Wrap(
err, "couldn't transform from ResourceList")
}
//log.Printf("converted back to:\n%s\n", outOut.String())
return outOut.Bytes(), nil
}