Skip to content

Commit 8fbc344

Browse files
nak3estroz
authored andcommitted
Remove hyphen from controller generated code (#998)
* pkg/scaffold: replace `-` with an empty string in the user-specified operator API group for import identifiers and package paths
1 parent b21f838 commit 8fbc344

File tree

6 files changed

+16
-10
lines changed

6 files changed

+16
-10
lines changed

pkg/scaffold/addtoscheme.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type AddToScheme struct {
3333
func (s *AddToScheme) GetInput() (input.Input, error) {
3434
if s.Path == "" {
3535
fileName := fmt.Sprintf("addtoscheme_%s_%s.go",
36-
strings.ToLower(s.Resource.Group),
36+
s.Resource.GoImportGroup,
3737
strings.ToLower(s.Resource.Version))
3838
s.Path = filepath.Join(ApisDir, fileName)
3939
}
@@ -44,7 +44,7 @@ func (s *AddToScheme) GetInput() (input.Input, error) {
4444
const addToSchemeTemplate = `package apis
4545
4646
import (
47-
"{{ .Repo }}/pkg/apis/{{ .Resource.Group }}/{{ .Resource.Version }}"
47+
"{{ .Repo }}/pkg/apis/{{ .Resource.GoImportGroup}}/{{ .Resource.Version }}"
4848
)
4949
5050
func init() {

pkg/scaffold/controller_kind.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const controllerKindTemplate = `package {{ .Resource.LowerKind }}
4444
import (
4545
"context"
4646
47-
{{ .Resource.Group}}{{ .Resource.Version }} "{{ .Repo }}/pkg/apis/{{ .Resource.Group}}/{{ .Resource.Version }}"
47+
{{ .Resource.GoImportGroup}}{{ .Resource.Version }} "{{ .Repo }}/pkg/apis/{{ .Resource.GoImportGroup}}/{{ .Resource.Version }}"
4848
4949
corev1 "k8s.io/api/core/v1"
5050
"k8s.io/apimachinery/pkg/api/errors"
@@ -88,7 +88,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
8888
}
8989
9090
// Watch for changes to primary resource {{ .Resource.Kind }}
91-
err = c.Watch(&source.Kind{Type: &{{ .Resource.Group}}{{ .Resource.Version }}.{{ .Resource.Kind }}{}}, &handler.EnqueueRequestForObject{})
91+
err = c.Watch(&source.Kind{Type: &{{ .Resource.GoImportGroup}}{{ .Resource.Version }}.{{ .Resource.Kind }}{}}, &handler.EnqueueRequestForObject{})
9292
if err != nil {
9393
return err
9494
}
@@ -97,7 +97,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
9797
// Watch for changes to secondary resource Pods and requeue the owner {{ .Resource.Kind }}
9898
err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForOwner{
9999
IsController: true,
100-
OwnerType: &{{ .Resource.Group}}{{ .Resource.Version }}.{{ .Resource.Kind }}{},
100+
OwnerType: &{{ .Resource.GoImportGroup}}{{ .Resource.Version }}.{{ .Resource.Kind }}{},
101101
})
102102
if err != nil {
103103
return err
@@ -128,7 +128,7 @@ func (r *Reconcile{{ .Resource.Kind }}) Reconcile(request reconcile.Request) (re
128128
reqLogger.Info("Reconciling {{ .Resource.Kind }}")
129129
130130
// Fetch the {{ .Resource.Kind }} instance
131-
instance := &{{ .Resource.Group}}{{ .Resource.Version }}.{{ .Resource.Kind }}{}
131+
instance := &{{ .Resource.GoImportGroup}}{{ .Resource.Version }}.{{ .Resource.Kind }}{}
132132
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
133133
if err != nil {
134134
if errors.IsNotFound(err) {
@@ -171,7 +171,7 @@ func (r *Reconcile{{ .Resource.Kind }}) Reconcile(request reconcile.Request) (re
171171
}
172172
173173
// newPodForCR returns a busybox pod with the same name/namespace as the cr
174-
func newPodForCR(cr *{{ .Resource.Group}}{{ .Resource.Version }}.{{ .Resource.Kind }}) *corev1.Pod {
174+
func newPodForCR(cr *{{ .Resource.GoImportGroup}}{{ .Resource.Version }}.{{ .Resource.Kind }}) *corev1.Pod {
175175
labels := map[string]string{
176176
"app": cr.Name,
177177
}

pkg/scaffold/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Doc struct {
3434
func (s *Doc) GetInput() (input.Input, error) {
3535
if s.Path == "" {
3636
s.Path = filepath.Join(ApisDir,
37-
strings.ToLower(s.Resource.Group),
37+
s.Resource.GoImportGroup,
3838
strings.ToLower(s.Resource.Version),
3939
DocFile)
4040
}

pkg/scaffold/register.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Register struct {
3434
func (s *Register) GetInput() (input.Input, error) {
3535
if s.Path == "" {
3636
s.Path = filepath.Join(ApisDir,
37-
strings.ToLower(s.Resource.Group),
37+
s.Resource.GoImportGroup,
3838
strings.ToLower(s.Resource.Version),
3939
RegisterFile)
4040
}

pkg/scaffold/resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type Resource struct {
5151
// Parsed from APIVersion
5252
Group string
5353

54+
// GoImportGroup is the non-hyphenated go import group for this resource
55+
GoImportGroup string
56+
5457
// Version is the API version - e.g. v1alpha1
5558
// Parsed from APIVersion
5659
Version string
@@ -127,6 +130,9 @@ func (r *Resource) checkAndSetGroups() error {
127130
r.FullGroup = fg[0]
128131
r.Group = g[0]
129132

133+
s := strings.ToLower(r.Group)
134+
r.GoImportGroup = strings.Replace(s, "-", "", -1)
135+
130136
if err := validation.IsDNS1123Subdomain(r.Group); err != nil {
131137
return fmt.Errorf("group name is invalid: %v", err)
132138
}

pkg/scaffold/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Types struct {
3232
func (s *Types) GetInput() (input.Input, error) {
3333
if s.Path == "" {
3434
s.Path = filepath.Join(ApisDir,
35-
strings.ToLower(s.Resource.Group),
35+
s.Resource.GoImportGroup,
3636
strings.ToLower(s.Resource.Version),
3737
s.Resource.LowerKind+"_types.go")
3838
}

0 commit comments

Comments
 (0)