mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 09:51:23 +00:00
modify pluginator to add support for krm function
This commit is contained in:
30
cmd/pluginator/krmfunction/cmd.go
Normal file
30
cmd/pluginator/krmfunction/cmd.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package krmfunction
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// NewKrmFunctionCmd returns a pointer to a command
|
||||
func NewKrmFunctionCmd() *cobra.Command {
|
||||
var outputDir string
|
||||
var inputFile string
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "krm -i FILE -o DIR",
|
||||
Short: "Convert the plugin to KRM function instead of builtin function",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c := NewConverter(outputDir, inputFile)
|
||||
return c.Convert()
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&outputDir, "output", "o", "",
|
||||
"Path to the directory which will contain the KRM function")
|
||||
cmd.Flags().StringVarP(&inputFile, "input", "i", "",
|
||||
"Path to the input file")
|
||||
|
||||
cmd.MarkFlagRequired("output")
|
||||
cmd.MarkFlagRequired("input")
|
||||
|
||||
return cmd
|
||||
}
|
||||
165
cmd/pluginator/krmfunction/converter.go
Normal file
165
cmd/pluginator/krmfunction/converter.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package krmfunction
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/rakyll/statik/fs"
|
||||
// load embedded func wrapper
|
||||
_ "sigs.k8s.io/kustomize/cmd/pluginator/v2/krmfunction/funcwrapper"
|
||||
)
|
||||
|
||||
// Converter is a converter to convert the
|
||||
// plugin file to KRM function
|
||||
type Converter struct {
|
||||
// Path to the output directory
|
||||
outputDir string
|
||||
// Path to the input file
|
||||
inputFile string
|
||||
wrapperFileName string
|
||||
pluginFileName string
|
||||
goModFileName string
|
||||
dockerFileName string
|
||||
}
|
||||
|
||||
// NewConverter return a pointer to a new converter
|
||||
func NewConverter(outputDir, inputFile string) *Converter {
|
||||
return &Converter{
|
||||
outputDir: outputDir,
|
||||
inputFile: inputFile,
|
||||
wrapperFileName: "main.go",
|
||||
pluginFileName: "plugin.go",
|
||||
goModFileName: "go.mod",
|
||||
dockerFileName: "Dockerfile",
|
||||
}
|
||||
}
|
||||
|
||||
// Convert converts the input file to a executable
|
||||
// KRM function and writes to destination directory
|
||||
func (c *Converter) Convert() error {
|
||||
// read and process executable wrapper
|
||||
wrapper, err := c.readEmbeddedFile(c.wrapperFileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wrapper = c.prepareWrapper(wrapper)
|
||||
|
||||
if !strings.HasSuffix(c.inputFile, ".go") {
|
||||
return fmt.Errorf("input file %s is not a Go file", c.inputFile)
|
||||
}
|
||||
|
||||
// read and process plugin code
|
||||
pluginCode, err := c.readDiskFile(c.inputFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, c.pluginFileName = filepath.Split(c.inputFile)
|
||||
|
||||
// go.mod file
|
||||
goMod, err := c.readEmbeddedFile(c.goModFileName + ".src")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// prepare destination directory
|
||||
err = c.mkDstDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// write
|
||||
return c.write(map[string]string{
|
||||
c.wrapperFileName: wrapper,
|
||||
c.pluginFileName: pluginCode,
|
||||
c.goModFileName: goMod,
|
||||
c.dockerFileName: c.getDockerfile(),
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Converter) getDockerfile() string {
|
||||
return `FROM golang:1.13-stretch
|
||||
ENV CGO_ENABLED=0
|
||||
WORKDIR /go/src/
|
||||
COPY . .
|
||||
RUN go build -v -o /usr/local/bin/function ./
|
||||
FROM alpine:latest
|
||||
COPY --from=0 /usr/local/bin/function /usr/local/bin/function
|
||||
CMD ["function"]
|
||||
`
|
||||
}
|
||||
|
||||
func (c *Converter) prepareWrapper(content string) string {
|
||||
b := bytes.NewBufferString(content)
|
||||
o := &bytes.Buffer{}
|
||||
scanner := bufio.NewScanner(b)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
// Set the package name to main
|
||||
if strings.TrimSpace(line) == "package funcwrappersrc" {
|
||||
line = "package main"
|
||||
}
|
||||
// assign to plugin variable
|
||||
if strings.TrimSpace(line) == "var plugin resmap.Configurable" {
|
||||
line = line + `
|
||||
// KustomizePlugin is a global variable defined in every plugin
|
||||
plugin = &KustomizePlugin
|
||||
`
|
||||
}
|
||||
o.WriteString(line + "\n")
|
||||
}
|
||||
return o.String()
|
||||
}
|
||||
|
||||
// readEmbeddedFile read the file from embedded files with filename
|
||||
// name. Return the file content if it's successful.
|
||||
func (c *Converter) readEmbeddedFile(name string) (string, error) {
|
||||
statikFS, err := fs.New()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
r, err := statikFS.Open("/" + name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer r.Close()
|
||||
contents, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(contents), nil
|
||||
}
|
||||
|
||||
func (c *Converter) readDiskFile(path string) (string, error) {
|
||||
f, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(f), nil
|
||||
}
|
||||
|
||||
func (c *Converter) mkDstDir() error {
|
||||
p := c.outputDir
|
||||
f, err := os.Open(p)
|
||||
if err == nil || f != nil {
|
||||
return fmt.Errorf("directory %s has already existed", p)
|
||||
}
|
||||
|
||||
return os.MkdirAll(p, 0755)
|
||||
}
|
||||
|
||||
func (c *Converter) write(m map[string]string) error {
|
||||
for k, v := range m {
|
||||
p := filepath.Join(c.outputDir, k)
|
||||
err := ioutil.WriteFile(p, []byte(v), 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
234
cmd/pluginator/krmfunction/converter_test.go
Normal file
234
cmd/pluginator/krmfunction/converter_test.go
Normal file
@@ -0,0 +1,234 @@
|
||||
package krmfunction
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func makeTempDir(t *testing.T) string {
|
||||
s, err := ioutil.TempDir("", "pluginator-*")
|
||||
assert.NoError(t, err)
|
||||
return s
|
||||
}
|
||||
|
||||
func getTransformerCode() []byte {
|
||||
// a simple namespace transformer
|
||||
return []byte(`
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/yaml"
|
||||
"sigs.k8s.io/kustomize/api/filters/namespace"
|
||||
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
)
|
||||
|
||||
type plugin struct{
|
||||
Namespace string ` + "`json:\"namespace,omitempty\" yaml:\"namespace,omitempty\"`" + `
|
||||
FieldSpecs []types.FieldSpec ` + "`json:\"fieldSpecs,omitempty\" yaml:\"fieldSpecs,omitempty\"`" + `
|
||||
}
|
||||
|
||||
//noinspection GoUnusedGlobalVariable
|
||||
var KustomizePlugin plugin
|
||||
|
||||
func (p *plugin) Config(
|
||||
_ *resmap.PluginHelpers, config []byte) (err error) {
|
||||
return yaml.Unmarshal(config, p)
|
||||
}
|
||||
|
||||
func (p *plugin) Transform(rm resmap.ResMap) error {
|
||||
if len(p.Namespace) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, r := range rm.Resources() {
|
||||
if r.IsEmpty() {
|
||||
// Don't mutate empty objects?
|
||||
continue
|
||||
}
|
||||
err := filtersutil.ApplyToJSON(namespace.Filter{
|
||||
Namespace: p.Namespace,
|
||||
FsSlice: p.FieldSpecs,
|
||||
}, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
matches := rm.GetMatchingResourcesByCurrentId(r.CurId().Equals)
|
||||
if len(matches) != 1 {
|
||||
return fmt.Errorf(
|
||||
"namespace transformation produces ID conflict: %+v", matches)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
`)
|
||||
}
|
||||
|
||||
func getTransformerInputResource() []byte {
|
||||
return []byte(`
|
||||
apiVersion: config.kubernetes.io/v1beta1
|
||||
kind: ResourceList
|
||||
functionConfig:
|
||||
apiVersion: foo-corp.com/v1
|
||||
kind: FulfillmentCenter
|
||||
metadata:
|
||||
name: staging
|
||||
metadata:
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/foo:v1.0.0
|
||||
namespace: foo
|
||||
fieldSpecs:
|
||||
- path: metadata/namespace
|
||||
create: true
|
||||
items:
|
||||
- apiVersion: apps/v1
|
||||
kind: foobar
|
||||
metadata:
|
||||
name: whatever
|
||||
`)
|
||||
}
|
||||
|
||||
func runKrmFunction(t *testing.T, input []byte, dir string) []byte {
|
||||
cmd := exec.Command("go", "run", ".")
|
||||
ib := bytes.NewReader(input)
|
||||
cmd.Stdin = ib
|
||||
ob := bytes.NewBuffer([]byte{})
|
||||
cmd.Stdout = ob
|
||||
eb := bytes.NewBuffer([]byte{})
|
||||
cmd.Stderr = eb
|
||||
cmd.Dir = dir
|
||||
err := cmd.Run()
|
||||
assert.NoErrorf(t, err, "Stdout:\n%s\nStderr:\n%s\n", ob.String(), eb.String())
|
||||
return ob.Bytes()
|
||||
}
|
||||
|
||||
func TestTransformerConverter(t *testing.T) {
|
||||
dir := makeTempDir(t)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
ioutil.WriteFile(filepath.Join(dir, "Plugin.go"),
|
||||
getTransformerCode(), 0644)
|
||||
|
||||
c := NewConverter(filepath.Join(dir, "output"),
|
||||
filepath.Join(dir, "Plugin.go"))
|
||||
|
||||
err := c.Convert()
|
||||
assert.NoError(t, err)
|
||||
|
||||
output := runKrmFunction(t, getTransformerInputResource(), filepath.Join(dir, "output"))
|
||||
assert.Equal(t, `apiVersion: config.kubernetes.io/v1beta1
|
||||
kind: ResourceList
|
||||
items:
|
||||
- {"apiVersion": "apps/v1", "kind": "foobar", "metadata": {"name": "whatever", "namespace": "foo"}}
|
||||
functionConfig:
|
||||
apiVersion: foo-corp.com/v1
|
||||
kind: FulfillmentCenter
|
||||
metadata:
|
||||
name: staging
|
||||
metadata:
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/foo:v1.0.0
|
||||
namespace: foo
|
||||
fieldSpecs:
|
||||
- path: metadata/namespace
|
||||
create: true
|
||||
`, string(output))
|
||||
}
|
||||
|
||||
func getGeneratorCode() []byte {
|
||||
return []byte(`package main
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/kv"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type plugin struct {
|
||||
h *resmap.PluginHelpers
|
||||
types.ObjectMeta ` + "`json:\"metadata,omitempty\" yaml:\"metadata,omitempty\"`" + `
|
||||
types.ConfigMapArgs
|
||||
}
|
||||
|
||||
//noinspection GoUnusedGlobalVariable
|
||||
var KustomizePlugin plugin
|
||||
|
||||
func (p *plugin) Config(h *resmap.PluginHelpers, config []byte) (err error) {
|
||||
p.ConfigMapArgs = types.ConfigMapArgs{}
|
||||
err = yaml.Unmarshal(config, p)
|
||||
if p.ConfigMapArgs.Name == "" {
|
||||
p.ConfigMapArgs.Name = p.Name
|
||||
}
|
||||
if p.ConfigMapArgs.Namespace == "" {
|
||||
p.ConfigMapArgs.Namespace = p.Namespace
|
||||
}
|
||||
p.h = h
|
||||
return
|
||||
}
|
||||
|
||||
func (p *plugin) Generate() (resmap.ResMap, error) {
|
||||
return p.h.ResmapFactory().FromConfigMapArgs(
|
||||
kv.NewLoader(p.h.Loader(), p.h.Validator()), p.ConfigMapArgs)
|
||||
}`)
|
||||
}
|
||||
|
||||
func getGeneratorInputResource() []byte {
|
||||
return []byte(`
|
||||
apiVersion: config.kubernetes.io/v1beta1
|
||||
kind: ResourceList
|
||||
functionConfig:
|
||||
apiVersion: foo-corp.com/v1
|
||||
kind: FulfillmentCenter
|
||||
metadata:
|
||||
name: staging
|
||||
metadata:
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/foo:v1.0.0
|
||||
items: []
|
||||
`)
|
||||
}
|
||||
|
||||
func TestGeneratorConverter(t *testing.T) {
|
||||
dir := makeTempDir(t)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
ioutil.WriteFile(filepath.Join(dir, "Plugin.go"),
|
||||
getGeneratorCode(), 0644)
|
||||
|
||||
c := NewConverter(filepath.Join(dir, "output"),
|
||||
filepath.Join(dir, "Plugin.go"))
|
||||
|
||||
err := c.Convert()
|
||||
assert.NoError(t, err)
|
||||
output := runKrmFunction(t, getGeneratorInputResource(), filepath.Join(dir, "output"))
|
||||
assert.Equal(t, `apiVersion: config.kubernetes.io/v1beta1
|
||||
kind: ResourceList
|
||||
items:
|
||||
- {"apiVersion": "v1", "kind": "ConfigMap", "metadata": {"name": "staging"}}
|
||||
functionConfig:
|
||||
apiVersion: foo-corp.com/v1
|
||||
kind: FulfillmentCenter
|
||||
metadata:
|
||||
name: staging
|
||||
metadata:
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/foo:v1.0.0
|
||||
`, string(output))
|
||||
}
|
||||
14
cmd/pluginator/krmfunction/funcwrapper/statik.go
Normal file
14
cmd/pluginator/krmfunction/funcwrapper/statik.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// Code generated by statik. DO NOT EDIT.
|
||||
|
||||
package funcwrapper
|
||||
|
||||
import (
|
||||
"github.com/rakyll/statik/fs"
|
||||
)
|
||||
|
||||
|
||||
func init() {
|
||||
data := "PK\x03\x04\x14\x00\x08\x00\x08\x00K\xa7pQ\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\x00 \x00go.mod.srcUT\x05\x00\x01\xef\xe7\xb2_\x84\xce\xc1\xae\x820\x10\x85\xe15\xf3\x14]\xde\xbb`:3T\"\x0b}\x97\x82\x15\x1b\xa8EJI\xf4\xe9\x0dq\xe5B]\x9f/9\x7f\x88\xa7<:\x15\xac\xbf\x02\xf4Q1\xb2\x01\x98\xdd-\xfb\xd9\xa9?(z\xbf\\r\x8b]\x0c:Mg\xaet\x17\xdb\xd9\xaa\x95\x91\x90\xa0H\xbeO8\xec\x13\xfa\xa8\x87\x9c\x96\x18\xfc\xc3i;y\xb5\x12\xd6h>\x89\xe1n\xc3\xb8\x99\x06\xabw\xf3\x1a\x18\x05 \xfe\xb7\x94i\xb4\x9dS\xbf\x8e\xd4\xe1\xf8\xdd\x10R)$\xc4\xcc57\"\xc4\xa5\x95\x9d\x91\xc6\xba\xaa\x15\x03\xcf\x00\x00\x00\xff\xffPK\x07\x08\xb1\xe0\xcew\x9c\x00\x00\x00\n\x01\x00\x00PK\x03\x04\x14\x00\x08\x00\x08\x00C\xa7pQ\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07\x00 \x00main.goUT\x05\x00\x01\xdf\xe7\xb2_\x94T\xcbn\xdb:\x10]\x93_1\xd7\x8b\x0b\xe9\xc2\x90pwA\x02\xaf\x82\xa4\x0d\xda\x04A\xea]\xd1\x05M\x8d\x1cB|\x08C\xaaN\x1a\xe8\xdf\x0b\xea\xe1H\xca\xa3\xe9\xc6\x80Gg\xce93\xc3\x99<\x87\xed\xbd\xf2P*\x8dpPZ\xc3\x0e\xa1&'\xd1{,@\xd8\x02\xd0\xec\xb0(\xb0\x80\xe0\xa0\xd6\xcd^Y\x11\x1ce\x9c\xd7BVb\x8fP6V\x1eH\xd45\x92'\xc9\xb92\xb5\xa3\x00 g\xab\xd2\x84\x15g+\xe7W\x9c\xb3\x95W{\x9fU'>S.\xaf\x1a\x1f\x9cQ\xbf0\x17\xb5\xca\xab\x13_`\xed\xf3\xaa\xb1>P#\xbb\xac\xb7\xe1\x84\xde\x88\xfa\x8f\x18\xd7\x90\xc4\xb7Q\xd5\xa30:/m^\x920xpT-\xb1\x11\xb0\xe2)\xe7yn\x9dV6\xf0X+\x18\xa1l\x92\xc2\x13g?\x05\x0d=\x81\xdeSv\xeel\xa9\xf6\x0d\x89\x9dF\xce\xfa\xe0\xa5\x90\xc1\xd1#\x9cn\xc0\xe2\xe1\x0e\xfd\xf51\x96\xa4\x9c\xb3\x9e\xe13\xea\xd8\xc1\x01u;\x8d%3\x9e\x982V\xf7U\xf9\x103\xfe=\xd6\x90\xddM>=\xb5\x9c3i\x8a\x08yF\x9c;c\x84-\x92)\xc9\xba\x1bc\x92\x02\x129\x8a\xb5E\x8dkQ\xafc$\xe6\xcf<d7\xc7B\xc8\x99\xbb\x1bW\xe07\xad$\xce8\xb3\xab\x80\xc6\xa7\x9c1Uv,\xffl\xc0*\xdd\x913\xc2\xd0\x90\x8da\xceX\xcb\xd9\xd0\x85\xbe\x7fG\xd1h*(7\x84\xb7\xeev\x02\x9ak]\xce\x90\x1f\x13\xe5\x8cE\xc4f\x98\xe10\xbcd6\x8f5L\x8d}\x8c\xb7\xc3\x845\xb8*\xd60\x90\x0fC\xcc\xb6$\xac/\x1d\x19\xa4\xbe\x9a\xf4,\x02;\x9e\xdeMx\xc6$\xfd\x10\xa2\xeck\xbas\xe1N\xb9\x05\xd4\x1eA\x95\xb0\x7f\xc3\xc0'\xb4Hq\x87_\xc8O'\xbe\x81\xfd\x88\xc4\xe4o\xf4y\xfft\x16\xaf`$\xed%\xb2\xad\x9b\xbc\x99\x8f6u\x08X\xa59kS>\xe6\x9cn@\x9a\"\xbbx@\xd9D\xafgK\xa6\xd2\x84\xec\x96\x94\x0d\xda&H\x14\xe5\x9c\xcf.\x1eTH\xfeO9ky\xbb\xdc\xf0\xf7\x17\x10\xfe\x1b:9.\xe410K\x8a\xda\x83\xe5\xe1\xf3\xcd\x92\xd6*\xbd\x86\xeeg\xb1\xe1-?\x1aY\xdc\x8b\xa5x\xaf\xd2\xb7{rh\xc6P\xd4\x1cs\x8f\xd75\x06\xbf\x8c\x7f\x1a\xc2b@\\\x99Z'i\xfa\x8a\xed\x91b\xa1\xd4\x99O_\xf6\xef\xdd\xb5-%(\x1b\x90J!\xf1\xa9M!\xf9\xfec\xf7\x18p\xdd\x9f\x9et\xd2\xb6x\x7f\xb3kA\xfe^\xe8\xa4\x94Q\xe8w\x00\x00\x00\xff\xffPK\x07\x08\x01S\xc7P\x7f\x02\x00\x00\xb1\x06\x00\x00PK\x01\x02\x14\x03\x14\x00\x08\x00\x08\x00K\xa7pQ\xb1\xe0\xcew\x9c\x00\x00\x00\n\x01\x00\x00\n\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\xa0\x81\x00\x00\x00\x00go.mod.srcUT\x05\x00\x01\xef\xe7\xb2_PK\x01\x02\x14\x03\x14\x00\x08\x00\x08\x00C\xa7pQ\x01S\xc7P\x7f\x02\x00\x00\xb1\x06\x00\x00\x07\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\xa0\x81\xdd\x00\x00\x00main.goUT\x05\x00\x01\xdf\xe7\xb2_PK\x05\x06\x00\x00\x00\x00\x02\x00\x02\x00\x7f\x00\x00\x00\x9a\x03\x00\x00\x00\x00"
|
||||
fs.Register(data)
|
||||
}
|
||||
|
||||
20
cmd/pluginator/krmfunction/funcwrappersrc/fakeplugin.go
Normal file
20
cmd/pluginator/krmfunction/funcwrappersrc/fakeplugin.go
Normal file
@@ -0,0 +1,20 @@
|
||||
//nolint
|
||||
package funcwrappersrc
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
)
|
||||
|
||||
type plugin struct{}
|
||||
|
||||
//noinspection GoUnusedGlobalVariable
|
||||
var KustomizePlugin plugin
|
||||
|
||||
func (p *plugin) Config(
|
||||
_ *resmap.PluginHelpers, _ []byte) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *plugin) Transform(_ resmap.ResMap) error {
|
||||
return nil
|
||||
}
|
||||
12
cmd/pluginator/krmfunction/funcwrappersrc/go.mod.src
Normal file
12
cmd/pluginator/krmfunction/funcwrappersrc/go.mod.src
Normal file
@@ -0,0 +1,12 @@
|
||||
module main
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/spf13/cobra v1.0.0
|
||||
sigs.k8s.io/kustomize/api v0.6.4
|
||||
sigs.k8s.io/kustomize/kyaml v0.9.3
|
||||
sigs.k8s.io/yaml v1.2.0
|
||||
)
|
||||
|
||||
replace sigs.k8s.io/kustomize/api v0.6.4 => sigs.k8s.io/kustomize/api v0.0.0-20201116192201-a25429ae3b24
|
||||
77
cmd/pluginator/krmfunction/funcwrappersrc/main.go
Normal file
77
cmd/pluginator/krmfunction/funcwrappersrc/main.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// This file will be processed and embedded to pluginator.
|
||||
|
||||
package funcwrappersrc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/framework"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
//nolint
|
||||
func main() {
|
||||
var plugin resmap.Configurable
|
||||
resmapFactory := newResMapFactory()
|
||||
|
||||
pluginHelpers := newPluginHelpers(resmapFactory)
|
||||
|
||||
resourceList := &framework.ResourceList{}
|
||||
|
||||
cmd := framework.Command(resourceList, func() error {
|
||||
resMap, err := resmapFactory.NewResMapFromRNodeSlice(resourceList.Items)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pluginConfig, err := functionConfigToPluginConfig(resourceList.FunctionConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = plugin.Config(pluginHelpers, pluginConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if t, ok := plugin.(resmap.TransformerPlugin); ok {
|
||||
err = t.Transform(resMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if g, ok := plugin.(resmap.GeneratorPlugin); ok {
|
||||
resMap, err = g.Generate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
resourceList.Items, err = resMap.ToRNodeSlice()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err := cmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
//nolint
|
||||
func newPluginHelpers(resmapFactory *resmap.Factory) *resmap.PluginHelpers {
|
||||
return resmap.NewPluginHelpers(nil, nil, resmapFactory)
|
||||
}
|
||||
|
||||
//nolint
|
||||
func newResMapFactory() *resmap.Factory {
|
||||
resourceFactory := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
|
||||
return resmap.NewFactory(resourceFactory, nil)
|
||||
}
|
||||
|
||||
//nolint
|
||||
func functionConfigToPluginConfig(fc interface{}) ([]byte, error) {
|
||||
return yaml.Marshal(fc)
|
||||
}
|
||||
Reference in New Issue
Block a user