Push suffix/prefix code to plugin.

This commit is contained in:
jregan
2019-06-11 18:28:28 -07:00
parent fcc3082231
commit 11bb176a3f
19 changed files with 312 additions and 307 deletions

View File

@@ -101,7 +101,7 @@ To get the plugin ready to generator or transform,
it is given the entire contents of the
configuration file.
[NameTransformer]: ../plugin/builtin/nametransformer/NameTransformer_test.go
[NameTransformer]: ../plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer_test.go
[ChartInflator]: ../plugin/someteam.example.com/v1/chartinflator/ChartInflator_test.go
[plugins]: ../plugin/builtin

View File

@@ -53,11 +53,6 @@ func NewResIdWithSuffixNamespace(k gvk.Gvk, n, s, ns string) ResId {
return ResId{ItemId: ItemId{Gvk: k, Name: n, Namespace: ns}, Suffix: s}
}
// NewResIdWithPrefixSuffix creates new resource identifier with a prefix and suffix
func NewResIdWithPrefixSuffix(k gvk.Gvk, n, p, s string) ResId {
return ResId{ItemId: ItemId{Gvk: k, Name: n}, Prefix: p, Suffix: s}
}
// NewResId creates new resource identifier
func NewResId(k gvk.Gvk, n string) ResId {
return ResId{ItemId: ItemId{Gvk: k, Name: n}}

View File

@@ -206,8 +206,8 @@ func (kt *KustTarget) configureBuiltinNameTransformer(
c.Prefix = kt.kustomization.NamePrefix
c.Suffix = kt.kustomization.NameSuffix
c.FieldSpecs = tConfig.NamePrefix
p := builtin.NewNameTransformerPlugin()
err = kt.configureBuiltinPlugin(p, c, "name")
p := builtin.NewPrefixSuffixTransformerPlugin()
err = kt.configureBuiltinPlugin(p, c, "prefixsuffix")
if err != nil {
return nil, err
}

View File

@@ -49,7 +49,7 @@ func (pt *imageTransformer) Transform(m resmap.ResMap) error {
if !r.Id().Gvk().IsSelected(&path.Gvk) {
continue
}
err := mutateField(r.Map(), path.PathSlice(), false, pt.mutateImage)
err := MutateField(r.Map(), path.PathSlice(), false, pt.mutateImage)
if err != nil {
return err
}

View File

@@ -64,7 +64,7 @@ func (o *mapTransformer) Transform(m resmap.ResMap) error {
if !r.Id().Gvk().IsSelected(&path.Gvk) {
continue
}
err := mutateField(
err := MutateField(
r.Map(), path.PathSlice(),
path.CreateIfNotPresent, o.addMap)
if err != nil {

View File

@@ -24,7 +24,7 @@ import (
type mutateFunc func(interface{}) (interface{}, error)
func mutateField(
func MutateField(
m map[string]interface{},
pathToField []string,
createIfNotPresent bool,
@@ -61,7 +61,7 @@ func mutateField(
strings.Join(pathToField, "."))
return nil
case map[string]interface{}:
return mutateField(typedV, newPathToField, createIfNotPresent, fns...)
return MutateField(typedV, newPathToField, createIfNotPresent, fns...)
case []interface{}:
for i := range typedV {
item := typedV[i]
@@ -69,7 +69,7 @@ func mutateField(
if !ok {
return fmt.Errorf("%#v is expected to be %T", item, typedItem)
}
err := mutateField(typedItem, newPathToField, createIfNotPresent, fns...)
err := MutateField(typedItem, newPathToField, createIfNotPresent, fns...)
if err != nil {
return err
}

View File

@@ -89,7 +89,7 @@ func getFieldValue(t *testing.T, obj ifc.Kunstructured, fieldName string) string
func TestNoPath(t *testing.T) {
obj := makeTestDeployment()
m := &noopMutator{}
err := mutateField(
err := MutateField(
obj.Map(), []string{}, false, m.mutate)
if m.wasCalled {
t.Fatalf("mutator should not have been called.")
@@ -111,7 +111,7 @@ func TestHappyPath(t *testing.T) {
}
m := &noopMutator{}
err := mutateField(
err := MutateField(
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
if !m.wasCalled {
t.Fatalf("mutator should have been called.")
@@ -125,7 +125,7 @@ func TestHappyPath(t *testing.T) {
}
m = &noopMutator{}
err = mutateField(
err = MutateField(
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
if !m.wasCalled {
t.Fatalf("mutator should have been called.")
@@ -142,7 +142,7 @@ func TestHappyPath(t *testing.T) {
func TestWithError(t *testing.T) {
obj := makeTestDeployment()
m := noopMutator{errorToReturn: errExpected}
err := mutateField(
err := MutateField(
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
if !m.wasCalled {
t.Fatalf("mutator was not called!")
@@ -160,7 +160,7 @@ func TestWithNil(t *testing.T) {
foo.(map[string]interface{})["labels"] = nil
m := &noopMutator{}
err := mutateField(
err := MutateField(
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
if err != nil {
t.Fatalf("Unexpected error: %v", err)

View File

@@ -97,7 +97,7 @@ func (o *nameReferenceTransformer) Transform(m resmap.ResMap) error {
if candidates == nil {
candidates = m.SubsetThatCouldBeReferencedBy(referrer)
}
err := mutateField(
err := MutateField(
res.Map(),
fSpec.PathSlice(),
fSpec.CreateIfNotPresent,

View File

@@ -59,7 +59,7 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
case "metadata/namespace":
if id.Gvk().IsSelected(&path.Gvk) && !id.Gvk().IsClusterKind() {
if len(objMap) > 0 {
err := mutateField(
err := MutateField(
objMap, path.PathSlice(), path.CreateIfNotPresent,
func(_ interface{}) (interface{}, error) {
return o.namespace, nil
@@ -75,7 +75,7 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
}
// make sure the object is non empty
if len(objMap) > 0 {
err := mutateField(
err := MutateField(
objMap, path.PathSlice(), path.CreateIfNotPresent,
func(_ interface{}) (interface{}, error) {
return o.namespace, nil
@@ -144,7 +144,7 @@ func (o *namespaceTransformer) updateClusterRoleBinding(m resmap.ResMap) {
// a ServiceAccount named “default” exists in every active namespace
if name.(string) == "default" || saMap[name.(string)] {
subject := subjects[i].(map[string]interface{})
mutateField(subject, []string{"namespace"}, true, func(_ interface{}) (interface{}, error) {
MutateField(subject, []string{"namespace"}, true, func(_ interface{}) (interface{}, error) {
return o.namespace, nil
})
subjects[i] = subject

View File

@@ -1,99 +0,0 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package transformers
import (
"errors"
"fmt"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers/config"
)
// prefixSuffixTransformer contains the prefix, suffix, and the FieldSpecs
// for each field needing a prefix and suffix.
type prefixSuffixTransformer struct {
prefix string
suffix string
fieldSpecsToUse []config.FieldSpec
fieldSpecsToSkip []config.FieldSpec
}
var _ Transformer = &prefixSuffixTransformer{}
// Not placed in a file yet due to lack of demand.
var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{
{
Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"},
},
}
// NewPrefixSuffixTransformer makes a prefixSuffixTransformer.
func NewPrefixSuffixTransformer(
np, ns string, fieldSpecs []config.FieldSpec) (Transformer, error) {
if len(np) == 0 && len(ns) == 0 {
return NewNoOpTransformer(), nil
}
if fieldSpecs == nil {
return nil, errors.New("fieldSpecs is not expected to be nil")
}
return &prefixSuffixTransformer{
prefix: np,
suffix: ns,
fieldSpecsToUse: fieldSpecs,
fieldSpecsToSkip: prefixSuffixFieldSpecsToSkip}, nil
}
// Transform prepends the prefix and appends the suffix to the field contents.
// TODO: this transformer breaks internal
// ordering and depends on Id hackery. Rewrite completely.
func (o *prefixSuffixTransformer) Transform(m resmap.ResMap) error {
// Fill map "mf" with entries subject to name modification, and
// delete these entries from "m", so that for now m retains only
// the entries whose names will not be modified.
mf := resmap.New()
for id, r := range m.AsMap() {
found := false
for _, path := range o.fieldSpecsToSkip {
if id.Gvk().IsSelected(&path.Gvk) {
found = true
break
}
}
if !found {
mf.AppendWithId(id, r)
m.Remove(id)
}
}
for id, r := range mf.AsMap() {
objMap := r.Map()
for _, path := range o.fieldSpecsToUse {
if !id.Gvk().IsSelected(&path.Gvk) {
continue
}
err := mutateField(
objMap,
path.PathSlice(),
path.CreateIfNotPresent,
o.addPrefixSuffix)
if err != nil {
return err
}
newId := id.CopyWithNewPrefixSuffix(o.prefix, o.suffix)
m.AppendWithId(newId, r)
}
}
return nil
}
func (o *prefixSuffixTransformer) addPrefixSuffix(
in interface{}) (interface{}, error) {
s, ok := in.(string)
if !ok {
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
}
return fmt.Sprintf("%s%s%s", o.prefix, s, o.suffix), nil
}

View File

@@ -1,83 +0,0 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package transformers
import (
"testing"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
)
func TestPrefixSuffixNameRun(t *testing.T) {
rf := resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl())
m := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(cmap, "cm1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm1",
},
}),
resid.NewResId(cmap, "cm2"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm2",
},
}),
resid.NewResId(crd, "crd"): rf.FromMap(
map[string]interface{}{
"apiVersion": "apiextensions.k8s.io/v1beta1",
"kind": "CustomResourceDefinition",
"metadata": map[string]interface{}{
"name": "crd",
},
}),
})
expected := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResIdWithPrefixSuffix(cmap, "cm1", "someprefix-", "-somesuffix"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "someprefix-cm1-somesuffix",
},
}),
resid.NewResIdWithPrefixSuffix(cmap, "cm2", "someprefix-", "-somesuffix"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "someprefix-cm2-somesuffix",
},
}),
resid.NewResId(crd, "crd"): rf.FromMap(
map[string]interface{}{
"apiVersion": "apiextensions.k8s.io/v1beta1",
"kind": "CustomResourceDefinition",
"metadata": map[string]interface{}{
"name": "crd",
},
}),
})
npst, err := NewPrefixSuffixTransformer(
"someprefix-", "-somesuffix", defaultTransformerConfig.NamePrefix)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = npst.Transform(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err = expected.ErrorIfNotEqualSets(m); err != nil {
t.Fatalf("actual doesn't match expected: %v", err)
}
}

View File

@@ -98,7 +98,7 @@ func (rv *RefVarTransformer) Transform(m resmap.ResMap) error {
for _, res := range m.Resources() {
for _, fieldSpec := range rv.fieldSpecs {
if res.Id().Gvk().IsSelected(&fieldSpec.Gvk) {
if err := mutateField(
if err := MutateField(
res.Map(), fieldSpec.PathSlice(),
false, rv.replaceVars); err != nil {
return err

View File

@@ -1,41 +0,0 @@
// Code generated by pluginator on NameTransformer; DO NOT EDIT.
package builtin
import (
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers"
"sigs.k8s.io/kustomize/pkg/transformers/config"
"sigs.k8s.io/yaml"
)
// Add the given prefix and suffix to the resource name.
type NameTransformerPlugin struct {
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
Suffix string `json:"suffix,omitempty" yaml:"suffix,omitempty"`
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
}
func NewNameTransformerPlugin() *NameTransformerPlugin {
return &NameTransformerPlugin{}
}
func (p *NameTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.Prefix = ""
p.Suffix = ""
p.FieldSpecs = nil
return yaml.Unmarshal(c, p)
}
func (p *NameTransformerPlugin) Transform(m resmap.ResMap) error {
t, err := transformers.NewPrefixSuffixTransformer(
p.Prefix,
p.Suffix,
p.FieldSpecs,
)
if err != nil {
return err
}
return t.Transform(m)
}

View File

@@ -0,0 +1,100 @@
// Code generated by pluginator on PrefixSuffixTransformer; DO NOT EDIT.
package builtin
import (
"errors"
"fmt"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers"
"sigs.k8s.io/kustomize/pkg/transformers/config"
"sigs.k8s.io/yaml"
)
// Add the given prefix and suffix to the field.
type PrefixSuffixTransformerPlugin struct {
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
Suffix string `json:"suffix,omitempty" yaml:"suffix,omitempty"`
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
}
func NewPrefixSuffixTransformerPlugin() *PrefixSuffixTransformerPlugin {
return &PrefixSuffixTransformerPlugin{}
}
// Not placed in a file yet due to lack of demand.
var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{
{
Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"},
},
}
func (p *PrefixSuffixTransformerPlugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.Prefix = ""
p.Suffix = ""
p.FieldSpecs = nil
err = yaml.Unmarshal(c, p)
if err != nil {
return
}
if p.FieldSpecs == nil {
return errors.New("fieldSpecs is not expected to be nil")
}
return
}
func (p *PrefixSuffixTransformerPlugin) Transform(m resmap.ResMap) error {
if len(p.Prefix) == 0 && len(p.Suffix) == 0 {
return nil
}
// Fill map "mf" with entries subject to name modification, and
// delete these entries from "m", so that for now m retains only
// the entries whose names will not be modified.
mf := resmap.New()
for id, r := range m.AsMap() {
found := false
for _, path := range prefixSuffixFieldSpecsToSkip {
if id.Gvk().IsSelected(&path.Gvk) {
found = true
break
}
}
if !found {
mf.AppendWithId(id, r)
m.Remove(id)
}
}
for id, r := range mf.AsMap() {
objMap := r.Map()
for _, path := range p.FieldSpecs {
if !id.Gvk().IsSelected(&path.Gvk) {
continue
}
err := transformers.MutateField(
objMap,
path.PathSlice(),
path.CreateIfNotPresent,
p.addPrefixSuffix)
if err != nil {
return err
}
newId := id.CopyWithNewPrefixSuffix(p.Prefix, p.Suffix)
m.AppendWithId(newId, r)
}
}
return nil
}
func (p *PrefixSuffixTransformerPlugin) addPrefixSuffix(
in interface{}) (interface{}, error) {
s, ok := in.(string)
if !ok {
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
}
return fmt.Sprintf("%s%s%s", p.Prefix, s, p.Suffix), nil
}

View File

@@ -1,42 +0,0 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
//go:generate go run sigs.k8s.io/kustomize/plugin/pluginator
package main
import (
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers"
"sigs.k8s.io/kustomize/pkg/transformers/config"
"sigs.k8s.io/yaml"
)
// Add the given prefix and suffix to the field.
type plugin struct {
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
Suffix string `json:"suffix,omitempty" yaml:"suffix,omitempty"`
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
}
var KustomizePlugin plugin
func (p *plugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.Prefix = ""
p.Suffix = ""
p.FieldSpecs = nil
return yaml.Unmarshal(c, p)
}
func (p *plugin) Transform(m resmap.ResMap) error {
t, err := transformers.NewPrefixSuffixTransformer(
p.Prefix,
p.Suffix,
p.FieldSpecs,
)
if err != nil {
return err
}
return t.Transform(m)
}

View File

@@ -0,0 +1,101 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
//go:generate go run sigs.k8s.io/kustomize/plugin/pluginator
package main
import (
"errors"
"fmt"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers"
"sigs.k8s.io/kustomize/pkg/transformers/config"
"sigs.k8s.io/yaml"
)
// Add the given prefix and suffix to the field.
type plugin struct {
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
Suffix string `json:"suffix,omitempty" yaml:"suffix,omitempty"`
FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
}
var KustomizePlugin plugin
// Not placed in a file yet due to lack of demand.
var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{
{
Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"},
},
}
func (p *plugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) (err error) {
p.Prefix = ""
p.Suffix = ""
p.FieldSpecs = nil
err = yaml.Unmarshal(c, p)
if err != nil {
return
}
if p.FieldSpecs == nil {
return errors.New("fieldSpecs is not expected to be nil")
}
return
}
func (p *plugin) Transform(m resmap.ResMap) error {
if len(p.Prefix) == 0 && len(p.Suffix) == 0 {
return nil
}
// Fill map "mf" with entries subject to name modification, and
// delete these entries from "m", so that for now m retains only
// the entries whose names will not be modified.
mf := resmap.New()
for id, r := range m.AsMap() {
found := false
for _, path := range prefixSuffixFieldSpecsToSkip {
if id.Gvk().IsSelected(&path.Gvk) {
found = true
break
}
}
if !found {
mf.AppendWithId(id, r)
m.Remove(id)
}
}
for id, r := range mf.AsMap() {
objMap := r.Map()
for _, path := range p.FieldSpecs {
if !id.Gvk().IsSelected(&path.Gvk) {
continue
}
err := transformers.MutateField(
objMap,
path.PathSlice(),
path.CreateIfNotPresent,
p.addPrefixSuffix)
if err != nil {
return err
}
newId := id.CopyWithNewPrefixSuffix(p.Prefix, p.Suffix)
m.AppendWithId(newId, r)
}
}
return nil
}
func (p *plugin) addPrefixSuffix(
in interface{}) (interface{}, error) {
s, ok := in.(string)
if !ok {
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
}
return fmt.Sprintf("%s%s%s", p.Prefix, s, p.Suffix), nil
}

View File

@@ -10,17 +10,17 @@ import (
"sigs.k8s.io/kustomize/plugin"
)
func TestNameTransformer(t *testing.T) {
func TestPrefixSuffixTransformer(t *testing.T) {
tc := plugin.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
"builtin", "", "NameTransformer")
"builtin", "", "PrefixSuffixTransformer")
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
rm := th.LoadAndRunTransformer(`
apiVersion: builtin
kind: NameTransformer
kind: PrefixSuffixTransformer
metadata:
name: notImportantHere
prefix: baked-
@@ -35,9 +35,29 @@ metadata:
spec:
ports:
- port: 7002
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: crd
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cm
`)
th.AssertActualEqualsExpected(rm, `
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: crd
---
apiVersion: v1
kind: ConfigMap
metadata:
name: baked-cm-pie
---
apiVersion: v1
kind: Service
metadata:

View File

@@ -4,18 +4,51 @@
package main
import (
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers"
"sigs.k8s.io/kustomize/pkg/transformers/config"
"sigs.k8s.io/kustomize/plugin/builtin"
"sigs.k8s.io/yaml"
)
type plugin struct{}
// Add a date prefix to the name.
// A plugin that adapts another plugin.
type plugin struct {
t transformers.Transformer
}
var KustomizePlugin plugin
func (p *plugin) makePrefixSuffixPluginConfig() ([]byte, error) {
var s struct {
Prefix string
Suffix string
FieldSpecs []config.FieldSpec
}
s.Prefix = getDate() + "-"
s.FieldSpecs = []config.FieldSpec{
{Path: "metadata/name"},
}
return yaml.Marshal(s)
}
func (p *plugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) error {
// Ignore the incoming c, compute new config.
c, err := p.makePrefixSuffixPluginConfig()
if err != nil {
return errors.Wrapf(
err, "dateprefixer makeconfig")
}
prefixer := builtin.NewPrefixSuffixTransformerPlugin()
err = prefixer.Config(ldr, rf, c)
if err != nil {
return errors.Wrapf(
err, "prefixsuffix configure")
}
p.t = prefixer
return nil
}
@@ -28,11 +61,5 @@ func getDate() string {
}
func (p *plugin) Transform(m resmap.ResMap) error {
tr, err := transformers.NewPrefixSuffixTransformer(
getDate()+"-", "",
config.MakeDefaultConfig().NamePrefix)
if err != nil {
return err
}
return tr.Transform(m)
return p.t.Transform(m)
}

View File

@@ -4,15 +4,20 @@
package main
import (
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/transformers"
"sigs.k8s.io/kustomize/pkg/transformers/config"
"sigs.k8s.io/kustomize/plugin/builtin"
"sigs.k8s.io/yaml"
)
// Add a string prefix to the name.
// A plugin that adapts another plugin.
type plugin struct {
Metadata metaData `json:"metadata,omitempty" yaml:"metadata,omitempty"`
t transformers.Transformer
}
type metaData struct {
@@ -21,17 +26,39 @@ type metaData struct {
var KustomizePlugin plugin
func (p *plugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) error {
return yaml.Unmarshal(c, p)
func (p *plugin) makePrefixSuffixPluginConfig(n string) ([]byte, error) {
var s struct {
Prefix string
Suffix string
FieldSpecs []config.FieldSpec
}
s.Prefix = n + "-"
s.FieldSpecs = []config.FieldSpec{
{Path: "metadata/name"},
}
return yaml.Marshal(s)
}
func (p *plugin) Transform(m resmap.ResMap) error {
tr, err := transformers.NewPrefixSuffixTransformer(
p.Metadata.Name+"-", "",
config.MakeDefaultConfig().NamePrefix)
func (p *plugin) Config(
ldr ifc.Loader, rf *resmap.Factory, c []byte) error {
err := yaml.Unmarshal(c, p)
if err != nil {
return err
}
return tr.Transform(m)
c, err = p.makePrefixSuffixPluginConfig(p.Metadata.Name)
if err != nil {
return err
}
prefixer := builtin.NewPrefixSuffixTransformerPlugin()
err = prefixer.Config(ldr, rf, c)
if err != nil {
return errors.Wrapf(
err, "stringprefixer configure")
}
p.t = prefixer
return nil
}
func (p *plugin) Transform(m resmap.ResMap) error {
return p.t.Transform(m)
}