Skip to content

Commit 804b842

Browse files
committed
Address feedback on gen.go
1 parent 0023b01 commit 804b842

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

pkg/applyconfiguration/gen.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/gengo/v2/generator"
3131
"k8s.io/gengo/v2/parser"
3232

33+
kerrors "k8s.io/apimachinery/pkg/util/errors"
3334
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
3435
"sigs.k8s.io/controller-tools/pkg/genall"
3536
"sigs.k8s.io/controller-tools/pkg/loader"
@@ -45,7 +46,7 @@ var (
4546
enableTypeMarker = markers.Must(markers.MakeDefinition("kubebuilder:ac:generate", markers.DescribesType, false))
4647
)
4748

48-
const importPathSuffix = "applyconfiguration"
49+
const defaultOutputPackage = "applyconfiguration"
4950

5051
// +controllertools:marker:generateHelp
5152

@@ -72,11 +73,11 @@ func (Generator) RegisterMarkers(into *markers.Registry) error {
7273
into.AddHelp(isCRDMarker,
7374
markers.SimpleHelp("apply", "enables apply configuration generation for this type"))
7475
into.AddHelp(
75-
enableTypeMarker, markers.SimpleHelp("apply", "overrides enabling or disabling applyconfigurations generation for the type"))
76+
enableTypeMarker, markers.SimpleHelp("apply", "overrides enabling or disabling applyconfiguration generation for the type, can be used to generate applyconfiguration for a single type when the package generation is disabled, or to disable generation for a single type when the package generation is enabled"))
7677
into.AddHelp(
77-
enablePkgMarker, markers.SimpleHelp("apply", "overrides enabling or disabling applyconfigurations generation for the package"))
78+
enablePkgMarker, markers.SimpleHelp("apply", "overrides enabling or disabling applyconfiguration generation for the package"))
7879
into.AddHelp(
79-
outputPkgMarker, markers.SimpleHelp("apply", "overrides the default output package for the applyconfigurations generation"))
80+
outputPkgMarker, markers.SimpleHelp("apply", "overrides the default output package for the applyconfiguration generation, supports relative paths to the API directory. The default value is \"applyconfiguration\""))
8081
return nil
8182
}
8283

@@ -92,27 +93,28 @@ func enabledOnPackage(col *markers.Collector, pkg *loader.Package) (bool, error)
9293
return false, nil
9394
}
9495

95-
// enableOnType marks whether applyconfiguration generation is enabled for the type.
9696
func enabledOnType(info *markers.TypeInfo) bool {
9797
if typeMarker := info.Markers.Get(enableTypeMarker.Name); typeMarker != nil {
9898
return typeMarker.(bool)
9999
}
100100
return isCRD(info)
101101
}
102102

103-
func outputPkg(col *markers.Collector, pkg *loader.Package) (string, error) {
103+
func outputPkg(col *markers.Collector, pkg *loader.Package) string {
104104
pkgMarkers, err := markers.PackageMarkers(col, pkg)
105105
if err != nil {
106-
return "", err
106+
// Use the default when there's an error.
107+
return defaultOutputPackage
107108
}
109+
108110
pkgMarker := pkgMarkers.Get(outputPkgMarker.Name)
109111
if pkgMarker != nil {
110-
return pkgMarker.(string), nil
112+
return pkgMarker.(string)
111113
}
112-
return "", nil
114+
115+
return defaultOutputPackage
113116
}
114117

115-
// isCRD marks whether the type is a CRD based on the +kubebuilder:resource marker.
116118
func isCRD(info *markers.TypeInfo) bool {
117119
objectEnabled := info.Markers.Get(isCRDMarker.Name)
118120
return objectEnabled != nil
@@ -126,7 +128,9 @@ func (d Generator) Generate(ctx *genall.GenerationContext) error {
126128
if err != nil {
127129
return fmt.Errorf("failed to create temporary file: %w", err)
128130
}
129-
tmpFile.Close()
131+
if err := tmpFile.Close(); err != nil {
132+
return fmt.Errorf("failed to close temporary file: %w", err)
133+
}
130134

131135
defer os.Remove(tmpFile.Name())
132136

@@ -139,11 +143,17 @@ func (d Generator) Generate(ctx *genall.GenerationContext) error {
139143
HeaderFilePath: headerFilePath,
140144
}
141145

146+
errs := []error{}
142147
for _, pkg := range ctx.Roots {
143148
if err := objGenCtx.generateForPackage(pkg); err != nil {
144-
return err
149+
errs = append(errs, err)
145150
}
146151
}
152+
153+
if len(errs) > 0 {
154+
return kerrors.NewAggregate(errs)
155+
}
156+
147157
return nil
148158
}
149159

@@ -158,7 +168,6 @@ type ObjectGenCtx struct {
158168

159169
// generateForPackage generates apply configuration implementations for
160170
// types in the given package, writing the formatted result to given writer.
161-
// May return nil if source could not be generated.
162171
func (ctx *ObjectGenCtx) generateForPackage(root *loader.Package) error {
163172
enabled, _ := enabledOnPackage(ctx.Collector, root)
164173
if !enabled {
@@ -171,10 +180,7 @@ func (ctx *ObjectGenCtx) generateForPackage(root *loader.Package) error {
171180
arguments := args.New()
172181
arguments.GoHeaderFile = ctx.HeaderFilePath
173182

174-
outpkg, _ := outputPkg(ctx.Collector, root)
175-
if outpkg == "" {
176-
outpkg = importPathSuffix
177-
}
183+
outpkg := outputPkg(ctx.Collector, root)
178184

179185
arguments.OutputDir = filepath.Join(root.Dir, outpkg)
180186
arguments.OutputPkg = filepath.Join(root.Package.PkgPath, outpkg)

0 commit comments

Comments
 (0)