mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Refactor crd package to depend on transformerconfig
This commit is contained in:
@@ -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"))
|
||||
}
|
||||
|
||||
115
pkg/crds/crds.go
115
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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{}
|
||||
}
|
||||
|
||||
127
pkg/transformerconfig/transformerconfig_test.go
Normal file
127
pkg/transformerconfig/transformerconfig_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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...)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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...)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user