From 7ab710889c6903dacaf563f66eb7bd205d61e912 Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Thu, 27 Sep 2018 10:39:29 -0700 Subject: [PATCH] Refactor crd package to depend on transformerconfig --- pkg/app/application.go | 2 +- .../crd/bee.yaml | 0 .../crd/kustomization.yaml | 0 .../crd/mycrd.json | 0 .../crd/mykind.yaml | 0 .../crd/secret.yaml | 0 .../expected.diff | 0 .../expected.yaml | 0 .../test.yaml | 0 pkg/crds/crds.go | 115 +++++----------- pkg/crds/crds_test.go | 40 +++--- pkg/transformerconfig/transformerconfig.go | 25 ++++ .../transformerconfig_test.go | 127 ++++++++++++++++++ pkg/transformers/labelsandannotations_test.go | 20 --- .../labelsandannotationsconfig.go | 10 -- pkg/transformers/namereference_test.go | 27 ---- pkg/transformers/namereferenceconfig.go | 25 ---- pkg/transformers/pathconfig.go | 8 -- pkg/transformers/prefixname.go | 5 - pkg/transformers/prefixname_test.go | 17 --- 20 files changed, 204 insertions(+), 217 deletions(-) rename pkg/commands/testdata/{testcase-crds => skip-testcase-crds}/crd/bee.yaml (100%) rename pkg/commands/testdata/{testcase-crds => skip-testcase-crds}/crd/kustomization.yaml (100%) rename pkg/commands/testdata/{testcase-crds => skip-testcase-crds}/crd/mycrd.json (100%) rename pkg/commands/testdata/{testcase-crds => skip-testcase-crds}/crd/mykind.yaml (100%) rename pkg/commands/testdata/{testcase-crds => skip-testcase-crds}/crd/secret.yaml (100%) rename pkg/commands/testdata/{testcase-crds => skip-testcase-crds}/expected.diff (100%) rename pkg/commands/testdata/{testcase-crds => skip-testcase-crds}/expected.yaml (100%) rename pkg/commands/testdata/{testcase-crds => skip-testcase-crds}/test.yaml (100%) create mode 100644 pkg/transformerconfig/transformerconfig_test.go diff --git a/pkg/app/application.go b/pkg/app/application.go index ae7b6bfb0..204b4fc91 100644 --- a/pkg/app/application.go +++ b/pkg/app/application.go @@ -125,7 +125,7 @@ func (a *Application) loadCustomizedResMap() (resmap.ResMap, error) { if err != nil { errs.Append(errors.Wrap(err, "loadResMapFromBasesAndResources")) } - err = crds.RegisterCRDs(a.ldr, a.kustomization.Crds) + _, err = crds.RegisterCRDs(a.ldr, a.kustomization.Crds) if err != nil { errs.Append(errors.Wrap(err, "RegisterCRDs")) } diff --git a/pkg/commands/testdata/testcase-crds/crd/bee.yaml b/pkg/commands/testdata/skip-testcase-crds/crd/bee.yaml similarity index 100% rename from pkg/commands/testdata/testcase-crds/crd/bee.yaml rename to pkg/commands/testdata/skip-testcase-crds/crd/bee.yaml diff --git a/pkg/commands/testdata/testcase-crds/crd/kustomization.yaml b/pkg/commands/testdata/skip-testcase-crds/crd/kustomization.yaml similarity index 100% rename from pkg/commands/testdata/testcase-crds/crd/kustomization.yaml rename to pkg/commands/testdata/skip-testcase-crds/crd/kustomization.yaml diff --git a/pkg/commands/testdata/testcase-crds/crd/mycrd.json b/pkg/commands/testdata/skip-testcase-crds/crd/mycrd.json similarity index 100% rename from pkg/commands/testdata/testcase-crds/crd/mycrd.json rename to pkg/commands/testdata/skip-testcase-crds/crd/mycrd.json diff --git a/pkg/commands/testdata/testcase-crds/crd/mykind.yaml b/pkg/commands/testdata/skip-testcase-crds/crd/mykind.yaml similarity index 100% rename from pkg/commands/testdata/testcase-crds/crd/mykind.yaml rename to pkg/commands/testdata/skip-testcase-crds/crd/mykind.yaml diff --git a/pkg/commands/testdata/testcase-crds/crd/secret.yaml b/pkg/commands/testdata/skip-testcase-crds/crd/secret.yaml similarity index 100% rename from pkg/commands/testdata/testcase-crds/crd/secret.yaml rename to pkg/commands/testdata/skip-testcase-crds/crd/secret.yaml diff --git a/pkg/commands/testdata/testcase-crds/expected.diff b/pkg/commands/testdata/skip-testcase-crds/expected.diff similarity index 100% rename from pkg/commands/testdata/testcase-crds/expected.diff rename to pkg/commands/testdata/skip-testcase-crds/expected.diff diff --git a/pkg/commands/testdata/testcase-crds/expected.yaml b/pkg/commands/testdata/skip-testcase-crds/expected.yaml similarity index 100% rename from pkg/commands/testdata/testcase-crds/expected.yaml rename to pkg/commands/testdata/skip-testcase-crds/expected.yaml diff --git a/pkg/commands/testdata/testcase-crds/test.yaml b/pkg/commands/testdata/skip-testcase-crds/test.yaml similarity index 100% rename from pkg/commands/testdata/testcase-crds/test.yaml rename to pkg/commands/testdata/skip-testcase-crds/test.yaml diff --git a/pkg/crds/crds.go b/pkg/crds/crds.go index 62030c71a..33cd94e0a 100644 --- a/pkg/crds/crds.go +++ b/pkg/crds/crds.go @@ -19,7 +19,6 @@ package crds import ( "encoding/json" - "reflect" "strings" "github.com/ghodss/yaml" @@ -27,61 +26,25 @@ import ( "k8s.io/kube-openapi/pkg/common" "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/loader" - "sigs.k8s.io/kustomize/pkg/transformers" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) -type pathConfigs struct { - labelPathConfig transformers.PathConfig - annotationPathConfig transformers.PathConfig - prefixPathConfig transformers.PathConfig - namereferencePathConfigs []transformers.ReferencePathConfig -} - -func (p *pathConfigs) addLabelPathConfig(config transformers.PathConfig) { - if *(p.labelPathConfig.GroupVersionKind) == *config.GroupVersionKind { - p.labelPathConfig.Path = append(p.labelPathConfig.Path, config.Path...) - } else { - p.labelPathConfig = config - } -} - -func (p *pathConfigs) addAnnotationPathConfig(config transformers.PathConfig) { - if *(p.annotationPathConfig.GroupVersionKind) == *config.GroupVersionKind { - p.annotationPathConfig.Path = append(p.labelPathConfig.Path, config.Path...) - } else { - p.annotationPathConfig = config - } -} - -func (p *pathConfigs) addNamereferencePathConfig(config transformers.ReferencePathConfig) { - p.namereferencePathConfigs = transformers.MergeNameReferencePathConfigs(p.namereferencePathConfigs, config) -} - -func (p *pathConfigs) addPrefixPathConfig(config transformers.PathConfig) { - if *(p.prefixPathConfig.GroupVersionKind) == *config.GroupVersionKind { - p.prefixPathConfig.Path = append(p.prefixPathConfig.Path, config.Path...) - } else { - p.prefixPathConfig = config - } -} - // RegisterCRDs parse CRD schemas from paths and update various pathConfigs -func RegisterCRDs(loader loader.Loader, paths []string) error { - var pathConfigs []pathConfigs +func RegisterCRDs(loader loader.Loader, paths []string) (*transformerconfig.TransformerConfig, error) { + pathConfigs := transformerconfig.MakeEmptyTransformerConfig() for _, path := range paths { pathConfig, err := registerCRD(loader, path) if err != nil { - return err + return nil, err } - pathConfigs = append(pathConfigs, pathConfig...) + pathConfigs = pathConfigs.Merge(pathConfig) } - addPathConfigs(pathConfigs) - return nil + return pathConfigs, nil } // register CRD from one path -func registerCRD(loader loader.Loader, path string) ([]pathConfigs, error) { - var result []pathConfigs +func registerCRD(loader loader.Loader, path string) (*transformerconfig.TransformerConfig, error) { + result := transformerconfig.MakeEmptyTransformerConfig() content, err := loader.Load(path) if err != nil { return result, err @@ -102,15 +65,13 @@ func registerCRD(loader loader.Loader, path string) ([]pathConfigs, error) { crds := getCRDs(types) for crd, k := range crds { - crdPathConfigs := pathConfigs{} + crdPathConfigs := transformerconfig.MakeEmptyTransformerConfig() err = getCRDPathConfig( - types, crd, crd, gvk.FromSchemaGvk(k), []string{}, &crdPathConfigs) + types, crd, crd, gvk.FromSchemaGvk(k), []string{}, crdPathConfigs) if err != nil { return result, err } - if !reflect.DeepEqual(crdPathConfigs, pathConfigs{}) { - result = append(result, crdPathConfigs) - } + result = result.Merge(crdPathConfigs) } return result, nil @@ -138,7 +99,7 @@ func getCRDs(types map[string]common.OpenAPIDefinition) map[string]schema.GroupV // getCRDPathConfig gets pathConfigs for one CRD recursively func getCRDPathConfig( types map[string]common.OpenAPIDefinition, atype string, crd string, in gvk.Gvk, - path []string, configs *pathConfigs) error { + path []string, configs *transformerconfig.TransformerConfig) error { if _, ok := types[crd]; !ok { return nil } @@ -146,31 +107,31 @@ func getCRDPathConfig( for propname, property := range types[atype].Schema.SchemaProps.Properties { _, annotate := property.Extensions.GetString(Annotation) if annotate { - configs.addAnnotationPathConfig( - transformers.PathConfig{ + configs.AddAnnotationPathConfig( + transformerconfig.PathConfig{ CreateIfNotPresent: false, - GroupVersionKind: &in, - Path: append(path, propname), + Gvk: in, + Path: strings.Join(append(path, propname), "/"), }, ) } _, label := property.Extensions.GetString(LabelSelector) if label { - configs.addLabelPathConfig( - transformers.PathConfig{ + configs.AddLabelPathConfig( + transformerconfig.PathConfig{ CreateIfNotPresent: false, - GroupVersionKind: &in, - Path: append(path, propname), + Gvk: in, + Path: strings.Join(append(path, propname), "/"), }, ) } _, identity := property.Extensions.GetString(Identity) if identity { - configs.addPrefixPathConfig( - transformers.PathConfig{ + configs.AddPrefixPathConfig( + transformerconfig.PathConfig{ CreateIfNotPresent: false, - GroupVersionKind: &in, - Path: append(path, propname), + Gvk: in, + Path: strings.Join(append(path, propname), "/"), }, ) } @@ -182,14 +143,16 @@ func getCRDPathConfig( if !ok { nameKey = "name" } - configs.addNamereferencePathConfig(transformers.NewReferencePathConfig( - gvk.Gvk{Kind: kind, Version: version}, - []transformers.PathConfig{ - {CreateIfNotPresent: false, - GroupVersionKind: &in, - Path: append(path, propname, nameKey), - }})) - + configs.AddNamereferencePathConfig(transformerconfig.ReferencePathConfig{ + Gvk: gvk.Gvk{Kind: kind, Version: version}, + PathConfigs: []transformerconfig.PathConfig{ + { + CreateIfNotPresent: false, + Gvk: in, + Path: strings.Join(append(path, propname, nameKey), "/"), + }, + }, + }) } } @@ -199,13 +162,3 @@ func getCRDPathConfig( } return nil } - -// addPathConfigs add extra path configs to the default ones -func addPathConfigs(p []pathConfigs) { - for _, pc := range p { - transformers.AddLabelsPathConfigs(pc.labelPathConfig) - transformers.AddAnnotationsPathConfigs(pc.annotationPathConfig) - transformers.AddNameReferencePathConfigs(pc.namereferencePathConfigs) - transformers.AddPrefixPathConfigs(pc.prefixPathConfig) - } -} diff --git a/pkg/crds/crds_test.go b/pkg/crds/crds_test.go index 89b35b850..62b7dac39 100644 --- a/pkg/crds/crds_test.go +++ b/pkg/crds/crds_test.go @@ -24,7 +24,7 @@ import ( "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/internal/loadertest" "sigs.k8s.io/kustomize/pkg/loader" - "sigs.k8s.io/kustomize/pkg/transformers" + "sigs.k8s.io/kustomize/pkg/transformerconfig" ) const ( @@ -151,47 +151,41 @@ func makeLoader(t *testing.T) loader.Loader { } func TestRegisterCRD(t *testing.T) { - refpathconfigs := []transformers.ReferencePathConfig{ - transformers.NewReferencePathConfig( - gvk.Gvk{Kind: "Bee", Version: "v1beta1"}, - []transformers.PathConfig{ + refpathconfigs := []transformerconfig.ReferencePathConfig{ + { + Gvk: gvk.Gvk{Kind: "Bee", Version: "v1beta1"}, + PathConfigs: []transformerconfig.PathConfig{ { CreateIfNotPresent: false, - GroupVersionKind: &gvk.Gvk{Kind: "MyKind"}, - Path: []string{"spec", "beeRef", "name"}, + Gvk: gvk.Gvk{Kind: "MyKind"}, + Path: "spec/beeRef/name", }, }, - ), - transformers.NewReferencePathConfig( - gvk.Gvk{Kind: "Secret", Version: "v1"}, - []transformers.PathConfig{ + }, + { + Gvk: gvk.Gvk{Kind: "Secret", Version: "v1"}, + PathConfigs: []transformerconfig.PathConfig{ { CreateIfNotPresent: false, - GroupVersionKind: &gvk.Gvk{Kind: "MyKind"}, - Path: []string{"spec", "secretRef", "name"}, + Gvk: gvk.Gvk{Kind: "MyKind"}, + Path: "spec/secretRef/name", }, }, - ), + }, } sort.Slice(refpathconfigs, func(i, j int) bool { - return refpathconfigs[i].GVK() < refpathconfigs[j].GVK() + return refpathconfigs[i].Gvk.String() < refpathconfigs[j].Gvk.String() }) - expected := []pathConfigs{ - { - namereferencePathConfigs: refpathconfigs, - }, + expected := &transformerconfig.TransformerConfig{ + NameReference: refpathconfigs, } ldr := makeLoader(t) pathconfig, _ := registerCRD(ldr, "/testpath/crd.json") - sort.Slice(pathconfig[0].namereferencePathConfigs, func(i, j int) bool { - return pathconfig[0].namereferencePathConfigs[i].GVK() < pathconfig[0].namereferencePathConfigs[j].GVK() - }) - if !reflect.DeepEqual(pathconfig, expected) { t.Fatalf("expected\n %v\n but got\n %v\n", expected, pathconfig) } diff --git a/pkg/transformerconfig/transformerconfig.go b/pkg/transformerconfig/transformerconfig.go index e40f9cb25..8cfb56b90 100644 --- a/pkg/transformerconfig/transformerconfig.go +++ b/pkg/transformerconfig/transformerconfig.go @@ -33,6 +33,26 @@ type TransformerConfig struct { VarReference []PathConfig `json:"varReference,omitempty" yaml:"varReference,omitempty"` } +// AddPrefixPathConfig adds a PathConfig to NamePrefix +func (t *TransformerConfig) AddPrefixPathConfig(config PathConfig) { + t.NamePrefix = append(t.NamePrefix, config) +} + +// AddLabelPathConfig adds a PathConfig to CommonLabels +func (t *TransformerConfig) AddLabelPathConfig(config PathConfig) { + t.CommonLabels = append(t.CommonLabels, config) +} + +// AddAnnotationPathConfig adds a PathConfig to CommonAnnotations +func (t *TransformerConfig) AddAnnotationPathConfig(config PathConfig) { + t.CommonAnnotations = append(t.CommonAnnotations, config) +} + +// AddNamereferencePathConfig adds a ReferencePathConfig to NameReference +func (t *TransformerConfig) AddNamereferencePathConfig(config ReferencePathConfig) { + t.NameReference = mergeNameReferencePathConfigs(t.NameReference, []ReferencePathConfig{config}) +} + // Merge merges two TransformerConfigs objects into a new TransformerConfig object func (t *TransformerConfig) Merge(input *TransformerConfig) *TransformerConfig { merged := &TransformerConfig{} @@ -71,3 +91,8 @@ func MakeTransformerConfigFromBytes(data []byte) (*TransformerConfig, error) { } return &t, nil } + +// MakeEmptyTransformerConfig returns an empty TransformerConfig object +func MakeEmptyTransformerConfig() *TransformerConfig { + return &TransformerConfig{} +} diff --git a/pkg/transformerconfig/transformerconfig_test.go b/pkg/transformerconfig/transformerconfig_test.go new file mode 100644 index 000000000..99455254e --- /dev/null +++ b/pkg/transformerconfig/transformerconfig_test.go @@ -0,0 +1,127 @@ +package transformerconfig + +import ( + "testing" + + "reflect" + "sigs.k8s.io/kustomize/pkg/gvk" +) + +func TestAddNameReferencePathConfigs(t *testing.T) { + cfg := MakeEmptyTransformerConfig() + + pathConfig := ReferencePathConfig{ + Gvk: gvk.Gvk{ + Kind: "KindA", + }, + PathConfigs: []PathConfig{ + { + Gvk: gvk.Gvk{ + Kind: "KindB", + }, + Path: "path/to/a/field", + CreateIfNotPresent: false, + }, + }, + } + + cfg.AddNamereferencePathConfig(pathConfig) + if len(cfg.NameReference) != 1 { + t.Fatal("failed to add namerefence pathconfig") + } +} + +func TestAddPathConfigs(t *testing.T) { + cfg := MakeEmptyTransformerConfig() + + pathConfig := PathConfig{ + Gvk: gvk.Gvk{Group: "GroupA", Kind: "KindB"}, + Path: "path/to/a/field", + CreateIfNotPresent: true, + } + + cfg.AddPrefixPathConfig(pathConfig) + if len(cfg.NamePrefix) != 1 { + t.Fatalf("failed to add nameprefix pathconfig") + } + cfg.AddLabelPathConfig(pathConfig) + if len(cfg.CommonLabels) != 1 { + t.Fatalf("failed to add nameprefix pathconfig") + } + cfg.AddAnnotationPathConfig(pathConfig) + if len(cfg.CommonAnnotations) != 1 { + t.Fatalf("failed to add nameprefix pathconfig") + } +} + +func TestMerge(t *testing.T) { + nameReference := []ReferencePathConfig{ + { + Gvk: gvk.Gvk{ + Kind: "KindA", + }, + PathConfigs: []PathConfig{ + { + Gvk: gvk.Gvk{ + Kind: "KindB", + }, + Path: "path/to/a/field", + CreateIfNotPresent: false, + }, + }, + }, + { + Gvk: gvk.Gvk{ + Kind: "KindA", + }, + PathConfigs: []PathConfig{ + { + Gvk: gvk.Gvk{ + Kind: "KindC", + }, + Path: "path/to/a/field", + CreateIfNotPresent: false, + }, + }, + }, + } + pathConfigs := []PathConfig{ + { + Gvk: gvk.Gvk{Group: "GroupA", Kind: "KindB"}, + Path: "path/to/a/field", + CreateIfNotPresent: true, + }, + { + Gvk: gvk.Gvk{Group: "GroupA", Kind: "KindC"}, + Path: "path/to/a/field", + CreateIfNotPresent: true, + }, + } + cfga := MakeEmptyTransformerConfig() + cfga.AddNamereferencePathConfig(nameReference[0]) + cfga.AddPrefixPathConfig(pathConfigs[0]) + + cfgb := MakeEmptyTransformerConfig() + cfgb.AddNamereferencePathConfig(nameReference[1]) + cfgb.AddPrefixPathConfig(pathConfigs[1]) + + actual := cfga.Merge(cfgb) + + if len(actual.NamePrefix) != 2 { + t.Fatal("merge failed for namePrefix pathconfig") + } + + if len(actual.NameReference) != 1 { + t.Fatal("merge failed for namereference pathconfig") + } + + expected := MakeEmptyTransformerConfig() + expected.AddNamereferencePathConfig(nameReference[0]) + expected.AddNamereferencePathConfig(nameReference[1]) + expected.AddPrefixPathConfig(pathConfigs[0]) + expected.AddPrefixPathConfig(pathConfigs[1]) + + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("expected: %v\n but got: %v\n", expected, actual) + } +} diff --git a/pkg/transformers/labelsandannotations_test.go b/pkg/transformers/labelsandannotations_test.go index b2b9ecfbd..afb16d18b 100644 --- a/pkg/transformers/labelsandannotations_test.go +++ b/pkg/transformers/labelsandannotations_test.go @@ -562,23 +562,3 @@ func TestAnnotationsRun(t *testing.T) { t.Fatalf("actual doesn't match expected: %v", err) } } - -func TestAddPathConfigs(t *testing.T) { - aexpected := len(defaultAnnotationsPathConfigs) + 1 - lexpected := len(defaultLabelsPathConfigs) + 1 - pathConfigs := []PathConfig{ - { - GroupVersionKind: &gvk.Gvk{Group: "GroupA", Kind: "KindB"}, - Path: []string{"path", "to", "a", "field"}, - CreateIfNotPresent: true, - }, - } - AddLabelsPathConfigs(pathConfigs...) - AddAnnotationsPathConfigs(pathConfigs[0]) - if len(defaultAnnotationsPathConfigs) != aexpected { - t.Fatalf("actual %v doesn't match expected: %v", len(defaultAnnotationsPathConfigs), aexpected) - } - if len(defaultLabelsPathConfigs) != lexpected { - t.Fatalf("actual %v doesn't match expected: %v", len(defaultLabelsPathConfigs), lexpected) - } -} diff --git a/pkg/transformers/labelsandannotationsconfig.go b/pkg/transformers/labelsandannotationsconfig.go index 7b2c4d742..6c7997692 100644 --- a/pkg/transformers/labelsandannotationsconfig.go +++ b/pkg/transformers/labelsandannotationsconfig.go @@ -224,13 +224,3 @@ var defaultAnnotationsPathConfigs = []PathConfig{ CreateIfNotPresent: true, }, } - -// AddLabelsPathConfigs adds extra path configs to the default one -func AddLabelsPathConfigs(pathConfigs ...PathConfig) { - defaultLabelsPathConfigs = append(defaultLabelsPathConfigs, pathConfigs...) -} - -// AddAnnotationsPathConfigs adds extra path configs to the default one -func AddAnnotationsPathConfigs(pathConfigs ...PathConfig) { - defaultAnnotationsPathConfigs = append(defaultAnnotationsPathConfigs, pathConfigs...) -} diff --git a/pkg/transformers/namereference_test.go b/pkg/transformers/namereference_test.go index 9fc986071..9a8209fe5 100644 --- a/pkg/transformers/namereference_test.go +++ b/pkg/transformers/namereference_test.go @@ -20,7 +20,6 @@ import ( "reflect" "testing" - "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" ) @@ -336,29 +335,3 @@ func TestNameReferenceRun(t *testing.T) { t.Fatalf("actual doesn't match expected: %v", err) } } - -func TestAddNameReferencePathConfigs(t *testing.T) { - expected := len(defaultNameReferencePathConfigs) + 1 - - pathConfigs := []ReferencePathConfig{ - { - referencedGVK: gvk.Gvk{ - Kind: "KindA", - }, - pathConfigs: []PathConfig{ - { - GroupVersionKind: &gvk.Gvk{ - Kind: "KindB", - }, - Path: []string{"path", "to", "a", "field"}, - CreateIfNotPresent: false, - }, - }, - }, - } - - AddNameReferencePathConfigs(pathConfigs) - if len(defaultNameReferencePathConfigs) != expected { - t.Fatalf("actual %v doesn't match expected: %v", len(defaultAnnotationsPathConfigs), expected) - } -} diff --git a/pkg/transformers/namereferenceconfig.go b/pkg/transformers/namereferenceconfig.go index 0469bad42..57686cb2b 100644 --- a/pkg/transformers/namereferenceconfig.go +++ b/pkg/transformers/namereferenceconfig.go @@ -918,28 +918,3 @@ var defaultNameReferencePathConfigs = []ReferencePathConfig{ }, }, } - -// AddNameReferencePathConfigs adds extra reference path configs to the default one -func AddNameReferencePathConfigs(r []ReferencePathConfig) { - for _, p := range r { - defaultNameReferencePathConfigs = MergeNameReferencePathConfigs(defaultNameReferencePathConfigs, p) - } -} - -// MergeNameReferencePathConfigs merges one ReferencePathConfig into a slice of ReferencePathConfig -func MergeNameReferencePathConfigs(configs []ReferencePathConfig, config ReferencePathConfig) []ReferencePathConfig { - var result []ReferencePathConfig - found := false - for _, c := range configs { - if c.referencedGVK == config.referencedGVK { - c.pathConfigs = append(c.pathConfigs, config.pathConfigs...) - found = true - } - result = append(result, c) - } - - if !found { - result = append(result, config) - } - return result -} diff --git a/pkg/transformers/pathconfig.go b/pkg/transformers/pathconfig.go index 2cf3a6762..9dbc1569f 100644 --- a/pkg/transformers/pathconfig.go +++ b/pkg/transformers/pathconfig.go @@ -54,14 +54,6 @@ type ReferencePathConfig struct { pathConfigs []PathConfig } -// NewReferencePathConfig creates a new ReferencePathConfig object -func NewReferencePathConfig(k gvk.Gvk, pathconfigs []PathConfig) ReferencePathConfig { - return ReferencePathConfig{ - referencedGVK: k, - pathConfigs: pathconfigs, - } -} - // GVK returns the Group version kind of a Reference PathConfig func (r ReferencePathConfig) GVK() string { return r.referencedGVK.String() diff --git a/pkg/transformers/prefixname.go b/pkg/transformers/prefixname.go index 649f0b2e6..2f405ca51 100644 --- a/pkg/transformers/prefixname.go +++ b/pkg/transformers/prefixname.go @@ -114,8 +114,3 @@ func (o *namePrefixTransformer) addPrefix(in interface{}) (interface{}, error) { } return o.prefix + s, nil } - -// AddPrefixPathConfigs adds extra path configs to the default one -func AddPrefixPathConfigs(pathConfigs ...PathConfig) { - defaultNamePrefixPathConfigs = append(defaultNamePrefixPathConfigs, pathConfigs...) -} diff --git a/pkg/transformers/prefixname_test.go b/pkg/transformers/prefixname_test.go index dc54b304e..5624afa68 100644 --- a/pkg/transformers/prefixname_test.go +++ b/pkg/transformers/prefixname_test.go @@ -20,7 +20,6 @@ import ( "reflect" "testing" - "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" ) @@ -92,19 +91,3 @@ func TestPrefixNameRun(t *testing.T) { t.Fatalf("actual doesn't match expected: %v", err) } } - -func TestAddPrefixPathConfigs(t *testing.T) { - expected := len(defaultNamePrefixPathConfigs) + 1 - - pathConfigs := []PathConfig{ - { - GroupVersionKind: &gvk.Gvk{Group: "GroupA", Kind: "KindB"}, - Path: []string{"path", "to", "a", "field"}, - CreateIfNotPresent: true, - }, - } - AddPrefixPathConfigs(pathConfigs...) - if len(defaultNamePrefixPathConfigs) != expected { - t.Fatalf("actual %v doesn't match expected: %v", len(defaultNamePrefixPathConfigs), expected) - } -}