Rename the prefix/suffix transformer.

This commit is contained in:
jregan
2019-06-11 17:47:23 -07:00
parent fa23026b80
commit 49d94f5318
6 changed files with 21 additions and 46 deletions

View File

@@ -1,18 +1,5 @@
/*
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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package transformers
@@ -25,25 +12,26 @@ import (
"sigs.k8s.io/kustomize/pkg/transformers/config"
)
// namePrefixSuffixTransformer contains the prefix, suffix, and the FieldSpecs
// for each field needing a name prefix and suffix.
type namePrefixSuffixTransformer struct {
// 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 = &namePrefixSuffixTransformer{}
var _ Transformer = &prefixSuffixTransformer{}
// Not placed in a file yet due to lack of demand.
var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{
{
Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"},
},
}
// NewNamePrefixSuffixTransformer makes a namePrefixSuffixTransformer.
func NewNamePrefixSuffixTransformer(
// NewPrefixSuffixTransformer makes a prefixSuffixTransformer.
func NewPrefixSuffixTransformer(
np, ns string, fieldSpecs []config.FieldSpec) (Transformer, error) {
if len(np) == 0 && len(ns) == 0 {
return NewNoOpTransformer(), nil
@@ -51,17 +39,17 @@ func NewNamePrefixSuffixTransformer(
if fieldSpecs == nil {
return nil, errors.New("fieldSpecs is not expected to be nil")
}
return &namePrefixSuffixTransformer{
return &prefixSuffixTransformer{
prefix: np,
suffix: ns,
fieldSpecsToUse: fieldSpecs,
fieldSpecsToSkip: prefixSuffixFieldSpecsToSkip}, nil
}
// Transform prepends the name prefix and appends the name suffix.
// 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 *namePrefixSuffixTransformer) Transform(m resmap.ResMap) error {
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.
@@ -101,7 +89,7 @@ func (o *namePrefixSuffixTransformer) Transform(m resmap.ResMap) error {
return nil
}
func (o *namePrefixSuffixTransformer) addPrefixSuffix(
func (o *prefixSuffixTransformer) addPrefixSuffix(
in interface{}) (interface{}, error) {
s, ok := in.(string)
if !ok {

View File

@@ -1,18 +1,5 @@
/*
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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package transformers
@@ -81,7 +68,7 @@ func TestPrefixSuffixNameRun(t *testing.T) {
}),
})
npst, err := NewNamePrefixSuffixTransformer(
npst, err := NewPrefixSuffixTransformer(
"someprefix-", "-somesuffix", defaultTransformerConfig.NamePrefix)
if err != nil {
t.Fatalf("unexpected error: %v", err)

View File

@@ -29,7 +29,7 @@ func (p *NameTransformerPlugin) Config(
}
func (p *NameTransformerPlugin) Transform(m resmap.ResMap) error {
t, err := transformers.NewNamePrefixSuffixTransformer(
t, err := transformers.NewPrefixSuffixTransformer(
p.Prefix,
p.Suffix,
p.FieldSpecs,

View File

@@ -12,7 +12,7 @@ import (
"sigs.k8s.io/yaml"
)
// Add the given prefix and suffix to the resource name.
// 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"`
@@ -30,7 +30,7 @@ func (p *plugin) Config(
}
func (p *plugin) Transform(m resmap.ResMap) error {
t, err := transformers.NewNamePrefixSuffixTransformer(
t, err := transformers.NewPrefixSuffixTransformer(
p.Prefix,
p.Suffix,
p.FieldSpecs,

View File

@@ -28,7 +28,7 @@ func getDate() string {
}
func (p *plugin) Transform(m resmap.ResMap) error {
tr, err := transformers.NewNamePrefixSuffixTransformer(
tr, err := transformers.NewPrefixSuffixTransformer(
getDate()+"-", "",
config.MakeDefaultConfig().NamePrefix)
if err != nil {

View File

@@ -27,7 +27,7 @@ func (p *plugin) Config(
}
func (p *plugin) Transform(m resmap.ResMap) error {
tr, err := transformers.NewNamePrefixSuffixTransformer(
tr, err := transformers.NewPrefixSuffixTransformer(
p.Metadata.Name+"-", "",
config.MakeDefaultConfig().NamePrefix)
if err != nil {