Skip to content

Commit 7ab7108

Browse files
committed
Refactor crd package to depend on transformerconfig
1 parent 20fa90a commit 7ab7108

20 files changed

+204
-217
lines changed

pkg/app/application.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (a *Application) loadCustomizedResMap() (resmap.ResMap, error) {
125125
if err != nil {
126126
errs.Append(errors.Wrap(err, "loadResMapFromBasesAndResources"))
127127
}
128-
err = crds.RegisterCRDs(a.ldr, a.kustomization.Crds)
128+
_, err = crds.RegisterCRDs(a.ldr, a.kustomization.Crds)
129129
if err != nil {
130130
errs.Append(errors.Wrap(err, "RegisterCRDs"))
131131
}

pkg/crds/crds.go

Lines changed: 34 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,32 @@ package crds
1919

2020
import (
2121
"encoding/json"
22-
"reflect"
2322
"strings"
2423

2524
"github.com/ghodss/yaml"
2625
"k8s.io/apimachinery/pkg/runtime/schema"
2726
"k8s.io/kube-openapi/pkg/common"
2827
"sigs.k8s.io/kustomize/pkg/gvk"
2928
"sigs.k8s.io/kustomize/pkg/loader"
30-
"sigs.k8s.io/kustomize/pkg/transformers"
29+
"sigs.k8s.io/kustomize/pkg/transformerconfig"
3130
)
3231

33-
type pathConfigs struct {
34-
labelPathConfig transformers.PathConfig
35-
annotationPathConfig transformers.PathConfig
36-
prefixPathConfig transformers.PathConfig
37-
namereferencePathConfigs []transformers.ReferencePathConfig
38-
}
39-
40-
func (p *pathConfigs) addLabelPathConfig(config transformers.PathConfig) {
41-
if *(p.labelPathConfig.GroupVersionKind) == *config.GroupVersionKind {
42-
p.labelPathConfig.Path = append(p.labelPathConfig.Path, config.Path...)
43-
} else {
44-
p.labelPathConfig = config
45-
}
46-
}
47-
48-
func (p *pathConfigs) addAnnotationPathConfig(config transformers.PathConfig) {
49-
if *(p.annotationPathConfig.GroupVersionKind) == *config.GroupVersionKind {
50-
p.annotationPathConfig.Path = append(p.labelPathConfig.Path, config.Path...)
51-
} else {
52-
p.annotationPathConfig = config
53-
}
54-
}
55-
56-
func (p *pathConfigs) addNamereferencePathConfig(config transformers.ReferencePathConfig) {
57-
p.namereferencePathConfigs = transformers.MergeNameReferencePathConfigs(p.namereferencePathConfigs, config)
58-
}
59-
60-
func (p *pathConfigs) addPrefixPathConfig(config transformers.PathConfig) {
61-
if *(p.prefixPathConfig.GroupVersionKind) == *config.GroupVersionKind {
62-
p.prefixPathConfig.Path = append(p.prefixPathConfig.Path, config.Path...)
63-
} else {
64-
p.prefixPathConfig = config
65-
}
66-
}
67-
6832
// RegisterCRDs parse CRD schemas from paths and update various pathConfigs
69-
func RegisterCRDs(loader loader.Loader, paths []string) error {
70-
var pathConfigs []pathConfigs
33+
func RegisterCRDs(loader loader.Loader, paths []string) (*transformerconfig.TransformerConfig, error) {
34+
pathConfigs := transformerconfig.MakeEmptyTransformerConfig()
7135
for _, path := range paths {
7236
pathConfig, err := registerCRD(loader, path)
7337
if err != nil {
74-
return err
38+
return nil, err
7539
}
76-
pathConfigs = append(pathConfigs, pathConfig...)
40+
pathConfigs = pathConfigs.Merge(pathConfig)
7741
}
78-
addPathConfigs(pathConfigs)
79-
return nil
42+
return pathConfigs, nil
8043
}
8144

8245
// register CRD from one path
83-
func registerCRD(loader loader.Loader, path string) ([]pathConfigs, error) {
84-
var result []pathConfigs
46+
func registerCRD(loader loader.Loader, path string) (*transformerconfig.TransformerConfig, error) {
47+
result := transformerconfig.MakeEmptyTransformerConfig()
8548
content, err := loader.Load(path)
8649
if err != nil {
8750
return result, err
@@ -102,15 +65,13 @@ func registerCRD(loader loader.Loader, path string) ([]pathConfigs, error) {
10265

10366
crds := getCRDs(types)
10467
for crd, k := range crds {
105-
crdPathConfigs := pathConfigs{}
68+
crdPathConfigs := transformerconfig.MakeEmptyTransformerConfig()
10669
err = getCRDPathConfig(
107-
types, crd, crd, gvk.FromSchemaGvk(k), []string{}, &crdPathConfigs)
70+
types, crd, crd, gvk.FromSchemaGvk(k), []string{}, crdPathConfigs)
10871
if err != nil {
10972
return result, err
11073
}
111-
if !reflect.DeepEqual(crdPathConfigs, pathConfigs{}) {
112-
result = append(result, crdPathConfigs)
113-
}
74+
result = result.Merge(crdPathConfigs)
11475
}
11576

11677
return result, nil
@@ -138,39 +99,39 @@ func getCRDs(types map[string]common.OpenAPIDefinition) map[string]schema.GroupV
13899
// getCRDPathConfig gets pathConfigs for one CRD recursively
139100
func getCRDPathConfig(
140101
types map[string]common.OpenAPIDefinition, atype string, crd string, in gvk.Gvk,
141-
path []string, configs *pathConfigs) error {
102+
path []string, configs *transformerconfig.TransformerConfig) error {
142103
if _, ok := types[crd]; !ok {
143104
return nil
144105
}
145106

146107
for propname, property := range types[atype].Schema.SchemaProps.Properties {
147108
_, annotate := property.Extensions.GetString(Annotation)
148109
if annotate {
149-
configs.addAnnotationPathConfig(
150-
transformers.PathConfig{
110+
configs.AddAnnotationPathConfig(
111+
transformerconfig.PathConfig{
151112
CreateIfNotPresent: false,
152-
GroupVersionKind: &in,
153-
Path: append(path, propname),
113+
Gvk: in,
114+
Path: strings.Join(append(path, propname), "/"),
154115
},
155116
)
156117
}
157118
_, label := property.Extensions.GetString(LabelSelector)
158119
if label {
159-
configs.addLabelPathConfig(
160-
transformers.PathConfig{
120+
configs.AddLabelPathConfig(
121+
transformerconfig.PathConfig{
161122
CreateIfNotPresent: false,
162-
GroupVersionKind: &in,
163-
Path: append(path, propname),
123+
Gvk: in,
124+
Path: strings.Join(append(path, propname), "/"),
164125
},
165126
)
166127
}
167128
_, identity := property.Extensions.GetString(Identity)
168129
if identity {
169-
configs.addPrefixPathConfig(
170-
transformers.PathConfig{
130+
configs.AddPrefixPathConfig(
131+
transformerconfig.PathConfig{
171132
CreateIfNotPresent: false,
172-
GroupVersionKind: &in,
173-
Path: append(path, propname),
133+
Gvk: in,
134+
Path: strings.Join(append(path, propname), "/"),
174135
},
175136
)
176137
}
@@ -182,14 +143,16 @@ func getCRDPathConfig(
182143
if !ok {
183144
nameKey = "name"
184145
}
185-
configs.addNamereferencePathConfig(transformers.NewReferencePathConfig(
186-
gvk.Gvk{Kind: kind, Version: version},
187-
[]transformers.PathConfig{
188-
{CreateIfNotPresent: false,
189-
GroupVersionKind: &in,
190-
Path: append(path, propname, nameKey),
191-
}}))
192-
146+
configs.AddNamereferencePathConfig(transformerconfig.ReferencePathConfig{
147+
Gvk: gvk.Gvk{Kind: kind, Version: version},
148+
PathConfigs: []transformerconfig.PathConfig{
149+
{
150+
CreateIfNotPresent: false,
151+
Gvk: in,
152+
Path: strings.Join(append(path, propname, nameKey), "/"),
153+
},
154+
},
155+
})
193156
}
194157
}
195158

@@ -199,13 +162,3 @@ func getCRDPathConfig(
199162
}
200163
return nil
201164
}
202-
203-
// addPathConfigs add extra path configs to the default ones
204-
func addPathConfigs(p []pathConfigs) {
205-
for _, pc := range p {
206-
transformers.AddLabelsPathConfigs(pc.labelPathConfig)
207-
transformers.AddAnnotationsPathConfigs(pc.annotationPathConfig)
208-
transformers.AddNameReferencePathConfigs(pc.namereferencePathConfigs)
209-
transformers.AddPrefixPathConfigs(pc.prefixPathConfig)
210-
}
211-
}

0 commit comments

Comments
 (0)