Skip to content

Commit aea7324

Browse files
committed
1 parent 7c982e0 commit aea7324

File tree

7 files changed

+73
-35
lines changed

7 files changed

+73
-35
lines changed

pkg/applyconfiguration/applyconfiguration_integration_test.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,37 +118,46 @@ var _ = Describe("ApplyConfiguration generation from API types", func() {
118118

119119
rt.OutputRules = genall.OutputRules{Default: output}
120120

121-
originalFS := os.DirFS(filepath.Join(originalCWD, cronjobDir))
122-
tmpFS := os.DirFS(".")
121+
fixturePath := filepath.Join(originalCWD, cronjobDir)
123122

124123
By("Running the generator")
125124
hadErrs := rt.Run()
126125

127126
By("Checking for generation errors")
128127
Expect(hadErrs).To(BeFalse(), "Generator should run without errors")
129128

129+
tmpFS := os.DirFS(".")
130+
if os.Getenv("UPDATE") != "" && outputPackage == "applyconfiguration" {
131+
Expect(os.RemoveAll(fixturePath)).NotTo(HaveOccurred())
132+
Expect(os.CopyFS(fixturePath, tmpFS)).NotTo(HaveOccurred())
133+
}
134+
135+
originalFS := os.DirFS(fixturePath)
136+
130137
filesInOriginal := make(map[string][]byte)
131138
originalFileNames := sets.New[string]()
132-
Expect(fs.WalkDir(originalFS, filepath.Join("api/v1", applyConfigurationDir), func(path string, d fs.DirEntry, err error) error {
133-
if err != nil {
134-
return err
135-
}
136-
137-
if d.IsDir() {
139+
Expect(fs.WalkDir(originalFS, filepath.Join("api/v1", applyConfigurationDir),
140+
func(path string, d fs.DirEntry, err error) error {
141+
if err != nil {
142+
return err
143+
}
144+
145+
if d.IsDir() {
146+
return nil
147+
}
148+
149+
data, err := os.ReadFile(filepath.Join(originalCWD, cronjobDir, path))
150+
if err != nil {
151+
return fmt.Errorf("error reading file %s: %w", path, err)
152+
}
153+
154+
// Record the path without the path prefix for comparison later.
155+
path = strings.TrimPrefix(path, filepath.Join("api/v1", applyConfigurationDir)+"/")
156+
originalFileNames.Insert(path)
157+
filesInOriginal[path] = data
138158
return nil
139-
}
140-
141-
data, err := os.ReadFile(filepath.Join(originalCWD, cronjobDir, path))
142-
if err != nil {
143-
return fmt.Errorf("error reading file %s: %w", path, err)
144-
}
145-
146-
// Record the path without the path prefix for comparison later.
147-
path = strings.TrimPrefix(path, filepath.Join("api/v1", applyConfigurationDir)+"/")
148-
originalFileNames.Insert(path)
149-
filesInOriginal[path] = data
150-
return nil
151-
})).To(Succeed())
159+
},
160+
)).To(Succeed())
152161

153162
filesInOutput := make(map[string][]byte)
154163
outputFileNames := sets.New[string]()

pkg/applyconfiguration/gen.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package applyconfiguration
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"go/ast"
2223
"os"
@@ -33,6 +34,7 @@ import (
3334
kerrors "k8s.io/apimachinery/pkg/util/errors"
3435
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
3536
"sigs.k8s.io/controller-tools/pkg/genall"
37+
"sigs.k8s.io/controller-tools/pkg/internal/crd"
3638
"sigs.k8s.io/controller-tools/pkg/loader"
3739
"sigs.k8s.io/controller-tools/pkg/markers"
3840
)
@@ -205,6 +207,18 @@ func (ctx *ObjectGenCtx) generateForPackage(root *loader.Package) error {
205207
return fmt.Errorf("package %q not found in universe", root.Name)
206208
}
207209

210+
pkgMarkers, err := markers.PackageMarkers(ctx.Collector, root)
211+
if err != nil {
212+
return fmt.Errorf("failed to get package markers: %w", err)
213+
}
214+
215+
gv := crd.GroupVersionForPackage(pkgMarkers, root)
216+
if gv.Empty() {
217+
return errors.New("could not infer groupVersion for package - Is the `// +groupName` marker set?")
218+
}
219+
220+
pkg.Comments = append(pkg.Comments, "+groupName="+gv.Group)
221+
208222
// For each type we think should be generated, make sure it has a genclient
209223
// marker else the apply generator will not generate it.
210224
if err := markers.EachType(ctx.Collector, root, func(info *markers.TypeInfo) {

pkg/applyconfiguration/testdata/cronjob/api/v1/applyconfiguration/api/v1/cronjob.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/applyconfiguration/testdata/cronjob/api/v1/applyconfiguration/utils.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/applyconfiguration/testdata/cronjob/api/v1/cronjob_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616
// TODO(directxman12): test this across both versions (right now we're just
1717
// trusting k/k conversion, which is probably fine though)
1818

19-
//go:generate ../../../.run-controller-gen.sh crd:ignoreUnexportedFields=true,allowDangerousTypes=true paths=./;./deprecated;./unserved;./job/... output:dir=.
19+
//go:generate ../../../../../../.run-controller-gen.sh crd:ignoreUnexportedFields=true,allowDangerousTypes=true paths=./;./deprecated;./unserved;./job/... output:dir=.
2020

2121
// +groupName=testdata.kubebuilder.io
2222
// +versionName=v1

pkg/crd/parser.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2323
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"sigs.k8s.io/controller-tools/pkg/internal/crd"
2425
"sigs.k8s.io/controller-tools/pkg/loader"
2526
"sigs.k8s.io/controller-tools/pkg/markers"
2627
)
@@ -132,17 +133,8 @@ func (p *Parser) indexTypes(pkg *loader.Package) {
132133
if skipPkg := pkgMarkers.Get("kubebuilder:skip"); skipPkg != nil {
133134
return
134135
}
135-
if nameVal := pkgMarkers.Get("groupName"); nameVal != nil {
136-
versionVal := pkg.Name // a reasonable guess
137-
if versionMarker := pkgMarkers.Get("versionName"); versionMarker != nil {
138-
versionVal = versionMarker.(string)
139-
}
140-
141-
p.GroupVersions[pkg] = schema.GroupVersion{
142-
Version: versionVal,
143-
Group: nameVal.(string),
144-
}
145-
}
136+
137+
p.GroupVersions[pkg] = crd.GroupVersionForPackage(pkgMarkers, pkg)
146138
}
147139

148140
if err := markers.EachType(p.Collector, pkg, func(info *markers.TypeInfo) {

pkg/internal/crd/crd.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package crd
2+
3+
import (
4+
"k8s.io/apimachinery/pkg/runtime/schema"
5+
"sigs.k8s.io/controller-tools/pkg/loader"
6+
"sigs.k8s.io/controller-tools/pkg/markers"
7+
)
8+
9+
func GroupVersionForPackage(pkgMarkers markers.MarkerValues, pkg *loader.Package) schema.GroupVersion {
10+
if nameVal := pkgMarkers.Get("groupName"); nameVal != nil {
11+
versionVal := pkg.Name // a reasonable guess
12+
if versionMarker := pkgMarkers.Get("versionName"); versionMarker != nil {
13+
versionVal = versionMarker.(string)
14+
}
15+
16+
return schema.GroupVersion{
17+
Version: versionVal,
18+
Group: nameVal.(string),
19+
}
20+
}
21+
22+
return schema.GroupVersion{}
23+
}

0 commit comments

Comments
 (0)