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

🌱 Enable the lint to check exported methods and fix issues #4463

Merged
merged 1 commit into from
Jan 1, 2025
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
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ linters-settings:
- name: error-strings
- name: error-naming
- name: exported
disabled: true # TODO: Investigate if it should be enabled. Disabled for now due to many findings.
- name: if-return
- name: increment-decrement
- name: var-naming
Expand Down
15 changes: 7 additions & 8 deletions hack/docs/generate_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ package main

import (
log "github.com/sirupsen/logrus"

cronjob "sigs.k8s.io/kubebuilder/v4/hack/docs/internal/cronjob-tutorial"
gettingstarted "sigs.k8s.io/kubebuilder/v4/hack/docs/internal/getting-started"
multiversion "sigs.k8s.io/kubebuilder/v4/hack/docs/internal/multiversion-tutorial"
)

// Make sure executing `build_kb` to generate kb executable from the source code
// KubebuilderBinName make sure executing `build_kb` to generate kb executable from the source code
const KubebuilderBinName = "/tmp/kubebuilder/bin/kubebuilder"

type tutorialGenerator interface {
Expand All @@ -38,9 +37,9 @@ func main() {
type generator func()

tutorials := map[string]generator{
"cronjob": UpdateCronjobTutorial,
"getting-started": UpdateGettingStarted,
"multiversion": UpdateMultiversionTutorial,
"cronjob": updateCronjobTutorial,
"getting-started": updateGettingStarted,
"multiversion": updateMultiversionTutorial,
}

log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})
Expand All @@ -59,19 +58,19 @@ func updateTutorial(generator tutorialGenerator) {
generator.CodeGen()
}

func UpdateCronjobTutorial() {
func updateCronjobTutorial() {
samplePath := "docs/book/src/cronjob-tutorial/testdata/project/"
sp := cronjob.NewSample(KubebuilderBinName, samplePath)
updateTutorial(&sp)
}

func UpdateGettingStarted() {
func updateGettingStarted() {
samplePath := "docs/book/src/getting-started/testdata/project"
sp := gettingstarted.NewSample(KubebuilderBinName, samplePath)
updateTutorial(&sp)
}

func UpdateMultiversionTutorial() {
func updateMultiversionTutorial() {
samplePath := "docs/book/src/multiversion-tutorial/testdata/project"
sp := cronjob.NewSample(KubebuilderBinName, samplePath)
updateTutorial(&sp)
Expand Down
6 changes: 3 additions & 3 deletions hack/docs/internal/cronjob-tutorial/api_design.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

package cronjob

const CronjobSpecExplaination = `
const cronjobSpecExplaination = `

// +kubebuilder:docs-gen:collapse=Imports

Expand Down Expand Up @@ -49,7 +49,7 @@ const CronjobSpecExplaination = `
*/
`

const CronjobSpecStruct = `
const cronjobSpecStruct = `
// +kubebuilder:validation:MinLength=0

// The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
Expand Down Expand Up @@ -128,7 +128,7 @@ const (
serialization, as mentioned above.
*/`

const CronjobList = `
const cronjobList = `

// A list of pointers to currently running jobs.
// +optional
Expand Down
12 changes: 6 additions & 6 deletions hack/docs/internal/cronjob-tutorial/controller_implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ limitations under the License.

package cronjob

const ControllerIntro = `
const controllerIntro = `
// +kubebuilder:docs-gen:collapse=Apache License

/*
We'll start out with some imports. You'll see below that we'll need a few more imports
than those scaffolded for us. We'll talk about each one when we use it.
*/`

const ControllerImport = `import (
const controllerImport = `import (
"context"
"fmt"
"sort"
Expand All @@ -48,7 +48,7 @@ Next, we'll need a Clock, which will allow us to fake timing in our tests.
*/
`

const ControllerMockClock = `
const controllerMockClock = `
/*
We'll mock out the clock to make it easier to jump around in time while testing,
the "real" clock just calls` + " `" + `time.Now` + "`" + `.
Expand All @@ -72,7 +72,7 @@ a couple more [markers](/reference/markers/rbac.md).
*/
`

const ControllerReconcile = `
const controllerReconcile = `
// +kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=batch,resources=jobs/status,verbs=get

Expand All @@ -84,7 +84,7 @@ var (
)
`

const ControllerReconcileLogic = `log := log.FromContext(ctx)
const controllerReconcileLogic = `log := log.FromContext(ctx)

/*
### 1: Load the CronJob by name
Expand Down Expand Up @@ -533,7 +533,7 @@ var (
apiGVStr = batchv1.GroupVersion.String()
)
`
const ControllerSetupWithManager = `
const controllerSetupWithManager = `
// set up a real clock, since we're not in a test
if r.Clock == nil {
r.Clock = realClock{}
Expand Down
47 changes: 25 additions & 22 deletions hack/docs/internal/cronjob-tutorial/generate_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/afero"

hackutils "sigs.k8s.io/kubebuilder/v4/hack/docs/utils"
pluginutil "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
)

// Sample define the sample which will be scaffolded
type Sample struct {
ctx *utils.TestContext
}

// NewSample create a new instance of the cronjob sample and configure the KB CLI that will be used
func NewSample(binaryPath, samplePath string) Sample {
log.Infof("Generating the sample context of Cronjob...")
ctx := hackutils.NewSampleContext(binaryPath, samplePath, "GO111MODULE=on")
Expand All @@ -49,6 +50,7 @@ func (sp *Sample) Prepare() {
hackutils.CheckError("creating directory for sample project", err)
}

// GenerateSampleProject will generate the sample
func (sp *Sample) GenerateSampleProject() {
log.Infof("Initializing the cronjob project")

Expand Down Expand Up @@ -79,6 +81,7 @@ func (sp *Sample) GenerateSampleProject() {
hackutils.CheckError("Implementing admission webhook", err)
}

// UpdateTutorial the cronjob tutorial with the scaffold changes
func (sp *Sample) UpdateTutorial() {
log.Println("Update tutorial with cronjob code")
// 1. update specs
Expand Down Expand Up @@ -168,12 +171,12 @@ func (sp *Sample) updateSpec() {

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`to be serialized.`, CronjobSpecExplaination)
`to be serialized.`, cronjobSpecExplaination)
hackutils.CheckError("fixing cronjob_types.go", err)

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`type CronJobSpec struct {`, CronjobSpecStruct)
`type CronJobSpec struct {`, cronjobSpecStruct)
hackutils.CheckError("fixing cronjob_types.go", err)

err = pluginutil.ReplaceInFile(
Expand All @@ -187,7 +190,7 @@ func (sp *Sample) updateSpec() {

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/cronjob_types.go"),
`// Important: Run "make" to regenerate code after modifying this file`, CronjobList)
`// Important: Run "make" to regenerate code after modifying this file`, cronjobList)
hackutils.CheckError("fixing cronjob_types.go", err)

err = pluginutil.InsertCode(
Expand Down Expand Up @@ -229,13 +232,13 @@ func (sp *Sample) updateAPIStuff() {
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/groupversion_info.go"),
`limitations under the License.
*/`, GroupversionIntro)
*/`, groupVersionIntro)
hackutils.CheckError("fixing groupversion_info.go", err)

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "api/v1/groupversion_info.go"),
` "sigs.k8s.io/controller-runtime/pkg/scheme"
)`, GroupversionSchema)
)`, groupVersionSchema)
hackutils.CheckError("fixing groupversion_info.go", err)
}

Expand All @@ -244,7 +247,7 @@ func (sp *Sample) updateController() {
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`limitations under the License.
*/`, ControllerIntro)
*/`, controllerIntro)
hackutils.CheckError("fixing cronjob_controller.go", err)

err = pluginutil.ReplaceInFile(
Expand All @@ -258,7 +261,7 @@ func (sp *Sample) updateController() {
"sigs.k8s.io/controller-runtime/pkg/log"

batchv1 "tutorial.kubebuilder.io/project/api/v1"
)`, ControllerImport)
)`, controllerImport)
hackutils.CheckError("fixing cronjob_controller.go", err)

err = pluginutil.InsertCode(
Expand All @@ -270,12 +273,12 @@ func (sp *Sample) updateController() {
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
` Clock
}`, ControllerMockClock)
}`, controllerMockClock)
hackutils.CheckError("fixing cronjob_controller.go", err)

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`// +kubebuilder:rbac:groups=batch.tutorial.kubebuilder.io,resources=cronjobs/finalizers,verbs=update`, ControllerReconcile)
`// +kubebuilder:rbac:groups=batch.tutorial.kubebuilder.io,resources=cronjobs/finalizers,verbs=update`, controllerReconcile)
hackutils.CheckError("fixing cronjob_controller.go", err)

err = pluginutil.ReplaceInFile(
Expand All @@ -285,12 +288,12 @@ func (sp *Sample) updateController() {
// TODO(user): your logic here

return ctrl.Result{}, nil
}`, ControllerReconcileLogic)
}`, controllerReconcileLogic)
hackutils.CheckError("fixing cronjob_controller.go", err)

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller.go"),
`SetupWithManager(mgr ctrl.Manager) error {`, ControllerSetupWithManager)
`SetupWithManager(mgr ctrl.Manager) error {`, controllerSetupWithManager)
hackutils.CheckError("fixing cronjob_controller.go", err)

err = pluginutil.InsertCode(
Expand All @@ -313,7 +316,7 @@ func (sp *Sample) updateMain() {
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`// +kubebuilder:scaffold:imports
)`, MainBatch)
)`, mainBatch)
hackutils.CheckError("fixing main.go", err)

err = pluginutil.InsertCode(
Expand Down Expand Up @@ -347,7 +350,7 @@ CronJob controller's`+" `"+`SetupWithManager`+"`"+` method.
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
`setupLog.Error(err, "unable to create controller", "controller", "CronJob")
os.Exit(1)
}`, MainEnableWebhook)
}`, mainEnableWebhook)
hackutils.CheckError("fixing main.go", err)

err = pluginutil.InsertCode(
Expand Down Expand Up @@ -512,7 +515,7 @@ func (sp *Sample) updateSuiteTest() {
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`limitations under the License.
*/`, SuiteTestIntro)
*/`, suiteTestIntro)
hackutils.CheckError("updating suite_test.go to add license intro", err)

err = pluginutil.InsertCode(
Expand All @@ -534,13 +537,13 @@ var (
cfg *rest.Config
k8sClient client.Client
)
`, SuiteTestEnv)
`, suiteTestEnv)
hackutils.CheckError("updating suite_test.go to add more variables", err)

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`ctx, cancel = context.WithCancel(context.TODO())
`, SuiteTestReadCRD)
`, suiteTestReadCRD)
hackutils.CheckError("updating suite_test.go to add text about CRD", err)

err = pluginutil.InsertCode(
Expand All @@ -559,7 +562,7 @@ var (
Expect(err).NotTo(HaveOccurred())

// +kubebuilder:scaffold:scheme
`, SuiteTestAddSchema)
`, suiteTestAddSchema)
hackutils.CheckError("updating suite_test.go to add schema", err)

err = pluginutil.InsertCode(
Expand All @@ -568,7 +571,7 @@ var (
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
`, SuiteTestDescription)
`, suiteTestDescription)
hackutils.CheckError("updating suite_test.go for test description", err)

err = pluginutil.ReplaceInFile(
Expand All @@ -580,7 +583,7 @@ var _ = AfterSuite(func() {
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
`, SuiteTestCleanup)
`, suiteTestCleanup)
hackutils.CheckError("updating suite_test.go to cleanup tests", err)
}

Expand Down Expand Up @@ -623,7 +626,7 @@ func (sp *Sample) updateExample() {
// samples/batch_v1_cronjob
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "config/samples/batch_v1_cronjob.yaml"),
`spec:`, CronjobSample)
`spec:`, cronjobSample)
hackutils.CheckError("fixing samples/batch_v1_cronjob.yaml", err)

err = pluginutil.ReplaceInFile(
Expand All @@ -634,6 +637,6 @@ func (sp *Sample) updateExample() {

func (sp *Sample) addControllerTest() {
var fs = afero.NewOsFs()
err := afero.WriteFile(fs, filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller_test.go"), []byte(ControllerTest), 0600)
err := afero.WriteFile(fs, filepath.Join(sp.ctx.Dir, "internal/controller/cronjob_controller_test.go"), []byte(controllerTest), 0600)
hackutils.CheckError("adding cronjob_controller_test", err)
}
4 changes: 2 additions & 2 deletions hack/docs/internal/cronjob-tutorial/main_revisited.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

package cronjob

const MainBatch = `
const mainBatch = `
// +kubebuilder:docs-gen:collapse=Imports

/*
Expand All @@ -28,7 +28,7 @@ If we would be using any other CRD we would have to add their scheme the same wa
Builtin types such as Job have their scheme added by` + " `" + `clientgoscheme` + "`" + `.
*/`

const MainEnableWebhook = `
const mainEnableWebhook = `

/*
We'll also set up webhooks for our type, which we'll talk about next.
Expand Down
4 changes: 2 additions & 2 deletions hack/docs/internal/cronjob-tutorial/other_api_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

package cronjob

const GroupversionIntro = `
const groupVersionIntro = `
// +kubebuilder:docs-gen:collapse=Apache License

/*
Expand All @@ -28,7 +28,7 @@ metadata for the CRDs it creates from this package.
*/
`

const GroupversionSchema = `
const groupVersionSchema = `
/*
Then, we have the commonly useful variables that help us set up our Scheme.
Since we need to use all the types in this package in our controller, it's
Expand Down
2 changes: 1 addition & 1 deletion hack/docs/internal/cronjob-tutorial/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

package cronjob

const CronjobSample = `
const cronjobSample = `
schedule: "*/1 * * * *"
startingDeadlineSeconds: 60
concurrencyPolicy: Allow # explicitly specify, but Allow is also default.
Expand Down
Loading
Loading