From 2050afdeb46c359c62e959492d389bc2d093e0bd Mon Sep 17 00:00:00 2001 From: jregan Date: Wed, 11 Sep 2019 20:17:33 -0700 Subject: [PATCH] Ease doing custom configuration of builtin plugins. --- cmd/pluginator/main.go | 36 +- cmd/pluginator/plugintype_string.go | 25 + pkg/accumulator/resaccumulator.go | 2 +- pkg/plugins/builtinplugintype_string.go | 37 ++ pkg/plugins/builtins.go | 75 +++ pkg/plugins/config.go | 10 +- pkg/plugins/loader.go | 67 ++- pkg/resmap/resmap.go | 34 +- pkg/target/kusttarget.go | 30 +- pkg/target/kusttarget_configplugin.go | 492 +++++++++--------- pkg/target/transformerplugin_test.go | 2 +- pkg/transformers/doc.go | 9 + pkg/transformers/labelsandannotations.go | 8 +- pkg/transformers/multitransformer.go | 12 +- pkg/transformers/namereference.go | 4 +- pkg/transformers/nooptransformer.go | 4 +- pkg/transformers/transformer.go | 33 -- plugin/builtin/AnnotationsTransformer.go | 9 +- plugin/builtin/ConfigMapGenerator.go | 9 +- plugin/builtin/HashTransformer.go | 9 +- plugin/builtin/ImageTagTransformer.go | 9 +- plugin/builtin/InventoryTransformer.go | 9 +- plugin/builtin/LabelTransformer.go | 9 +- plugin/builtin/LegacyOrderTransformer.go | 9 +- plugin/builtin/NamespaceTransformer.go | 9 +- plugin/builtin/PatchJson6902Transformer.go | 9 +- .../builtin/PatchStrategicMergeTransformer.go | 9 +- plugin/builtin/PatchTransformer.go | 9 +- plugin/builtin/PrefixSuffixTransformer.go | 9 +- plugin/builtin/ReplicaCountTransformer.go | 9 +- plugin/builtin/SecretGenerator.go | 9 +- .../v1/dateprefixer/DatePrefixer.go | 3 +- .../v1/stringprefixer/StringPrefixer.go | 3 +- 33 files changed, 588 insertions(+), 424 deletions(-) create mode 100644 cmd/pluginator/plugintype_string.go create mode 100644 pkg/plugins/builtinplugintype_string.go create mode 100644 pkg/plugins/builtins.go create mode 100644 pkg/transformers/doc.go delete mode 100644 pkg/transformers/transformer.go diff --git a/cmd/pluginator/main.go b/cmd/pluginator/main.go index 4dd4e554c6..b4fdb4face 100644 --- a/cmd/pluginator/main.go +++ b/cmd/pluginator/main.go @@ -16,6 +16,15 @@ import ( "sigs.k8s.io/kustomize/v3/pkg/plugins" ) +//go:generate stringer -type=pluginType +type pluginType int + +const ( + unknown pluginType = iota + Transformer + Generator +) + func main() { root := inputFileRoot() file, err := os.Open(root + ".go") @@ -34,24 +43,41 @@ func main() { fmt.Sprintf( "// Code generated by pluginator on %s; DO NOT EDIT.", root)) - w.write("package builtin") + w.write("package " + plugins.BuiltinPluginPackage) + + pType := unknown for scanner.Scan() { l := scanner.Text() if strings.HasPrefix(l, "//go:generate") { continue } + if strings.HasPrefix(l, "//noinspection") { + continue + } if l == "var "+plugins.PluginSymbol+" plugin" { - w.write("func New" + root + "Plugin() *" + root + "Plugin {") - w.write(" return &" + root + "Plugin{}") - w.write("}") 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) } if err := scanner.Err(); err != nil { 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 { @@ -93,7 +119,7 @@ func makeOutputFileName(root string) string { pgmconfig.DomainName, pgmconfig.ProgramName, pgmconfig.PluginRoot, - "builtin", + plugins.BuiltinPluginPackage, root+".go") } diff --git a/cmd/pluginator/plugintype_string.go b/cmd/pluginator/plugintype_string.go new file mode 100644 index 0000000000..e65fb5af8c --- /dev/null +++ b/cmd/pluginator/plugintype_string.go @@ -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]] +} diff --git a/pkg/accumulator/resaccumulator.go b/pkg/accumulator/resaccumulator.go index ef2f4b9bc2..7ebfc04bdb 100644 --- a/pkg/accumulator/resaccumulator.go +++ b/pkg/accumulator/resaccumulator.go @@ -135,7 +135,7 @@ func (ra *ResAccumulator) makeVarReplacementMap() (map[string]interface{}, error return result, nil } -func (ra *ResAccumulator) Transform(t transformers.Transformer) error { +func (ra *ResAccumulator) Transform(t resmap.Transformer) error { return t.Transform(ra.resMap) } diff --git a/pkg/plugins/builtinplugintype_string.go b/pkg/plugins/builtinplugintype_string.go new file mode 100644 index 0000000000..cb49df8b3a --- /dev/null +++ b/pkg/plugins/builtinplugintype_string.go @@ -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]] +} diff --git a/pkg/plugins/builtins.go b/pkg/plugins/builtins.go new file mode 100644 index 0000000000..989a118b4e --- /dev/null +++ b/pkg/plugins/builtins.go @@ -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, +} diff --git a/pkg/plugins/config.go b/pkg/plugins/config.go index 80d82ecc54..0d3b186cd4 100644 --- a/pkg/plugins/config.go +++ b/pkg/plugins/config.go @@ -12,13 +12,15 @@ import ( ) const ( - PluginSymbol = "KustomizePlugin" - flagEnablePluginsName = "enable_alpha_plugins" - flagEnablePluginsHelp = `enable plugins, an alpha feature. + PluginSymbol = "KustomizePlugin" + BuiltinPluginPackage = "builtin" + BuiltinPluginApiVersion = BuiltinPluginPackage + flagEnablePluginsName = "enable_alpha_plugins" + flagEnablePluginsHelp = `enable plugins, an alpha feature. See https://github.com/kubernetes-sigs/kustomize/blob/master/docs/plugins.md ` flagErrorFmt = ` -unable to load plugin %s because plugins disabled +unable to load external plugin %s because plugins disabled specify the flag --%s to %s` diff --git a/pkg/plugins/loader.go b/pkg/plugins/loader.go index 1e892fc2e6..3387aaf36d 100644 --- a/pkg/plugins/loader.go +++ b/pkg/plugins/loader.go @@ -11,18 +11,14 @@ import ( "strings" "github.com/pkg/errors" + "sigs.k8s.io/kustomize/v3/pkg/gvk" "sigs.k8s.io/kustomize/v3/pkg/ifc" "sigs.k8s.io/kustomize/v3/pkg/resid" "sigs.k8s.io/kustomize/v3/pkg/resmap" "sigs.k8s.io/kustomize/v3/pkg/resource" - "sigs.k8s.io/kustomize/v3/pkg/transformers" "sigs.k8s.io/kustomize/v3/pkg/types" ) -type Configurable interface { - Config(ldr ifc.Loader, rf *resmap.Factory, config []byte) error -} - type Loader struct { pc *types.PluginConfig rf *resmap.Factory @@ -34,8 +30,8 @@ func NewLoader( } func (l *Loader) LoadGenerators( - ldr ifc.Loader, rm resmap.ResMap) ([]transformers.Generator, error) { - var result []transformers.Generator + ldr ifc.Loader, rm resmap.ResMap) ([]resmap.Generator, error) { + var result []resmap.Generator for _, res := range rm.Resources() { g, err := l.LoadGenerator(ldr, res) if err != nil { @@ -47,12 +43,12 @@ func (l *Loader) LoadGenerators( } 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) if err != nil { return nil, err } - g, ok := c.(transformers.Generator) + g, ok := c.(resmap.Generator) if !ok { return nil, fmt.Errorf("plugin %s not a generator", res.OrgId()) } @@ -60,8 +56,8 @@ func (l *Loader) LoadGenerator( } func (l *Loader) LoadTransformers( - ldr ifc.Loader, rm resmap.ResMap) ([]transformers.Transformer, error) { - var result []transformers.Transformer + ldr ifc.Loader, rm resmap.ResMap) ([]resmap.Transformer, error) { + var result []resmap.Transformer for _, res := range rm.Resources() { t, err := l.LoadTransformer(ldr, res) if err != nil { @@ -73,12 +69,12 @@ func (l *Loader) LoadTransformers( } 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) if err != nil { return nil, err } - t, ok := c.(transformers.Transformer) + t, ok := c.(resmap.Transformer) if !ok { 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) } -// 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( - ldr ifc.Loader, res *resource.Resource) (Configurable, error) { - if !l.pc.Enabled { - return nil, NotEnabledErr(res.OrgId().Kind) + ldr ifc.Loader, res *resource.Resource) (c resmap.Configurable, err error) { + if isBuiltinPlugin(res) { + // 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 { return nil, err } @@ -123,7 +131,18 @@ func (l *Loader) loadAndConfigurePlugin( 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)) if p.isAvailable() { 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 // "this plugin already loaded" errors if the registry is maintained // 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) if c, ok := registry[regId]; ok { 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", regId, PluginSymbol) } - c, ok := symbol.(Configurable) + c, ok := symbol.(resmap.Configurable) if !ok { 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 } -func copyPlugin(c Configurable) Configurable { +func copyPlugin(c resmap.Configurable) resmap.Configurable { indirect := reflect.Indirect(reflect.ValueOf(c)) newIndirect := reflect.New(indirect.Type()) newIndirect.Elem().Set(reflect.ValueOf(indirect.Interface())) newNamed := newIndirect.Interface() - return newNamed.(Configurable) + return newNamed.(resmap.Configurable) } diff --git a/pkg/resmap/resmap.go b/pkg/resmap/resmap.go index 1862024d62..12976f5502 100644 --- a/pkg/resmap/resmap.go +++ b/pkg/resmap/resmap.go @@ -11,13 +11,45 @@ import ( "regexp" "github.com/pkg/errors" - + "sigs.k8s.io/kustomize/v3/pkg/ifc" "sigs.k8s.io/kustomize/v3/pkg/resid" "sigs.k8s.io/kustomize/v3/pkg/resource" "sigs.k8s.io/kustomize/v3/pkg/types" "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 // core kustomize data structure, a list of Resources. // diff --git a/pkg/target/kusttarget.go b/pkg/target/kusttarget.go index 984de8cd7c..3abb129722 100644 --- a/pkg/target/kusttarget.go +++ b/pkg/target/kusttarget.go @@ -160,7 +160,7 @@ func (kt *KustTarget) makeCustomizedResMap( func (kt *KustTarget) addHashesToNames( ra *accumulator.ResAccumulator) error { p := builtin.NewHashTransformerPlugin() - err := kt.configureBuiltinPlugin(p, nil, "hash") + err := kt.configureBuiltinPlugin(p, nil, plugins.HashTransformer) if err != nil { return err } @@ -181,7 +181,6 @@ func (kt *KustTarget) computeInventory( return fmt.Errorf("namespace mismatch") } - p := builtin.NewInventoryTransformerPlugin() var c struct { Policy string types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"` @@ -189,8 +188,8 @@ func (kt *KustTarget) computeInventory( c.Name = inv.ConfigMap.Name c.Namespace = inv.ConfigMap.Namespace c.Policy = garbagePolicy.String() - - err := kt.configureBuiltinPlugin(p, c, "inventory") + p := builtin.NewInventoryTransformerPlugin() + err := kt.configureBuiltinPlugin(p, c, plugins.InventoryTransformer) if err != nil { return err } @@ -283,7 +282,7 @@ func (kt *KustTarget) runGenerators( return nil } -func (kt *KustTarget) configureExternalGenerators() ([]transformers.Generator, error) { +func (kt *KustTarget) configureExternalGenerators() ([]resmap.Generator, error) { ra := accumulator.MakeEmptyAccumulator() err := kt.accumulateResources(ra, kt.kustomization.Generators) if err != nil { @@ -293,7 +292,7 @@ func (kt *KustTarget) configureExternalGenerators() ([]transformers.Generator, e } func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error { - var r []transformers.Transformer + var r []resmap.Transformer tConfig := ra.GetTransformerConfig() lts, err := kt.configureBuiltinTransformers(tConfig) if err != nil { @@ -309,7 +308,7 @@ func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error { return ra.Transform(t) } -func (kt *KustTarget) configureExternalTransformers() ([]transformers.Transformer, error) { +func (kt *KustTarget) configureExternalTransformers() ([]resmap.Transformer, error) { ra := accumulator.MakeEmptyAccumulator() err := kt.accumulateResources(ra, kt.kustomization.Transformers) if err != nil { @@ -374,3 +373,20 @@ func (kt *KustTarget) accumulateFile( } 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 +} diff --git a/pkg/target/kusttarget_configplugin.go b/pkg/target/kusttarget_configplugin.go index 75148eca0e..a4e9d010ba 100644 --- a/pkg/target/kusttarget_configplugin.go +++ b/pkg/target/kusttarget_configplugin.go @@ -4,50 +4,36 @@ package target import ( - "github.com/pkg/errors" "sigs.k8s.io/kustomize/v3/pkg/image" "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/types" - "sigs.k8s.io/kustomize/v3/plugin/builtin" - "sigs.k8s.io/yaml" ) // Functions dedicated to configuring the builtin // 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 -// 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 -// the kustomization file would, say, allow one to +// the kustomization file would, say, allow someone to // request multiple secrets be made, or run multiple -// image tag transforms, so we need to run the plugins -// N times (plugins are easier to write, configure and -// 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) +// image tag transforms. In these cases, we'll need +// N plugin instances with differing configurations. func (kt *KustTarget) configureBuiltinGenerators() ( - []transformers.Generator, error) { - configurators := []generatorConfigurator{ - kt.configureBuiltinConfigMapGenerator, - kt.configureBuiltinSecretGenerator, - } - var result []transformers.Generator - for _, f := range configurators { - r, err := f() + result []resmap.Generator, err error) { + for _, bpt := range []plugins.BuiltinPluginType{ + plugins.ConfigMapGenerator, + plugins.SecretGenerator, + } { + r, err := generatorConfigurators[bpt]( + kt, bpt, plugins.GeneratorFactories[bpt]) if err != nil { return nil, err } @@ -57,270 +43,256 @@ func (kt *KustTarget) configureBuiltinGenerators() ( } func (kt *KustTarget) configureBuiltinTransformers( - tConfig *config.TransformerConfig) ( - []transformers.Transformer, error) { - // TODO: Convert remaining legacy transformers to plugins - // with tests: - // - patch SMP - configurators := []transformerConfigurator{ - kt.configureBuiltinPatchStrategicMergeTransformer, - kt.configureBuiltinPatchTransformer, - kt.configureBuiltinNamespaceTransformer, - kt.configureBuiltinNameTransformer, - kt.configureBuiltinLabelTransformer, - kt.configureBuiltinAnnotationsTransformer, - kt.configureBuiltinPatchJson6902Transformer, - kt.configureBuiltinReplicaCountTransformer, - kt.configureBuiltinImageTagTransformer, - } - var result []transformers.Transformer - for _, f := range configurators { - r, err := f(tConfig) + tc *config.TransformerConfig) ( + result []resmap.Transformer, err error) { + for _, bpt := range []plugins.BuiltinPluginType{ + plugins.PatchStrategicMergeTransformer, + plugins.PatchTransformer, + plugins.NamespaceTransformer, + plugins.PrefixSuffixTransformer, + plugins.LabelTransformer, + plugins.AnnotationsTransformer, + plugins.PatchJson6902Transformer, + plugins.ReplicaCountTransformer, + plugins.ImageTagTransformer, + } { + r, err := transformerConfigurators[bpt]( + kt, bpt, plugins.TransformerFactories[bpt], tc) if err != nil { return nil, err } result = append(result, r...) } - return result, nil } -func (kt *KustTarget) configureBuiltinSecretGenerator() ( - result []transformers.Generator, err error) { - var c struct { - types.GeneratorOptions - types.SecretArgs - } - if kt.kustomization.GeneratorOptions != nil { - c.GeneratorOptions = *kt.kustomization.GeneratorOptions - } - for _, args := range kt.kustomization.SecretGenerator { - c.SecretArgs = args - p := builtin.NewSecretGeneratorPlugin() - err = kt.configureBuiltinPlugin(p, c, "secret") - if err != nil { - return nil, err +type gFactory func() resmap.GeneratorPlugin + +var generatorConfigurators = map[plugins.BuiltinPluginType]func( + kt *KustTarget, + bpt plugins.BuiltinPluginType, + factory gFactory) (result []resmap.Generator, err error){ + plugins.SecretGenerator: func(kt *KustTarget, bpt plugins.BuiltinPluginType, f gFactory) ( + result []resmap.Generator, err error) { + var c struct { + types.GeneratorOptions + types.SecretArgs } - result = append(result, p) - } - return -} + if kt.kustomization.GeneratorOptions != nil { + c.GeneratorOptions = *kt.kustomization.GeneratorOptions + } + for _, args := range kt.kustomization.SecretGenerator { + c.SecretArgs = args + p := f() + err := kt.configureBuiltinPlugin(p, c, bpt) + if err != nil { + return nil, err + } + result = append(result, p) + } + 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 + plugins.ConfigMapGenerator: func(kt *KustTarget, bpt plugins.BuiltinPluginType, f gFactory) ( + result []resmap.Generator, err error) { + var c struct { + types.GeneratorOptions + types.ConfigMapArgs } - result = append(result, p) - } - return + 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 + }, } -func (kt *KustTarget) configureBuiltinNamespaceTransformer( - tConfig *config.TransformerConfig) ( - result []transformers.Transformer, err error) { - 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 -} +type tFactory func() resmap.TransformerPlugin -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") +var transformerConfigurators = map[plugins.BuiltinPluginType]func( + kt *KustTarget, + bpt plugins.BuiltinPluginType, + f tFactory, + tc *config.TransformerConfig) (result []resmap.Transformer, err error){ + plugins.NamespaceTransformer: func( + 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 { 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 - } - 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( - tConfig *config.TransformerConfig) ( - result []transformers.Transformer, err error) { - if len(kt.kustomization.Patches) == 0 { + plugins.PatchJson6902Transformer: func( + kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, _ *config.TransformerConfig) ( + result []resmap.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 := f() + err = kt.configureBuiltinPlugin(p, c, bpt) + if err != nil { + return nil, err + } + result = append(result, p) + } return - } - var c struct { - Path string `json:"path,omitempty" yaml:"path,omitempty"` - Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` - Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"` - } - for _, patch := range kt.kustomization.Patches { - c.Target = patch.Target - c.Patch = patch.Patch - c.Path = patch.Path - p := builtin.NewPatchTransformerPlugin() - err = kt.configureBuiltinPlugin(p, c, "patch") + }, + plugins.PatchStrategicMergeTransformer: func( + kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, _ *config.TransformerConfig) ( + result []resmap.Transformer, err error) { + if len(kt.kustomization.PatchesStrategicMerge) == 0 { + 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 := f() + err = kt.configureBuiltinPlugin(p, c, bpt) if err != nil { return nil, err } result = append(result, p) - } - return -} - -func (kt *KustTarget) configureBuiltinLabelTransformer( - tConfig *config.TransformerConfig) ( - result []transformers.Transformer, err error) { - var c struct { - Labels map[string]string - FieldSpecs []config.FieldSpec - } - c.Labels = kt.kustomization.CommonLabels - c.FieldSpecs = tConfig.CommonLabels - p := builtin.NewLabelTransformerPlugin() - err = kt.configureBuiltinPlugin(p, c, "label") - if err != nil { - return nil, err - } - result = append(result, p) - return -} - -func (kt *KustTarget) configureBuiltinAnnotationsTransformer( - tConfig *config.TransformerConfig) ( - result []transformers.Transformer, err error) { - var c struct { - Annotations map[string]string - FieldSpecs []config.FieldSpec - } - c.Annotations = kt.kustomization.CommonAnnotations - c.FieldSpecs = tConfig.CommonAnnotations - p := builtin.NewAnnotationsTransformerPlugin() - err = kt.configureBuiltinPlugin(p, c, "annotations") - if err != nil { - return nil, err - } - result = append(result, p) - return -} - -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") + return + }, + plugins.PatchTransformer: func( + kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, _ *config.TransformerConfig) ( + result []resmap.Transformer, err error) { + if len(kt.kustomization.Patches) == 0 { + return + } + var c struct { + Path string `json:"path,omitempty" yaml:"path,omitempty"` + Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` + Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"` + } + for _, pc := range kt.kustomization.Patches { + c.Target = pc.Target + 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 { return nil, err } result = append(result, p) - } - return -} - -func (kt *KustTarget) configureBuiltinReplicaCountTransformer( - tConfig *config.TransformerConfig) ( - result []transformers.Transformer, err error) { - var c struct { - Replica types.Replica - FieldSpecs []config.FieldSpec - } - for _, args := range kt.kustomization.Replicas { - c.Replica = args - c.FieldSpecs = tConfig.Replicas - p := builtin.NewReplicaCountTransformerPlugin() - err = kt.configureBuiltinPlugin(p, c, "replica") + return + }, + plugins.AnnotationsTransformer: func( + kt *KustTarget, bpt plugins.BuiltinPluginType, f tFactory, tc *config.TransformerConfig) ( + result []resmap.Transformer, err error) { + var c struct { + Annotations map[string]string + FieldSpecs []config.FieldSpec + } + c.Annotations = kt.kustomization.CommonAnnotations + c.FieldSpecs = tc.CommonAnnotations + p := f() + err = kt.configureBuiltinPlugin(p, c, bpt) if err != nil { return nil, err } result = append(result, p) - } - return -} - -func (kt *KustTarget) configureBuiltinPlugin( - p plugins.Configurable, c interface{}, id string) (err error) { - var y []byte - if c != nil { - y, err = yaml.Marshal(c) + 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 errors.Wrapf( - err, "builtin %s marshal", id) + return nil, err } - } - err = p.Config(kt.ldr, kt.rFactory, y) - if err != nil { - return errors.Wrapf(err, "builtin %s config: %v", id, y) - } - return nil + 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 + }, } diff --git a/pkg/target/transformerplugin_test.go b/pkg/target/transformerplugin_test.go index 922292e2c8..e5e1e19579 100644 --- a/pkg/target/transformerplugin_test.go +++ b/pkg/target/transformerplugin_test.go @@ -113,7 +113,7 @@ transformers: if err == nil { 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) } } diff --git a/pkg/transformers/doc.go b/pkg/transformers/doc.go new file mode 100644 index 0000000000..c3226119bd --- /dev/null +++ b/pkg/transformers/doc.go @@ -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 diff --git a/pkg/transformers/labelsandannotations.go b/pkg/transformers/labelsandannotations.go index 52f7db5fc7..284121d085 100644 --- a/pkg/transformers/labelsandannotations.go +++ b/pkg/transformers/labelsandannotations.go @@ -30,23 +30,23 @@ type mapTransformer struct { fieldSpecs []config.FieldSpec } -var _ Transformer = &mapTransformer{} +var _ resmap.Transformer = &mapTransformer{} // NewLabelsMapTransformer constructs a mapTransformer. 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) } // NewAnnotationsMapTransformer construct a mapTransformer. 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) } // NewMapTransformer construct a mapTransformer. func NewMapTransformer( - pc []config.FieldSpec, m map[string]string) (Transformer, error) { + pc []config.FieldSpec, m map[string]string) (resmap.Transformer, error) { if m == nil { return NewNoOpTransformer(), nil } diff --git a/pkg/transformers/multitransformer.go b/pkg/transformers/multitransformer.go index 954dad5dd8..40ed07aeaa 100644 --- a/pkg/transformers/multitransformer.go +++ b/pkg/transformers/multitransformer.go @@ -24,25 +24,25 @@ import ( // multiTransformer contains a list of transformers. type multiTransformer struct { - transformers []Transformer + transformers []resmap.Transformer checkConflictEnabled bool } -var _ Transformer = &multiTransformer{} +var _ resmap.Transformer = &multiTransformer{} // NewMultiTransformer constructs a multiTransformer. -func NewMultiTransformer(t []Transformer) Transformer { +func NewMultiTransformer(t []resmap.Transformer) resmap.Transformer { r := &multiTransformer{ - transformers: make([]Transformer, len(t)), + transformers: make([]resmap.Transformer, len(t)), checkConflictEnabled: false} copy(r.transformers, t) return r } // NewMultiTransformerWithConflictCheck constructs a multiTransformer with checking of conflicts. -func NewMultiTransformerWithConflictCheck(t []Transformer) Transformer { +func NewMultiTransformerWithConflictCheck(t []resmap.Transformer) resmap.Transformer { r := &multiTransformer{ - transformers: make([]Transformer, len(t)), + transformers: make([]resmap.Transformer, len(t)), checkConflictEnabled: true} copy(r.transformers, t) return r diff --git a/pkg/transformers/namereference.go b/pkg/transformers/namereference.go index 6dbb4545e4..27fde2eaea 100644 --- a/pkg/transformers/namereference.go +++ b/pkg/transformers/namereference.go @@ -18,11 +18,11 @@ type nameReferenceTransformer struct { backRefs []config.NameBackReferences } -var _ Transformer = &nameReferenceTransformer{} +var _ resmap.Transformer = &nameReferenceTransformer{} // NewNameReferenceTransformer constructs a nameReferenceTransformer // with a given slice of NameBackReferences. -func NewNameReferenceTransformer(br []config.NameBackReferences) Transformer { +func NewNameReferenceTransformer(br []config.NameBackReferences) resmap.Transformer { if br == nil { log.Fatal("backrefs not expected to be nil") } diff --git a/pkg/transformers/nooptransformer.go b/pkg/transformers/nooptransformer.go index 066d23022c..6985ada505 100644 --- a/pkg/transformers/nooptransformer.go +++ b/pkg/transformers/nooptransformer.go @@ -21,10 +21,10 @@ import "sigs.k8s.io/kustomize/v3/pkg/resmap" // noOpTransformer contains a no-op transformer. type noOpTransformer struct{} -var _ Transformer = &noOpTransformer{} +var _ resmap.Transformer = &noOpTransformer{} // NewNoOpTransformer constructs a noOpTransformer. -func NewNoOpTransformer() Transformer { +func NewNoOpTransformer() resmap.Transformer { return &noOpTransformer{} } diff --git a/pkg/transformers/transformer.go b/pkg/transformers/transformer.go deleted file mode 100644 index cf088cddd8..0000000000 --- a/pkg/transformers/transformer.go +++ /dev/null @@ -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) -} diff --git a/plugin/builtin/AnnotationsTransformer.go b/plugin/builtin/AnnotationsTransformer.go index 64f63740d7..1da027ffee 100644 --- a/plugin/builtin/AnnotationsTransformer.go +++ b/plugin/builtin/AnnotationsTransformer.go @@ -15,11 +15,6 @@ type AnnotationsTransformerPlugin struct { FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` } -//noinspection GoUnusedGlobalVariable -func NewAnnotationsTransformerPlugin() *AnnotationsTransformerPlugin { - return &AnnotationsTransformerPlugin{} -} - func (p *AnnotationsTransformerPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { p.Annotations = nil @@ -37,3 +32,7 @@ func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error { } return t.Transform(m) } + +func NewAnnotationsTransformerPlugin() resmap.TransformerPlugin { + return &AnnotationsTransformerPlugin{} +} diff --git a/plugin/builtin/ConfigMapGenerator.go b/plugin/builtin/ConfigMapGenerator.go index 5bb0c1eb65..a0734db6cd 100644 --- a/plugin/builtin/ConfigMapGenerator.go +++ b/plugin/builtin/ConfigMapGenerator.go @@ -16,11 +16,6 @@ type ConfigMapGeneratorPlugin struct { types.ConfigMapArgs } -//noinspection GoUnusedGlobalVariable -func NewConfigMapGeneratorPlugin() *ConfigMapGeneratorPlugin { - return &ConfigMapGeneratorPlugin{} -} - func (p *ConfigMapGeneratorPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, config []byte) (err error) { p.GeneratorOptions = types.GeneratorOptions{} @@ -40,3 +35,7 @@ func (p *ConfigMapGeneratorPlugin) Config( func (p *ConfigMapGeneratorPlugin) Generate() (resmap.ResMap, error) { return p.rf.FromConfigMapArgs(p.ldr, &p.GeneratorOptions, p.ConfigMapArgs) } + +func NewConfigMapGeneratorPlugin() resmap.GeneratorPlugin { + return &ConfigMapGeneratorPlugin{} +} diff --git a/plugin/builtin/HashTransformer.go b/plugin/builtin/HashTransformer.go index fe1f0075ae..2f1933689f 100644 --- a/plugin/builtin/HashTransformer.go +++ b/plugin/builtin/HashTransformer.go @@ -12,11 +12,6 @@ type HashTransformerPlugin struct { hasher ifc.KunstructuredHasher } -//noinspection GoUnusedGlobalVariable -func NewHashTransformerPlugin() *HashTransformerPlugin { - return &HashTransformerPlugin{} -} - func (p *HashTransformerPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, config []byte) (err error) { p.hasher = rf.RF().Hasher() @@ -36,3 +31,7 @@ func (p *HashTransformerPlugin) Transform(m resmap.ResMap) error { } return nil } + +func NewHashTransformerPlugin() resmap.TransformerPlugin { + return &HashTransformerPlugin{} +} diff --git a/plugin/builtin/ImageTagTransformer.go b/plugin/builtin/ImageTagTransformer.go index a334445442..2e9b112809 100644 --- a/plugin/builtin/ImageTagTransformer.go +++ b/plugin/builtin/ImageTagTransformer.go @@ -21,11 +21,6 @@ type ImageTagTransformerPlugin struct { FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` } -//noinspection GoUnusedGlobalVariable -func NewImageTagTransformerPlugin() *ImageTagTransformerPlugin { - return &ImageTagTransformerPlugin{} -} - func (p *ImageTagTransformerPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { p.ImageTag = image.Image{} @@ -183,3 +178,7 @@ func split(imageName string) (name string, tag string) { tag = imageName[i:] return } + +func NewImageTagTransformerPlugin() resmap.TransformerPlugin { + return &ImageTagTransformerPlugin{} +} diff --git a/plugin/builtin/InventoryTransformer.go b/plugin/builtin/InventoryTransformer.go index 93419be94d..5ca2b293b7 100644 --- a/plugin/builtin/InventoryTransformer.go +++ b/plugin/builtin/InventoryTransformer.go @@ -22,11 +22,6 @@ type InventoryTransformerPlugin struct { Policy string `json:"policy,omitempty" yaml:"policy,omitempty"` } -//noinspection GoUnusedGlobalVariable -func NewInventoryTransformerPlugin() *InventoryTransformerPlugin { - return &InventoryTransformerPlugin{} -} - func (p *InventoryTransformerPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { p.ldr = ldr @@ -127,3 +122,7 @@ func computeRefs( } return } + +func NewInventoryTransformerPlugin() resmap.TransformerPlugin { + return &InventoryTransformerPlugin{} +} diff --git a/plugin/builtin/LabelTransformer.go b/plugin/builtin/LabelTransformer.go index 6865826b5c..e0aac60ddc 100644 --- a/plugin/builtin/LabelTransformer.go +++ b/plugin/builtin/LabelTransformer.go @@ -15,11 +15,6 @@ type LabelTransformerPlugin struct { FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` } -//noinspection GoUnusedGlobalVariable -func NewLabelTransformerPlugin() *LabelTransformerPlugin { - return &LabelTransformerPlugin{} -} - func (p *LabelTransformerPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { p.Labels = nil @@ -37,3 +32,7 @@ func (p *LabelTransformerPlugin) Transform(m resmap.ResMap) error { } return t.Transform(m) } + +func NewLabelTransformerPlugin() resmap.TransformerPlugin { + return &LabelTransformerPlugin{} +} diff --git a/plugin/builtin/LegacyOrderTransformer.go b/plugin/builtin/LegacyOrderTransformer.go index 4a8278df84..fb1a40a327 100644 --- a/plugin/builtin/LegacyOrderTransformer.go +++ b/plugin/builtin/LegacyOrderTransformer.go @@ -16,11 +16,6 @@ import ( // (like ValidatingWebhookConfiguration) last. type LegacyOrderTransformerPlugin struct{} -//noinspection GoUnusedGlobalVariable -func NewLegacyOrderTransformerPlugin() *LegacyOrderTransformerPlugin { - return &LegacyOrderTransformerPlugin{} -} - // Nothing needed for configuration. func (p *LegacyOrderTransformerPlugin) Config( 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 } + +func NewLegacyOrderTransformerPlugin() resmap.TransformerPlugin { + return &LegacyOrderTransformerPlugin{} +} diff --git a/plugin/builtin/NamespaceTransformer.go b/plugin/builtin/NamespaceTransformer.go index 3acae5152f..c9c8ca7645 100644 --- a/plugin/builtin/NamespaceTransformer.go +++ b/plugin/builtin/NamespaceTransformer.go @@ -20,11 +20,6 @@ type NamespaceTransformerPlugin struct { FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` } -//noinspection GoUnusedGlobalVariable -func NewNamespaceTransformerPlugin() *NamespaceTransformerPlugin { - return &NamespaceTransformerPlugin{} -} - func (p *NamespaceTransformerPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { p.Namespace = "" @@ -129,3 +124,7 @@ func (o *NamespaceTransformerPlugin) changeNamespace( } } } + +func NewNamespaceTransformerPlugin() resmap.TransformerPlugin { + return &NamespaceTransformerPlugin{} +} diff --git a/plugin/builtin/PatchJson6902Transformer.go b/plugin/builtin/PatchJson6902Transformer.go index 40f1629007..b28d7dd251 100644 --- a/plugin/builtin/PatchJson6902Transformer.go +++ b/plugin/builtin/PatchJson6902Transformer.go @@ -21,11 +21,6 @@ type PatchJson6902TransformerPlugin struct { JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"` } -//noinspection GoUnusedGlobalVariable -func NewPatchJson6902TransformerPlugin() *PatchJson6902TransformerPlugin { - return &PatchJson6902TransformerPlugin{} -} - func (p *PatchJson6902TransformerPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { p.ldr = ldr @@ -97,3 +92,7 @@ func (p *PatchJson6902TransformerPlugin) Transform(m resmap.ResMap) error { } return obj.UnmarshalJSON(modifiedObj) } + +func NewPatchJson6902TransformerPlugin() resmap.TransformerPlugin { + return &PatchJson6902TransformerPlugin{} +} diff --git a/plugin/builtin/PatchStrategicMergeTransformer.go b/plugin/builtin/PatchStrategicMergeTransformer.go index 1f4f4f8d3c..01d0565efe 100644 --- a/plugin/builtin/PatchStrategicMergeTransformer.go +++ b/plugin/builtin/PatchStrategicMergeTransformer.go @@ -18,11 +18,6 @@ type PatchStrategicMergeTransformerPlugin struct { Patches string `json:"patches,omitempty" yaml:"patches,omitempty"` } -//noinspection GoUnusedGlobalVariable -func NewPatchStrategicMergeTransformerPlugin() *PatchStrategicMergeTransformerPlugin { - return &PatchStrategicMergeTransformerPlugin{} -} - func (p *PatchStrategicMergeTransformerPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { p.ldr = ldr @@ -88,3 +83,7 @@ func (p *PatchStrategicMergeTransformerPlugin) Transform(m resmap.ResMap) error } return nil } + +func NewPatchStrategicMergeTransformerPlugin() resmap.TransformerPlugin { + return &PatchStrategicMergeTransformerPlugin{} +} diff --git a/plugin/builtin/PatchTransformer.go b/plugin/builtin/PatchTransformer.go index 2ab410edef..dba6f81bda 100644 --- a/plugin/builtin/PatchTransformer.go +++ b/plugin/builtin/PatchTransformer.go @@ -22,11 +22,6 @@ type PatchTransformerPlugin struct { Target *types.Selector `json:"target,omitempty", yaml:"target,omitempty"` } -//noinspection GoUnusedGlobalVariable -func NewPatchTransformerPlugin() *PatchTransformerPlugin { - return &PatchTransformerPlugin{} -} - func (p *PatchTransformerPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) { p.ldr = ldr @@ -146,3 +141,7 @@ func jsonPatchFromBytes( } return jsonpatch.DecodePatch([]byte(ops)) } + +func NewPatchTransformerPlugin() resmap.TransformerPlugin { + return &PatchTransformerPlugin{} +} diff --git a/plugin/builtin/PrefixSuffixTransformer.go b/plugin/builtin/PrefixSuffixTransformer.go index 6e4e2e6d6d..db4af420c4 100644 --- a/plugin/builtin/PrefixSuffixTransformer.go +++ b/plugin/builtin/PrefixSuffixTransformer.go @@ -21,11 +21,6 @@ type PrefixSuffixTransformerPlugin struct { 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. var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{ { @@ -118,3 +113,7 @@ func (p *PrefixSuffixTransformerPlugin) addPrefixSuffix( } return fmt.Sprintf("%s%s%s", p.Prefix, s, p.Suffix), nil } + +func NewPrefixSuffixTransformerPlugin() resmap.TransformerPlugin { + return &PrefixSuffixTransformerPlugin{} +} diff --git a/plugin/builtin/ReplicaCountTransformer.go b/plugin/builtin/ReplicaCountTransformer.go index 1f493a4ed9..93235ab5c9 100644 --- a/plugin/builtin/ReplicaCountTransformer.go +++ b/plugin/builtin/ReplicaCountTransformer.go @@ -20,11 +20,6 @@ type ReplicaCountTransformerPlugin struct { FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` } -//noinspection GoUnusedGlobalVariable -func NewReplicaCountTransformerPlugin() *ReplicaCountTransformerPlugin { - return &ReplicaCountTransformerPlugin{} -} - func (p *ReplicaCountTransformerPlugin) Config( 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 } + +func NewReplicaCountTransformerPlugin() resmap.TransformerPlugin { + return &ReplicaCountTransformerPlugin{} +} diff --git a/plugin/builtin/SecretGenerator.go b/plugin/builtin/SecretGenerator.go index ce954f5b49..bb92bfb488 100644 --- a/plugin/builtin/SecretGenerator.go +++ b/plugin/builtin/SecretGenerator.go @@ -16,11 +16,6 @@ type SecretGeneratorPlugin struct { types.SecretArgs } -//noinspection GoUnusedGlobalVariable -func NewSecretGeneratorPlugin() *SecretGeneratorPlugin { - return &SecretGeneratorPlugin{} -} - func (p *SecretGeneratorPlugin) Config( ldr ifc.Loader, rf *resmap.Factory, config []byte) (err error) { p.GeneratorOptions = types.GeneratorOptions{} @@ -40,3 +35,7 @@ func (p *SecretGeneratorPlugin) Config( func (p *SecretGeneratorPlugin) Generate() (resmap.ResMap, error) { return p.rf.FromSecretArgs(p.ldr, &p.GeneratorOptions, p.SecretArgs) } + +func NewSecretGeneratorPlugin() resmap.GeneratorPlugin { + return &SecretGeneratorPlugin{} +} diff --git a/plugin/someteam.example.com/v1/dateprefixer/DatePrefixer.go b/plugin/someteam.example.com/v1/dateprefixer/DatePrefixer.go index 3f1fc15e5c..d25fdde6c7 100644 --- a/plugin/someteam.example.com/v1/dateprefixer/DatePrefixer.go +++ b/plugin/someteam.example.com/v1/dateprefixer/DatePrefixer.go @@ -7,7 +7,6 @@ import ( "github.com/pkg/errors" "sigs.k8s.io/kustomize/v3/pkg/ifc" "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/plugin/builtin" "sigs.k8s.io/yaml" @@ -16,7 +15,7 @@ import ( // Add a date prefix to the name. // A plugin that adapts another plugin. type plugin struct { - t transformers.Transformer + t resmap.Transformer } //nolint: golint diff --git a/plugin/someteam.example.com/v1/stringprefixer/StringPrefixer.go b/plugin/someteam.example.com/v1/stringprefixer/StringPrefixer.go index e34d620b13..01127cc949 100644 --- a/plugin/someteam.example.com/v1/stringprefixer/StringPrefixer.go +++ b/plugin/someteam.example.com/v1/stringprefixer/StringPrefixer.go @@ -7,7 +7,6 @@ import ( "github.com/pkg/errors" "sigs.k8s.io/kustomize/v3/pkg/ifc" "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/types" "sigs.k8s.io/kustomize/v3/plugin/builtin" @@ -18,7 +17,7 @@ import ( // A plugin that adapts another plugin. type plugin struct { types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` - t transformers.Transformer + t resmap.Transformer } //nolint: golint