mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
move files into internal
This commit is contained in:
176
cmd/pluginator/internal/builtinplugin/builtinplugin.go
Normal file
176
cmd/pluginator/internal/builtinplugin/builtinplugin.go
Normal file
@@ -0,0 +1,176 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinplugin
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/provenance"
|
||||
)
|
||||
|
||||
//go:generate stringer -type=pluginType
|
||||
type pluginType int
|
||||
|
||||
const packageForGeneratedCode = "builtins"
|
||||
|
||||
const (
|
||||
unknown pluginType = iota
|
||||
Transformer
|
||||
Generator
|
||||
)
|
||||
|
||||
// ConvertToBuiltInPlugin converts the input plugin file to
|
||||
// kustomize builtin plugin and writes it to proper directory
|
||||
func ConvertToBuiltInPlugin() error {
|
||||
root, err := inputFileRoot()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.Open(root + ".go")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
err = readToPackageMain(scanner, file.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w, err := newWriter(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer w.close()
|
||||
|
||||
// This particular phrasing is required.
|
||||
w.write(
|
||||
fmt.Sprintf(
|
||||
"// Code generated by pluginator on %s; DO NOT EDIT.",
|
||||
root))
|
||||
w.write(
|
||||
fmt.Sprintf(
|
||||
"// pluginator %s\n", provenance.GetProvenance().Short()))
|
||||
w.write("\n")
|
||||
w.write("package " + packageForGeneratedCode)
|
||||
|
||||
pType := unknown
|
||||
|
||||
for scanner.Scan() {
|
||||
l := scanner.Text()
|
||||
if strings.HasPrefix(l, "//go:generate") {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(l, "//noinspection") {
|
||||
continue
|
||||
}
|
||||
if l == "var "+konfig.PluginSymbol+" plugin" {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(l, " Transform(") {
|
||||
if pType != unknown {
|
||||
return fmt.Errorf("unexpected Transform(")
|
||||
}
|
||||
pType = Transformer
|
||||
} else if strings.Contains(l, " Generate(") {
|
||||
if pType != unknown {
|
||||
return fmt.Errorf("unexpected Generate(")
|
||||
}
|
||||
pType = Generator
|
||||
}
|
||||
w.write(l)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
w.write("")
|
||||
w.write("func New" + root + "Plugin() resmap." + pType.String() + "Plugin {")
|
||||
w.write(" return &" + root + "Plugin{}")
|
||||
w.write("}")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func inputFileRoot() (string, error) {
|
||||
n := os.Getenv("GOFILE")
|
||||
if !strings.HasSuffix(n, ".go") {
|
||||
return "", fmt.Errorf("%+v, expecting .go suffix on %s", provenance.GetProvenance(), n)
|
||||
}
|
||||
return n[:len(n)-len(".go")], nil
|
||||
}
|
||||
|
||||
func readToPackageMain(s *bufio.Scanner, f string) error {
|
||||
gotMain := false
|
||||
for !gotMain && s.Scan() {
|
||||
gotMain = strings.HasPrefix(s.Text(), "package main")
|
||||
}
|
||||
if !gotMain {
|
||||
return fmt.Errorf("%s missing package main", f)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type writer struct {
|
||||
root string
|
||||
f *os.File
|
||||
}
|
||||
|
||||
func newWriter(r string) (*writer, error) {
|
||||
n := makeOutputFileName(r)
|
||||
f, err := os.Create(n)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to create `%s`; %v", n, err)
|
||||
}
|
||||
return &writer{root: r, f: f}, nil
|
||||
}
|
||||
|
||||
// Assume that this command is running with a $PWD of
|
||||
// $HOME/kustomize/plugin/builtin/secretGenerator
|
||||
// (for example). Then we want to write to
|
||||
// $HOME/kustomize/api/builtins
|
||||
func makeOutputFileName(root string) string {
|
||||
return filepath.Join(
|
||||
"..", "..", "..", "api", packageForGeneratedCode, root+".go")
|
||||
}
|
||||
|
||||
func (w *writer) close() {
|
||||
// Do this for debugging.
|
||||
// fmt.Println("Generated " + makeOutputFileName(w.root))
|
||||
w.f.Close()
|
||||
}
|
||||
|
||||
func (w *writer) write(line string) {
|
||||
_, err := w.f.WriteString(w.filter(line) + "\n")
|
||||
if err != nil {
|
||||
fmt.Printf("Trouble writing: %s", line)
|
||||
fmt.Printf("Error: %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *writer) filter(in string) string {
|
||||
if ok, newer := w.replace(in, "type plugin struct"); ok {
|
||||
return newer
|
||||
}
|
||||
if ok, newer := w.replace(in, "*plugin)"); ok {
|
||||
return newer
|
||||
}
|
||||
return in
|
||||
}
|
||||
|
||||
// replace 'plugin' with 'FooPlugin' in context
|
||||
// sensitive manner.
|
||||
func (w *writer) replace(in, target string) (bool, string) {
|
||||
if !strings.Contains(in, target) {
|
||||
return false, ""
|
||||
}
|
||||
newer := strings.Replace(
|
||||
target, "plugin", w.root+"Plugin", 1)
|
||||
return true, strings.Replace(in, target, newer, 1)
|
||||
}
|
||||
25
cmd/pluginator/internal/builtinplugin/plugintype_string.go
Normal file
25
cmd/pluginator/internal/builtinplugin/plugintype_string.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Code generated by "stringer -type=pluginType"; DO NOT EDIT.
|
||||
|
||||
package builtinplugin
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[unknown-0]
|
||||
_ = x[Transformer-1]
|
||||
_ = x[Generator-2]
|
||||
}
|
||||
|
||||
const _pluginType_name = "unknownTransformerGenerator"
|
||||
|
||||
var _pluginType_index = [...]uint8{0, 7, 18, 27}
|
||||
|
||||
func (i pluginType) String() string {
|
||||
if i < 0 || i >= pluginType(len(_pluginType_index)-1) {
|
||||
return "pluginType(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _pluginType_name[_pluginType_index[i]:_pluginType_index[i+1]]
|
||||
}
|
||||
30
cmd/pluginator/internal/krmfunction/cmd.go
Normal file
30
cmd/pluginator/internal/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/internal/krmfunction/converter.go
Normal file
165
cmd/pluginator/internal/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/internal/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/internal/krmfunction/converter_test.go
Normal file
234
cmd/pluginator/internal/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/internal/krmfunction/funcwrapper/statik.go
Normal file
14
cmd/pluginator/internal/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\x00\x98\xa0rQ\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\x00 \x00go.mod.srcUT\x05\x00\x01a~\xb5_\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\x00\x98\xa0rQ\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07\x00 \x00main.goUT\x05\x00\x01a~\xb5_\x94TMo\xdb8\x10=\x93\xbfb\xd6\x87\x85\xb40$\xec-H\xe0S\x90\xec\x06m\x82 \xf5\xad\xe8\x81\xa6F\x0e!~\x08C\xaaN\x1a\xe8\xbf\x17\xd4\x87#\xa9N\x9a^\x0cx\xf4\xe6\xbd73\x9c\xc9s\xd8>*\x0f\xa5\xd2\x08\x07\xa55\xec\x10jr\x12\xbd\xc7\x02\x84-\x00\xcd\x0e\x8b\x02\x0b\x08\x0ej\xdd\xec\x95\x15\xc1Q\xc6y-d%\xf6\x08ec\xe5\x81D]#y\x92\x9c+S;\n\x90p\xb6*MXq\xb6r~\xc59[y\xb5\xf7Yu\xe63\xe5\xf2\xaa\xf1\xc1\x19\xf5\x03sQ\xab\xbc:\xf3\x05\xd6>\xaf\x1a\xeb\x035\xb2\xcbz\x1bN\xe8\x8d\xa8\x7f\x8bq\x0dI|\x1bU=\x0b\xa3\xf3\xd2\xe6% \x83\x07G\xd5\x12\x1b\x01+\x9er\x9e\xe7\xd6ie\x03\x8f\xb5\x82\x11\xca&)\xbcp\xf6]\xd0\xd0\x13\xe8=e\x97\xce\x96j\xdf\x90\xd8i\xe4\xac\x0f^\x0b\x19\x1c=\xc3\xf9\x06,\x1e\x1e\xd0\xdf\x1ecI\xca9\xeb\x19\xfeG\x1d;8\xa0\xee\xa7\xb1d\xc6\x13S\xc6\xea>+\x1fb\xc6\xdf\xc7\x1a\xb2\x87\xc9\xa7\x97\x96s&M\x11!\xaf\x88Kg\x8c\xb0E2%YwcLR@\"G\xb1\xb6\xa8q+\xeau\x8c\xc4\xfc\x99\x87\xec\xeeX\x089\xf3p\xe7\n\xfc\xa2\x95\xc4\x19gv\x13\xd0\xf8\x943\xa6\xca\x8e\xe5\xaf\x0dX\xa5;rF\x18\x1a\xb21\xcc\x19k9\x1b\xba\xd0\xf7\xef(\x1aM\x05\xe5\x86\xf0\xd6\xddO@s\xad\xeb\x19\xf2c\xa2\x9c\xb1\x88\xd8\x0c3\x1c\x86\x97\xcc\xe6\xb1\x86\xa9\xb1\x8f\xf1v\x98\xb0\x06W\xc5\x1a\x06\xf2a\x88\xd9\x96\x84\xf5\xa5#\x83\xd4W\x93^D`\xc7\xd3\xbb \xaf\x98\xa4\x1fB\x94=\xa5;\x17\xee\x94[@\xed\x11T \xfb7\x0c\xfc\x87\x16)\xee\xf0/\xf2\xd3\x89o`?\"1\xf9\x13}\xde?\x9d\xc5+\x18I{\x89l\xeb&o\xe6\xa3M\x1d\x02Vi\xce\xda\x94\x8f9\xe7\x1b\x90\xa6\xc8\xae\x9eP6\xd1\xeb\xc5\x92\xa94!\xbb'e\x83\xb6 \x12E9\xe7\xb3\xab'\x15\x92\x7fS\xceZ\xde.7\xfc\xfd\x05\x84\x7f\x86N\x8e\x0by\x0c\xcc\x92\xa2\xf6`y\xf8|\xb7\xa4\xb5J\xaf\xa1\xfbYl\xf8)G\x8b\xc3\xb1t\xd1\xcb\xf5}\x9f\\\x9c1\x14\xc5\xc7\xdc\xe3\x99\x8d\xc1O\xe3\x9f\x86\xb0\x18\x107\xa6\xd6I\x9a\x9e\xf0?R,\x94\xba*N\xd8~w\x7fK \xca\x06\xa4RH|iSH\xbe~\xdb=\x07\\\xf77(\x9d\xf4/\x1e\xe2\xecV\x90\x7f\x14:)e\x14\xfa\x19\x00\x00\xff\xffPK\x07\x08\xd7\xcf\xa4\xce}\x02\x00\x00\xba\x06\x00\x00PK\x01\x02\x14\x03\x14\x00\x08\x00\x08\x00\x98\xa0rQ\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\x01a~\xb5_PK\x01\x02\x14\x03\x14\x00\x08\x00\x08\x00\x98\xa0rQ\xd7\xcf\xa4\xce}\x02\x00\x00\xba\x06\x00\x00\x07\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\xa0\x81\xdd\x00\x00\x00main.goUT\x05\x00\x01a~\xb5_PK\x05\x06\x00\x00\x00\x00\x02\x00\x02\x00\x7f\x00\x00\x00\x98\x03\x00\x00\x00\x00"
|
||||
fs.Register(data)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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/internal/krmfunction/funcwrappersrc/main.go
Normal file
77
cmd/pluginator/internal/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