@@ -20,13 +20,10 @@ import (
2020 "fmt"
2121 "go/ast"
2222 "go/types"
23- "io"
2423 "os"
2524 "path/filepath"
26- "sort"
2725 "strings"
2826
29- "golang.org/x/tools/go/packages"
3027 "k8s.io/apimachinery/pkg/util/sets"
3128 generatorargs "k8s.io/code-generator/cmd/applyconfiguration-gen/args"
3229 applygenerator "k8s.io/code-generator/cmd/applyconfiguration-gen/generators"
@@ -52,17 +49,14 @@ var importMapping = map[string]string{
5249 "k8s.io/api/" : "k8s.io/client-go/applyconfigurations/" ,
5350}
5451
55- const importPathSuffix = "ac"
56- const packageFileName = "zz_generated.applyconfigurations.go"
52+ const importPathSuffix = "applyconfiguration"
5753
5854// +controllertools:marker:generateHelp
5955
6056// Generator generates code containing apply configuration type implementations.
6157type Generator struct {
6258 // HeaderFile specifies the header text (e.g. license) to prepend to generated files.
6359 HeaderFile string `marker:",optional"`
64- // Year specifies the year to substitute for " YEAR" in the header file.
65- Year string `marker:",optional"`
6660}
6761
6862func (Generator ) CheckFilter () loader.NodeFilter {
@@ -117,25 +111,7 @@ func enabledOnType(info *markers.TypeInfo) bool {
117111// isCRD marks whether the type is a CRD based on the +kubebuilder:resource marker.
118112func isCRD (info * markers.TypeInfo ) bool {
119113 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
139115}
140116
141117func (d Generator ) Generate (ctx * genall.GenerationContext ) error {
@@ -176,96 +152,12 @@ type ObjectGenCtx struct {
176152 HeaderFilePath string
177153}
178154
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-
260155type Universe struct {
261156 typeMetadata map [types.Type ]* typeMetadata
262157}
263158
264159type typeMetadata struct {
265- info * markers.TypeInfo
266- root * loader.Package
267- eligible bool
268- used bool
160+ used bool
269161}
270162
271163func (u * Universe ) existingApplyConfigPath (_ * types.Named , pkgPath string ) (string , bool ) {
@@ -362,38 +254,3 @@ func (ctx *ObjectGenCtx) generateForPackage(root *loader.Package) error {
362254
363255 return nil
364256}
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