Merge pull request #1532 from monopole/runBuiltinSansFlag

Ease configuring and running the builtin plugins.
This commit is contained in:
Kubernetes Prow Robot
2019-09-13 15:28:00 -07:00
committed by GitHub
33 changed files with 596 additions and 432 deletions

View File

@@ -16,6 +16,15 @@ import (
"sigs.k8s.io/kustomize/v3/pkg/plugins" "sigs.k8s.io/kustomize/v3/pkg/plugins"
) )
//go:generate stringer -type=pluginType
type pluginType int
const (
unknown pluginType = iota
Transformer
Generator
)
func main() { func main() {
root := inputFileRoot() root := inputFileRoot()
file, err := os.Open(root + ".go") file, err := os.Open(root + ".go")
@@ -34,24 +43,41 @@ func main() {
fmt.Sprintf( fmt.Sprintf(
"// Code generated by pluginator on %s; DO NOT EDIT.", "// Code generated by pluginator on %s; DO NOT EDIT.",
root)) root))
w.write("package builtin") w.write("package " + plugins.BuiltinPluginPackage)
pType := unknown
for scanner.Scan() { for scanner.Scan() {
l := scanner.Text() l := scanner.Text()
if strings.HasPrefix(l, "//go:generate") { if strings.HasPrefix(l, "//go:generate") {
continue continue
} }
if l == "var "+plugins.PluginSymbol+" plugin" { if strings.HasPrefix(l, "//noinspection") {
w.write("func New" + root + "Plugin() *" + root + "Plugin {")
w.write(" return &" + root + "Plugin{}")
w.write("}")
continue continue
} }
if l == "var "+plugins.PluginSymbol+" plugin" {
continue
}
if strings.Contains(l, " Transform(") {
if pType != unknown {
log.Fatal("unexpected Transform(")
}
pType = Transformer
} else if strings.Contains(l, " Generate(") {
if pType != unknown {
log.Fatal("unexpected Generate(")
}
pType = Generator
}
w.write(l) w.write(l)
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
w.write("")
w.write("func New" + root + "Plugin() resmap." + pType.String() + "Plugin {")
w.write(" return &" + root + "Plugin{}")
w.write("}")
} }
func inputFileRoot() string { func inputFileRoot() string {
@@ -93,7 +119,7 @@ func makeOutputFileName(root string) string {
pgmconfig.DomainName, pgmconfig.DomainName,
pgmconfig.ProgramName, pgmconfig.ProgramName,
pgmconfig.PluginRoot, pgmconfig.PluginRoot,
"builtin", plugins.BuiltinPluginPackage,
root+".go") root+".go")
} }

View File

@@ -0,0 +1,25 @@
// Code generated by "stringer -type=pluginType"; DO NOT EDIT.
package main
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]]
}

View File

@@ -135,7 +135,7 @@ func (ra *ResAccumulator) makeVarReplacementMap() (map[string]interface{}, error
return result, nil return result, nil
} }
func (ra *ResAccumulator) Transform(t transformers.Transformer) error { func (ra *ResAccumulator) Transform(t resmap.Transformer) error {
return t.Transform(ra.resMap) return t.Transform(ra.resMap)
} }

View File

@@ -0,0 +1,37 @@
// Code generated by "stringer -type=BuiltinPluginType"; DO NOT EDIT.
package plugins
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[SecretGenerator-1]
_ = x[ConfigMapGenerator-2]
_ = x[ReplicaCountTransformer-3]
_ = x[NamespaceTransformer-4]
_ = x[PatchJson6902Transformer-5]
_ = x[PatchStrategicMergeTransformer-6]
_ = x[PatchTransformer-7]
_ = x[LabelTransformer-8]
_ = x[AnnotationsTransformer-9]
_ = x[PrefixSuffixTransformer-10]
_ = x[ImageTagTransformer-11]
_ = x[HashTransformer-12]
_ = x[InventoryTransformer-13]
_ = x[LegacyOrderTransformer-14]
}
const _BuiltinPluginType_name = "UnknownSecretGeneratorConfigMapGeneratorReplicaCountTransformerNamespaceTransformerPatchJson6902TransformerPatchStrategicMergeTransformerPatchTransformerLabelTransformerAnnotationsTransformerPrefixSuffixTransformerImageTagTransformerHashTransformerInventoryTransformerLegacyOrderTransformer"
var _BuiltinPluginType_index = [...]uint16{0, 7, 22, 40, 63, 83, 107, 137, 153, 169, 191, 214, 233, 248, 268, 290}
func (i BuiltinPluginType) String() string {
if i < 0 || i >= BuiltinPluginType(len(_BuiltinPluginType_index)-1) {
return "BuiltinPluginType(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _BuiltinPluginType_name[_BuiltinPluginType_index[i]:_BuiltinPluginType_index[i+1]]
}

75
pkg/plugins/builtins.go Normal file
View File

@@ -0,0 +1,75 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package plugins
import (
"sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/plugin/builtin"
)
//go:generate stringer -type=BuiltinPluginType
type BuiltinPluginType int
const (
Unknown BuiltinPluginType = iota
SecretGenerator
ConfigMapGenerator
ReplicaCountTransformer
NamespaceTransformer
PatchJson6902Transformer
PatchStrategicMergeTransformer
PatchTransformer
LabelTransformer
AnnotationsTransformer
PrefixSuffixTransformer
ImageTagTransformer
HashTransformer
InventoryTransformer
LegacyOrderTransformer
)
var stringToBuiltinPluginTypeMap map[string]BuiltinPluginType
func init() {
stringToBuiltinPluginTypeMap = makeStringToBuiltinPluginTypeMap()
}
func makeStringToBuiltinPluginTypeMap() (result map[string]BuiltinPluginType) {
result = make(map[string]BuiltinPluginType, 23)
for k := range GeneratorFactories {
result[k.String()] = k
}
for k := range TransformerFactories {
result[k.String()] = k
}
return
}
func GetBuiltinPluginType(n string) BuiltinPluginType {
result, ok := stringToBuiltinPluginTypeMap[n]
if ok {
return result
}
return Unknown
}
var GeneratorFactories = map[BuiltinPluginType]func() resmap.GeneratorPlugin{
SecretGenerator: builtin.NewSecretGeneratorPlugin,
ConfigMapGenerator: builtin.NewConfigMapGeneratorPlugin,
}
var TransformerFactories = map[BuiltinPluginType]func() resmap.TransformerPlugin{
NamespaceTransformer: builtin.NewNamespaceTransformerPlugin,
ReplicaCountTransformer: builtin.NewReplicaCountTransformerPlugin,
PatchJson6902Transformer: builtin.NewPatchJson6902TransformerPlugin,
PatchStrategicMergeTransformer: builtin.NewPatchStrategicMergeTransformerPlugin,
PatchTransformer: builtin.NewPatchTransformerPlugin,
LabelTransformer: builtin.NewLabelTransformerPlugin,
AnnotationsTransformer: builtin.NewAnnotationsTransformerPlugin,
PrefixSuffixTransformer: builtin.NewPrefixSuffixTransformerPlugin,
ImageTagTransformer: builtin.NewImageTagTransformerPlugin,
HashTransformer: builtin.NewHashTransformerPlugin,
InventoryTransformer: builtin.NewInventoryTransformerPlugin,
LegacyOrderTransformer: builtin.NewLegacyOrderTransformerPlugin,
}

View File

@@ -12,13 +12,15 @@ import (
) )
const ( const (
PluginSymbol = "KustomizePlugin" PluginSymbol = "KustomizePlugin"
flagEnablePluginsName = "enable_alpha_plugins" BuiltinPluginPackage = "builtin"
flagEnablePluginsHelp = `enable plugins, an alpha feature. BuiltinPluginApiVersion = BuiltinPluginPackage
flagEnablePluginsName = "enable_alpha_plugins"
flagEnablePluginsHelp = `enable plugins, an alpha feature.
See https://github.com/kubernetes-sigs/kustomize/blob/master/docs/plugins.md See https://github.com/kubernetes-sigs/kustomize/blob/master/docs/plugins.md
` `
flagErrorFmt = ` flagErrorFmt = `
unable to load plugin %s because plugins disabled unable to load external plugin %s because plugins disabled
specify the flag specify the flag
--%s --%s
to %s` to %s`

View File

@@ -11,18 +11,14 @@ import (
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"sigs.k8s.io/kustomize/v3/pkg/gvk"
"sigs.k8s.io/kustomize/v3/pkg/ifc" "sigs.k8s.io/kustomize/v3/pkg/ifc"
"sigs.k8s.io/kustomize/v3/pkg/resid" "sigs.k8s.io/kustomize/v3/pkg/resid"
"sigs.k8s.io/kustomize/v3/pkg/resmap" "sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/pkg/resource" "sigs.k8s.io/kustomize/v3/pkg/resource"
"sigs.k8s.io/kustomize/v3/pkg/transformers"
"sigs.k8s.io/kustomize/v3/pkg/types" "sigs.k8s.io/kustomize/v3/pkg/types"
) )
type Configurable interface {
Config(ldr ifc.Loader, rf *resmap.Factory, config []byte) error
}
type Loader struct { type Loader struct {
pc *types.PluginConfig pc *types.PluginConfig
rf *resmap.Factory rf *resmap.Factory
@@ -34,8 +30,8 @@ func NewLoader(
} }
func (l *Loader) LoadGenerators( func (l *Loader) LoadGenerators(
ldr ifc.Loader, rm resmap.ResMap) ([]transformers.Generator, error) { ldr ifc.Loader, rm resmap.ResMap) ([]resmap.Generator, error) {
var result []transformers.Generator var result []resmap.Generator
for _, res := range rm.Resources() { for _, res := range rm.Resources() {
g, err := l.LoadGenerator(ldr, res) g, err := l.LoadGenerator(ldr, res)
if err != nil { if err != nil {
@@ -47,12 +43,12 @@ func (l *Loader) LoadGenerators(
} }
func (l *Loader) LoadGenerator( func (l *Loader) LoadGenerator(
ldr ifc.Loader, res *resource.Resource) (transformers.Generator, error) { ldr ifc.Loader, res *resource.Resource) (resmap.Generator, error) {
c, err := l.loadAndConfigurePlugin(ldr, res) c, err := l.loadAndConfigurePlugin(ldr, res)
if err != nil { if err != nil {
return nil, err return nil, err
} }
g, ok := c.(transformers.Generator) g, ok := c.(resmap.Generator)
if !ok { if !ok {
return nil, fmt.Errorf("plugin %s not a generator", res.OrgId()) return nil, fmt.Errorf("plugin %s not a generator", res.OrgId())
} }
@@ -60,8 +56,8 @@ func (l *Loader) LoadGenerator(
} }
func (l *Loader) LoadTransformers( func (l *Loader) LoadTransformers(
ldr ifc.Loader, rm resmap.ResMap) ([]transformers.Transformer, error) { ldr ifc.Loader, rm resmap.ResMap) ([]resmap.Transformer, error) {
var result []transformers.Transformer var result []resmap.Transformer
for _, res := range rm.Resources() { for _, res := range rm.Resources() {
t, err := l.LoadTransformer(ldr, res) t, err := l.LoadTransformer(ldr, res)
if err != nil { if err != nil {
@@ -73,12 +69,12 @@ func (l *Loader) LoadTransformers(
} }
func (l *Loader) LoadTransformer( func (l *Loader) LoadTransformer(
ldr ifc.Loader, res *resource.Resource) (transformers.Transformer, error) { ldr ifc.Loader, res *resource.Resource) (resmap.Transformer, error) {
c, err := l.loadAndConfigurePlugin(ldr, res) c, err := l.loadAndConfigurePlugin(ldr, res)
if err != nil { if err != nil {
return nil, err return nil, err
} }
t, ok := c.(transformers.Transformer) t, ok := c.(resmap.Transformer)
if !ok { if !ok {
return nil, fmt.Errorf("plugin %s not a transformer", res.OrgId()) return nil, fmt.Errorf("plugin %s not a transformer", res.OrgId())
} }
@@ -101,13 +97,25 @@ func (l *Loader) absolutePluginPath(id resid.ResId) string {
return AbsolutePluginPath(l.pc, id) return AbsolutePluginPath(l.pc, id)
} }
// TODO: https://github.com/kubernetes-sigs/kustomize/issues/1164 func isBuiltinPlugin(res *resource.Resource) bool {
// TODO: the special string should appear in Group, not Version.
return res.GetGvk().Group == "" &&
res.GetGvk().Version == BuiltinPluginApiVersion
}
func (l *Loader) loadAndConfigurePlugin( func (l *Loader) loadAndConfigurePlugin(
ldr ifc.Loader, res *resource.Resource) (Configurable, error) { ldr ifc.Loader, res *resource.Resource) (c resmap.Configurable, err error) {
if !l.pc.Enabled { if isBuiltinPlugin(res) {
return nil, NotEnabledErr(res.OrgId().Kind) // Instead of looking for and loading a .so file, just
// instantiate the plugin from a generated factory
// function (see "pluginator"). Being able to do this
// is what makes a plugin "builtin".
c, err = l.makeBuiltinPlugin(res.GetGvk())
} else if l.pc.Enabled {
c, err = l.loadPlugin(res.OrgId())
} else {
err = NotEnabledErr(res.OrgId().Kind)
} }
c, err := l.loadPlugin(res.OrgId())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -123,7 +131,18 @@ func (l *Loader) loadAndConfigurePlugin(
return c, nil return c, nil
} }
func (l *Loader) loadPlugin(resId resid.ResId) (Configurable, error) { func (l *Loader) makeBuiltinPlugin(r gvk.Gvk) (resmap.Configurable, error) {
bpt := GetBuiltinPluginType(r.Kind)
if f, ok := GeneratorFactories[bpt]; ok {
return f(), nil
}
if f, ok := TransformerFactories[bpt]; ok {
return f(), nil
}
return nil, errors.Errorf("unable to load builtin %s", r)
}
func (l *Loader) loadPlugin(resId resid.ResId) (resmap.Configurable, error) {
p := NewExecPlugin(l.absolutePluginPath(resId)) p := NewExecPlugin(l.absolutePluginPath(resId))
if p.isAvailable() { if p.isAvailable() {
return p, nil return p, nil
@@ -141,9 +160,9 @@ func (l *Loader) loadPlugin(resId resid.ResId) (Configurable, error) {
// but the loaded .so files are in shared memory, so one will get // but the loaded .so files are in shared memory, so one will get
// "this plugin already loaded" errors if the registry is maintained // "this plugin already loaded" errors if the registry is maintained
// as a Loader instance variable. So make it a package variable. // as a Loader instance variable. So make it a package variable.
var registry = make(map[string]Configurable) var registry = make(map[string]resmap.Configurable)
func (l *Loader) loadGoPlugin(id resid.ResId) (Configurable, error) { func (l *Loader) loadGoPlugin(id resid.ResId) (resmap.Configurable, error) {
regId := relativePluginPath(id) regId := relativePluginPath(id)
if c, ok := registry[regId]; ok { if c, ok := registry[regId]; ok {
return copyPlugin(c), nil return copyPlugin(c), nil
@@ -159,7 +178,7 @@ func (l *Loader) loadGoPlugin(id resid.ResId) (Configurable, error) {
err, "plugin %s doesn't have symbol %s", err, "plugin %s doesn't have symbol %s",
regId, PluginSymbol) regId, PluginSymbol)
} }
c, ok := symbol.(Configurable) c, ok := symbol.(resmap.Configurable)
if !ok { if !ok {
return nil, fmt.Errorf("plugin %s not configurable", regId) return nil, fmt.Errorf("plugin %s not configurable", regId)
} }
@@ -167,10 +186,10 @@ func (l *Loader) loadGoPlugin(id resid.ResId) (Configurable, error) {
return copyPlugin(c), nil return copyPlugin(c), nil
} }
func copyPlugin(c Configurable) Configurable { func copyPlugin(c resmap.Configurable) resmap.Configurable {
indirect := reflect.Indirect(reflect.ValueOf(c)) indirect := reflect.Indirect(reflect.ValueOf(c))
newIndirect := reflect.New(indirect.Type()) newIndirect := reflect.New(indirect.Type())
newIndirect.Elem().Set(reflect.ValueOf(indirect.Interface())) newIndirect.Elem().Set(reflect.ValueOf(indirect.Interface()))
newNamed := newIndirect.Interface() newNamed := newIndirect.Interface()
return newNamed.(Configurable) return newNamed.(resmap.Configurable)
} }

View File

@@ -11,13 +11,45 @@ import (
"regexp" "regexp"
"github.com/pkg/errors" "github.com/pkg/errors"
"sigs.k8s.io/kustomize/v3/pkg/ifc"
"sigs.k8s.io/kustomize/v3/pkg/resid" "sigs.k8s.io/kustomize/v3/pkg/resid"
"sigs.k8s.io/kustomize/v3/pkg/resource" "sigs.k8s.io/kustomize/v3/pkg/resource"
"sigs.k8s.io/kustomize/v3/pkg/types" "sigs.k8s.io/kustomize/v3/pkg/types"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
) )
// A Transformer modifies an instance of ResMap.
type Transformer interface {
// Transform modifies data in the argument,
// e.g. adding labels to resources that can be labelled.
Transform(m ResMap) error
}
// A Generator creates an instance of ResMap.
type Generator interface {
Generate() (ResMap, error)
}
// Something that's configurable accepts a config
// object (typically YAML in []byte form), and an
// ifc.Loader to possible read more configuration
// from the file system (e.g. patch files) and
// a resource factory to build any type-sensitive
// parts. The factory could probably be factored out.
type Configurable interface {
Config(ldr ifc.Loader, rf *Factory, config []byte) error
}
type GeneratorPlugin interface {
Generator
Configurable
}
type TransformerPlugin interface {
Transformer
Configurable
}
// ResMap is an interface describing operations on the // ResMap is an interface describing operations on the
// core kustomize data structure, a list of Resources. // core kustomize data structure, a list of Resources.
// //

View File

@@ -160,7 +160,7 @@ func (kt *KustTarget) makeCustomizedResMap(
func (kt *KustTarget) addHashesToNames( func (kt *KustTarget) addHashesToNames(
ra *accumulator.ResAccumulator) error { ra *accumulator.ResAccumulator) error {
p := builtin.NewHashTransformerPlugin() p := builtin.NewHashTransformerPlugin()
err := kt.configureBuiltinPlugin(p, nil, "hash") err := kt.configureBuiltinPlugin(p, nil, plugins.HashTransformer)
if err != nil { if err != nil {
return err return err
} }
@@ -181,7 +181,6 @@ func (kt *KustTarget) computeInventory(
return fmt.Errorf("namespace mismatch") return fmt.Errorf("namespace mismatch")
} }
p := builtin.NewInventoryTransformerPlugin()
var c struct { var c struct {
Policy string Policy string
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"` types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
@@ -189,8 +188,8 @@ func (kt *KustTarget) computeInventory(
c.Name = inv.ConfigMap.Name c.Name = inv.ConfigMap.Name
c.Namespace = inv.ConfigMap.Namespace c.Namespace = inv.ConfigMap.Namespace
c.Policy = garbagePolicy.String() c.Policy = garbagePolicy.String()
p := builtin.NewInventoryTransformerPlugin()
err := kt.configureBuiltinPlugin(p, c, "inventory") err := kt.configureBuiltinPlugin(p, c, plugins.InventoryTransformer)
if err != nil { if err != nil {
return err return err
} }
@@ -283,7 +282,7 @@ func (kt *KustTarget) runGenerators(
return nil return nil
} }
func (kt *KustTarget) configureExternalGenerators() ([]transformers.Generator, error) { func (kt *KustTarget) configureExternalGenerators() ([]resmap.Generator, error) {
ra := accumulator.MakeEmptyAccumulator() ra := accumulator.MakeEmptyAccumulator()
err := kt.accumulateResources(ra, kt.kustomization.Generators) err := kt.accumulateResources(ra, kt.kustomization.Generators)
if err != nil { if err != nil {
@@ -293,7 +292,7 @@ func (kt *KustTarget) configureExternalGenerators() ([]transformers.Generator, e
} }
func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error { func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error {
var r []transformers.Transformer var r []resmap.Transformer
tConfig := ra.GetTransformerConfig() tConfig := ra.GetTransformerConfig()
lts, err := kt.configureBuiltinTransformers(tConfig) lts, err := kt.configureBuiltinTransformers(tConfig)
if err != nil { if err != nil {
@@ -309,7 +308,7 @@ func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error {
return ra.Transform(t) return ra.Transform(t)
} }
func (kt *KustTarget) configureExternalTransformers() ([]transformers.Transformer, error) { func (kt *KustTarget) configureExternalTransformers() ([]resmap.Transformer, error) {
ra := accumulator.MakeEmptyAccumulator() ra := accumulator.MakeEmptyAccumulator()
err := kt.accumulateResources(ra, kt.kustomization.Transformers) err := kt.accumulateResources(ra, kt.kustomization.Transformers)
if err != nil { if err != nil {
@@ -374,3 +373,20 @@ func (kt *KustTarget) accumulateFile(
} }
return nil return nil
} }
func (kt *KustTarget) configureBuiltinPlugin(
p resmap.Configurable, c interface{}, bpt plugins.BuiltinPluginType) (err error) {
var y []byte
if c != nil {
y, err = yaml.Marshal(c)
if err != nil {
return errors.Wrapf(
err, "builtin %s marshal", bpt)
}
}
err = p.Config(kt.ldr, kt.rFactory, y)
if err != nil {
return errors.Wrapf(err, "builtin %s config: %v", bpt, y)
}
return nil
}

View File

@@ -4,50 +4,36 @@
package target package target
import ( import (
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/v3/pkg/image" "sigs.k8s.io/kustomize/v3/pkg/image"
"sigs.k8s.io/kustomize/v3/pkg/plugins" "sigs.k8s.io/kustomize/v3/pkg/plugins"
"sigs.k8s.io/kustomize/v3/pkg/transformers" "sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/pkg/transformers/config" "sigs.k8s.io/kustomize/v3/pkg/transformers/config"
"sigs.k8s.io/kustomize/v3/pkg/types" "sigs.k8s.io/kustomize/v3/pkg/types"
"sigs.k8s.io/kustomize/v3/plugin/builtin"
"sigs.k8s.io/yaml"
) )
// Functions dedicated to configuring the builtin // Functions dedicated to configuring the builtin
// transformer and generator plugins using config data // transformer and generator plugins using config data
// read from a kustomization file. // read from a kustomization file and from the
// config.TransformerConfig, whose data may be a
// mix of hardcoded values and data read from file.
// //
// Non-builtin plugins will get their configuration // Non-builtin plugins will get their configuration
// from their own dedicated structs and yaml files. // from their own dedicated structs and YAML files.
// //
// There are some loops in the functions below because // There are some loops in the functions below because
// the kustomization file would, say, allow one to // the kustomization file would, say, allow someone to
// request multiple secrets be made, or run multiple // request multiple secrets be made, or run multiple
// image tag transforms, so we need to run the plugins // image tag transforms. In these cases, we'll need
// N times (plugins are easier to write, configure and // N plugin instances with differing configurations.
// test if they do just one thing).
//
// TODO: Push code down into the plugins, as the first pass
// at this writes plugins as thin layers over calls
// into existing packages. The builtin plugins should
// be viewed as examples, and the packages they access
// directory should be public, while everything else
// should go into internal.
type generatorConfigurator func() ([]transformers.Generator, error)
type transformerConfigurator func(
tConfig *config.TransformerConfig) ([]transformers.Transformer, error)
func (kt *KustTarget) configureBuiltinGenerators() ( func (kt *KustTarget) configureBuiltinGenerators() (
[]transformers.Generator, error) { result []resmap.Generator, err error) {
configurators := []generatorConfigurator{ for _, bpt := range []plugins.BuiltinPluginType{
kt.configureBuiltinConfigMapGenerator, plugins.ConfigMapGenerator,
kt.configureBuiltinSecretGenerator, plugins.SecretGenerator,
} } {
var result []transformers.Generator r, err := generatorConfigurators[bpt](
for _, f := range configurators { kt, bpt, plugins.GeneratorFactories[bpt])
r, err := f()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -57,270 +43,256 @@ func (kt *KustTarget) configureBuiltinGenerators() (
} }
func (kt *KustTarget) configureBuiltinTransformers( func (kt *KustTarget) configureBuiltinTransformers(
tConfig *config.TransformerConfig) ( tc *config.TransformerConfig) (
[]transformers.Transformer, error) { result []resmap.Transformer, err error) {
// TODO: Convert remaining legacy transformers to plugins for _, bpt := range []plugins.BuiltinPluginType{
// with tests: plugins.PatchStrategicMergeTransformer,
// - patch SMP plugins.PatchTransformer,
configurators := []transformerConfigurator{ plugins.NamespaceTransformer,
kt.configureBuiltinPatchStrategicMergeTransformer, plugins.PrefixSuffixTransformer,
kt.configureBuiltinPatchTransformer, plugins.LabelTransformer,
kt.configureBuiltinNamespaceTransformer, plugins.AnnotationsTransformer,
kt.configureBuiltinNameTransformer, plugins.PatchJson6902Transformer,
kt.configureBuiltinLabelTransformer, plugins.ReplicaCountTransformer,
kt.configureBuiltinAnnotationsTransformer, plugins.ImageTagTransformer,
kt.configureBuiltinPatchJson6902Transformer, } {
kt.configureBuiltinReplicaCountTransformer, r, err := transformerConfigurators[bpt](
kt.configureBuiltinImageTagTransformer, kt, bpt, plugins.TransformerFactories[bpt], tc)
}
var result []transformers.Transformer
for _, f := range configurators {
r, err := f(tConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
result = append(result, r...) result = append(result, r...)
} }
return result, nil return result, nil
} }
func (kt *KustTarget) configureBuiltinSecretGenerator() ( type gFactory func() resmap.GeneratorPlugin
result []transformers.Generator, err error) {
var c struct { var generatorConfigurators = map[plugins.BuiltinPluginType]func(
types.GeneratorOptions kt *KustTarget,
types.SecretArgs bpt plugins.BuiltinPluginType,
} factory gFactory) (result []resmap.Generator, err error){
if kt.kustomization.GeneratorOptions != nil { plugins.SecretGenerator: func(kt *KustTarget, bpt plugins.BuiltinPluginType, f gFactory) (
c.GeneratorOptions = *kt.kustomization.GeneratorOptions result []resmap.Generator, err error) {
} var c struct {
for _, args := range kt.kustomization.SecretGenerator { types.GeneratorOptions
c.SecretArgs = args types.SecretArgs
p := builtin.NewSecretGeneratorPlugin()
err = kt.configureBuiltinPlugin(p, c, "secret")
if err != nil {
return nil, err
} }
result = append(result, p) if kt.kustomization.GeneratorOptions != nil {
} c.GeneratorOptions = *kt.kustomization.GeneratorOptions
return
}
func (kt *KustTarget) configureBuiltinConfigMapGenerator() (
result []transformers.Generator, err error) {
var c struct {
types.GeneratorOptions
types.ConfigMapArgs
}
if kt.kustomization.GeneratorOptions != nil {
c.GeneratorOptions = *kt.kustomization.GeneratorOptions
}
for _, args := range kt.kustomization.ConfigMapGenerator {
c.ConfigMapArgs = args
p := builtin.NewConfigMapGeneratorPlugin()
err = kt.configureBuiltinPlugin(p, c, "configmap")
if err != nil {
return nil, err
} }
result = append(result, p) for _, args := range kt.kustomization.SecretGenerator {
} c.SecretArgs = args
return p := f()
} err := kt.configureBuiltinPlugin(p, c, bpt)
if err != nil {
func (kt *KustTarget) configureBuiltinNamespaceTransformer( return nil, err
tConfig *config.TransformerConfig) ( }
result []transformers.Transformer, err error) { result = append(result, p)
var c struct {
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
FieldSpecs []config.FieldSpec
}
c.Namespace = kt.kustomization.Namespace
c.FieldSpecs = tConfig.NameSpace
p := builtin.NewNamespaceTransformerPlugin()
err = kt.configureBuiltinPlugin(p, c, "namespace")
if err != nil {
return nil, err
}
result = append(result, p)
return
}
func (kt *KustTarget) configureBuiltinPatchJson6902Transformer(
tConfig *config.TransformerConfig) (
result []transformers.Transformer, err error) {
var c struct {
Target types.PatchTarget `json:"target,omitempty" yaml:"target,omitempty"`
Path string `json:"path,omitempty" yaml:"path,omitempty"`
JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"`
}
for _, args := range kt.kustomization.PatchesJson6902 {
c.Target = *args.Target
c.Path = args.Path
c.JsonOp = args.Patch
p := builtin.NewPatchJson6902TransformerPlugin()
err = kt.configureBuiltinPlugin(p, c, "patchJson6902")
if err != nil {
return nil, err
} }
result = append(result, p)
}
return
}
func (kt *KustTarget) configureBuiltinPatchStrategicMergeTransformer(
tConfig *config.TransformerConfig) (
result []transformers.Transformer, err error) {
if len(kt.kustomization.PatchesStrategicMerge) == 0 {
return return
} },
var c struct {
Paths []types.PatchStrategicMerge `json:"paths,omitempty" yaml:"paths,omitempty"`
Patches string `json:"patches,omitempty" yaml:"patches,omitempty"`
}
c.Paths = kt.kustomization.PatchesStrategicMerge
p := builtin.NewPatchStrategicMergeTransformerPlugin()
err = kt.configureBuiltinPlugin(p, c, "patchStrategicMerge")
if err != nil {
return nil, err
}
result = append(result, p)
return
}
func (kt *KustTarget) configureBuiltinPatchTransformer( plugins.ConfigMapGenerator: func(kt *KustTarget, bpt plugins.BuiltinPluginType, f gFactory) (
tConfig *config.TransformerConfig) ( result []resmap.Generator, err error) {
result []transformers.Transformer, err error) { var c struct {
if len(kt.kustomization.Patches) == 0 { types.GeneratorOptions
types.ConfigMapArgs
}
if kt.kustomization.GeneratorOptions != nil {
c.GeneratorOptions = *kt.kustomization.GeneratorOptions
}
for _, args := range kt.kustomization.ConfigMapGenerator {
c.ConfigMapArgs = args
p := f()
err := kt.configureBuiltinPlugin(p, c, bpt)
if err != nil {
return nil, err
}
result = append(result, p)
}
return return
} },
var c struct { }
Path string `json:"path,omitempty" yaml:"path,omitempty"`
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` type tFactory func() resmap.TransformerPlugin
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
} var transformerConfigurators = map[plugins.BuiltinPluginType]func(
for _, patch := range kt.kustomization.Patches { kt *KustTarget,
c.Target = patch.Target bpt plugins.BuiltinPluginType,
c.Patch = patch.Patch f tFactory,
c.Path = patch.Path tc *config.TransformerConfig) (result []resmap.Transformer, err error){
p := builtin.NewPatchTransformerPlugin() plugins.NamespaceTransformer: func(
err = kt.configureBuiltinPlugin(p, c, "patch") kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, tc *config.TransformerConfig) (
result []resmap.Transformer, err error) {
var c struct {
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
FieldSpecs []config.FieldSpec
}
c.Namespace = kt.kustomization.Namespace
c.FieldSpecs = tc.NameSpace
p := f()
err = kt.configureBuiltinPlugin(p, c, bpt)
if err != nil { if err != nil {
return nil, err return nil, err
} }
result = append(result, p) result = append(result, p)
} return
return },
}
func (kt *KustTarget) configureBuiltinLabelTransformer( plugins.PatchJson6902Transformer: func(
tConfig *config.TransformerConfig) ( kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, _ *config.TransformerConfig) (
result []transformers.Transformer, err error) { result []resmap.Transformer, err error) {
var c struct { var c struct {
Labels map[string]string Target types.PatchTarget `json:"target,omitempty" yaml:"target,omitempty"`
FieldSpecs []config.FieldSpec Path string `json:"path,omitempty" yaml:"path,omitempty"`
} JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"`
c.Labels = kt.kustomization.CommonLabels }
c.FieldSpecs = tConfig.CommonLabels for _, args := range kt.kustomization.PatchesJson6902 {
p := builtin.NewLabelTransformerPlugin() c.Target = *args.Target
err = kt.configureBuiltinPlugin(p, c, "label") c.Path = args.Path
if err != nil { c.JsonOp = args.Patch
return nil, err p := f()
} err = kt.configureBuiltinPlugin(p, c, bpt)
result = append(result, p) if err != nil {
return return nil, err
} }
result = append(result, p)
func (kt *KustTarget) configureBuiltinAnnotationsTransformer( }
tConfig *config.TransformerConfig) ( return
result []transformers.Transformer, err error) { },
var c struct { plugins.PatchStrategicMergeTransformer: func(
Annotations map[string]string kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, _ *config.TransformerConfig) (
FieldSpecs []config.FieldSpec result []resmap.Transformer, err error) {
} if len(kt.kustomization.PatchesStrategicMerge) == 0 {
c.Annotations = kt.kustomization.CommonAnnotations return
c.FieldSpecs = tConfig.CommonAnnotations }
p := builtin.NewAnnotationsTransformerPlugin() var c struct {
err = kt.configureBuiltinPlugin(p, c, "annotations") Paths []types.PatchStrategicMerge `json:"paths,omitempty" yaml:"paths,omitempty"`
if err != nil { Patches string `json:"patches,omitempty" yaml:"patches,omitempty"`
return nil, err }
} c.Paths = kt.kustomization.PatchesStrategicMerge
result = append(result, p) p := f()
return err = kt.configureBuiltinPlugin(p, c, bpt)
}
func (kt *KustTarget) configureBuiltinNameTransformer(
tConfig *config.TransformerConfig) (
result []transformers.Transformer, err error) {
var c struct {
Prefix string
Suffix string
FieldSpecs []config.FieldSpec
}
c.Prefix = kt.kustomization.NamePrefix
c.Suffix = kt.kustomization.NameSuffix
c.FieldSpecs = tConfig.NamePrefix
p := builtin.NewPrefixSuffixTransformerPlugin()
err = kt.configureBuiltinPlugin(p, c, "prefixsuffix")
if err != nil {
return nil, err
}
result = append(result, p)
return
}
func (kt *KustTarget) configureBuiltinImageTagTransformer(
tConfig *config.TransformerConfig) (
result []transformers.Transformer, err error) {
var c struct {
ImageTag image.Image
FieldSpecs []config.FieldSpec
}
for _, args := range kt.kustomization.Images {
c.ImageTag = args
c.FieldSpecs = tConfig.Images
p := builtin.NewImageTagTransformerPlugin()
err = kt.configureBuiltinPlugin(p, c, "imageTag")
if err != nil { if err != nil {
return nil, err return nil, err
} }
result = append(result, p) result = append(result, p)
} return
return },
} plugins.PatchTransformer: func(
kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, _ *config.TransformerConfig) (
func (kt *KustTarget) configureBuiltinReplicaCountTransformer( result []resmap.Transformer, err error) {
tConfig *config.TransformerConfig) ( if len(kt.kustomization.Patches) == 0 {
result []transformers.Transformer, err error) { return
var c struct { }
Replica types.Replica var c struct {
FieldSpecs []config.FieldSpec Path string `json:"path,omitempty" yaml:"path,omitempty"`
} Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
for _, args := range kt.kustomization.Replicas { Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
c.Replica = args }
c.FieldSpecs = tConfig.Replicas for _, pc := range kt.kustomization.Patches {
p := builtin.NewReplicaCountTransformerPlugin() c.Target = pc.Target
err = kt.configureBuiltinPlugin(p, c, "replica") c.Patch = pc.Patch
c.Path = pc.Path
p := f()
err = kt.configureBuiltinPlugin(p, c, bpt)
if err != nil {
return nil, err
}
result = append(result, p)
}
return
},
plugins.LabelTransformer: func(
kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, tc *config.TransformerConfig) (
result []resmap.Transformer, err error) {
var c struct {
Labels map[string]string
FieldSpecs []config.FieldSpec
}
c.Labels = kt.kustomization.CommonLabels
c.FieldSpecs = tc.CommonLabels
p := f()
err = kt.configureBuiltinPlugin(p, c, bpt)
if err != nil { if err != nil {
return nil, err return nil, err
} }
result = append(result, p) result = append(result, p)
} return
return },
} plugins.AnnotationsTransformer: func(
kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, tc *config.TransformerConfig) (
func (kt *KustTarget) configureBuiltinPlugin( result []resmap.Transformer, err error) {
p plugins.Configurable, c interface{}, id string) (err error) { var c struct {
var y []byte Annotations map[string]string
if c != nil { FieldSpecs []config.FieldSpec
y, err = yaml.Marshal(c)
if err != nil {
return errors.Wrapf(
err, "builtin %s marshal", id)
} }
} c.Annotations = kt.kustomization.CommonAnnotations
err = p.Config(kt.ldr, kt.rFactory, y) c.FieldSpecs = tc.CommonAnnotations
if err != nil { p := f()
return errors.Wrapf(err, "builtin %s config: %v", id, y) err = kt.configureBuiltinPlugin(p, c, bpt)
} if err != nil {
return nil return nil, err
}
result = append(result, p)
return
},
plugins.PrefixSuffixTransformer: func(
kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, tc *config.TransformerConfig) (
result []resmap.Transformer, err error) {
var c struct {
Prefix string
Suffix string
FieldSpecs []config.FieldSpec
}
c.Prefix = kt.kustomization.NamePrefix
c.Suffix = kt.kustomization.NameSuffix
c.FieldSpecs = tc.NamePrefix
p := f()
err = kt.configureBuiltinPlugin(p, c, bpt)
if err != nil {
return nil, err
}
result = append(result, p)
return
},
plugins.ImageTagTransformer: func(
kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, tc *config.TransformerConfig) (
result []resmap.Transformer, err error) {
var c struct {
ImageTag image.Image
FieldSpecs []config.FieldSpec
}
for _, args := range kt.kustomization.Images {
c.ImageTag = args
c.FieldSpecs = tc.Images
p := f()
err = kt.configureBuiltinPlugin(p, c, bpt)
if err != nil {
return nil, err
}
result = append(result, p)
}
return
},
plugins.ReplicaCountTransformer: func(
kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, tc *config.TransformerConfig) (
result []resmap.Transformer, err error) {
var c struct {
Replica types.Replica
FieldSpecs []config.FieldSpec
}
for _, args := range kt.kustomization.Replicas {
c.Replica = args
c.FieldSpecs = tc.Replicas
p := f()
err = kt.configureBuiltinPlugin(p, c, bpt)
if err != nil {
return nil, err
}
result = append(result, p)
}
return
},
} }

View File

@@ -113,7 +113,7 @@ transformers:
if err == nil { if err == nil {
t.Fatalf("expected error") t.Fatalf("expected error")
} }
if !strings.Contains(err.Error(), "unable to load plugin StringPrefixer") { if !strings.Contains(err.Error(), "unable to load external plugin StringPrefixer") {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
} }

9
pkg/transformers/doc.go Normal file
View File

@@ -0,0 +1,9 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// Utilities to aid transformer plugins.
//
// TODO: Push remaining code down into the plugins
// or into an appropriately named utility package.
// This package made more sense in the pre-plugin days.
package transformers

View File

@@ -30,23 +30,23 @@ type mapTransformer struct {
fieldSpecs []config.FieldSpec fieldSpecs []config.FieldSpec
} }
var _ Transformer = &mapTransformer{} var _ resmap.Transformer = &mapTransformer{}
// NewLabelsMapTransformer constructs a mapTransformer. // NewLabelsMapTransformer constructs a mapTransformer.
func NewLabelsMapTransformer( func NewLabelsMapTransformer(
m map[string]string, fs []config.FieldSpec) (Transformer, error) { m map[string]string, fs []config.FieldSpec) (resmap.Transformer, error) {
return NewMapTransformer(fs, m) return NewMapTransformer(fs, m)
} }
// NewAnnotationsMapTransformer construct a mapTransformer. // NewAnnotationsMapTransformer construct a mapTransformer.
func NewAnnotationsMapTransformer( func NewAnnotationsMapTransformer(
m map[string]string, fs []config.FieldSpec) (Transformer, error) { m map[string]string, fs []config.FieldSpec) (resmap.Transformer, error) {
return NewMapTransformer(fs, m) return NewMapTransformer(fs, m)
} }
// NewMapTransformer construct a mapTransformer. // NewMapTransformer construct a mapTransformer.
func NewMapTransformer( func NewMapTransformer(
pc []config.FieldSpec, m map[string]string) (Transformer, error) { pc []config.FieldSpec, m map[string]string) (resmap.Transformer, error) {
if m == nil { if m == nil {
return NewNoOpTransformer(), nil return NewNoOpTransformer(), nil
} }

View File

@@ -24,25 +24,25 @@ import (
// multiTransformer contains a list of transformers. // multiTransformer contains a list of transformers.
type multiTransformer struct { type multiTransformer struct {
transformers []Transformer transformers []resmap.Transformer
checkConflictEnabled bool checkConflictEnabled bool
} }
var _ Transformer = &multiTransformer{} var _ resmap.Transformer = &multiTransformer{}
// NewMultiTransformer constructs a multiTransformer. // NewMultiTransformer constructs a multiTransformer.
func NewMultiTransformer(t []Transformer) Transformer { func NewMultiTransformer(t []resmap.Transformer) resmap.Transformer {
r := &multiTransformer{ r := &multiTransformer{
transformers: make([]Transformer, len(t)), transformers: make([]resmap.Transformer, len(t)),
checkConflictEnabled: false} checkConflictEnabled: false}
copy(r.transformers, t) copy(r.transformers, t)
return r return r
} }
// NewMultiTransformerWithConflictCheck constructs a multiTransformer with checking of conflicts. // NewMultiTransformerWithConflictCheck constructs a multiTransformer with checking of conflicts.
func NewMultiTransformerWithConflictCheck(t []Transformer) Transformer { func NewMultiTransformerWithConflictCheck(t []resmap.Transformer) resmap.Transformer {
r := &multiTransformer{ r := &multiTransformer{
transformers: make([]Transformer, len(t)), transformers: make([]resmap.Transformer, len(t)),
checkConflictEnabled: true} checkConflictEnabled: true}
copy(r.transformers, t) copy(r.transformers, t)
return r return r

View File

@@ -18,11 +18,11 @@ type nameReferenceTransformer struct {
backRefs []config.NameBackReferences backRefs []config.NameBackReferences
} }
var _ Transformer = &nameReferenceTransformer{} var _ resmap.Transformer = &nameReferenceTransformer{}
// NewNameReferenceTransformer constructs a nameReferenceTransformer // NewNameReferenceTransformer constructs a nameReferenceTransformer
// with a given slice of NameBackReferences. // with a given slice of NameBackReferences.
func NewNameReferenceTransformer(br []config.NameBackReferences) Transformer { func NewNameReferenceTransformer(br []config.NameBackReferences) resmap.Transformer {
if br == nil { if br == nil {
log.Fatal("backrefs not expected to be nil") log.Fatal("backrefs not expected to be nil")
} }

View File

@@ -21,10 +21,10 @@ import "sigs.k8s.io/kustomize/v3/pkg/resmap"
// noOpTransformer contains a no-op transformer. // noOpTransformer contains a no-op transformer.
type noOpTransformer struct{} type noOpTransformer struct{}
var _ Transformer = &noOpTransformer{} var _ resmap.Transformer = &noOpTransformer{}
// NewNoOpTransformer constructs a noOpTransformer. // NewNoOpTransformer constructs a noOpTransformer.
func NewNoOpTransformer() Transformer { func NewNoOpTransformer() resmap.Transformer {
return &noOpTransformer{} return &noOpTransformer{}
} }

View File

@@ -1,33 +0,0 @@
/*
Copyright 2018 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 transformers has implementations of resmap.ResMap transformers.
package transformers
import (
"sigs.k8s.io/kustomize/v3/pkg/resmap"
)
// A Transformer modifies an instance of resmap.ResMap.
type Transformer interface {
// Transform modifies data in the argument, e.g. adding labels to resources that can be labelled.
Transform(m resmap.ResMap) error
}
// A Generator creates an instance of resmap.ResMap.
type Generator interface {
Generate() (resmap.ResMap, error)
}

View File

@@ -15,11 +15,6 @@ type AnnotationsTransformerPlugin struct {
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
} }
//noinspection GoUnusedGlobalVariable
func NewAnnotationsTransformerPlugin() *AnnotationsTransformerPlugin {
return &AnnotationsTransformerPlugin{}
}
func (p *AnnotationsTransformerPlugin) Config( func (p *AnnotationsTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.Annotations = nil p.Annotations = nil
@@ -37,3 +32,7 @@ func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error {
} }
return t.Transform(m) return t.Transform(m)
} }
func NewAnnotationsTransformerPlugin() resmap.TransformerPlugin {
return &AnnotationsTransformerPlugin{}
}

View File

@@ -16,11 +16,6 @@ type ConfigMapGeneratorPlugin struct {
types.ConfigMapArgs types.ConfigMapArgs
} }
//noinspection GoUnusedGlobalVariable
func NewConfigMapGeneratorPlugin() *ConfigMapGeneratorPlugin {
return &ConfigMapGeneratorPlugin{}
}
func (p *ConfigMapGeneratorPlugin) Config( func (p *ConfigMapGeneratorPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, config []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, config []byte) (err error) {
p.GeneratorOptions = types.GeneratorOptions{} p.GeneratorOptions = types.GeneratorOptions{}
@@ -40,3 +35,7 @@ func (p *ConfigMapGeneratorPlugin) Config(
func (p *ConfigMapGeneratorPlugin) Generate() (resmap.ResMap, error) { func (p *ConfigMapGeneratorPlugin) Generate() (resmap.ResMap, error) {
return p.rf.FromConfigMapArgs(p.ldr, &p.GeneratorOptions, p.ConfigMapArgs) return p.rf.FromConfigMapArgs(p.ldr, &p.GeneratorOptions, p.ConfigMapArgs)
} }
func NewConfigMapGeneratorPlugin() resmap.GeneratorPlugin {
return &ConfigMapGeneratorPlugin{}
}

View File

@@ -12,11 +12,6 @@ type HashTransformerPlugin struct {
hasher ifc.KunstructuredHasher hasher ifc.KunstructuredHasher
} }
//noinspection GoUnusedGlobalVariable
func NewHashTransformerPlugin() *HashTransformerPlugin {
return &HashTransformerPlugin{}
}
func (p *HashTransformerPlugin) Config( func (p *HashTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, config []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, config []byte) (err error) {
p.hasher = rf.RF().Hasher() p.hasher = rf.RF().Hasher()
@@ -36,3 +31,7 @@ func (p *HashTransformerPlugin) Transform(m resmap.ResMap) error {
} }
return nil return nil
} }
func NewHashTransformerPlugin() resmap.TransformerPlugin {
return &HashTransformerPlugin{}
}

View File

@@ -21,11 +21,6 @@ type ImageTagTransformerPlugin struct {
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
} }
//noinspection GoUnusedGlobalVariable
func NewImageTagTransformerPlugin() *ImageTagTransformerPlugin {
return &ImageTagTransformerPlugin{}
}
func (p *ImageTagTransformerPlugin) Config( func (p *ImageTagTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.ImageTag = image.Image{} p.ImageTag = image.Image{}
@@ -183,3 +178,7 @@ func split(imageName string) (name string, tag string) {
tag = imageName[i:] tag = imageName[i:]
return return
} }
func NewImageTagTransformerPlugin() resmap.TransformerPlugin {
return &ImageTagTransformerPlugin{}
}

View File

@@ -22,11 +22,6 @@ type InventoryTransformerPlugin struct {
Policy string `json:"policy,omitempty" yaml:"policy,omitempty"` Policy string `json:"policy,omitempty" yaml:"policy,omitempty"`
} }
//noinspection GoUnusedGlobalVariable
func NewInventoryTransformerPlugin() *InventoryTransformerPlugin {
return &InventoryTransformerPlugin{}
}
func (p *InventoryTransformerPlugin) Config( func (p *InventoryTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.ldr = ldr p.ldr = ldr
@@ -127,3 +122,7 @@ func computeRefs(
} }
return return
} }
func NewInventoryTransformerPlugin() resmap.TransformerPlugin {
return &InventoryTransformerPlugin{}
}

View File

@@ -15,11 +15,6 @@ type LabelTransformerPlugin struct {
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
} }
//noinspection GoUnusedGlobalVariable
func NewLabelTransformerPlugin() *LabelTransformerPlugin {
return &LabelTransformerPlugin{}
}
func (p *LabelTransformerPlugin) Config( func (p *LabelTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.Labels = nil p.Labels = nil
@@ -37,3 +32,7 @@ func (p *LabelTransformerPlugin) Transform(m resmap.ResMap) error {
} }
return t.Transform(m) return t.Transform(m)
} }
func NewLabelTransformerPlugin() resmap.TransformerPlugin {
return &LabelTransformerPlugin{}
}

View File

@@ -16,11 +16,6 @@ import (
// (like ValidatingWebhookConfiguration) last. // (like ValidatingWebhookConfiguration) last.
type LegacyOrderTransformerPlugin struct{} type LegacyOrderTransformerPlugin struct{}
//noinspection GoUnusedGlobalVariable
func NewLegacyOrderTransformerPlugin() *LegacyOrderTransformerPlugin {
return &LegacyOrderTransformerPlugin{}
}
// Nothing needed for configuration. // Nothing needed for configuration.
func (p *LegacyOrderTransformerPlugin) Config( func (p *LegacyOrderTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
@@ -43,3 +38,7 @@ func (p *LegacyOrderTransformerPlugin) Transform(m resmap.ResMap) (err error) {
} }
return nil return nil
} }
func NewLegacyOrderTransformerPlugin() resmap.TransformerPlugin {
return &LegacyOrderTransformerPlugin{}
}

View File

@@ -20,11 +20,6 @@ type NamespaceTransformerPlugin struct {
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
} }
//noinspection GoUnusedGlobalVariable
func NewNamespaceTransformerPlugin() *NamespaceTransformerPlugin {
return &NamespaceTransformerPlugin{}
}
func (p *NamespaceTransformerPlugin) Config( func (p *NamespaceTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.Namespace = "" p.Namespace = ""
@@ -129,3 +124,7 @@ func (o *NamespaceTransformerPlugin) changeNamespace(
} }
} }
} }
func NewNamespaceTransformerPlugin() resmap.TransformerPlugin {
return &NamespaceTransformerPlugin{}
}

View File

@@ -21,11 +21,6 @@ type PatchJson6902TransformerPlugin struct {
JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"` JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"`
} }
//noinspection GoUnusedGlobalVariable
func NewPatchJson6902TransformerPlugin() *PatchJson6902TransformerPlugin {
return &PatchJson6902TransformerPlugin{}
}
func (p *PatchJson6902TransformerPlugin) Config( func (p *PatchJson6902TransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.ldr = ldr p.ldr = ldr
@@ -97,3 +92,7 @@ func (p *PatchJson6902TransformerPlugin) Transform(m resmap.ResMap) error {
} }
return obj.UnmarshalJSON(modifiedObj) return obj.UnmarshalJSON(modifiedObj)
} }
func NewPatchJson6902TransformerPlugin() resmap.TransformerPlugin {
return &PatchJson6902TransformerPlugin{}
}

View File

@@ -18,11 +18,6 @@ type PatchStrategicMergeTransformerPlugin struct {
Patches string `json:"patches,omitempty" yaml:"patches,omitempty"` Patches string `json:"patches,omitempty" yaml:"patches,omitempty"`
} }
//noinspection GoUnusedGlobalVariable
func NewPatchStrategicMergeTransformerPlugin() *PatchStrategicMergeTransformerPlugin {
return &PatchStrategicMergeTransformerPlugin{}
}
func (p *PatchStrategicMergeTransformerPlugin) Config( func (p *PatchStrategicMergeTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.ldr = ldr p.ldr = ldr
@@ -88,3 +83,7 @@ func (p *PatchStrategicMergeTransformerPlugin) Transform(m resmap.ResMap) error
} }
return nil return nil
} }
func NewPatchStrategicMergeTransformerPlugin() resmap.TransformerPlugin {
return &PatchStrategicMergeTransformerPlugin{}
}

View File

@@ -22,11 +22,6 @@ type PatchTransformerPlugin struct {
Target *types.Selector `json:"target,omitempty", yaml:"target,omitempty"` Target *types.Selector `json:"target,omitempty", yaml:"target,omitempty"`
} }
//noinspection GoUnusedGlobalVariable
func NewPatchTransformerPlugin() *PatchTransformerPlugin {
return &PatchTransformerPlugin{}
}
func (p *PatchTransformerPlugin) Config( func (p *PatchTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.ldr = ldr p.ldr = ldr
@@ -146,3 +141,7 @@ func jsonPatchFromBytes(
} }
return jsonpatch.DecodePatch([]byte(ops)) return jsonpatch.DecodePatch([]byte(ops))
} }
func NewPatchTransformerPlugin() resmap.TransformerPlugin {
return &PatchTransformerPlugin{}
}

View File

@@ -21,11 +21,6 @@ type PrefixSuffixTransformerPlugin struct {
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
} }
//noinspection GoUnusedGlobalVariable
func NewPrefixSuffixTransformerPlugin() *PrefixSuffixTransformerPlugin {
return &PrefixSuffixTransformerPlugin{}
}
// Not placed in a file yet due to lack of demand. // Not placed in a file yet due to lack of demand.
var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{ var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{
{ {
@@ -118,3 +113,7 @@ func (p *PrefixSuffixTransformerPlugin) addPrefixSuffix(
} }
return fmt.Sprintf("%s%s%s", p.Prefix, s, p.Suffix), nil return fmt.Sprintf("%s%s%s", p.Prefix, s, p.Suffix), nil
} }
func NewPrefixSuffixTransformerPlugin() resmap.TransformerPlugin {
return &PrefixSuffixTransformerPlugin{}
}

View File

@@ -20,11 +20,6 @@ type ReplicaCountTransformerPlugin struct {
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
} }
//noinspection GoUnusedGlobalVariable
func NewReplicaCountTransformerPlugin() *ReplicaCountTransformerPlugin {
return &ReplicaCountTransformerPlugin{}
}
func (p *ReplicaCountTransformerPlugin) Config( func (p *ReplicaCountTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
@@ -88,3 +83,7 @@ func (p *ReplicaCountTransformerPlugin) addReplicas(in interface{}) (interface{}
} }
return p.Replica.Count, nil return p.Replica.Count, nil
} }
func NewReplicaCountTransformerPlugin() resmap.TransformerPlugin {
return &ReplicaCountTransformerPlugin{}
}

View File

@@ -16,11 +16,6 @@ type SecretGeneratorPlugin struct {
types.SecretArgs types.SecretArgs
} }
//noinspection GoUnusedGlobalVariable
func NewSecretGeneratorPlugin() *SecretGeneratorPlugin {
return &SecretGeneratorPlugin{}
}
func (p *SecretGeneratorPlugin) Config( func (p *SecretGeneratorPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, config []byte) (err error) { ldr ifc.Loader, rf *resmap.Factory, config []byte) (err error) {
p.GeneratorOptions = types.GeneratorOptions{} p.GeneratorOptions = types.GeneratorOptions{}
@@ -40,3 +35,7 @@ func (p *SecretGeneratorPlugin) Config(
func (p *SecretGeneratorPlugin) Generate() (resmap.ResMap, error) { func (p *SecretGeneratorPlugin) Generate() (resmap.ResMap, error) {
return p.rf.FromSecretArgs(p.ldr, &p.GeneratorOptions, p.SecretArgs) return p.rf.FromSecretArgs(p.ldr, &p.GeneratorOptions, p.SecretArgs)
} }
func NewSecretGeneratorPlugin() resmap.GeneratorPlugin {
return &SecretGeneratorPlugin{}
}

View File

@@ -7,7 +7,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"sigs.k8s.io/kustomize/v3/pkg/ifc" "sigs.k8s.io/kustomize/v3/pkg/ifc"
"sigs.k8s.io/kustomize/v3/pkg/resmap" "sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/pkg/transformers"
"sigs.k8s.io/kustomize/v3/pkg/transformers/config" "sigs.k8s.io/kustomize/v3/pkg/transformers/config"
"sigs.k8s.io/kustomize/v3/plugin/builtin" "sigs.k8s.io/kustomize/v3/plugin/builtin"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
@@ -16,7 +15,7 @@ import (
// Add a date prefix to the name. // Add a date prefix to the name.
// A plugin that adapts another plugin. // A plugin that adapts another plugin.
type plugin struct { type plugin struct {
t transformers.Transformer t resmap.Transformer
} }
//nolint: golint //nolint: golint

View File

@@ -7,7 +7,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"sigs.k8s.io/kustomize/v3/pkg/ifc" "sigs.k8s.io/kustomize/v3/pkg/ifc"
"sigs.k8s.io/kustomize/v3/pkg/resmap" "sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/pkg/transformers"
"sigs.k8s.io/kustomize/v3/pkg/transformers/config" "sigs.k8s.io/kustomize/v3/pkg/transformers/config"
"sigs.k8s.io/kustomize/v3/pkg/types" "sigs.k8s.io/kustomize/v3/pkg/types"
"sigs.k8s.io/kustomize/v3/plugin/builtin" "sigs.k8s.io/kustomize/v3/plugin/builtin"
@@ -18,7 +17,7 @@ import (
// A plugin that adapts another plugin. // A plugin that adapts another plugin.
type plugin struct { type plugin struct {
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
t transformers.Transformer t resmap.Transformer
} }
//nolint: golint //nolint: golint