Skip to content

Commit

Permalink
combine transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
zoncoen committed Nov 15, 2018
1 parent c1e7f1b commit d481dba
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 268 deletions.
19 changes: 11 additions & 8 deletions pkg/resid/resid.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,17 @@ func (n ResId) Namespace() string {
return n.namespace
}

// CopyWithNewPrefix make a new copy from current ResId and append a new prefix
func (n ResId) CopyWithNewPrefix(p string) ResId {
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.concatPrefix(p), suffix: n.suffix, namespace: n.namespace}
}

// CopyWithNewSuffix make a new copy from current ResId and append a new suffix
func (n ResId) CopyWithNewSuffix(p string) ResId {
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.prefix, suffix: n.concatSuffix(p), namespace: n.namespace}
// CopyWithNewPrefixSuffix make a new copy from current ResId and append a new prefix and suffix
func (n ResId) CopyWithNewPrefixSuffix(p, s string) ResId {
prefix := n.prefix
if p != "" {
prefix = n.concatPrefix(p)
}
suffix := n.suffix
if s != "" {
suffix = n.concatSuffix(s)
}
return ResId{gvKind: n.gvKind, name: n.name, prefix: prefix, suffix: suffix, namespace: n.namespace}
}

// CopyWithNewNamespace make a new copy from current ResId and set a new namespace
Expand Down
7 changes: 5 additions & 2 deletions pkg/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,11 @@ func (kt *KustTarget) newTransformer(patches []*resource.Resource) (transformers
r = append(r, t)
r = append(r, transformers.NewNamespaceTransformer(
string(kt.kustomization.Namespace), kt.tConfig.NameSpace))
t, err = transformers.NewNamePrefixTransformer(
string(kt.kustomization.NamePrefix), kt.tConfig.NamePrefix)
t, err = transformers.NewNamePrefixSuffixTransformer(
string(kt.kustomization.NamePrefix),
"", // TODO(zoncoen): pass the name suffix
kt.tConfig.NamePrefix,
)
if err != nil {
return nil, err
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/transformers/config/defaultconfig/defaultconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
func GetDefaultFieldSpecs() []byte {
configData := [][]byte{
[]byte(namePrefixFieldSpecs),
[]byte(nameSuffixFieldSpecs),
[]byte(commonLabelFieldSpecs),
[]byte(commonAnnotationFieldSpecs),
[]byte(namespaceFieldSpecs),
Expand All @@ -41,7 +40,6 @@ func GetDefaultFieldSpecs() []byte {
func GetDefaultFieldSpecsAsMap() map[string]string {
result := make(map[string]string)
result["nameprefix"] = namePrefixFieldSpecs
result["namesuffix"] = nameSuffixFieldSpecs
result["commonlabels"] = commonLabelFieldSpecs
result["commonannotations"] = commonAnnotationFieldSpecs
result["namespace"] = namespaceFieldSpecs
Expand Down
24 changes: 0 additions & 24 deletions pkg/transformers/config/defaultconfig/namesuffix.go

This file was deleted.

98 changes: 0 additions & 98 deletions pkg/transformers/prefixname_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,41 @@ import (
"sigs.k8s.io/kustomize/pkg/transformers/config"
)

// namePrefixTransformer contains the prefix and the FieldSpecs
// for each field needing a name prefix.
type namePrefixTransformer struct {
// namePrefixSuffixTransformer contains the prefix, suffix, and the FieldSpecs
// for each field needing a name prefix and suffix.
type namePrefixSuffixTransformer struct {
prefix string
suffix string
fieldSpecsToUse []config.FieldSpec
fieldSpecsToSkip []config.FieldSpec
}

var _ Transformer = &namePrefixTransformer{}
var _ Transformer = &namePrefixSuffixTransformer{}

var prefixFieldSpecsToSkip = []config.FieldSpec{
var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{
{
Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"},
},
}

// deprecateNamePrefixFieldSpec will be moved into prefixFieldSpecsToSkip in next release
var deprecateNamePrefixFieldSpec = config.FieldSpec{
// deprecateNamePrefixSuffixFieldSpec will be moved into prefixSuffixFieldSpecsToSkip in next release
var deprecateNamePrefixSuffixFieldSpec = config.FieldSpec{
Gvk: gvk.Gvk{Kind: "Namespace"},
}

// NewNamePrefixTransformer construct a namePrefixTransformer.
func NewNamePrefixTransformer(np string, pc []config.FieldSpec) (Transformer, error) {
if len(np) == 0 {
// NewNamePrefixSuffixTransformer construct a namePrefixSuffixTransformer.
func NewNamePrefixSuffixTransformer(np, ns string, pc []config.FieldSpec) (Transformer, error) {
if len(np) == 0 && len(ns) == 0 {
return NewNoOpTransformer(), nil
}
if pc == nil {
return nil, errors.New("fieldSpecs is not expected to be nil")
}
return &namePrefixTransformer{fieldSpecsToUse: pc, prefix: np, fieldSpecsToSkip: prefixFieldSpecsToSkip}, nil
return &namePrefixSuffixTransformer{fieldSpecsToUse: pc, prefix: np, suffix: ns, fieldSpecsToSkip: prefixSuffixFieldSpecsToSkip}, nil
}

// Transform prepends the name prefix.
func (o *namePrefixTransformer) Transform(m resmap.ResMap) error {
// Transform prepends the name prefix and appends the name suffix.
func (o *namePrefixSuffixTransformer) 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.
Expand All @@ -79,29 +80,29 @@ func (o *namePrefixTransformer) Transform(m resmap.ResMap) error {
}

for id := range mf {
if id.Gvk().IsSelected(&deprecateNamePrefixFieldSpec.Gvk) {
log.Println("Adding nameprefix to Namespace resource will be deprecated in next release.")
if id.Gvk().IsSelected(&deprecateNamePrefixSuffixFieldSpec.Gvk) {
log.Println("Adding nameprefix and namesuffix to Namespace resource will be deprecated in next release.")
}
objMap := mf[id].Map()
for _, path := range o.fieldSpecsToUse {
if !id.Gvk().IsSelected(&path.Gvk) {
continue
}
err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addPrefix)
err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addPrefixSuffix)
if err != nil {
return err
}
newId := id.CopyWithNewPrefix(o.prefix)
newId := id.CopyWithNewPrefixSuffix(o.prefix, o.suffix)
m[newId] = mf[id]
}
}
return nil
}

func (o *namePrefixTransformer) addPrefix(in interface{}) (interface{}, error) {
func (o *namePrefixSuffixTransformer) addPrefixSuffix(in interface{}) (interface{}, error) {
s, ok := in.(string)
if !ok {
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
}
return o.prefix + s, nil
return fmt.Sprintf("%s%s%s", o.prefix, s, o.suffix), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"sigs.k8s.io/kustomize/pkg/resource"
)

func TestSuffixNameRun(t *testing.T) {
func TestPrefixSuffixNameRun(t *testing.T) {
rf := resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl())
m := resmap.ResMap{
Expand Down Expand Up @@ -56,20 +56,20 @@ func TestSuffixNameRun(t *testing.T) {
}),
}
expected := resmap.ResMap{
resid.NewResIdWithSuffix(cmap, "cm1", "-somesuffix"): rf.FromMap(
resid.NewResIdWithPrefixSuffix(cmap, "cm1", "someprefix-", "-somesuffix"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm1-somesuffix",
"name": "someprefix-cm1-somesuffix",
},
}),
resid.NewResIdWithSuffix(cmap, "cm2", "-somesuffix"): rf.FromMap(
resid.NewResIdWithPrefixSuffix(cmap, "cm2", "someprefix-", "-somesuffix"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm2-somesuffix",
"name": "someprefix-cm2-somesuffix",
},
}),
resid.NewResId(crd, "crd"): rf.FromMap(
Expand All @@ -82,12 +82,12 @@ func TestSuffixNameRun(t *testing.T) {
}),
}

npt, err := NewNameSuffixTransformer(
"-somesuffix", defaultTransformerConfig.NameSuffix)
npst, err := NewNamePrefixSuffixTransformer(
"someprefix-", "-somesuffix", defaultTransformerConfig.NamePrefix)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = npt.Transform(m)
err = npst.Transform(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
Loading

0 comments on commit d481dba

Please sign in to comment.