diff --git a/api/internal/plugins/fnplugin/fnplugin.go b/api/internal/plugins/fnplugin/fnplugin.go new file mode 100644 index 000000000..32cd8c3f4 --- /dev/null +++ b/api/internal/plugins/fnplugin/fnplugin.go @@ -0,0 +1,91 @@ +package fnplugin + +import ( + "sigs.k8s.io/kustomize/api/resmap" + "sigs.k8s.io/kustomize/api/resource" + "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 { + // Plugin configuration data. + cfg []byte + + // PluginHelpers + h *resmap.PluginHelpers + + // Function runner + RunFns runfn.RunFns +} + +func bytesToRNode(yml []byte) (*yaml.RNode, error) { + rnode, err := yaml.Parse(string(yml)) + if err != nil { + return nil, err + } + return rnode, nil +} + +func resourceToRNode(res *resource.Resource) (*yaml.RNode, error) { + yml, err := res.AsYAML() + if err != nil { + return nil, err + } + + return bytesToRNode(yml) +} + +func getFunctionSpec(res *resource.Resource) *runtimeutil.FunctionSpec { + rnode, err := resourceToRNode(res) + if err != nil { + return nil + } + + 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 +} + +// NewFnPlugin returns a FnPlugin struct +func NewFnPlugin(res *resource.Resource) *FnPlugin { + return &FnPlugin{} +} + +// 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) + if err != nil { + return err + } + + f.RunFns.Functions = append(f.RunFns.Functions, fn) + + 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 +} + +// 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 +} diff --git a/api/internal/plugins/loader/loader.go b/api/internal/plugins/loader/loader.go index c61f264c4..43f4b2cbb 100644 --- a/api/internal/plugins/loader/loader.go +++ b/api/internal/plugins/loader/loader.go @@ -16,6 +16,7 @@ import ( "sigs.k8s.io/kustomize/api/ifc" "sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers" "sigs.k8s.io/kustomize/api/internal/plugins/execplugin" + "sigs.k8s.io/kustomize/api/internal/plugins/fnplugin" "sigs.k8s.io/kustomize/api/internal/plugins/utils" "sigs.k8s.io/kustomize/api/konfig" "sigs.k8s.io/kustomize/api/resid"