From 6784efa711d1d76da3b3b8cf03ae2104469dcff3 Mon Sep 17 00:00:00 2001 From: Jongwoo Han Date: Wed, 1 Jan 2025 22:33:34 +0900 Subject: [PATCH] ci: Add linter for the samples scaffolded under docs This commit change the action to add linter for the samples scaffolded under docs Signed-off-by: Jongwoo Han --- .github/workflows/lint-sample.yml | 5 ++++- .../project/internal/controller/cronjob_controller.go | 8 +++++--- .../internal/controller/cronjob_controller_test.go | 2 +- .../project/internal/webhook/v1/cronjob_webhook.go | 4 +++- .../project/internal/webhook/v1/cronjob_webhook_test.go | 8 ++++---- .../project/internal/controller/memcached_controller.go | 8 +++++--- .../project/internal/controller/cronjob_controller.go | 8 +++++--- .../internal/controller/cronjob_controller_test.go | 2 +- .../project/internal/webhook/v1/cronjob_webhook.go | 4 +++- .../project/internal/webhook/v1/cronjob_webhook_test.go | 8 ++++---- 10 files changed, 35 insertions(+), 22 deletions(-) diff --git a/.github/workflows/lint-sample.yml b/.github/workflows/lint-sample.yml index a28c23bbbea..d626bd4d6c9 100644 --- a/.github/workflows/lint-sample.yml +++ b/.github/workflows/lint-sample.yml @@ -16,7 +16,10 @@ jobs: folder: [ "testdata/project-v4", "testdata/project-v4-with-plugins", - "testdata/project-v4-multigroup" + "testdata/project-v4-multigroup", + "docs/book/src/cronjob-tutorial/testdata/project", + "docs/book/src/getting-started/testdata/project", + "docs/book/src/multiversion-tutorial/testdata/project" ] if: (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository) steps: diff --git a/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go index dd9a79c9f65..6e93cb783c2 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go @@ -28,15 +28,16 @@ import ( "sort" "time" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + "github.com/robfig/cron" kbatch "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ref "k8s.io/client-go/tools/reference" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/log" batchv1 "tutorial.kubebuilder.io/project/api/v1" ) @@ -96,6 +97,7 @@ var ( // // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.1/pkg/reconcile +// nolint:gocyclo func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx) diff --git a/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go index 6da599b350f..95a08527731 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go @@ -129,7 +129,7 @@ var _ = Describe("CronJob controller", func() { By("By checking the CronJob has zero active Jobs") Consistently(func(g Gomega) { g.Expect(k8sClient.Get(ctx, cronjobLookupKey, createdCronjob)).To(Succeed()) - g.Expect(createdCronjob.Status.Active).To(HaveLen(0)) + g.Expect(createdCronjob.Status.Active).To(HaveLen(0)) // nolint:ginkgolinter }, duration, interval).Should(Succeed()) /* Next, we actually create a stubbed Job that will belong to our CronJob, as well as its downstream template specs. diff --git a/docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook.go b/docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook.go index 55b66e4c7d4..a1487cd2270 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook.go @@ -20,18 +20,20 @@ package v1 import ( "context" "fmt" + "github.com/robfig/cron" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime/schema" validationutils "k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation/field" - "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" + "k8s.io/apimachinery/pkg/runtime" + batchv1 "tutorial.kubebuilder.io/project/api/v1" ) diff --git a/docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook_test.go b/docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook_test.go index da4421ba71e..63f0504c4fe 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook_test.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook_test.go @@ -80,7 +80,7 @@ var _ = Describe("CronJob Webhook", func() { obj.Spec.FailedJobsHistoryLimit = nil // This should default to 1 By("calling the Default method to apply defaults") - defaulter.Default(ctx, obj) + defaulter.Default(ctx, obj) // nolint:errcheck By("checking that the default values are set") Expect(obj.Spec.ConcurrencyPolicy).To(Equal(batchv1.AllowConcurrent), "Expected ConcurrencyPolicy to default to AllowConcurrent") @@ -100,7 +100,7 @@ var _ = Describe("CronJob Webhook", func() { *obj.Spec.FailedJobsHistoryLimit = 2 By("calling the Default method to apply defaults") - defaulter.Default(ctx, obj) + defaulter.Default(ctx, obj) // nolint:errcheck By("checking that the fields were not overwritten") Expect(obj.Spec.ConcurrencyPolicy).To(Equal(batchv1.ForbidConcurrent), "Expected ConcurrencyPolicy to retain its set value") @@ -119,7 +119,7 @@ var _ = Describe("CronJob Webhook", func() { }) It("Should admit creation if the name is valid", func() { - obj.ObjectMeta.Name = "valid-cronjob-name" + obj.ObjectMeta.Name = "valid-cronjob-name" // nolint:goconst Expect(validator.ValidateCreate(ctx, obj)).To(BeNil(), "Expected name validation to pass for a valid name") }) @@ -132,7 +132,7 @@ var _ = Describe("CronJob Webhook", func() { }) It("Should admit creation if the schedule is valid", func() { - obj.Spec.Schedule = "*/5 * * * *" + obj.Spec.Schedule = "*/5 * * * *" // nolint:goconst Expect(validator.ValidateCreate(ctx, obj)).To(BeNil(), "Expected spec validation to pass for a valid schedule") }) diff --git a/docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go b/docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go index 041729e01f8..5f09d03927a 100644 --- a/docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go +++ b/docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go @@ -19,6 +19,8 @@ package controller import ( "context" "fmt" + "time" + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -26,13 +28,13 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/utils/ptr" - "time" - "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" + "k8s.io/apimachinery/pkg/runtime" + cachev1alpha1 "example.com/memcached/api/v1alpha1" ) @@ -89,7 +91,7 @@ func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( } // Let's just set the status as Unknown when no status is available - if memcached.Status.Conditions == nil || len(memcached.Status.Conditions) == 0 { + if memcached.Status.Conditions == nil || len(memcached.Status.Conditions) == 0 { // nolint:gosimple meta.SetStatusCondition(&memcached.Status.Conditions, metav1.Condition{Type: typeAvailableMemcached, Status: metav1.ConditionUnknown, Reason: "Reconciling", Message: "Starting reconciliation"}) if err = r.Status().Update(ctx, memcached); err != nil { log.Error(err, "Failed to update Memcached status") diff --git a/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller.go b/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller.go index dd9a79c9f65..6e93cb783c2 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller.go +++ b/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller.go @@ -28,15 +28,16 @@ import ( "sort" "time" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + "github.com/robfig/cron" kbatch "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ref "k8s.io/client-go/tools/reference" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/log" batchv1 "tutorial.kubebuilder.io/project/api/v1" ) @@ -96,6 +97,7 @@ var ( // // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.1/pkg/reconcile +// nolint:gocyclo func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx) diff --git a/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller_test.go b/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller_test.go index 6da599b350f..95a08527731 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller_test.go +++ b/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller_test.go @@ -129,7 +129,7 @@ var _ = Describe("CronJob controller", func() { By("By checking the CronJob has zero active Jobs") Consistently(func(g Gomega) { g.Expect(k8sClient.Get(ctx, cronjobLookupKey, createdCronjob)).To(Succeed()) - g.Expect(createdCronjob.Status.Active).To(HaveLen(0)) + g.Expect(createdCronjob.Status.Active).To(HaveLen(0)) // nolint:ginkgolinter }, duration, interval).Should(Succeed()) /* Next, we actually create a stubbed Job that will belong to our CronJob, as well as its downstream template specs. diff --git a/docs/book/src/multiversion-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook.go b/docs/book/src/multiversion-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook.go index f0fdd40d15d..b1d9f1d00c0 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook.go +++ b/docs/book/src/multiversion-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook.go @@ -20,18 +20,20 @@ package v1 import ( "context" "fmt" + "github.com/robfig/cron" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime/schema" validationutils "k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation/field" - "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" + "k8s.io/apimachinery/pkg/runtime" + batchv1 "tutorial.kubebuilder.io/project/api/v1" ) diff --git a/docs/book/src/multiversion-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook_test.go b/docs/book/src/multiversion-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook_test.go index b6d3ac3e720..38c60444f4d 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook_test.go +++ b/docs/book/src/multiversion-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook_test.go @@ -80,7 +80,7 @@ var _ = Describe("CronJob Webhook", func() { obj.Spec.FailedJobsHistoryLimit = nil // This should default to 1 By("calling the Default method to apply defaults") - defaulter.Default(ctx, obj) + defaulter.Default(ctx, obj) // nolint:errcheck By("checking that the default values are set") Expect(obj.Spec.ConcurrencyPolicy).To(Equal(batchv1.AllowConcurrent), "Expected ConcurrencyPolicy to default to AllowConcurrent") @@ -100,7 +100,7 @@ var _ = Describe("CronJob Webhook", func() { *obj.Spec.FailedJobsHistoryLimit = 2 By("calling the Default method to apply defaults") - defaulter.Default(ctx, obj) + defaulter.Default(ctx, obj) //nolint:errcheck By("checking that the fields were not overwritten") Expect(obj.Spec.ConcurrencyPolicy).To(Equal(batchv1.ForbidConcurrent), "Expected ConcurrencyPolicy to retain its set value") @@ -119,7 +119,7 @@ var _ = Describe("CronJob Webhook", func() { }) It("Should admit creation if the name is valid", func() { - obj.ObjectMeta.Name = "valid-cronjob-name" + obj.ObjectMeta.Name = "valid-cronjob-name" // nolint:goconst Expect(validator.ValidateCreate(ctx, obj)).To(BeNil(), "Expected name validation to pass for a valid name") }) @@ -132,7 +132,7 @@ var _ = Describe("CronJob Webhook", func() { }) It("Should admit creation if the schedule is valid", func() { - obj.Spec.Schedule = "*/5 * * * *" + obj.Spec.Schedule = "*/5 * * * *" // nolint:goconst Expect(validator.ValidateCreate(ctx, obj)).To(BeNil(), "Expected spec validation to pass for a valid schedule") })