Simplify plugin loader code.

* use one place to build plugin file names,
 * use one loader instance,
 * test for plugin enabled flag in just one place to
   avoid errors and reduce if statements,
 * don't return private objects,
 * factor goplugin loading to a method,
 * fix a related test that was commented out.
This commit is contained in:
jregan
2019-04-21 16:12:55 -07:00
committed by Jeffrey Regan
parent c9bf70fd4b
commit 76a3179868
13 changed files with 169 additions and 154 deletions

View File

@@ -3,8 +3,6 @@
package main
import (
"time"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers"
@@ -20,9 +18,17 @@ func (p *plugin) Config(
return nil
}
// Returns a constant, rather than
// time.Now().Format("2006-01-02")
// to make tests happy.
// This is just an example.
func getDate() string {
return "2018-05-11"
}
func (p *plugin) Transform(m resmap.ResMap) error {
tr, err := transformers.NewNamePrefixSuffixTransformer(
time.Now().Format("2006-01-02")+"-", "",
getDate()+"-", "",
config.MakeDefaultConfig().NamePrefix)
if err != nil {
return err

View File

@@ -12,6 +12,7 @@
package main
import (
"fmt"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers"
@@ -26,11 +27,11 @@ var KustomizePlugin plugin
func (p *plugin) Config(
ldr ifc.Loader, rf *resmap.Factory, k ifc.Kunstructured) error {
var err error
p.prefix, err = k.GetFieldValue("prefix")
if err != nil {
return err
name := k.GetName()
if name == "" {
return fmt.Errorf("name cannot be empty")
}
p.prefix = name + "-"
return nil
}