Skip to content

Commit f0d9904

Browse files
committed
Migrate to gengo v2
1 parent fe36a09 commit f0d9904

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

pkg/applyconfigurations/gen.go

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ import (
2323
"path/filepath"
2424

2525
"k8s.io/apimachinery/pkg/util/sets"
26-
generatorargs "k8s.io/code-generator/cmd/applyconfiguration-gen/args"
27-
applygenerator "k8s.io/code-generator/cmd/applyconfiguration-gen/generators"
28-
"k8s.io/gengo/generator"
26+
"k8s.io/code-generator/cmd/applyconfiguration-gen/args"
27+
"k8s.io/code-generator/cmd/applyconfiguration-gen/generators"
28+
29+
"k8s.io/gengo/v2"
30+
"k8s.io/gengo/v2/generator"
31+
"k8s.io/gengo/v2/parser"
32+
2933
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
3034
"sigs.k8s.io/controller-tools/pkg/genall"
3135
"sigs.k8s.io/controller-tools/pkg/loader"
@@ -74,7 +78,6 @@ func (Generator) RegisterMarkers(into *markers.Registry) error {
7478
into.AddHelp(
7579
outputPkgMarker, markers.SimpleHelp("apply", "overrides the default output package for the applyconfigurations generation"))
7680
return nil
77-
7881
}
7982

8083
func enabledOnPackage(col *markers.Collector, pkg *loader.Package) (bool, error) {
@@ -165,42 +168,31 @@ func (ctx *ObjectGenCtx) generateForPackage(root *loader.Package) error {
165168
return nil
166169
}
167170

168-
genericArgs, _ := generatorargs.NewDefaults()
169-
genericArgs.InputDirs = []string{root.PkgPath}
170-
genericArgs.GoHeaderFilePath = ctx.HeaderFilePath
171+
arguments := args.New()
172+
arguments.GoHeaderFile = ctx.HeaderFilePath
171173

172174
outpkg, _ := outputPkg(ctx.Collector, root)
173175
if outpkg == "" {
174176
outpkg = importPathSuffix
175177
}
176-
genericArgs.OutputPackagePath = filepath.Join(root.PkgPath, outpkg)
177-
178-
// attempts to retrieve the correct base directory to output apply configurations to by
179-
// looking into the package and retrieving the first go file it finds, and using that as the output base.
180-
// this is because we cannot rely on gogen calculating the correct output base.
181-
// if we leave this empty, gogen will attempt to use GOPATH to write the files which is not wanted
182-
genericArgs.OutputBase = filepath.Dir(root.GoFiles[0])
183-
trimPathPrefix := filepath.Join(genericArgs.OutputBase, root.PkgPath) + "/"
184-
185-
// Make the generated header static so that it doesn't rely on the compiled binary name.
186-
genericArgs.GeneratedByCommentTemplate = "// Code generated by applyconfiguration-gen. DO NOT EDIT.\n"
187178

188-
if err := generatorargs.Validate(genericArgs); err != nil {
189-
return err
190-
}
179+
arguments.OutputDir = filepath.Join(root.Dir, outpkg)
180+
arguments.OutputPkg = filepath.Join(root.Package.PkgPath, outpkg)
191181

192-
b, err := genericArgs.NewBuilder()
193-
if err != nil {
194-
return err
182+
// The following code is based on gengo/v2.Execute.
183+
// We have lifted it from there so that we can adjust the markers on the types to make sure
184+
// that Kubebuilder generation markers are converted into the genclient marker
185+
// prior to executing the targets.
186+
buildTags := []string{gengo.StdBuildTag}
187+
p := parser.NewWithOptions(parser.Options{BuildTags: buildTags})
188+
if err := p.LoadPackages(root.PkgPath); err != nil {
189+
return fmt.Errorf("failed making a parser: %w", err)
195190
}
196191

197-
c, err := generator.NewContext(b, applygenerator.NameSystems(), applygenerator.DefaultNameSystem())
192+
c, err := generator.NewContext(p, generators.NameSystems(), generators.DefaultNameSystem())
198193
if err != nil {
199-
return err
194+
return fmt.Errorf("failed making a context: %w", err)
200195
}
201-
// The output package path is fully qualified. It contains the OutputBase (which is the module directory)
202-
// that means we will have to trim the fully qualified pkg down again.
203-
c.TrimPathPrefix = trimPathPrefix
204196

205197
pkg, ok := c.Universe[root.PkgPath]
206198
if !ok {
@@ -229,9 +221,9 @@ func (ctx *ObjectGenCtx) generateForPackage(root *loader.Package) error {
229221
return err
230222
}
231223

232-
packages := applygenerator.Packages(c, genericArgs)
233-
if err := c.ExecutePackages(genericArgs.OutputBase, packages); err != nil {
234-
return fmt.Errorf("error executing packages: %w", err)
224+
targets := generators.GetTargets(c, arguments)
225+
if err := c.ExecuteTargets(targets); err != nil {
226+
return fmt.Errorf("failed executing generator: %w", err)
235227
}
236228

237229
return nil

0 commit comments

Comments
 (0)