mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-09-15 12:18:57 +00:00
Made generators work in addition to transformers
Made go fmt to make linter happy
This commit is contained in:
@@ -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,22 +17,22 @@ 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 (
|
||||||
idAnnotation = "kustomize.config.k8s.io/id"
|
idAnnotation = "kustomize.config.k8s.io/id"
|
||||||
HashAnnotation = "kustomize.config.k8s.io/needs-hash"
|
HashAnnotation = "kustomize.config.k8s.io/needs-hash"
|
||||||
BehaviorAnnotation = "kustomize.config.k8s.io/behavior"
|
BehaviorAnnotation = "kustomize.config.k8s.io/behavior"
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
||||||
@@ -42,11 +42,11 @@ type FnPlugin struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func bytesToRNode(yml []byte) (*kyaml.RNode, error) {
|
func bytesToRNode(yml []byte) (*kyaml.RNode, error) {
|
||||||
rnode, err := kyaml.Parse(string(yml))
|
rnode, err := kyaml.Parse(string(yml))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return rnode, nil
|
return rnode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceToRNode(res *resource.Resource) (*kyaml.RNode, error) {
|
func resourceToRNode(res *resource.Resource) (*kyaml.RNode, error) {
|
||||||
@@ -81,10 +81,10 @@ 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,
|
||||||
EnableStarlark: o.EnableStar,
|
EnableStarlark: o.EnableStar,
|
||||||
@@ -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")
|
||||||
@@ -186,7 +198,7 @@ func (p *FnPlugin) invokePlugin(input []byte) ([]byte, error) {
|
|||||||
|
|
||||||
err = kio.Pipeline{
|
err = kio.Pipeline{
|
||||||
Inputs: []kio.Reader{&kio.ByteReader{Reader: outIn}},
|
Inputs: []kio.Reader{&kio.ByteReader{Reader: outIn}},
|
||||||
Outputs: []kio.Writer{kio.ByteWriter{Writer: &outOut}},
|
Outputs: []kio.Writer{kio.ByteWriter{Writer: &outOut}},
|
||||||
}.Execute()
|
}.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(
|
return nil, errors.Wrap(
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
|
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/execplugin"
|
"sigs.k8s.io/kustomize/api/internal/plugins/execplugin"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/fnplugin"
|
"sigs.k8s.io/kustomize/api/internal/plugins/fnplugin"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
|
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
|
||||||
"sigs.k8s.io/kustomize/api/konfig"
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,12 +45,12 @@ const (
|
|||||||
// FnPluginLoadingOptions set way functions-based pluing are restricted
|
// FnPluginLoadingOptions set way functions-based pluing are restricted
|
||||||
type FnPluginLoadingOptions struct {
|
type FnPluginLoadingOptions struct {
|
||||||
// Allow to run executables
|
// Allow to run executables
|
||||||
EnableExec bool
|
EnableExec bool
|
||||||
// Allow to run starlark
|
// Allow to run starlark
|
||||||
EnableStar bool
|
EnableStar bool
|
||||||
// Allow container access to network
|
// Allow container access to network
|
||||||
Network bool
|
Network bool
|
||||||
NetworkName string
|
NetworkName string
|
||||||
// list of mounts
|
// list of mounts
|
||||||
Mounts []string
|
Mounts []string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user