Skip to content

Commit f8ebaed

Browse files
fix: Error in the scaffold files when the group name has
1 parent b2c4917 commit f8ebaed

10 files changed

+20
-14
lines changed

pkg/scaffold/v1/resource/doc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (a *Doc) Validate() error {
5151

5252
var docGoTemplate = `{{ .Boilerplate }}
5353
54-
// Package {{.Resource.Version}} contains API Schema definitions for the {{ .Resource.Group }} {{.Resource.Version}} API group
54+
// Package {{.Resource.Version}} contains API Schema definitions for the {{ .Resource.GroupImportSafe }} {{.Resource.Version}} API group
5555
// +k8s:openapi-gen=true
5656
// +k8s:deepcopy-gen=package,register
5757
// +k8s:conversion-gen={{ .Repo }}/pkg/apis/{{ .Resource.Group }}

pkg/scaffold/v1/resource/group.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ func (g *Group) Validate() error {
4848

4949
var groupTemplate = `{{ .Boilerplate }}
5050
51-
// Package {{ .Resource.Group }} contains {{ .Resource.Group }} API versions
52-
package {{ .Resource.Group }}
51+
// Package {{ .Resource.GroupImportSafe }} contains {{ .Resource.Group }} API versions
52+
package {{ .Resource.GroupImportSafe }}
5353
`

pkg/scaffold/v1/resource/register.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import (
6565
6666
var (
6767
// SchemeGroupVersion is group version used to register these objects
68-
SchemeGroupVersion = schema.GroupVersion{Group: "{{ .Resource.Group }}.{{ .Domain }}", Version: "{{ .Resource.Version }}"}
68+
SchemeGroupVersion = schema.GroupVersion{Group: "{{ .Resource.GroupImportSafe }}.{{ .Domain }}", Version: "{{ .Resource.Version }}"}
6969
7070
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
7171
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}

pkg/scaffold/v1/resource/resource.go

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type Resource struct {
3434
// Group is the API Group. Does not contain the domain.
3535
Group string
3636

37+
// GroupImportSafe is the API Group. Does not contain the domain and it the "-"
38+
// It is used to do safe imports.
39+
GroupImportSafe string
40+
3741
// Version is the API version - e.g. v1beta1
3842
Version string
3943

@@ -71,6 +75,8 @@ func (r *Resource) Validate() error {
7175
return fmt.Errorf("group must match %s (was %s)", GroupMatchRegex, r.Group)
7276
}
7377

78+
r.GroupImportSafe = strings.Replace(r.Group, "-", "", -1)
79+
7480
versionMatch := regexp.MustCompile("^v\\d+(alpha\\d+|beta\\d+)?$")
7581
if !versionMatch.MatchString(r.Version) {
7682
return fmt.Errorf(

pkg/scaffold/v1/resource/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Types struct {
3737
// GetInput implements input.File
3838
func (t *Types) GetInput() (input.Input, error) {
3939
if t.Path == "" {
40-
t.Path = filepath.Join("pkg", "apis", t.Resource.Group, t.Resource.Version,
40+
t.Path = filepath.Join("pkg", "apis", t.Resource.GroupImportSafe, t.Resource.Version,
4141
fmt.Sprintf("%s_types.go", strings.ToLower(t.Resource.Kind)))
4242
}
4343
t.TemplateBody = typesTemplate

pkg/scaffold/v1/resource/version_suitetest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type VersionSuiteTest struct {
3636
// GetInput implements input.File
3737
func (v *VersionSuiteTest) GetInput() (input.Input, error) {
3838
if v.Path == "" {
39-
v.Path = filepath.Join("pkg", "apis", v.Resource.Group, v.Resource.Version,
39+
v.Path = filepath.Join("pkg", "apis", v.Resource.GroupImportSafe, v.Resource.Version,
4040
fmt.Sprintf("%s_suite_test.go", v.Resource.Version))
4141
}
4242
v.TemplateBody = versionSuiteTestTemplate

pkg/scaffold/v2/controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import (
7575
"sigs.k8s.io/controller-runtime/pkg/client"
7676
"github.com/go-logr/logr"
7777
78-
{{ .Resource.Group }}{{ .Resource.Version }} "{{ .ResourcePackage }}/{{ .Resource.Version }}"
78+
{{ .Resource.GroupImportSafe }}{{ .Resource.Version }} "{{ .ResourcePackage }}/{{ .Resource.Version }}"
7979
)
8080
8181
// {{ .Resource.Kind }}Reconciler reconciles a {{ .Resource.Kind }} object
@@ -98,7 +98,7 @@ func (r *{{ .Resource.Kind }}Reconciler) Reconcile(req ctrl.Request) (ctrl.Resul
9898
9999
func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) error {
100100
return ctrl.NewControllerManagedBy(mgr).
101-
For(&{{ .Resource.Group }}{{ .Resource.Version }}.{{ .Resource.Kind }}{}).
101+
For(&{{ .Resource.GroupImportSafe }}{{ .Resource.Version }}.{{ .Resource.Kind }}{}).
102102
Complete(r)
103103
}
104104
`

pkg/scaffold/v2/controller_suitetest.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ func (a *ControllerSuiteTest) Update() error {
139139
ctrlImportCodeFragment := fmt.Sprintf(`"%s/controllers"
140140
`, a.Repo)
141141
apiImportCodeFragment := fmt.Sprintf(`%s%s "%s/%s"
142-
`, a.Resource.Group, a.Resource.Version, a.ResourcePackage, a.Resource.Version)
142+
`, a.Resource.GroupImportSafe, a.Resource.Version, a.ResourcePackage, a.Resource.Version)
143143

144144
addschemeCodeFragment := fmt.Sprintf(`err = %s%s.AddToScheme(scheme.Scheme)
145145
Expect(err).NotTo(HaveOccurred())
146146
147-
`, a.Resource.Group, a.Resource.Version)
147+
`, a.Resource.GroupImportSafe, a.Resource.Version)
148148

149149
err := internal.InsertStringsInFile(a.Path,
150150
map[string][]string{

pkg/scaffold/v2/group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (g *Group) Validate() error {
4949

5050
var groupTemplate = `{{ .Boilerplate }}
5151
52-
// Package {{.Resource.Version}} contains API Schema definitions for the {{ .Resource.Group }} {{.Resource.Version}} API group
52+
// Package {{.Resource.Version}} contains API Schema definitions for the {{ .Resource.GroupImportSafe }} {{.Resource.Version}} API group
5353
// +kubebuilder:object:generate=true
5454
// +groupName={{ .Resource.Group }}.{{ .Domain }}
5555
package {{ .Resource.Version }}

pkg/scaffold/v2/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ func (m *Main) Update(opts *MainUpdateOptions) error {
5757

5858
// generate all the code fragments
5959
apiImportCodeFragment := fmt.Sprintf(`%s%s "%s/%s"
60-
`, opts.Resource.Group, opts.Resource.Version, resPkg, opts.Resource.Version)
60+
`, opts.Resource.GroupImportSafe, opts.Resource.Version, resPkg, opts.Resource.Version)
6161
ctrlImportCodeFragment := fmt.Sprintf(`"%s/controllers"
6262
`, opts.Project.Repo)
6363
addschemeCodeFragment := fmt.Sprintf(`_ = %s%s.AddToScheme(scheme)
64-
`, opts.Resource.Group, opts.Resource.Version)
64+
`, opts.Resource.GroupImportSafe, opts.Resource.Version)
6565
reconcilerSetupCodeFragment := fmt.Sprintf(`if err = (&controllers.%sReconciler{
6666
Client: mgr.GetClient(),
6767
Log: ctrl.Log.WithName("controllers").WithName("%s"),
@@ -74,7 +74,7 @@ func (m *Main) Update(opts *MainUpdateOptions) error {
7474
setupLog.Error(err, "unable to create webhook", "webhook", "%s")
7575
os.Exit(1)
7676
}
77-
`, opts.Resource.Group, opts.Resource.Version, opts.Resource.Kind, opts.Resource.Kind)
77+
`, opts.Resource.GroupImportSafe, opts.Resource.Version, opts.Resource.Kind, opts.Resource.Kind)
7878

7979
if opts.WireResource {
8080
err := internal.InsertStringsInFile(path,

0 commit comments

Comments
 (0)