Skip to content

Commit

Permalink
Remove spaces from machine-readable comments (v3 only)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <[email protected]>
  • Loading branch information
Adirio committed Dec 16, 2020
1 parent bba9577 commit 3bbd8d2
Show file tree
Hide file tree
Showing 129 changed files with 365 additions and 360 deletions.
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": "#",
// 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(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 @@ -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 @@ -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
6 changes: 3 additions & 3 deletions testdata/project-v2-addon/api/v1/firstmate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ type FirstMateStatus 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

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

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

// FirstMateList contains a list of FirstMate
type FirstMateList struct {
Expand Down
4 changes: 2 additions & 2 deletions testdata/project-v2-addon/api/v1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ limitations under the License.
*/

// Package v1 contains API Schema definitions for the crew v1 API group
// +kubebuilder:object:generate=true
// +groupName=crew.testproject.org
//+kubebuilder:object:generate=true
//+groupName=crew.testproject.org
package v1

import (
Expand Down
4 changes: 2 additions & 2 deletions testdata/project-v2-addon/controllers/admiral_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type AdmiralReconciler struct {
declarative.Reconciler
}

// +kubebuilder:rbac:groups=crew.testproject.org,resources=admirals,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=crew.testproject.org,resources=admirals/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=crew.testproject.org,resources=admirals,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=crew.testproject.org,resources=admirals/status,verbs=get;update;patch

func (r *AdmiralReconciler) SetupWithManager(mgr ctrl.Manager) error {
addon.Init()
Expand Down
4 changes: 2 additions & 2 deletions testdata/project-v2-addon/controllers/captain_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type CaptainReconciler struct {
declarative.Reconciler
}

// +kubebuilder:rbac:groups=crew.testproject.org,resources=captains,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=crew.testproject.org,resources=captains/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=crew.testproject.org,resources=captains,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=crew.testproject.org,resources=captains/status,verbs=get;update;patch

func (r *CaptainReconciler) SetupWithManager(mgr ctrl.Manager) error {
addon.Init()
Expand Down
4 changes: 2 additions & 2 deletions testdata/project-v2-addon/controllers/firstmate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type FirstMateReconciler struct {
declarative.Reconciler
}

// +kubebuilder:rbac:groups=crew.testproject.org,resources=firstmates,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=crew.testproject.org,resources=firstmates/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=crew.testproject.org,resources=firstmates,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=crew.testproject.org,resources=firstmates/status,verbs=get;update;patch

func (r *FirstMateReconciler) SetupWithManager(mgr ctrl.Manager) error {
addon.Init()
Expand Down
Loading

0 comments on commit 3bbd8d2

Please sign in to comment.