@@ -19,69 +19,32 @@ package crds
19
19
20
20
import (
21
21
"encoding/json"
22
- "reflect"
23
22
"strings"
24
23
25
24
"github.com/ghodss/yaml"
26
25
"k8s.io/apimachinery/pkg/runtime/schema"
27
26
"k8s.io/kube-openapi/pkg/common"
28
27
"sigs.k8s.io/kustomize/pkg/gvk"
29
28
"sigs.k8s.io/kustomize/pkg/loader"
30
- "sigs.k8s.io/kustomize/pkg/transformers "
29
+ "sigs.k8s.io/kustomize/pkg/transformerconfig "
31
30
)
32
31
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
-
68
32
// 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 ()
71
35
for _ , path := range paths {
72
36
pathConfig , err := registerCRD (loader , path )
73
37
if err != nil {
74
- return err
38
+ return nil , err
75
39
}
76
- pathConfigs = append ( pathConfigs , pathConfig ... )
40
+ pathConfigs = pathConfigs . Merge ( pathConfig )
77
41
}
78
- addPathConfigs (pathConfigs )
79
- return nil
42
+ return pathConfigs , nil
80
43
}
81
44
82
45
// 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 ()
85
48
content , err := loader .Load (path )
86
49
if err != nil {
87
50
return result , err
@@ -102,15 +65,13 @@ func registerCRD(loader loader.Loader, path string) ([]pathConfigs, error) {
102
65
103
66
crds := getCRDs (types )
104
67
for crd , k := range crds {
105
- crdPathConfigs := pathConfigs {}
68
+ crdPathConfigs := transformerconfig . MakeEmptyTransformerConfig ()
106
69
err = getCRDPathConfig (
107
- types , crd , crd , gvk .FromSchemaGvk (k ), []string {}, & crdPathConfigs )
70
+ types , crd , crd , gvk .FromSchemaGvk (k ), []string {}, crdPathConfigs )
108
71
if err != nil {
109
72
return result , err
110
73
}
111
- if ! reflect .DeepEqual (crdPathConfigs , pathConfigs {}) {
112
- result = append (result , crdPathConfigs )
113
- }
74
+ result = result .Merge (crdPathConfigs )
114
75
}
115
76
116
77
return result , nil
@@ -138,39 +99,39 @@ func getCRDs(types map[string]common.OpenAPIDefinition) map[string]schema.GroupV
138
99
// getCRDPathConfig gets pathConfigs for one CRD recursively
139
100
func getCRDPathConfig (
140
101
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 {
142
103
if _ , ok := types [crd ]; ! ok {
143
104
return nil
144
105
}
145
106
146
107
for propname , property := range types [atype ].Schema .SchemaProps .Properties {
147
108
_ , annotate := property .Extensions .GetString (Annotation )
148
109
if annotate {
149
- configs .addAnnotationPathConfig (
150
- transformers .PathConfig {
110
+ configs .AddAnnotationPathConfig (
111
+ transformerconfig .PathConfig {
151
112
CreateIfNotPresent : false ,
152
- GroupVersionKind : & in ,
153
- Path : append (path , propname ),
113
+ Gvk : in ,
114
+ Path : strings . Join ( append (path , propname ), "/" ),
154
115
},
155
116
)
156
117
}
157
118
_ , label := property .Extensions .GetString (LabelSelector )
158
119
if label {
159
- configs .addLabelPathConfig (
160
- transformers .PathConfig {
120
+ configs .AddLabelPathConfig (
121
+ transformerconfig .PathConfig {
161
122
CreateIfNotPresent : false ,
162
- GroupVersionKind : & in ,
163
- Path : append (path , propname ),
123
+ Gvk : in ,
124
+ Path : strings . Join ( append (path , propname ), "/" ),
164
125
},
165
126
)
166
127
}
167
128
_ , identity := property .Extensions .GetString (Identity )
168
129
if identity {
169
- configs .addPrefixPathConfig (
170
- transformers .PathConfig {
130
+ configs .AddPrefixPathConfig (
131
+ transformerconfig .PathConfig {
171
132
CreateIfNotPresent : false ,
172
- GroupVersionKind : & in ,
173
- Path : append (path , propname ),
133
+ Gvk : in ,
134
+ Path : strings . Join ( append (path , propname ), "/" ),
174
135
},
175
136
)
176
137
}
@@ -182,14 +143,16 @@ func getCRDPathConfig(
182
143
if ! ok {
183
144
nameKey = "name"
184
145
}
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
+ })
193
156
}
194
157
}
195
158
@@ -199,13 +162,3 @@ func getCRDPathConfig(
199
162
}
200
163
return nil
201
164
}
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