mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
Merge pull request #1173 from monopole/prefixSuffixPlugin
Push suffix/prefix code to plugin.
This commit is contained in:
@@ -101,7 +101,7 @@ To get the plugin ready to generator or transform,
|
|||||||
it is given the entire contents of the
|
it is given the entire contents of the
|
||||||
configuration file.
|
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
|
[ChartInflator]: ../plugin/someteam.example.com/v1/chartinflator/ChartInflator_test.go
|
||||||
[plugins]: ../plugin/builtin
|
[plugins]: ../plugin/builtin
|
||||||
|
|
||||||
|
|||||||
@@ -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}
|
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
|
// NewResId creates new resource identifier
|
||||||
func NewResId(k gvk.Gvk, n string) ResId {
|
func NewResId(k gvk.Gvk, n string) ResId {
|
||||||
return ResId{ItemId: ItemId{Gvk: k, Name: n}}
|
return ResId{ItemId: ItemId{Gvk: k, Name: n}}
|
||||||
|
|||||||
@@ -206,8 +206,8 @@ func (kt *KustTarget) configureBuiltinNameTransformer(
|
|||||||
c.Prefix = kt.kustomization.NamePrefix
|
c.Prefix = kt.kustomization.NamePrefix
|
||||||
c.Suffix = kt.kustomization.NameSuffix
|
c.Suffix = kt.kustomization.NameSuffix
|
||||||
c.FieldSpecs = tConfig.NamePrefix
|
c.FieldSpecs = tConfig.NamePrefix
|
||||||
p := builtin.NewNameTransformerPlugin()
|
p := builtin.NewPrefixSuffixTransformerPlugin()
|
||||||
err = kt.configureBuiltinPlugin(p, c, "name")
|
err = kt.configureBuiltinPlugin(p, c, "prefixsuffix")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func (pt *imageTransformer) Transform(m resmap.ResMap) error {
|
|||||||
if !r.Id().Gvk().IsSelected(&path.Gvk) {
|
if !r.Id().Gvk().IsSelected(&path.Gvk) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err := mutateField(r.Map(), path.PathSlice(), false, pt.mutateImage)
|
err := MutateField(r.Map(), path.PathSlice(), false, pt.mutateImage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func (o *mapTransformer) Transform(m resmap.ResMap) error {
|
|||||||
if !r.Id().Gvk().IsSelected(&path.Gvk) {
|
if !r.Id().Gvk().IsSelected(&path.Gvk) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err := mutateField(
|
err := MutateField(
|
||||||
r.Map(), path.PathSlice(),
|
r.Map(), path.PathSlice(),
|
||||||
path.CreateIfNotPresent, o.addMap)
|
path.CreateIfNotPresent, o.addMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import (
|
|||||||
|
|
||||||
type mutateFunc func(interface{}) (interface{}, error)
|
type mutateFunc func(interface{}) (interface{}, error)
|
||||||
|
|
||||||
func mutateField(
|
func MutateField(
|
||||||
m map[string]interface{},
|
m map[string]interface{},
|
||||||
pathToField []string,
|
pathToField []string,
|
||||||
createIfNotPresent bool,
|
createIfNotPresent bool,
|
||||||
@@ -61,7 +61,7 @@ func mutateField(
|
|||||||
strings.Join(pathToField, "."))
|
strings.Join(pathToField, "."))
|
||||||
return nil
|
return nil
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
return mutateField(typedV, newPathToField, createIfNotPresent, fns...)
|
return MutateField(typedV, newPathToField, createIfNotPresent, fns...)
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
for i := range typedV {
|
for i := range typedV {
|
||||||
item := typedV[i]
|
item := typedV[i]
|
||||||
@@ -69,7 +69,7 @@ func mutateField(
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("%#v is expected to be %T", item, typedItem)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ func getFieldValue(t *testing.T, obj ifc.Kunstructured, fieldName string) string
|
|||||||
func TestNoPath(t *testing.T) {
|
func TestNoPath(t *testing.T) {
|
||||||
obj := makeTestDeployment()
|
obj := makeTestDeployment()
|
||||||
m := &noopMutator{}
|
m := &noopMutator{}
|
||||||
err := mutateField(
|
err := MutateField(
|
||||||
obj.Map(), []string{}, false, m.mutate)
|
obj.Map(), []string{}, false, m.mutate)
|
||||||
if m.wasCalled {
|
if m.wasCalled {
|
||||||
t.Fatalf("mutator should not have been called.")
|
t.Fatalf("mutator should not have been called.")
|
||||||
@@ -111,7 +111,7 @@ func TestHappyPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m := &noopMutator{}
|
m := &noopMutator{}
|
||||||
err := mutateField(
|
err := MutateField(
|
||||||
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
|
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
|
||||||
if !m.wasCalled {
|
if !m.wasCalled {
|
||||||
t.Fatalf("mutator should have been called.")
|
t.Fatalf("mutator should have been called.")
|
||||||
@@ -125,7 +125,7 @@ func TestHappyPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m = &noopMutator{}
|
m = &noopMutator{}
|
||||||
err = mutateField(
|
err = MutateField(
|
||||||
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
|
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
|
||||||
if !m.wasCalled {
|
if !m.wasCalled {
|
||||||
t.Fatalf("mutator should have been called.")
|
t.Fatalf("mutator should have been called.")
|
||||||
@@ -142,7 +142,7 @@ func TestHappyPath(t *testing.T) {
|
|||||||
func TestWithError(t *testing.T) {
|
func TestWithError(t *testing.T) {
|
||||||
obj := makeTestDeployment()
|
obj := makeTestDeployment()
|
||||||
m := noopMutator{errorToReturn: errExpected}
|
m := noopMutator{errorToReturn: errExpected}
|
||||||
err := mutateField(
|
err := MutateField(
|
||||||
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
|
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
|
||||||
if !m.wasCalled {
|
if !m.wasCalled {
|
||||||
t.Fatalf("mutator was not called!")
|
t.Fatalf("mutator was not called!")
|
||||||
@@ -160,7 +160,7 @@ func TestWithNil(t *testing.T) {
|
|||||||
foo.(map[string]interface{})["labels"] = nil
|
foo.(map[string]interface{})["labels"] = nil
|
||||||
|
|
||||||
m := &noopMutator{}
|
m := &noopMutator{}
|
||||||
err := mutateField(
|
err := MutateField(
|
||||||
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
|
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected error: %v", err)
|
t.Fatalf("Unexpected error: %v", err)
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ func (o *nameReferenceTransformer) Transform(m resmap.ResMap) error {
|
|||||||
if candidates == nil {
|
if candidates == nil {
|
||||||
candidates = m.SubsetThatCouldBeReferencedBy(referrer)
|
candidates = m.SubsetThatCouldBeReferencedBy(referrer)
|
||||||
}
|
}
|
||||||
err := mutateField(
|
err := MutateField(
|
||||||
res.Map(),
|
res.Map(),
|
||||||
fSpec.PathSlice(),
|
fSpec.PathSlice(),
|
||||||
fSpec.CreateIfNotPresent,
|
fSpec.CreateIfNotPresent,
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
|
|||||||
case "metadata/namespace":
|
case "metadata/namespace":
|
||||||
if id.Gvk().IsSelected(&path.Gvk) && !id.Gvk().IsClusterKind() {
|
if id.Gvk().IsSelected(&path.Gvk) && !id.Gvk().IsClusterKind() {
|
||||||
if len(objMap) > 0 {
|
if len(objMap) > 0 {
|
||||||
err := mutateField(
|
err := MutateField(
|
||||||
objMap, path.PathSlice(), path.CreateIfNotPresent,
|
objMap, path.PathSlice(), path.CreateIfNotPresent,
|
||||||
func(_ interface{}) (interface{}, error) {
|
func(_ interface{}) (interface{}, error) {
|
||||||
return o.namespace, nil
|
return o.namespace, nil
|
||||||
@@ -75,7 +75,7 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
|
|||||||
}
|
}
|
||||||
// make sure the object is non empty
|
// make sure the object is non empty
|
||||||
if len(objMap) > 0 {
|
if len(objMap) > 0 {
|
||||||
err := mutateField(
|
err := MutateField(
|
||||||
objMap, path.PathSlice(), path.CreateIfNotPresent,
|
objMap, path.PathSlice(), path.CreateIfNotPresent,
|
||||||
func(_ interface{}) (interface{}, error) {
|
func(_ interface{}) (interface{}, error) {
|
||||||
return o.namespace, nil
|
return o.namespace, nil
|
||||||
@@ -144,7 +144,7 @@ func (o *namespaceTransformer) updateClusterRoleBinding(m resmap.ResMap) {
|
|||||||
// a ServiceAccount named “default” exists in every active namespace
|
// a ServiceAccount named “default” exists in every active namespace
|
||||||
if name.(string) == "default" || saMap[name.(string)] {
|
if name.(string) == "default" || saMap[name.(string)] {
|
||||||
subject := subjects[i].(map[string]interface{})
|
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
|
return o.namespace, nil
|
||||||
})
|
})
|
||||||
subjects[i] = subject
|
subjects[i] = subject
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -98,7 +98,7 @@ func (rv *RefVarTransformer) Transform(m resmap.ResMap) error {
|
|||||||
for _, res := range m.Resources() {
|
for _, res := range m.Resources() {
|
||||||
for _, fieldSpec := range rv.fieldSpecs {
|
for _, fieldSpec := range rv.fieldSpecs {
|
||||||
if res.Id().Gvk().IsSelected(&fieldSpec.Gvk) {
|
if res.Id().Gvk().IsSelected(&fieldSpec.Gvk) {
|
||||||
if err := mutateField(
|
if err := MutateField(
|
||||||
res.Map(), fieldSpec.PathSlice(),
|
res.Map(), fieldSpec.PathSlice(),
|
||||||
false, rv.replaceVars); err != nil {
|
false, rv.replaceVars); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -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)
|
|
||||||
}
|
|
||||||
100
plugin/builtin/PrefixSuffixTransformer.go
Normal file
100
plugin/builtin/PrefixSuffixTransformer.go
Normal 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
|
||||||
|
}
|
||||||
@@ -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)
|
|
||||||
}
|
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -10,17 +10,17 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/plugin"
|
"sigs.k8s.io/kustomize/plugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNameTransformer(t *testing.T) {
|
func TestPrefixSuffixTransformer(t *testing.T) {
|
||||||
tc := plugin.NewEnvForTest(t).Set()
|
tc := plugin.NewEnvForTest(t).Set()
|
||||||
defer tc.Reset()
|
defer tc.Reset()
|
||||||
|
|
||||||
tc.BuildGoPlugin(
|
tc.BuildGoPlugin(
|
||||||
"builtin", "", "NameTransformer")
|
"builtin", "", "PrefixSuffixTransformer")
|
||||||
|
|
||||||
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
|
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
|
||||||
rm := th.LoadAndRunTransformer(`
|
rm := th.LoadAndRunTransformer(`
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: NameTransformer
|
kind: PrefixSuffixTransformer
|
||||||
metadata:
|
metadata:
|
||||||
name: notImportantHere
|
name: notImportantHere
|
||||||
prefix: baked-
|
prefix: baked-
|
||||||
@@ -35,9 +35,29 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
ports:
|
ports:
|
||||||
- port: 7002
|
- port: 7002
|
||||||
|
---
|
||||||
|
apiVersion: apiextensions.k8s.io/v1beta1
|
||||||
|
kind: CustomResourceDefinition
|
||||||
|
metadata:
|
||||||
|
name: crd
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: cm
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.AssertActualEqualsExpected(rm, `
|
th.AssertActualEqualsExpected(rm, `
|
||||||
|
apiVersion: apiextensions.k8s.io/v1beta1
|
||||||
|
kind: CustomResourceDefinition
|
||||||
|
metadata:
|
||||||
|
name: crd
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: baked-cm-pie
|
||||||
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
metadata:
|
metadata:
|
||||||
@@ -4,18 +4,51 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||||
"sigs.k8s.io/kustomize/pkg/transformers"
|
"sigs.k8s.io/kustomize/pkg/transformers"
|
||||||
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
"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
|
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(
|
func (p *plugin) Config(
|
||||||
ldr ifc.Loader, rf *resmap.Factory, c []byte) error {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,11 +61,5 @@ func getDate() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *plugin) Transform(m resmap.ResMap) error {
|
func (p *plugin) Transform(m resmap.ResMap) error {
|
||||||
tr, err := transformers.NewPrefixSuffixTransformer(
|
return p.t.Transform(m)
|
||||||
getDate()+"-", "",
|
|
||||||
config.MakeDefaultConfig().NamePrefix)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return tr.Transform(m)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,20 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||||
"sigs.k8s.io/kustomize/pkg/transformers"
|
"sigs.k8s.io/kustomize/pkg/transformers"
|
||||||
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
||||||
|
"sigs.k8s.io/kustomize/plugin/builtin"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Add a string prefix to the name.
|
||||||
|
// A plugin that adapts another plugin.
|
||||||
type plugin struct {
|
type plugin struct {
|
||||||
Metadata metaData `json:"metadata,omitempty" yaml:"metadata,omitempty"`
|
Metadata metaData `json:"metadata,omitempty" yaml:"metadata,omitempty"`
|
||||||
|
t transformers.Transformer
|
||||||
}
|
}
|
||||||
|
|
||||||
type metaData struct {
|
type metaData struct {
|
||||||
@@ -21,17 +26,39 @@ type metaData struct {
|
|||||||
|
|
||||||
var KustomizePlugin plugin
|
var KustomizePlugin plugin
|
||||||
|
|
||||||
func (p *plugin) Config(
|
func (p *plugin) makePrefixSuffixPluginConfig(n string) ([]byte, error) {
|
||||||
ldr ifc.Loader, rf *resmap.Factory, c []byte) error {
|
var s struct {
|
||||||
return yaml.Unmarshal(c, p)
|
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 {
|
func (p *plugin) Config(
|
||||||
tr, err := transformers.NewPrefixSuffixTransformer(
|
ldr ifc.Loader, rf *resmap.Factory, c []byte) error {
|
||||||
p.Metadata.Name+"-", "",
|
err := yaml.Unmarshal(c, p)
|
||||||
config.MakeDefaultConfig().NamePrefix)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user