Made generators work in addition to transformers

Made go fmt to make linter happy
This commit is contained in:
Alexey Odinokov
2020-06-14 03:28:19 +00:00
parent 9bd4f78288
commit b78464c8b1
5 changed files with 54 additions and 42 deletions

View File

@@ -6,7 +6,7 @@ package fnplugin
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"log" //"log"
"strconv" "strconv"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -17,11 +17,11 @@ import (
"sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/kio"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/kustomize/kyaml/runfn"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil" "sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
"sigs.k8s.io/kustomize/kyaml/runfn"
) )
const ( const (
@@ -32,7 +32,7 @@ const (
type FnPlugin struct { type FnPlugin struct {
// Function runner // Function runner
RunFns runfn.RunFns runFns runfn.RunFns
// Plugin configuration data. // Plugin configuration data.
cfg []byte cfg []byte
@@ -81,9 +81,9 @@ func toStorageMounts(mounts []string) []runtimeutil.StorageMount {
} }
func NewFnPlugin(o *types.FnPluginLoadingOptions) *FnPlugin { func NewFnPlugin(o *types.FnPluginLoadingOptions) *FnPlugin {
log.Printf("options: %v\n", o) //log.Printf("options: %v\n", o)
return &FnPlugin{ return &FnPlugin{
RunFns: runfn.RunFns{ runFns: runfn.RunFns{
Functions: []*kyaml.RNode{}, Functions: []*kyaml.RNode{},
Network: o.Network, Network: o.Network,
NetworkName: o.NetworkName, NetworkName: o.NetworkName,
@@ -101,14 +101,6 @@ func (p *FnPlugin) Cfg() []byte {
func (p *FnPlugin) Config(h *resmap.PluginHelpers, config []byte) error { func (p *FnPlugin) Config(h *resmap.PluginHelpers, config []byte) error {
p.h = h p.h = h
p.cfg = config p.cfg = config
rnode, err := bytesToRNode(config)
if err != nil {
return err
}
p.RunFns.Functions = append(p.RunFns.Functions, rnode)
return nil return nil
} }
@@ -149,30 +141,50 @@ func (p *FnPlugin) Transform(rm resmap.ResMap) error {
// invokePlugin uses Function runner to run function as plugin // invokePlugin uses Function runner to run function as plugin
func (p *FnPlugin) invokePlugin(input []byte) ([]byte, error) { 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(kyaml.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 // Transform to ResourceList
var inOut bytes.Buffer var inOut bytes.Buffer
inIn := bytes.NewReader(input) inIn := bytes.NewReader(input)
err = kio.Pipeline{
err := kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: inIn}}, Inputs: []kio.Reader{&kio.ByteReader{Reader: inIn}},
Outputs: []kio.Writer{kio.ByteWriter{ Outputs: []kio.Writer{kio.ByteWriter{
Writer: &inOut, Writer: &inOut,
WrappingKind: kio.ResourceListKind, WrappingKind: kio.ResourceListKind,
WrappingAPIVersion: kio.ResourceListAPIVersion,}}, WrappingAPIVersion: kio.ResourceListAPIVersion}},
}.Execute() }.Execute()
if err != nil { if err != nil {
return nil, errors.Wrap( return nil, errors.Wrap(
err, "couldn't transform to ResourceList") err, "couldn't transform to ResourceList")
} }
//log.Printf("converted to:\n%s\n", inOut.String()) //log.Printf("converted to:\n%s\n", inOut.String())
// Execute Fn (it's configured - see Config()) // Configure and Execute Fn
var runFnsOut bytes.Buffer var runFnsOut bytes.Buffer
p.RunFns.Input = bytes.NewReader(inOut.Bytes()) p.runFns.Input = bytes.NewReader(inOut.Bytes())
p.RunFns.Output = &runFnsOut p.runFns.Functions = append(p.runFns.Functions, rnode)
p.runFns.Output = &runFnsOut
err = p.RunFns.Execute() err = p.runFns.Execute()
if err != nil { if err != nil {
return nil, errors.Wrap( return nil, errors.Wrap(
err, "couln't execute function") err, "couln't execute function")

View File

@@ -30,6 +30,6 @@ type PluginConfig struct {
// BpLoadingOptions distinguishes builtin plugin behaviors. // BpLoadingOptions distinguishes builtin plugin behaviors.
BpLoadingOptions BuiltinPluginLoadingOptions BpLoadingOptions BuiltinPluginLoadingOptions
// FnpLoadingOpeions sets the way function-based plugin behaviors. // FnpLoadingOptions sets the way function-based plugin behaviors.
FnpLoadingOptions FnPluginLoadingOptions FnpLoadingOptions FnPluginLoadingOptions
} }

View File

@@ -76,7 +76,7 @@ func NewCmdBuild(out io.Writer) *cobra.Command {
"output", "o", "", "output", "o", "",
"If specified, write the build output to this path.") "If specified, write the build output to this path.")
cmd.Flags().BoolVar( cmd.Flags().BoolVar(
&o.fnOptions.EnableExec, "enable-exec", false /*do not change!*/, &o.fnOptions.EnableExec, "enable-exec", false, /*do not change!*/
"enable support for exec functions -- note: exec functions run arbitrary code -- do not use for untrusted configs!!! (Alpha)") "enable support for exec functions -- note: exec functions run arbitrary code -- do not use for untrusted configs!!! (Alpha)")
cmd.Flags().BoolVar( cmd.Flags().BoolVar(
&o.fnOptions.EnableStar, "enable-star", false, &o.fnOptions.EnableStar, "enable-star", false,