mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
add support for exec plugins
This commit is contained in:
@@ -1,72 +0,0 @@
|
||||
// +build plugin
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/pgmconfig"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
)
|
||||
|
||||
type plugin struct {
|
||||
name string
|
||||
input string
|
||||
rf *resmap.Factory
|
||||
}
|
||||
|
||||
var KustomizePlugin plugin
|
||||
|
||||
func (p *plugin) Config(
|
||||
ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error {
|
||||
dir := filepath.Join(pgmconfig.ConfigRoot(), "plugins")
|
||||
id := k.GetGvk()
|
||||
p.name = filepath.Join(dir, id.Group, id.Version, id.Kind)
|
||||
content, err := yaml.Marshal(k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.input = string(content)
|
||||
p.rf = rf
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *plugin) Generate() (resmap.ResMap, error) {
|
||||
return p.run(nil)
|
||||
}
|
||||
|
||||
func (p *plugin) Transformer(rm resmap.ResMap) error {
|
||||
result, err := p.run(rm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for id := range rm {
|
||||
delete(rm, id)
|
||||
}
|
||||
for id, r := range result {
|
||||
rm[id] = r
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *plugin) run(rm resmap.ResMap) (resmap.ResMap, error) {
|
||||
cmd := exec.Command(p.name, p.input)
|
||||
cmd.Env = os.Environ()
|
||||
if rm != nil {
|
||||
content, err := rm.EncodeAsYaml()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmd.Stdin = bytes.NewReader(content)
|
||||
}
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.rf.NewResMapFromBytes(output)
|
||||
}
|
||||
@@ -17,7 +17,9 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
|
||||
@@ -51,6 +53,15 @@ func DefaultSrcRoot() (string, error) {
|
||||
}
|
||||
nope = append(nope, root)
|
||||
|
||||
// get the root kustomize source directory when
|
||||
// GOPATH is not set
|
||||
_, filename, _, _ := runtime.Caller(1)
|
||||
root = path.Join(path.Dir(filename), "../..", plugin.PluginRoot)
|
||||
if FileExists(root) {
|
||||
return root, nil
|
||||
}
|
||||
nope = append(nope, root)
|
||||
|
||||
root = filepath.Join(
|
||||
pgmconfig.ConfigRoot(), plugin.PluginRoot)
|
||||
if FileExists(root) {
|
||||
|
||||
140
pkg/plugins/executable.go
Normal file
140
pkg/plugins/executable.go
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sigs.k8s.io/kustomize/pkg/types"
|
||||
"strings"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/pgmconfig"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
)
|
||||
|
||||
// ExecPlugin record the name and args of an executable
|
||||
// It triggers the executable generator and transformer
|
||||
type ExecPlugin struct {
|
||||
// name of the executable
|
||||
name string
|
||||
|
||||
// one line of arguments for the executable
|
||||
argOneLiner string
|
||||
|
||||
// relative file path to a file
|
||||
// Each line of this file is treated as one argument
|
||||
argsFromFile string
|
||||
|
||||
// resmap Factory to make resources
|
||||
rf *resmap.Factory
|
||||
|
||||
// loader to load files
|
||||
ldr ifc.Loader
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Config(
|
||||
ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error {
|
||||
dir := filepath.Join(pgmconfig.ConfigRoot(), "plugins")
|
||||
id := k.GetGvk()
|
||||
p.name = filepath.Join(dir, id.Group, id.Version, id.Kind)
|
||||
p.rf = rf
|
||||
p.ldr = ldr
|
||||
|
||||
var err error
|
||||
p.argOneLiner, err = k.GetFieldValue("arg")
|
||||
if err != nil && !isNoFieldError(err) {
|
||||
return err
|
||||
}
|
||||
p.argsFromFile, err = k.GetFieldValue("file")
|
||||
if err != nil && !isNoFieldError(err) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Generate() (resmap.ResMap, error) {
|
||||
args, err := p.getArgs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cmd := exec.Command(p.name, args...)
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Stderr = os.Stderr
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.rf.NewResMapFromBytes(output)
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Transform(rm resmap.ResMap) error {
|
||||
args, err := p.getArgs()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for id, r := range rm {
|
||||
content, err := yaml.Marshal(r.Kunstructured)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := exec.Command(p.name, args...)
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Stdin = bytes.NewReader(content)
|
||||
cmd.Stderr = os.Stderr
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tmpMap, err := p.rf.NewResMapFromBytes(output)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(tmpMap) != 1 {
|
||||
return fmt.Errorf("Unable to put two resources into one")
|
||||
}
|
||||
for _, v := range tmpMap {
|
||||
rm[id].Kunstructured = v.Kunstructured
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) getArgs() ([]string, error) {
|
||||
args := strings.Split(p.argOneLiner, " ")
|
||||
if p.argsFromFile != "" {
|
||||
content, err := p.ldr.Load(p.argsFromFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args = append(args, strings.Split(string(content), "\n")...)
|
||||
}
|
||||
return args, nil
|
||||
}
|
||||
|
||||
func isNoFieldError(e error) bool {
|
||||
_, ok := e.(types.NoFieldError)
|
||||
if ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -18,12 +18,10 @@ package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
"runtime"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
kplugin "sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/resid"
|
||||
@@ -96,28 +94,30 @@ func loadAndConfigurePlugin(
|
||||
ldr ifc.Loader,
|
||||
rf *resmap.Factory, res *resource.Resource) (Configurable, error) {
|
||||
var fileName string
|
||||
var c Configurable
|
||||
|
||||
exec := execPluginFileName(dir, id)
|
||||
if isExecAvailable(exec) {
|
||||
_, f, _, _ := runtime.Caller(1)
|
||||
fileName = filepath.Join(filepath.Dir(f), "builtin", "executable.so")
|
||||
c = &ExecPlugin{}
|
||||
} else {
|
||||
fileName = goPluginFileName(dir, id)
|
||||
goPlugin, err := plugin.Open(fileName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "plugin %s fails to load", fileName)
|
||||
}
|
||||
symbol, err := goPlugin.Lookup(kplugin.PluginSymbol)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(
|
||||
err, "plugin %s doesn't have symbol %s",
|
||||
fileName, kplugin.PluginSymbol)
|
||||
}
|
||||
var ok bool
|
||||
c, ok = symbol.(Configurable)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %s not configurable", fileName)
|
||||
}
|
||||
}
|
||||
goPlugin, err := plugin.Open(fileName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "plugin %s fails to load", fileName)
|
||||
}
|
||||
symbol, err := goPlugin.Lookup(kplugin.PluginSymbol)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(
|
||||
err, "plugin %s doesn't have symbol %s",
|
||||
fileName, kplugin.PluginSymbol)
|
||||
}
|
||||
c, ok := symbol.(Configurable)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %s not configurable", fileName)
|
||||
}
|
||||
err = c.Config(ldr, rf, res)
|
||||
err := c.Config(ldr, rf, res)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "plugin %s fails configuration", fileName)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user