Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Remove spaces from machine-readable comments #1868

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pkg/model/file/marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ package file
import (
"fmt"
"path/filepath"
"strings"
)

const prefix = "+kubebuilder:scaffold:"

var commentsByExt = map[string]string{
// TODO(v3): machine-readable comments should not have spaces by Go convention. However,
// this is a backwards incompatible change, and thus should be done for next project version.
".go": "// ",
".yaml": "# ",
".go": "//",
".yaml": "#",
".yml": "#",
Adirio marked this conversation as resolved.
Show resolved Hide resolved
// When adding additional file extensions, update also the NewMarkerFor documentation and error
}
Expand All @@ -54,6 +53,12 @@ func (m Marker) String() string {
return m.comment + prefix + m.value
}

// EqualsLine compares a marker with a string representation to check if they are the same marker
func (m Marker) EqualsLine(line string) bool {
line = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), m.comment))
return line == prefix+m.value
}

// CodeFragments represents a set of code fragments
// A code fragment is a piece of code provided as a Go string, it may have multiple lines
type CodeFragments []string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func (f *Group) SetTemplateDefaults() error {
const groupTemplate = `{{ .Boilerplate }}

// Package {{ .Resource.Version }} contains API Schema definitions for the {{ .Resource.Group }} {{ .Resource.Version }} API group
// +kubebuilder:object:generate=true
// +groupName={{ .Resource.Domain }}
//+kubebuilder:object:generate=true
//+groupName={{ .Resource.Domain }}
package {{ .Resource.Version }}

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ type {{ .Resource.Kind }}Status struct {
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
{{ if not .Resource.Namespaced }} // +kubebuilder:resource:scope=Cluster {{ end }}
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
{{ if not .Resource.Namespaced }} //+kubebuilder:resource:scope=Cluster {{ end }}

// {{ .Resource.Kind }} is the Schema for the {{ .Resource.Plural }} API
type {{ .Resource.Kind }} struct {
Expand All @@ -91,7 +91,7 @@ type {{ .Resource.Kind }} struct {
Status {{ .Resource.Kind }}Status ` + "`" + `json:"status,omitempty"` + "`" + `
}

// +kubebuilder:object:root=true
//+kubebuilder:object:root=true

// {{ .Resource.Kind }}List contains a list of {{ .Resource.Kind }}
type {{ .Resource.Kind }}List struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error {

//nolint:lll
defaultingWebhookTemplate = `
// +kubebuilder:webhook:path=/mutate-{{ .GroupDomainWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=true,failurePolicy=fail,groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=m{{ lower .Resource.Kind }}.kb.io
//+kubebuilder:webhook:path=/mutate-{{ .GroupDomainWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=true,failurePolicy=fail,groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=m{{ lower .Resource.Kind }}.kb.io

var _ webhook.Defaulter = &{{ .Resource.Kind }}{}

Expand All @@ -114,7 +114,7 @@ func (r *{{ .Resource.Kind }}) Default() {
//nolint:lll
validatingWebhookTemplate = `
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// +kubebuilder:webhook:verbs=create;update,path=/validate-{{ .GroupDomainWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io
//+kubebuilder:webhook:verbs=create;update,path=/validate-{{ .GroupDomainWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io

var _ webhook.Validator = &{{ .Resource.Kind }}{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ type {{ .Resource.Kind }}Reconciler struct {
Scheme *runtime.Scheme
}

// +kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }}/status,verbs=get;update;patch
//+kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }}/status,verbs=get;update;patch

func (r *{{ .Resource.Kind }}Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
_ = context.Background()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (f *Group) SetTemplateDefaults() error {
const groupTemplate = `{{ .Boilerplate }}

// Package {{ .Resource.Version }} contains API Schema definitions for the {{ .Resource.Group }} {{ .Resource.Version }} API group
// +kubebuilder:object:generate=true
// +groupName={{ .Resource.Domain }}
//+kubebuilder:object:generate=true
//+groupName={{ .Resource.Domain }}
package {{ .Resource.Version }}

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ type {{ .Resource.Kind }}Status struct {
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
{{ if not .Resource.Namespaced }} // +kubebuilder:resource:scope=Cluster {{ end }}
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
{{ if not .Resource.Namespaced }} //+kubebuilder:resource:scope=Cluster {{ end }}

// {{ .Resource.Kind }} is the Schema for the {{ .Resource.Plural }} API
type {{ .Resource.Kind }} struct {
Expand All @@ -95,7 +95,7 @@ type {{ .Resource.Kind }} struct {
Status {{ .Resource.Kind }}Status ` + "`" + `json:"status,omitempty"` + "`" + `
}

// +kubebuilder:object:root=true
//+kubebuilder:object:root=true

// {{ .Resource.Kind }}List contains a list of {{ .Resource.Kind }}
type {{ .Resource.Kind }}List struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error {
// TODO(estroz): update admissionReviewVersions to include v1 when controller-runtime supports that version.
//nolint:lll
defaultingWebhookTemplate = `
// +kubebuilder:webhook:{{ if ne .WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .WebhookVersion }}{{"}"}},{{ end }}path=/mutate-{{ .GroupDomainWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=true,failurePolicy=fail,sideEffects=None,groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=m{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={v1,v1beta1}
//+kubebuilder:webhook:{{ if ne .WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .WebhookVersion }}{{"}"}},{{ end }}path=/mutate-{{ .GroupDomainWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=true,failurePolicy=fail,sideEffects=None,groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=m{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={v1,v1beta1}

var _ webhook.Defaulter = &{{ .Resource.Kind }}{}

Expand All @@ -133,7 +133,7 @@ func (r *{{ .Resource.Kind }}) Default() {
//nolint:lll
validatingWebhookTemplate = `
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// +kubebuilder:webhook:{{ if ne .WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .WebhookVersion }}{{"}"}},{{ end }}path=/validate-{{ .GroupDomainWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,sideEffects=None,groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={v1,v1beta1}
//+kubebuilder:webhook:{{ if ne .WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .WebhookVersion }}{{"}"}},{{ end }}path=/validate-{{ .GroupDomainWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,sideEffects=None,groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={v1,v1beta1}

var _ webhook.Validator = &{{ .Resource.Kind }}{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ func (f *WebhookSuite) GetCodeFragments() file.CodeFragmentsMap {
return fragments
}

const (
webhookTestSuiteTemplate = `{{ .Boilerplate }}
const webhookTestSuiteTemplate = `{{ .Boilerplate }}

package {{ .Resource.Version }}

Expand Down Expand Up @@ -225,4 +224,3 @@ var _ = AfterSuite(func() {
Expect(err).NotTo(HaveOccurred())
})
`
)
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ type {{ .Resource.Kind }}Reconciler struct {
Scheme *runtime.Scheme
}

// +kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }}/status,verbs=get;update;patch
// +kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }}/finalizers,verbs=update
//+kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }}/status,verbs=get;update;patch
//+kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }}/finalizers,verbs=update

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/internal/machinery/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func insertStrings(content string, codeFragmentsMap file.CodeFragmentsMap) ([]by
line := scanner.Text()

for marker, codeFragments := range codeFragmentsMap {
if strings.TrimSpace(line) == strings.TrimSpace(marker.String()) {
if marker.EqualsLine(line) {
for _, codeFragment := range codeFragments {
_, _ = out.WriteString(codeFragment) // bytes.Buffer.WriteString always returns nil errors
}
Expand Down
40 changes: 20 additions & 20 deletions pkg/plugins/internal/machinery/scaffold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,25 +206,25 @@ var _ = Describe("Scaffold", func() {
},
Entry("should insert lines for go files",
`
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
`,
`
1
2
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
`,
fakeInserter{codeFragments: file.CodeFragmentsMap{
file.NewMarkerFor("file.go", "-"): {"1\n", "2\n"}},
},
),
Entry("should insert lines for yaml files",
`
# +kubebuilder:scaffold:-
#+kubebuilder:scaffold:-
`,
`
1
2
# +kubebuilder:scaffold:-
#+kubebuilder:scaffold:-
`,
fakeInserter{codeFragments: file.CodeFragmentsMap{
file.NewMarkerFor("file.yaml", "-"): {"1\n", "2\n"}},
Expand All @@ -235,10 +235,10 @@ var _ = Describe("Scaffold", func() {
`
1
2
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
`,
fakeTemplate{fakeBuilder: fakeBuilder{ifExistsAction: file.Overwrite}, body: `
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
`},
fakeInserter{codeFragments: file.CodeFragmentsMap{
file.NewMarkerFor("file.go", "-"): {"1\n", "2\n"}},
Expand All @@ -249,23 +249,23 @@ var _ = Describe("Scaffold", func() {
`
1
2
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
`,
fakeTemplate{fakeBuilder: fakeBuilder{ifExistsAction: file.Overwrite}, body: `
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
`},
fakeInserter{codeFragments: file.CodeFragmentsMap{
file.NewMarkerFor("file.go", "-"): {"1\n", "2\n"}},
},
),
Entry("should use files over optional models",
`
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
`,
`
1
2
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
`,
fakeTemplate{body: fileContent},
fakeInserter{
Expand All @@ -276,14 +276,14 @@ var _ = Describe("Scaffold", func() {
),
Entry("should filter invalid markers",
`
// +kubebuilder:scaffold:-
// +kubebuilder:scaffold:*
//+kubebuilder:scaffold:-
//+kubebuilder:scaffold:*
`,
`
1
2
// +kubebuilder:scaffold:-
// +kubebuilder:scaffold:*
//+kubebuilder:scaffold:-
//+kubebuilder:scaffold:*
`,
fakeInserter{
markers: []file.Marker{file.NewMarkerFor("file.go", "-")},
Expand All @@ -296,18 +296,18 @@ var _ = Describe("Scaffold", func() {
Entry("should filter already existing one-line code fragments",
`
1
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
3
4
// +kubebuilder:scaffold:*
//+kubebuilder:scaffold:*
`,
`
1
2
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
3
4
// +kubebuilder:scaffold:*
//+kubebuilder:scaffold:*
`,
fakeInserter{
codeFragments: file.CodeFragmentsMap{
Expand All @@ -319,10 +319,10 @@ var _ = Describe("Scaffold", func() {
Entry("should not insert anything if no code fragment",
"", // input is provided through a template as mock fs doesn't copy it to the output buffer if no-op
`
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
`,
fakeTemplate{body: `
// +kubebuilder:scaffold:-
//+kubebuilder:scaffold:-
`},
fakeInserter{
codeFragments: file.CodeFragmentsMap{
Expand Down
4 changes: 2 additions & 2 deletions plugins/addon/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ type {{ .Resource.Kind }}Reconciler struct {
declarative.Reconciler
}

// +kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }}/status,verbs=get;update;patch
//+kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }},verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups={{ .Resource.Domain }},resources={{ .Resource.Plural }}/status,verbs=get;update;patch

func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) error {
addon.Init()
Expand Down
10 changes: 5 additions & 5 deletions plugins/addon/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ type {{.Resource.Kind}}Status struct {
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
{{ if not .Resource.Namespaced }} // +kubebuilder:resource:scope=Cluster {{ end }}
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
{{ if not .Resource.Namespaced }} //+kubebuilder:resource:scope=Cluster {{ end }}

// {{.Resource.Kind}} is the Schema for the {{ .Resource.Plural }} API
type {{.Resource.Kind}} struct {
Expand Down Expand Up @@ -109,8 +109,8 @@ func (o *{{.Resource.Kind}}) SetCommonStatus(s addonv1alpha1.CommonStatus) {
o.Status.CommonStatus = s
}

// +kubebuilder:object:root=true
{{ if not .Resource.Namespaced }} // +kubebuilder:resource:scope=Cluster {{ end }}
//+kubebuilder:object:root=true
{{ if not .Resource.Namespaced }} //+kubebuilder:resource:scope=Cluster {{ end }}

// {{.Resource.Kind}}List contains a list of {{.Resource.Kind}}
type {{.Resource.Kind}}List struct {
Expand Down
10 changes: 5 additions & 5 deletions testdata/project-v2-addon/api/v1/admiral_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ type AdmiralStatus struct {
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:scope=Cluster

// Admiral is the Schema for the admirals API
type Admiral struct {
Expand Down Expand Up @@ -76,8 +76,8 @@ func (o *Admiral) SetCommonStatus(s addonv1alpha1.CommonStatus) {
o.Status.CommonStatus = s
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:scope=Cluster
//+kubebuilder:object:root=true
//+kubebuilder:resource:scope=Cluster

// AdmiralList contains a list of Admiral
type AdmiralList struct {
Expand Down
6 changes: 3 additions & 3 deletions testdata/project-v2-addon/api/v1/captain_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ type CaptainStatus struct {
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// Captain is the Schema for the captains API
type Captain struct {
Expand Down Expand Up @@ -75,7 +75,7 @@ func (o *Captain) SetCommonStatus(s addonv1alpha1.CommonStatus) {
o.Status.CommonStatus = s
}

// +kubebuilder:object:root=true
//+kubebuilder:object:root=true

// CaptainList contains a list of Captain
type CaptainList struct {
Expand Down
Loading