@@ -20,13 +20,10 @@ import (
20
20
"fmt"
21
21
"go/ast"
22
22
"go/types"
23
- "io"
24
23
"os"
25
24
"path/filepath"
26
- "sort"
27
25
"strings"
28
26
29
- "golang.org/x/tools/go/packages"
30
27
"k8s.io/apimachinery/pkg/util/sets"
31
28
generatorargs "k8s.io/code-generator/cmd/applyconfiguration-gen/args"
32
29
applygenerator "k8s.io/code-generator/cmd/applyconfiguration-gen/generators"
@@ -52,17 +49,14 @@ var importMapping = map[string]string{
52
49
"k8s.io/api/" : "k8s.io/client-go/applyconfigurations/" ,
53
50
}
54
51
55
- const importPathSuffix = "ac"
56
- const packageFileName = "zz_generated.applyconfigurations.go"
52
+ const importPathSuffix = "applyconfiguration"
57
53
58
54
// +controllertools:marker:generateHelp
59
55
60
56
// Generator generates code containing apply configuration type implementations.
61
57
type Generator struct {
62
58
// HeaderFile specifies the header text (e.g. license) to prepend to generated files.
63
59
HeaderFile string `marker:",optional"`
64
- // Year specifies the year to substitute for " YEAR" in the header file.
65
- Year string `marker:",optional"`
66
60
}
67
61
68
62
func (Generator ) CheckFilter () loader.NodeFilter {
@@ -117,25 +111,7 @@ func enabledOnType(info *markers.TypeInfo) bool {
117
111
// isCRD marks whether the type is a CRD based on the +kubebuilder:resource marker.
118
112
func isCRD (info * markers.TypeInfo ) bool {
119
113
objectEnabled := info .Markers .Get (isCRDMarker .Name )
120
- if objectEnabled != nil {
121
- return true
122
- }
123
- return false
124
- }
125
-
126
- func isCRDClusterScope (info * markers.TypeInfo ) bool {
127
- if o := info .Markers .Get (isCRDMarker .Name ); o != nil {
128
- crd := o .(crdmarkers.Resource )
129
- return crd .Scope == "Cluster"
130
- }
131
- return false
132
- }
133
-
134
- func createApplyConfigPackage (pkg * loader.Package ) * loader.Package {
135
- newPkg := & loader.Package {Package : & packages.Package {}}
136
- dir := filepath .Dir (pkg .CompiledGoFiles [0 ])
137
- newPkg .CompiledGoFiles = append (newPkg .CompiledGoFiles , dir + "/" + importPathSuffix + "/" )
138
- return newPkg
114
+ return objectEnabled != nil
139
115
}
140
116
141
117
func (d Generator ) Generate (ctx * genall.GenerationContext ) error {
@@ -176,96 +152,12 @@ type ObjectGenCtx struct {
176
152
HeaderFilePath string
177
153
}
178
154
179
- // generateEligibleTypes generates a universe of all possible ApplyConfiguration types.
180
- // The function also scans all imported packages for types that are eligible to be ApplyConfigurations.
181
- // This first pass is necessary because the loader package is not able to follow references between packages
182
- // and this universe constructs the necessary references.
183
- func (ctx * ObjectGenCtx ) generateEligibleTypes (root * loader.Package , universe * Universe ) {
184
- ctx .Checker .Check (root )
185
- root .NeedTypesInfo ()
186
-
187
- if err := markers .EachType (ctx .Collector , root , func (info * markers.TypeInfo ) {
188
- // not all types required a generate apply configuration. For example, no apply configuration
189
- // type is needed for Quantity, IntOrString, RawExtension or Unknown.
190
-
191
- if shouldBeApplyConfiguration (root , info ) {
192
- typeInfo := root .TypesInfo .TypeOf (info .RawSpec .Name )
193
- universe .typeMetadata [typeInfo ] = & typeMetadata {
194
- info : info ,
195
- root : root ,
196
- eligible : true ,
197
- used : false ,
198
- }
199
- }
200
-
201
- }); err != nil {
202
- root .AddError (err )
203
- return
204
- }
205
- return
206
- }
207
-
208
- // generateUsedTypes does a breadth first search from each top level root object
209
- // to find all ApplyConfiguration types that must be generated based on the fields
210
- // that the object references.
211
- func (ctx * ObjectGenCtx ) generateUsedTypes (root * loader.Package , universe * Universe ) {
212
- ctx .Checker .Check (root )
213
- root .NeedTypesInfo ()
214
-
215
- if err := markers .EachType (ctx .Collector , root , func (info * markers.TypeInfo ) {
216
- if ! enabledOnType (info ) {
217
- return
218
- }
219
-
220
- var q []types.Type
221
- q = append (q , root .TypesInfo .TypeOf (info .RawSpec .Name ))
222
-
223
- for len (q ) > 0 {
224
- node := universe .typeMetadata [q [0 ]]
225
- q = q [1 :]
226
- if node .used {
227
- continue
228
- }
229
- node .used = true
230
- if len (node .info .Fields ) > 0 {
231
- for _ , field := range node .info .Fields {
232
- fieldType := node .root .TypesInfo .TypeOf (field .RawField .Type )
233
- resolved := false
234
- // TODO: Are these all the types that need to be resolved?
235
- for ! resolved {
236
- resolved = true
237
- switch typeInfo := fieldType .(type ) {
238
- case * types.Pointer :
239
- fieldType = typeInfo .Elem ()
240
- resolved = false
241
- case * types.Slice :
242
- fieldType = typeInfo .Elem ()
243
- resolved = false
244
- }
245
- }
246
-
247
- if _ , ok := universe .typeMetadata [fieldType ]; ok {
248
- q = append (q , fieldType )
249
- }
250
- }
251
- }
252
- }
253
- }); err != nil {
254
- root .AddError (err )
255
- return
256
- }
257
- return
258
- }
259
-
260
155
type Universe struct {
261
156
typeMetadata map [types.Type ]* typeMetadata
262
157
}
263
158
264
159
type typeMetadata struct {
265
- info * markers.TypeInfo
266
- root * loader.Package
267
- eligible bool
268
- used bool
160
+ used bool
269
161
}
270
162
271
163
func (u * Universe ) existingApplyConfigPath (_ * types.Named , pkgPath string ) (string , bool ) {
@@ -362,38 +254,3 @@ func (ctx *ObjectGenCtx) generateForPackage(root *loader.Package) error {
362
254
363
255
return nil
364
256
}
365
-
366
- // writeTypes writes each method to the file, sorted by type name.
367
- func writeTypes (pkg * loader.Package , out io.Writer , byType map [string ][]byte ) {
368
- sortedNames := make ([]string , 0 , len (byType ))
369
- for name := range byType {
370
- sortedNames = append (sortedNames , name )
371
- }
372
- sort .Strings (sortedNames )
373
-
374
- for _ , name := range sortedNames {
375
- _ , err := out .Write (byType [name ])
376
- if err != nil {
377
- pkg .AddError (err )
378
- }
379
- }
380
- }
381
-
382
- // writeFormatted outputs the given code, after gofmt-ing it. If we couldn't gofmt,
383
- // we write the unformatted code for debugging purposes.
384
- func writeOut (ctx * genall.GenerationContext , root * loader.Package , outBytes []byte ) {
385
- outputFile , err := ctx .Open (root , packageFileName )
386
- if err != nil {
387
- root .AddError (err )
388
- return
389
- }
390
- defer outputFile .Close ()
391
- n , err := outputFile .Write (outBytes )
392
- if err != nil {
393
- root .AddError (err )
394
- return
395
- }
396
- if n < len (outBytes ) {
397
- root .AddError (io .ErrShortWrite )
398
- }
399
- }
0 commit comments