mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 08:20:59 +00:00
Convert plugins to accept bytes instead of unstruct.
This commit is contained in:
@@ -33,8 +33,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
ArgsOneLiner = "argsOneLiner"
|
||||
ArgsFromFile = "argsFromFile"
|
||||
idAnnotation = "kustomize.config.k8s.io/id"
|
||||
)
|
||||
|
||||
@@ -75,30 +73,26 @@ func (p *ExecPlugin) isAvailable() bool {
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Config(
|
||||
ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error {
|
||||
ldr ifc.Loader, rf *resmap.Factory, config []byte) error {
|
||||
p.rf = rf
|
||||
p.ldr = ldr
|
||||
|
||||
var err error
|
||||
p.cfg, err = yaml.Marshal(k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = p.processOptionalArgsFields(k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
p.cfg = config
|
||||
return p.processOptionalArgsFields()
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) processOptionalArgsFields(k ifc.Kunstructured) error {
|
||||
args, err := k.GetFieldValue(ArgsOneLiner)
|
||||
if err == nil && args != "" {
|
||||
p.args = strings.Split(args, " ")
|
||||
type argsConfig struct {
|
||||
ArgsOneLiner string `json:"argsOneLiner,omitempty" yaml:"argsOneLiner,omitempty"`
|
||||
ArgsFromFile string `json:"argsFromFile,omitempty" yaml:"argsFromFile,omitempty"`
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) processOptionalArgsFields() error {
|
||||
var c argsConfig
|
||||
yaml.Unmarshal(p.cfg, &c)
|
||||
if c.ArgsOneLiner != "" {
|
||||
p.args = strings.Split(c.ArgsOneLiner, " ")
|
||||
}
|
||||
fileName, err := k.GetFieldValue(ArgsFromFile)
|
||||
if err == nil && fileName != "" {
|
||||
content, err := p.ldr.Load(fileName)
|
||||
if c.ArgsFromFile != "" {
|
||||
content, err := p.ldr.Load(c.ArgsFromFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -40,8 +40,8 @@ func TestExecPluginConfig(t *testing.T) {
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "some-random-name",
|
||||
},
|
||||
ArgsOneLiner: "one two",
|
||||
ArgsFromFile: "sed-input.txt",
|
||||
"argsOneLiner": "one two",
|
||||
"argsFromFile": "sed-input.txt",
|
||||
})
|
||||
|
||||
ldr.AddFile("/app/sed-input.txt", []byte(`
|
||||
@@ -54,7 +54,11 @@ s/$BAR/bar/g
|
||||
plugin.DefaultPluginConfig().DirectoryPath,
|
||||
pluginConfig.Id())
|
||||
|
||||
p.Config(ldr, rf, pluginConfig)
|
||||
yaml, err := pluginConfig.AsYAML()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected err: %v", err)
|
||||
}
|
||||
p.Config(ldr, rf, yaml)
|
||||
|
||||
expected := "/kustomize/plugin/someteam.example.com/v1/SedTransformer"
|
||||
if !strings.HasSuffix(p.name, expected) {
|
||||
|
||||
@@ -31,7 +31,7 @@ import (
|
||||
)
|
||||
|
||||
type Configurable interface {
|
||||
Config(ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error
|
||||
Config(ldr ifc.Loader, rf *resmap.Factory, config []byte) error
|
||||
}
|
||||
|
||||
type Loader struct {
|
||||
@@ -114,7 +114,11 @@ func (l *Loader) loadAndConfigurePlugin(
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
err = c.Config(ldr, l.rf, res)
|
||||
yaml, err := res.AsYAML()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "marshalling yaml from res %s", res.Id())
|
||||
}
|
||||
err = c.Config(ldr, l.rf, yaml)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(
|
||||
err, "plugin %s fails configuration", res.Id())
|
||||
|
||||
Reference in New Issue
Block a user