From ed8438ec10a467d65ebdabd9785790783c229c78 Mon Sep 17 00:00:00 2001 From: Adrian Orive Date: Wed, 12 Feb 2020 14:22:18 +0100 Subject: [PATCH] Fix issues raised by Go Report Card (mainly comments and style related changes) Signed-off-by: Adrian Orive --- cmd/api.go | 2 +- cmd/edit.go | 2 +- cmd/init.go | 2 +- cmd/internal/config.go | 1 + cmd/internal/exec.go | 3 ++- cmd/internal/go_version.go | 1 + cmd/update.go | 2 +- cmd/version/version.go | 3 +++ cmd/webhook.go | 6 ++--- docs/book/utils/litgo/literate.go | 5 +++- docs/book/utils/markerdocs/html.go | 7 ++++++ docs/book/utils/markerdocs/main.go | 14 ++++++++--- docs/book/utils/plugin/input.go | 3 +++ internal/config/config.go | 7 +++--- pkg/model/config/config.go | 2 +- pkg/model/file/mixins.go | 1 + pkg/model/resource/options.go | 11 +++++---- pkg/model/resource/options_test.go | 2 +- pkg/model/resource/resource.go | 1 + pkg/model/universe.go | 23 ++++++++++--------- pkg/scaffold/api.go | 4 ++++ pkg/scaffold/edit.go | 4 ++++ pkg/scaffold/init.go | 22 ++++++++++-------- pkg/scaffold/interface.go | 2 ++ pkg/scaffold/internal/machinery/scaffold.go | 1 + .../internal/templates/v1/authproxyservice.go | 4 ++-- .../templates/v1/controller/controller.go | 2 +- .../templates/v1/controller/controllertest.go | 2 +- .../internal/templates/v1/crd/crd_sample.go | 2 +- pkg/scaffold/internal/templates/v1/crd/doc.go | 2 +- .../internal/templates/v1/crd/register.go | 2 +- .../internal/templates/v1/manager/webhook.go | 2 +- .../templates/v1/webhook/admissionhandler.go | 2 +- .../v2/certmanager/kustomizeconfig.go | 2 +- .../templates/v2/controller/controller.go | 2 +- .../v2/crd/enablecainjection_patch.go | 4 ++-- .../templates/v2/crd/kustomizeconfig.go | 2 +- .../internal/templates/v2/crd_editor_rbac.go | 2 +- .../internal/templates/v2/crd_viewer_rbac.go | 2 +- pkg/scaffold/internal/templates/v2/group.go | 2 +- pkg/scaffold/internal/templates/v2/main.go | 3 ++- .../internal/templates/v2/makefile.go | 2 +- .../v2/metricsauth/authproxyservice.go | 4 ++-- .../v2/metricsauth/clientclusterrole.go | 4 ++-- .../templates/v2/webhook/kustomization.go | 4 ++-- .../templates/v2/webhook/kustomizeconfig.go | 6 ++--- .../internal/templates/v2/webhook/service.go | 4 ++-- .../internal/templates/v2/webhook/webhook.go | 16 ++++++------- .../templates/v2/webhook_manager_patch.go | 6 ++--- pkg/scaffold/update.go | 4 ++++ pkg/scaffold/webhook.go | 5 ++++ plugins/addon/channel.go | 1 + plugins/addon/controller.go | 3 ++- plugins/addon/helpers.go | 5 +++- plugins/addon/manifest.go | 1 + plugins/addon/plugin.go | 2 ++ plugins/addon/type.go | 1 + test/e2e/utils/test_context.go | 6 +++-- 58 files changed, 156 insertions(+), 86 deletions(-) diff --git a/cmd/api.go b/cmd/api.go index 1150add4bf9..448a2fd788d 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -206,7 +206,7 @@ func (o *apiOptions) validate(c *config.Config) error { func (o *apiOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { // Load the boilerplate - bp, err := ioutil.ReadFile(filepath.Join("hack", "boilerplate.go.txt")) // nolint:gosec + bp, err := ioutil.ReadFile(filepath.Join("hack", "boilerplate.go.txt")) //nolint:gosec if err != nil { return nil, fmt.Errorf("unable to load boilerplate: %v", err) } diff --git a/cmd/edit.go b/cmd/edit.go index e0cf30fedf7..cd66d543f47 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -89,7 +89,7 @@ func (o *editOptions) validate(c *config.Config) error { return nil } -func (o *editOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { // nolint:unparam +func (o *editOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { //nolint:unparam return scaffold.NewEditScaffolder(c, o.multigroup), nil } diff --git a/cmd/init.go b/cmd/init.go index 089b3d3cd34..6a6bc3e0bf0 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -179,7 +179,7 @@ func (o *initOptions) validate(c *config.Config) error { return nil } -func (o *initOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { // nolint:unparam +func (o *initOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { //nolint:unparam return scaffold.NewInitScaffolder(c, o.license, o.owner), nil } diff --git a/cmd/internal/config.go b/cmd/internal/config.go index c2249b955b8..04313dd6ea6 100644 --- a/cmd/internal/config.go +++ b/cmd/internal/config.go @@ -43,6 +43,7 @@ func ConfiguredAndV1() bool { return projectConfig.IsV1() } +// PrintV1DeprecationWarning prints a deprecation warning if some v1 feature is used func PrintV1DeprecationWarning() { fmt.Printf(noticeColor, "[Deprecation Notice] The v1 projects are deprecated and will not be supported beyond "+ "Feb 1, 2020.\nSee how to upgrade your project to v2: https://book.kubebuilder.io/migration/guide.html\n") diff --git a/cmd/internal/exec.go b/cmd/internal/exec.go index 89affd5c241..56e3d0ed9a1 100644 --- a/cmd/internal/exec.go +++ b/cmd/internal/exec.go @@ -23,8 +23,9 @@ import ( "strings" ) +// RunCmd prints the provided message and command and then executes it binding stdout and stderr func RunCmd(msg, cmd string, args ...string) error { - c := exec.Command(cmd, args...) // #nolint:gosec + c := exec.Command(cmd, args...) //nolint:gosec c.Stdout = os.Stdout c.Stderr = os.Stderr fmt.Println(msg + ":\n$ " + strings.Join(c.Args, " ")) diff --git a/cmd/internal/go_version.go b/cmd/internal/go_version.go index aaa00723759..079685e327a 100644 --- a/cmd/internal/go_version.go +++ b/cmd/internal/go_version.go @@ -24,6 +24,7 @@ import ( "strings" ) +// ValidateGoVersion verifies that Go is installed and the current go version is supported by kubebuilder func ValidateGoVersion() error { err := fetchAndCheckGoVersion() if err != nil { diff --git a/cmd/update.go b/cmd/update.go index 9ca35873590..12a3f20fafd 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -81,7 +81,7 @@ func (o *updateOptions) validate(c *config.Config) error { return nil } -func (o *updateOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { // nolint:unparam +func (o *updateOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { //nolint:unparam return scaffold.NewUpdateScaffolder(&c.Config), nil } diff --git a/cmd/version/version.go b/cmd/version/version.go index bfbd872f124..ff879882d6e 100644 --- a/cmd/version/version.go +++ b/cmd/version/version.go @@ -34,6 +34,7 @@ var ( buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') ) +// Version contains all the information related to the CLI version type Version struct { KubeBuilderVersion string `json:"kubeBuilderVersion"` KubernetesVendor string `json:"kubernetesVendor"` @@ -54,10 +55,12 @@ func getVersion() Version { } } +// Print prints the CLI version func (v Version) Print() { fmt.Printf("Version: %#v\n", v) } +// NewVersionCmd creates a new command that prints the CLI version func NewVersionCmd() *cobra.Command { return &cobra.Command{ Use: "version", diff --git a/cmd/webhook.go b/cmd/webhook.go index 8e136e78a19..6a0af27d6fb 100644 --- a/cmd/webhook.go +++ b/cmd/webhook.go @@ -114,7 +114,7 @@ func (o *webhookV1Options) validate(c *config.Config) error { func (o *webhookV1Options) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { // Load the boilerplate - bp, err := ioutil.ReadFile(filepath.Join("hack", "boilerplate.go.txt")) // nolint:gosec + bp, err := ioutil.ReadFile(filepath.Join("hack", "boilerplate.go.txt")) //nolint:gosec if err != nil { return nil, fmt.Errorf("unable to load boilerplate: %v", err) } @@ -220,9 +220,9 @@ func (o *webhookV2Options) validate(c *config.Config) error { return nil } -func (o *webhookV2Options) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { // nolint:unparam +func (o *webhookV2Options) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { //nolint:unparam // Load the boilerplate - bp, err := ioutil.ReadFile(filepath.Join("hack", "boilerplate.go.txt")) // nolint:gosec + bp, err := ioutil.ReadFile(filepath.Join("hack", "boilerplate.go.txt")) //nolint:gosec if err != nil { return nil, fmt.Errorf("unable to load boilerplate: %v", err) } diff --git a/docs/book/utils/litgo/literate.go b/docs/book/utils/litgo/literate.go index 056f9639ec3..6ba26b5ef7a 100644 --- a/docs/book/utils/litgo/literate.go +++ b/docs/book/utils/litgo/literate.go @@ -45,7 +45,10 @@ type Literate struct { BaseSourcePath *url.URL } -func (_ Literate) SupportsOutput(_ string) bool { return true } +// SupportsOutput implements plugin.Plugin +func (Literate) SupportsOutput(_ string) bool { return true } + +// Process implements plugin.Plugin func (l Literate) Process(input *plugin.Input) error { bookSrcDir := filepath.Join(input.Context.Root, input.Context.Config.Book.Src) return plugin.EachCommand(&input.Book, "literatego", func(chapter *plugin.BookChapter, relPath string) (string, error) { diff --git a/docs/book/utils/markerdocs/html.go b/docs/book/utils/markerdocs/html.go index 8be553ec66e..f06d45fc16f 100644 --- a/docs/book/utils/markerdocs/html.go +++ b/docs/book/utils/markerdocs/html.go @@ -39,6 +39,7 @@ type toHTML interface { // Text is a chunk of text in an HTML doc. type Text string +// WriteHTML writes the string as HTML to the given Writer func (t Text) WriteHTML(w io.Writer) error { _, err := io.WriteString(w, html.EscapeString(string(t))) return err @@ -51,6 +52,7 @@ type Tag struct { Children []toHTML } +// WriteHTML writes the tag as HTML to the given Writer func (t Tag) WriteHTML(w io.Writer) error { attrsOut := "" if t.Attrs != nil { @@ -76,6 +78,7 @@ func (t Tag) WriteHTML(w io.Writer) error { // Fragment is some series of tags, text, etc in an HTML doc. type Fragment []toHTML +// WriteHTML writes the fragment as HTML to the given Writer func (f Fragment) WriteHTML(w io.Writer) error { for _, item := range f { if err := item.WriteHTML(w); err != nil { @@ -94,11 +97,13 @@ type Attrs interface { // classes sets the class attribute to these class names. type classes []string +// ToAttrs implements Attrs func (c classes) ToAttrs() string { return fmt.Sprintf("class=%q", strings.Join(c, " ")) } // optionalClasses sets the the class attribute to these class names, if their values are true. type optionalClasses map[string]bool +// ToAttrs implements Attrs func (c optionalClasses) ToAttrs() string { actualClasses := make([]string, 0, len(c)) for class, active := range c { @@ -112,6 +117,7 @@ func (c optionalClasses) ToAttrs() string { // attrs joins together one or more Attrs. type attrs []Attrs +// ToAttrs implements Attrs func (a attrs) ToAttrs() string { parts := make([]string, len(a)) for i, attr := range a { @@ -126,6 +132,7 @@ type dataAttr struct { Value string } +// ToAttrs implements Attrs func (d dataAttr) ToAttrs() string { return fmt.Sprintf("data-%s=%q", d.Name, d.Value) } diff --git a/docs/book/utils/markerdocs/main.go b/docs/book/utils/markerdocs/main.go index 5eb87622aae..9a9332b102c 100644 --- a/docs/book/utils/markerdocs/main.go +++ b/docs/book/utils/markerdocs/main.go @@ -121,7 +121,10 @@ type MarkerDocs struct { Args map[string][]string } -func (_ MarkerDocs) SupportsOutput(_ string) bool { return true } +// SupportsOutput implements plugin.Plugin +func (MarkerDocs) SupportsOutput(_ string) bool { return true } + +// Process implements plugin.Plugin func (p MarkerDocs) Process(input *plugin.Input) error { markerDocs, err := p.getMarkerDocs() if err != nil { @@ -161,7 +164,10 @@ func (p MarkerDocs) Process(input *plugin.Input) error { content := new(strings.Builder) // NB(directxman12): wrap this in a div to prevent the markdown processor from inserting extra paragraphs - fmt.Fprintf(content, "
", categoryAlias) + _, err := fmt.Fprintf(content, "
", categoryAlias) + if err != nil { + return "", fmt.Errorf("unable to render marker documentation summary: %v", err) + } // write the markers for _, marker := range markers { @@ -170,7 +176,9 @@ func (p MarkerDocs) Process(input *plugin.Input) error { } } - fmt.Fprintf(content, "
") + if _, err = fmt.Fprintf(content, "
"); err != nil { + return "", fmt.Errorf("unable to render marker documentation: %v", err) + } usedCategories[category] = struct{}{} diff --git a/docs/book/utils/plugin/input.go b/docs/book/utils/plugin/input.go index 6bf3c9b8466..13d1979ea96 100644 --- a/docs/book/utils/plugin/input.go +++ b/docs/book/utils/plugin/input.go @@ -54,6 +54,7 @@ type BookSection struct { // according to what serde produces/expects. type BookItem bookItem +// UnmarshalJSON implements encoding/json.Unmarshaler func (b *BookItem) UnmarshalJSON(input []byte) error { // match how serde serializes rust enums. if input[0] == '"' { @@ -79,6 +80,7 @@ func (b *BookItem) UnmarshalJSON(input []byte) error { return nil } +// MarshalJSON implements encoding/json.Marshaler func (b BookItem) MarshalJSON() ([]byte, error) { if b.Separator { return json.Marshal("Separator") @@ -114,6 +116,7 @@ type Input struct { Book Book } +// UnmarshalJSON implements encoding/json.Unmarshaler func (p *Input) UnmarshalJSON(input []byte) error { // deserialize from the JSON equivalent to the Rust tuple // `(context, book)` diff --git a/internal/config/config.go b/internal/config/config.go index f231afe6692..ca4b67267bb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -27,10 +27,10 @@ import ( ) const ( - // Default path for the configuration file + // DefaultPath is the default path for the configuration file DefaultPath = "PROJECT" - // Default version if flag not provided + // DefaultVersion is the version which will be used when the version flag is not provided DefaultVersion = config.Version2 ) @@ -52,7 +52,7 @@ func exists(path string) (bool, error) { func readFrom(path string) (c config.Config, err error) { // Read the file - in, err := ioutil.ReadFile(path) // nolint: gosec + in, err := ioutil.ReadFile(path) //nolint:gosec if err != nil { return } @@ -152,6 +152,7 @@ func (c Config) Save() error { return nil } +// Path returns the path for configuration file func (c Config) Path() string { return c.path } diff --git a/pkg/model/config/config.go b/pkg/model/config/config.go index 4cba560b368..71ce159ab08 100644 --- a/pkg/model/config/config.go +++ b/pkg/model/config/config.go @@ -16,8 +16,8 @@ limitations under the License. package config +// Scaffolding versions const ( - // Scaffolding versions Version1 = "1" Version2 = "2" ) diff --git a/pkg/model/file/mixins.go b/pkg/model/file/mixins.go index 64bf886c59d..c3b6243491c 100644 --- a/pkg/model/file/mixins.go +++ b/pkg/model/file/mixins.go @@ -51,6 +51,7 @@ type TemplateMixin struct { TemplateBody string } +// GetBody implements Template func (t *TemplateMixin) GetBody() string { return t.TemplateBody } diff --git a/pkg/model/resource/options.go b/pkg/model/resource/options.go index 34ca15ab338..a890a0eb740 100644 --- a/pkg/model/resource/options.go +++ b/pkg/model/resource/options.go @@ -90,6 +90,7 @@ type Options struct { CreateExampleReconcileBody bool } +// Validate verifies that all the fields have valid values func (opts *Options) Validate() error { // Check that the required flags did not get a flag as their value // We can safely look for a '-' as the first char as none of the fields accepts it @@ -122,21 +123,20 @@ func (opts *Options) Validate() error { // Check if the version follows the valid pattern if !versionRegex.MatchString(opts.Version) { - return fmt.Errorf( - "version must match %s (was %s)", versionPattern, opts.Version) + return fmt.Errorf("version must match %s (was %s)", versionPattern, opts.Version) } validationErrors := []string{} // require Kind to start with an uppercase character if string(opts.Kind[0]) == strings.ToLower(string(opts.Kind[0])) { - validationErrors = append(validationErrors, "Kind must start with an uppercase character") + validationErrors = append(validationErrors, "kind must start with an uppercase character") } validationErrors = append(validationErrors, isDNS1035Label(strings.ToLower(opts.Kind))...) if len(validationErrors) != 0 { - return fmt.Errorf("Invalid Kind: %#v", validationErrors) + return fmt.Errorf("invalid Kind: %#v", validationErrors) } // TODO: validate plural strings if provided @@ -144,6 +144,7 @@ func (opts *Options) Validate() error { return nil } +// GVK returns the group-version-kind information to check against tracked resources in the configuration file func (opts *Options) GVK() config.GVK { return config.GVK{ Group: opts.Group, @@ -163,6 +164,7 @@ func (opts *Options) safeImport(unsafe string) string { return safe } +// NewV1Resource creates a new resource from the options specific to v1 func (opts *Options) NewV1Resource(c *config.Config, doResource bool) *Resource { res := opts.newResource() @@ -199,6 +201,7 @@ func (opts *Options) NewV1Resource(c *config.Config, doResource bool) *Resource return res } +// NewResource creates a new resource from the options func (opts *Options) NewResource(c *config.Config, doResource bool) *Resource { res := opts.newResource() diff --git a/pkg/model/resource/options_test.go b/pkg/model/resource/options_test.go index 59c48a79012..494652f5ea4 100644 --- a/pkg/model/resource/options_test.go +++ b/pkg/model/resource/options_test.go @@ -107,7 +107,7 @@ var _ = Describe("Resource Options", func() { It("should fail if Kind starts with a lowercase character", func() { options := &Options{Group: "crew", Kind: "lOWERCASESTART", Version: "v1"} err := options.Validate() - Expect(err).To(MatchError(ContainSubstring("Kind must start with an uppercase character"))) + Expect(err).To(MatchError(ContainSubstring("kind must start with an uppercase character"))) }) }) }) diff --git a/pkg/model/resource/resource.go b/pkg/model/resource/resource.go index 1dcefb48182..204e58f04da 100644 --- a/pkg/model/resource/resource.go +++ b/pkg/model/resource/resource.go @@ -54,6 +54,7 @@ type Resource struct { CreateExampleReconcileBody bool `json:"-"` } +// GVK returns the group-version-kind information to check against tracked resources in the configuration file func (r *Resource) GVK() config.GVK { return config.GVK{ Group: r.Group, diff --git a/pkg/model/universe.go b/pkg/model/universe.go index cf2878e5a95..65db8b44839 100644 --- a/pkg/model/universe.go +++ b/pkg/model/universe.go @@ -78,27 +78,28 @@ func WithResource(resource *resource.Resource) UniverseOption { } } -func (u Universe) InjectInto(t file.Builder) { +// InjectInto injects fields from the universe into the builder +func (u Universe) InjectInto(builder file.Builder) { // Inject project configuration if u.Config != nil { - if templateWithDomain, hasDomain := t.(file.HasDomain); hasDomain { - templateWithDomain.InjectDomain(u.Config.Domain) + if builderWithDomain, hasDomain := builder.(file.HasDomain); hasDomain { + builderWithDomain.InjectDomain(u.Config.Domain) } - if templateWithRepository, hasRepository := t.(file.HasRepository); hasRepository { - templateWithRepository.InjectRepository(u.Config.Repo) + if builderWithRepository, hasRepository := builder.(file.HasRepository); hasRepository { + builderWithRepository.InjectRepository(u.Config.Repo) } - if templateWithMultiGroup, hasMultiGroup := t.(file.HasMultiGroup); hasMultiGroup { - templateWithMultiGroup.InjectMultiGroup(u.Config.MultiGroup) + if builderWithMultiGroup, hasMultiGroup := builder.(file.HasMultiGroup); hasMultiGroup { + builderWithMultiGroup.InjectMultiGroup(u.Config.MultiGroup) } } // Inject boilerplate - if templateWithBoilerplate, hasBoilerplate := t.(file.HasBoilerplate); hasBoilerplate { - templateWithBoilerplate.InjectBoilerplate(u.Boilerplate) + if builderWithBoilerplate, hasBoilerplate := builder.(file.HasBoilerplate); hasBoilerplate { + builderWithBoilerplate.InjectBoilerplate(u.Boilerplate) } // Inject resource if u.Resource != nil { - if templateWithResource, hasResource := t.(file.HasResource); hasResource { - templateWithResource.InjectResource(u.Resource) + if builderWithResource, hasResource := builder.(file.HasResource); hasResource { + builderWithResource.InjectResource(u.Resource) } } } diff --git a/pkg/scaffold/api.go b/pkg/scaffold/api.go index eed382c9bc5..0738ab82c08 100644 --- a/pkg/scaffold/api.go +++ b/pkg/scaffold/api.go @@ -32,6 +32,8 @@ import ( crdv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2/crd" ) +var _ Scaffolder = &apiScaffolder{} + // apiScaffolder contains configuration for generating scaffolding for Go type // representing the API and controller that implements the behavior for the API. type apiScaffolder struct { @@ -46,6 +48,7 @@ type apiScaffolder struct { doController bool } +// NewAPIScaffolder returns a new Scaffolder for API/controller creation operations func NewAPIScaffolder( config *config.Config, boilerplate string, @@ -63,6 +66,7 @@ func NewAPIScaffolder( } } +// Scaffold implements Scaffolder func (s *apiScaffolder) Scaffold() error { fmt.Println("Writing scaffold for you to edit...") diff --git a/pkg/scaffold/edit.go b/pkg/scaffold/edit.go index e15a7f82db7..8e24857c469 100644 --- a/pkg/scaffold/edit.go +++ b/pkg/scaffold/edit.go @@ -20,11 +20,14 @@ import ( "sigs.k8s.io/kubebuilder/internal/config" ) +var _ Scaffolder = &editScaffolder{} + type editScaffolder struct { config *config.Config multigroup bool } +// NewEditScaffolder returns a new Scaffolder for configuration edit operations func NewEditScaffolder(config *config.Config, multigroup bool) Scaffolder { return &editScaffolder{ config: config, @@ -32,6 +35,7 @@ func NewEditScaffolder(config *config.Config, multigroup bool) Scaffolder { } } +// Scaffold implements Scaffolder func (s *editScaffolder) Scaffold() error { s.config.MultiGroup = s.multigroup diff --git a/pkg/scaffold/init.go b/pkg/scaffold/init.go index 8e1800f34b0..a520dc4ac26 100644 --- a/pkg/scaffold/init.go +++ b/pkg/scaffold/init.go @@ -36,14 +36,16 @@ import ( ) const ( - // controller runtime version to be used in the project + // ControllerRuntimeVersion is the kubernetes-sigs/controller-runtime version to be used in the project ControllerRuntimeVersion = "v0.4.0" - // ControllerTools version to be used in the project + // ControllerToolsVersion is the kubernetes-sigs/controller-tools version to be used in the project ControllerToolsVersion = "v0.2.4" - ImageName = "controller:latest" + imageName = "controller:latest" ) +var _ Scaffolder = &initScaffolder{} + type initScaffolder struct { config *config.Config boilerplatePath string @@ -51,6 +53,7 @@ type initScaffolder struct { owner string } +// NewInitScaffolder returns a new Scaffolder for project initialization operations func NewInitScaffolder(config *config.Config, license, owner string) Scaffolder { return &initScaffolder{ config: config, @@ -67,6 +70,7 @@ func (s *initScaffolder) newUniverse(boilerplate string) *model.Universe { ) } +// Scaffold implements Scaffolder func (s *initScaffolder) Scaffold() error { fmt.Println("Writing scaffold for you to edit...") @@ -96,7 +100,7 @@ func (s *initScaffolder) scaffoldV1() error { return err } - boilerplate, err := ioutil.ReadFile(s.boilerplatePath) // nolint:gosec + boilerplate, err := ioutil.ReadFile(s.boilerplatePath) //nolint:gosec if err != nil { return err } @@ -111,8 +115,8 @@ func (s *initScaffolder) scaffoldV1() error { &metricsauthv1.KustomizePrometheusMetricsPatch{}, &metricsauthv1.KustomizeAuthProxyPatch{}, &templatesv1.AuthProxyService{}, - &managerv1.Config{Image: ImageName}, - &templatesv1.Makefile{Image: ImageName}, + &managerv1.Config{Image: imageName}, + &templatesv1.Makefile{Image: imageName}, &templatesv1.GopkgToml{}, &managerv1.Dockerfile{}, &templatesv1.Kustomize{}, @@ -136,7 +140,7 @@ func (s *initScaffolder) scaffoldV2() error { return err } - boilerplate, err := ioutil.ReadFile(s.boilerplatePath) // nolint:gosec + boilerplate, err := ioutil.ReadFile(s.boilerplatePath) //nolint:gosec if err != nil { return err } @@ -149,11 +153,11 @@ func (s *initScaffolder) scaffoldV2() error { &metricsauthv2.AuthProxyPatch{}, &metricsauthv2.AuthProxyService{}, &metricsauthv2.ClientClusterRole{}, - &managerv2.Config{Image: ImageName}, + &managerv2.Config{Image: imageName}, &templatesv2.Main{}, &templatesv2.GoMod{ControllerRuntimeVersion: ControllerRuntimeVersion}, &templatesv2.Makefile{ - Image: ImageName, + Image: imageName, BoilerplatePath: s.boilerplatePath, ControllerToolsVersion: ControllerToolsVersion, }, diff --git a/pkg/scaffold/interface.go b/pkg/scaffold/interface.go index 9dabb5a2cd8..1d6498322cd 100644 --- a/pkg/scaffold/interface.go +++ b/pkg/scaffold/interface.go @@ -16,6 +16,8 @@ limitations under the License. package scaffold +// Scaffolder interface creates files to set up a controller manager type Scaffolder interface { + // Scaffold performs the scaffolding Scaffold() error } diff --git a/pkg/scaffold/internal/machinery/scaffold.go b/pkg/scaffold/internal/machinery/scaffold.go index 22cc377399e..11b7f3db57b 100644 --- a/pkg/scaffold/internal/machinery/scaffold.go +++ b/pkg/scaffold/internal/machinery/scaffold.go @@ -54,6 +54,7 @@ type scaffold struct { fs filesystem.FileSystem } +// NewScaffold returns a new Scaffold with the provided plugins func NewScaffold(plugins ...model.Plugin) Scaffold { return &scaffold{ plugins: plugins, diff --git a/pkg/scaffold/internal/templates/v1/authproxyservice.go b/pkg/scaffold/internal/templates/v1/authproxyservice.go index 79c0778785f..3a9b914d899 100644 --- a/pkg/scaffold/internal/templates/v1/authproxyservice.go +++ b/pkg/scaffold/internal/templates/v1/authproxyservice.go @@ -35,12 +35,12 @@ func (f *AuthProxyService) SetTemplateDefaults() error { f.Path = filepath.Join("config", "rbac", "auth_proxy_service.yaml") } - f.TemplateBody = AuthProxyServiceTemplate + f.TemplateBody = authProxyServiceTemplate return nil } -const AuthProxyServiceTemplate = `apiVersion: v1 +const authProxyServiceTemplate = `apiVersion: v1 kind: Service metadata: annotations: diff --git a/pkg/scaffold/internal/templates/v1/controller/controller.go b/pkg/scaffold/internal/templates/v1/controller/controller.go index 5e85d18be4a..851e0f9aa0c 100644 --- a/pkg/scaffold/internal/templates/v1/controller/controller.go +++ b/pkg/scaffold/internal/templates/v1/controller/controller.go @@ -47,7 +47,7 @@ func (f *Controller) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const controllerTemplate = `{{ .Boilerplate }} package {{ lower .Resource.Kind }} diff --git a/pkg/scaffold/internal/templates/v1/controller/controllertest.go b/pkg/scaffold/internal/templates/v1/controller/controllertest.go index 59401d0f88c..415925ab622 100644 --- a/pkg/scaffold/internal/templates/v1/controller/controllertest.go +++ b/pkg/scaffold/internal/templates/v1/controller/controllertest.go @@ -46,7 +46,7 @@ func (f *Test) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const controllerTestTemplate = `{{ .Boilerplate }} package {{ lower .Resource.Kind }} diff --git a/pkg/scaffold/internal/templates/v1/crd/crd_sample.go b/pkg/scaffold/internal/templates/v1/crd/crd_sample.go index e154f5d8662..5f035685fde 100644 --- a/pkg/scaffold/internal/templates/v1/crd/crd_sample.go +++ b/pkg/scaffold/internal/templates/v1/crd/crd_sample.go @@ -27,7 +27,7 @@ import ( var _ file.Template = &CRDSample{} // CRDSample scaffolds a manifest for CRD sample. -// nolint:golint +//nolint:golint type CRDSample struct { file.TemplateMixin file.ResourceMixin diff --git a/pkg/scaffold/internal/templates/v1/crd/doc.go b/pkg/scaffold/internal/templates/v1/crd/doc.go index bee1cc05cd0..da143244fc1 100644 --- a/pkg/scaffold/internal/templates/v1/crd/doc.go +++ b/pkg/scaffold/internal/templates/v1/crd/doc.go @@ -43,7 +43,7 @@ func (f *Doc) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const docGoTemplate = `{{ .Boilerplate }} // Package {{ .Resource.Version }} contains API Schema definitions for the {{ .Resource.Group }} {{ .Resource.Version }} API group diff --git a/pkg/scaffold/internal/templates/v1/crd/register.go b/pkg/scaffold/internal/templates/v1/crd/register.go index 25b4effaecb..1dc5f0a0f35 100644 --- a/pkg/scaffold/internal/templates/v1/crd/register.go +++ b/pkg/scaffold/internal/templates/v1/crd/register.go @@ -43,7 +43,7 @@ func (f *Register) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const registerTemplate = `{{ .Boilerplate }} // NOTE: Boilerplate only. Ignore this file. diff --git a/pkg/scaffold/internal/templates/v1/manager/webhook.go b/pkg/scaffold/internal/templates/v1/manager/webhook.go index 815f023d19c..bef81f89536 100644 --- a/pkg/scaffold/internal/templates/v1/manager/webhook.go +++ b/pkg/scaffold/internal/templates/v1/manager/webhook.go @@ -41,7 +41,7 @@ func (f *Webhook) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const webhookTemplate = `{{ .Boilerplate }} package webhook diff --git a/pkg/scaffold/internal/templates/v1/webhook/admissionhandler.go b/pkg/scaffold/internal/templates/v1/webhook/admissionhandler.go index d7a75e3492b..128a5531193 100644 --- a/pkg/scaffold/internal/templates/v1/webhook/admissionhandler.go +++ b/pkg/scaffold/internal/templates/v1/webhook/admissionhandler.go @@ -70,7 +70,7 @@ func (f *AdmissionHandler) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const addAdmissionHandlerTemplate = `{{ .Boilerplate }} package {{ .Type }} diff --git a/pkg/scaffold/internal/templates/v2/certmanager/kustomizeconfig.go b/pkg/scaffold/internal/templates/v2/certmanager/kustomizeconfig.go index e33219b744d..acbd30d3cda 100644 --- a/pkg/scaffold/internal/templates/v2/certmanager/kustomizeconfig.go +++ b/pkg/scaffold/internal/templates/v2/certmanager/kustomizeconfig.go @@ -40,7 +40,7 @@ func (f *KustomizeConfig) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const kustomizeConfigTemplate = `# This configuration is for teaching kustomize how to update name ref and var substitution nameReference: - kind: Issuer diff --git a/pkg/scaffold/internal/templates/v2/controller/controller.go b/pkg/scaffold/internal/templates/v2/controller/controller.go index ac833bc55f5..7b6b7815b2f 100644 --- a/pkg/scaffold/internal/templates/v2/controller/controller.go +++ b/pkg/scaffold/internal/templates/v2/controller/controller.go @@ -52,7 +52,7 @@ func (f *Controller) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const controllerTemplate = `{{ .Boilerplate }} package controllers diff --git a/pkg/scaffold/internal/templates/v2/crd/enablecainjection_patch.go b/pkg/scaffold/internal/templates/v2/crd/enablecainjection_patch.go index 225c793aab9..d74d3a1e58f 100644 --- a/pkg/scaffold/internal/templates/v2/crd/enablecainjection_patch.go +++ b/pkg/scaffold/internal/templates/v2/crd/enablecainjection_patch.go @@ -42,12 +42,12 @@ func (f *EnableCAInjectionPatch) SetTemplateDefaults() error { fmt.Sprintf("cainjection_in_%s.yaml", plural)) } - f.TemplateBody = EnableCAInjectionPatchTemplate + f.TemplateBody = enableCAInjectionPatchTemplate return nil } -const EnableCAInjectionPatchTemplate = `# The following patch adds a directive for certmanager to inject CA into the CRD +const enableCAInjectionPatchTemplate = `# The following patch adds a directive for certmanager to inject CA into the CRD # CRD conversion requires k8s 1.13 or later. apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition diff --git a/pkg/scaffold/internal/templates/v2/crd/kustomizeconfig.go b/pkg/scaffold/internal/templates/v2/crd/kustomizeconfig.go index 94d5d9bbc60..92aa2563713 100644 --- a/pkg/scaffold/internal/templates/v2/crd/kustomizeconfig.go +++ b/pkg/scaffold/internal/templates/v2/crd/kustomizeconfig.go @@ -40,7 +40,7 @@ func (f *KustomizeConfig) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const kustomizeConfigTemplate = `# This file is for teaching kustomize how to substitute name and namespace reference in CRD nameReference: - kind: Service diff --git a/pkg/scaffold/internal/templates/v2/crd_editor_rbac.go b/pkg/scaffold/internal/templates/v2/crd_editor_rbac.go index 9dd5e237587..1da46789e6e 100644 --- a/pkg/scaffold/internal/templates/v2/crd_editor_rbac.go +++ b/pkg/scaffold/internal/templates/v2/crd_editor_rbac.go @@ -26,7 +26,7 @@ import ( var _ file.Template = &CRDEditorRole{} -// CRD Editor role scaffolds the config/rbca/_editor_role.yaml +// CRDEditorRole scaffolds the config/rbac/_editor_role.yaml type CRDEditorRole struct { file.TemplateMixin file.ResourceMixin diff --git a/pkg/scaffold/internal/templates/v2/crd_viewer_rbac.go b/pkg/scaffold/internal/templates/v2/crd_viewer_rbac.go index 7c5c8d9c76f..2c280afb544 100644 --- a/pkg/scaffold/internal/templates/v2/crd_viewer_rbac.go +++ b/pkg/scaffold/internal/templates/v2/crd_viewer_rbac.go @@ -26,7 +26,7 @@ import ( var _ file.Template = &CRDViewerRole{} -// CRD Viewer role scaffolds the config/rbca/_viewer_role.yaml +// CRDViewerRole scaffolds the config/rbac/_viewer_role.yaml type CRDViewerRole struct { file.TemplateMixin file.ResourceMixin diff --git a/pkg/scaffold/internal/templates/v2/group.go b/pkg/scaffold/internal/templates/v2/group.go index 6bf529be3dd..199b41216b3 100644 --- a/pkg/scaffold/internal/templates/v2/group.go +++ b/pkg/scaffold/internal/templates/v2/group.go @@ -47,7 +47,7 @@ func (f *Group) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const groupTemplate = `{{ .Boilerplate }} // Package {{ .Resource.Version }} contains API Schema definitions for the {{ .Resource.Group }} {{ .Resource.Version }} API group diff --git a/pkg/scaffold/internal/templates/v2/main.go b/pkg/scaffold/internal/templates/v2/main.go index f5443bcc343..8d923e1f89e 100644 --- a/pkg/scaffold/internal/templates/v2/main.go +++ b/pkg/scaffold/internal/templates/v2/main.go @@ -27,6 +27,7 @@ const defaultMainPath = "main.go" var _ file.Template = &Main{} +// Main scaffolds the controller manager entry point type Main struct { file.TemplateMixin file.BoilerplateMixin @@ -64,7 +65,7 @@ func (*MainUpdater) GetPath() string { return defaultMainPath } -// GetPath implements Builder +// GetIfExistsAction implements Builder func (*MainUpdater) GetIfExistsAction() file.IfExistsAction { return file.Overwrite } diff --git a/pkg/scaffold/internal/templates/v2/makefile.go b/pkg/scaffold/internal/templates/v2/makefile.go index e29f5bb03cb..fa699db5eee 100644 --- a/pkg/scaffold/internal/templates/v2/makefile.go +++ b/pkg/scaffold/internal/templates/v2/makefile.go @@ -51,7 +51,7 @@ func (f *Makefile) SetTemplateDefaults() error { return nil } -// nolint:lll +//nolint:lll const makefileTemplate = ` # Image URL to use all building/pushing image targets IMG ?= {{ .Image }} diff --git a/pkg/scaffold/internal/templates/v2/metricsauth/authproxyservice.go b/pkg/scaffold/internal/templates/v2/metricsauth/authproxyservice.go index f69b8993afa..0021a5072df 100644 --- a/pkg/scaffold/internal/templates/v2/metricsauth/authproxyservice.go +++ b/pkg/scaffold/internal/templates/v2/metricsauth/authproxyservice.go @@ -35,12 +35,12 @@ func (f *AuthProxyService) SetTemplateDefaults() error { f.Path = filepath.Join("config", "rbac", "auth_proxy_service.yaml") } - f.TemplateBody = AuthProxyServiceTemplate + f.TemplateBody = authProxyServiceTemplate return nil } -const AuthProxyServiceTemplate = `apiVersion: v1 +const authProxyServiceTemplate = `apiVersion: v1 kind: Service metadata: labels: diff --git a/pkg/scaffold/internal/templates/v2/metricsauth/clientclusterrole.go b/pkg/scaffold/internal/templates/v2/metricsauth/clientclusterrole.go index 396bb81615c..06de93bed3e 100644 --- a/pkg/scaffold/internal/templates/v2/metricsauth/clientclusterrole.go +++ b/pkg/scaffold/internal/templates/v2/metricsauth/clientclusterrole.go @@ -35,12 +35,12 @@ func (f *ClientClusterRole) SetTemplateDefaults() error { f.Path = filepath.Join("config", "rbac", "auth_proxy_client_clusterrole.yaml") } - f.TemplateBody = ClientClusterRoleTemplate + f.TemplateBody = clientClusterRoleTemplate return nil } -const ClientClusterRoleTemplate = `apiVersion: rbac.authorization.k8s.io/v1beta1 +const clientClusterRoleTemplate = `apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRole metadata: name: metrics-reader diff --git a/pkg/scaffold/internal/templates/v2/webhook/kustomization.go b/pkg/scaffold/internal/templates/v2/webhook/kustomization.go index 4118505c112..a65fe8ce172 100644 --- a/pkg/scaffold/internal/templates/v2/webhook/kustomization.go +++ b/pkg/scaffold/internal/templates/v2/webhook/kustomization.go @@ -35,14 +35,14 @@ func (f *Kustomization) SetTemplateDefaults() error { f.Path = filepath.Join("config", "webhook", "kustomization.yaml") } - f.TemplateBody = KustomizeWebhookTemplate + f.TemplateBody = kustomizeWebhookTemplate f.IfExistsAction = file.Error return nil } -const KustomizeWebhookTemplate = `resources: +const kustomizeWebhookTemplate = `resources: - manifests.yaml - service.yaml diff --git a/pkg/scaffold/internal/templates/v2/webhook/kustomizeconfig.go b/pkg/scaffold/internal/templates/v2/webhook/kustomizeconfig.go index 5b4001f313b..712b7595178 100644 --- a/pkg/scaffold/internal/templates/v2/webhook/kustomizeconfig.go +++ b/pkg/scaffold/internal/templates/v2/webhook/kustomizeconfig.go @@ -35,15 +35,15 @@ func (f *KustomizeConfigWebhook) SetTemplateDefaults() error { f.Path = filepath.Join("config", "webhook", "kustomizeconfig.yaml") } - f.TemplateBody = KustomizeConfigWebhookTemplate + f.TemplateBody = kustomizeConfigWebhookTemplate f.IfExistsAction = file.Error return nil } -// nolint:lll -const KustomizeConfigWebhookTemplate = `# the following config is for teaching kustomize where to look at when substituting vars. +//nolint:lll +const kustomizeConfigWebhookTemplate = `# the following config is for teaching kustomize where to look at when substituting vars. # It requires kustomize v2.1.0 or newer to work properly. nameReference: - kind: Service diff --git a/pkg/scaffold/internal/templates/v2/webhook/service.go b/pkg/scaffold/internal/templates/v2/webhook/service.go index 1e179c45c9c..1009a6367fe 100644 --- a/pkg/scaffold/internal/templates/v2/webhook/service.go +++ b/pkg/scaffold/internal/templates/v2/webhook/service.go @@ -35,14 +35,14 @@ func (f *Service) SetTemplateDefaults() error { f.Path = filepath.Join("config", "webhook", "service.yaml") } - f.TemplateBody = ServiceTemplate + f.TemplateBody = serviceTemplate f.IfExistsAction = file.Error return nil } -const ServiceTemplate = ` +const serviceTemplate = ` apiVersion: v1 kind: Service metadata: diff --git a/pkg/scaffold/internal/templates/v2/webhook/webhook.go b/pkg/scaffold/internal/templates/v2/webhook/webhook.go index 358471c90bd..8c3bf09cf20 100644 --- a/pkg/scaffold/internal/templates/v2/webhook/webhook.go +++ b/pkg/scaffold/internal/templates/v2/webhook/webhook.go @@ -54,12 +54,12 @@ func (f *Webhook) SetTemplateDefaults() error { } } - webhookTemplate := WebhookTemplate + webhookTemplate := webhookTemplate if f.Defaulting { - webhookTemplate = webhookTemplate + DefaultingWebhookTemplate + webhookTemplate = webhookTemplate + defaultingWebhookTemplate } if f.Validating { - webhookTemplate = webhookTemplate + ValidatingWebhookTemplate + webhookTemplate = webhookTemplate + validatingWebhookTemplate } f.TemplateBody = webhookTemplate @@ -71,7 +71,7 @@ func (f *Webhook) SetTemplateDefaults() error { } const ( - WebhookTemplate = `{{ .Boilerplate }} + webhookTemplate = `{{ .Boilerplate }} package {{ .Resource.Version }} @@ -96,8 +96,8 @@ func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error { // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! ` - // nolint:lll - DefaultingWebhookTemplate = ` + //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 var _ webhook.Defaulter = &{{ .Resource.Kind }}{} @@ -109,8 +109,8 @@ func (r *{{ .Resource.Kind }}) Default() { // TODO(user): fill in your defaulting logic. } ` - // nolint:lll - ValidatingWebhookTemplate = ` + //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 diff --git a/pkg/scaffold/internal/templates/v2/webhook_manager_patch.go b/pkg/scaffold/internal/templates/v2/webhook_manager_patch.go index fadc3a794f0..49930f567a1 100644 --- a/pkg/scaffold/internal/templates/v2/webhook_manager_patch.go +++ b/pkg/scaffold/internal/templates/v2/webhook_manager_patch.go @@ -24,7 +24,7 @@ import ( var _ file.Template = &ManagerWebhookPatch{} -// CRDWebhookPatch scaffolds a CRDWebhookPatch for a Resource +// ManagerWebhookPatch scaffolds a ManagerWebhookPatch for a Resource type ManagerWebhookPatch struct { file.TemplateMixin } @@ -35,12 +35,12 @@ func (f *ManagerWebhookPatch) SetTemplateDefaults() error { f.Path = filepath.Join("config", "default", "manager_webhook_patch.yaml") } - f.TemplateBody = ManagerWebhookPatchTemplate + f.TemplateBody = managerWebhookPatchTemplate return nil } -const ManagerWebhookPatchTemplate = `apiVersion: apps/v1 +const managerWebhookPatchTemplate = `apiVersion: apps/v1 kind: Deployment metadata: name: controller-manager diff --git a/pkg/scaffold/update.go b/pkg/scaffold/update.go index 65cd5082df3..309858d544b 100644 --- a/pkg/scaffold/update.go +++ b/pkg/scaffold/update.go @@ -23,16 +23,20 @@ import ( templatesv1 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v1" ) +var _ Scaffolder = &updateScaffolder{} + type updateScaffolder struct { config *config.Config } +// NewUpdateScaffolder returns a new Scaffolder for vendor update operations func NewUpdateScaffolder(config *config.Config) Scaffolder { return &updateScaffolder{ config: config, } } +// Scaffold implements Scaffolder func (s *updateScaffolder) Scaffold() error { return machinery.NewScaffold().Execute( model.NewUniverse( diff --git a/pkg/scaffold/webhook.go b/pkg/scaffold/webhook.go index 9ff6454d390..6e1ce8d875a 100644 --- a/pkg/scaffold/webhook.go +++ b/pkg/scaffold/webhook.go @@ -31,6 +31,8 @@ import ( webhookv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/internal/templates/v2/webhook" ) +var _ Scaffolder = &webhookScaffolder{} + type webhookScaffolder struct { config *config.Config boilerplate string @@ -43,6 +45,7 @@ type webhookScaffolder struct { defaulting, validation, conversion bool } +// NewV1WebhookScaffolder returns a new Scaffolder for v1 webhook creation operations func NewV1WebhookScaffolder( config *config.Config, boilerplate string, @@ -61,6 +64,7 @@ func NewV1WebhookScaffolder( } } +// NewV2WebhookScaffolder returns a new Scaffolder for v2 webhook creation operations func NewV2WebhookScaffolder( config *config.Config, boilerplate string, @@ -79,6 +83,7 @@ func NewV2WebhookScaffolder( } } +// Scaffold implements Scaffolder func (s *webhookScaffolder) Scaffold() error { fmt.Println("Writing scaffold for you to edit...") diff --git a/plugins/addon/channel.go b/plugins/addon/channel.go index 57d89290067..d89a9d84bd8 100644 --- a/plugins/addon/channel.go +++ b/plugins/addon/channel.go @@ -28,6 +28,7 @@ manifests: - version: 0.0.1 ` +// ExampleChannel adds a model file for the channel func ExampleChannel(u *model.Universe) error { m := &file.File{ Path: filepath.Join("channels", "stable"), diff --git a/plugins/addon/controller.go b/plugins/addon/controller.go index bb5d9ee47de..842942472dd 100644 --- a/plugins/addon/controller.go +++ b/plugins/addon/controller.go @@ -8,6 +8,7 @@ import ( "sigs.k8s.io/kubebuilder/pkg/model/file" ) +// ReplaceController replaces the controller with a modified version func ReplaceController(u *model.Universe) error { templateBody := controllerTemplate @@ -28,7 +29,7 @@ func ReplaceController(u *model.Universe) error { return nil } -// nolint:lll +//nolint:lll const controllerTemplate = `{{ .Boilerplate }} package controllers diff --git a/plugins/addon/helpers.go b/plugins/addon/helpers.go index 99034f81e3c..66a8c3f5189 100644 --- a/plugins/addon/helpers.go +++ b/plugins/addon/helpers.go @@ -16,6 +16,7 @@ import ( // plugins. Once we have validated they are used in more than one // place, we can promote them to a shared location. +// PluginFunc executes a step of Plugin type PluginFunc func(u *model.Universe) error // AddFile adds the specified file to the model. @@ -37,7 +38,7 @@ func AddFile(u *model.Universe, add *file.File) (bool, error) { } // ReplaceFileIfExists replaces the specified file in the model by path -// Returns true iff the file was replaced. +// Returns true if the file was replaced. func ReplaceFileIfExists(u *model.Universe, add *file.File) bool { p := add.Path if p == "" { @@ -62,6 +63,7 @@ func ReplaceFile(u *model.Universe, add *file.File) error { return nil } +// DefaultTemplateFunctions returns a map of template helpers func DefaultTemplateFunctions() template.FuncMap { return template.FuncMap{ "title": strings.Title, @@ -70,6 +72,7 @@ func DefaultTemplateFunctions() template.FuncMap { } } +// RunTemplate parses a template func RunTemplate(templateName, templateValue string, data interface{}, funcMap template.FuncMap) (string, error) { t, err := template.New(templateName).Funcs(funcMap).Parse(templateValue) if err != nil { diff --git a/plugins/addon/manifest.go b/plugins/addon/manifest.go index cc5d96988d9..6be87a092b6 100644 --- a/plugins/addon/manifest.go +++ b/plugins/addon/manifest.go @@ -29,6 +29,7 @@ const exampleManifestVersion = "0.0.1" const exampleManifestContents = `# Placeholder manifest - replace with the manifest for your addon ` +// ExampleManifest adds a model file for the manifest placeholder func ExampleManifest(u *model.Universe) error { packageName := getPackageName(u) diff --git a/plugins/addon/plugin.go b/plugins/addon/plugin.go index 75d3d2b960b..597419108a8 100644 --- a/plugins/addon/plugin.go +++ b/plugins/addon/plugin.go @@ -4,9 +4,11 @@ import ( "sigs.k8s.io/kubebuilder/pkg/model" ) +// Plugin implements model.Plugin type Plugin struct { } +// Pipe implements model.Plugin func (p *Plugin) Pipe(u *model.Universe) error { functions := []PluginFunc{ ExampleManifest, diff --git a/plugins/addon/type.go b/plugins/addon/type.go index 49f2b8549e5..84d1ecb33d3 100644 --- a/plugins/addon/type.go +++ b/plugins/addon/type.go @@ -9,6 +9,7 @@ import ( "sigs.k8s.io/kubebuilder/pkg/model/file" ) +// ReplaceTypes replaces the API types with a modified version func ReplaceTypes(u *model.Universe) error { funcs := DefaultTemplateFunctions() funcs["JSONTag"] = JSONTag diff --git a/test/e2e/utils/test_context.go b/test/e2e/utils/test_context.go index 6a9c39a136c..bfe7e996ec4 100644 --- a/test/e2e/utils/test_context.go +++ b/test/e2e/utils/test_context.go @@ -166,9 +166,9 @@ func (kc *KBTestContext) Make(makeOptions ...string) error { return err } -// CleanupImage is for cleaning up the docker images for testing +// Destroy is for cleaning up the docker images for testing func (kc *KBTestContext) Destroy() { - // nolint:gosec + //nolint:gosec cmd := exec.Command("docker", "rmi", "-f", kc.ImageName) if _, err := kc.Run(cmd); err != nil { fmt.Fprintf(GinkgoWriter, "warning: error when removing the local image: %v\n", err) @@ -186,6 +186,7 @@ func (kc *KBTestContext) LoadImageToKindCluster() error { return err } +// CmdContext provides context for command execution type CmdContext struct { // environment variables in k=v format. Env []string @@ -193,6 +194,7 @@ type CmdContext struct { Stdin io.Reader } +// Run executes the provided command within this context func (cc *CmdContext) Run(cmd *exec.Cmd) ([]byte, error) { cmd.Dir = cc.Dir cmd.Env = append(os.Environ(), cc.Env...)