From 438443e88936ac44b6e8afa8dc99d9c358cc0989 Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Wed, 6 Sep 2023 12:08:10 +0100 Subject: [PATCH] add golangci integration to the default scaffold --- .../testdata/project/.golangci.yml | 40 +++++++++ .../testdata/project/.workflows/lint.yml | 24 ++++++ .../testdata/project/Makefile | 15 ++++ .../testdata/project/.golangci.yml | 40 +++++++++ .../testdata/project/.workflows/lint.yml | 24 ++++++ .../testdata/project/Makefile | 15 ++++ .../project/api/v1/cronjob_webhook.go | 1 + .../project/api/v1/webhook_suite_test.go | 3 +- .../templates/controllers/controller-test.go | 23 +++-- .../templates/controllers/controller.go | 1 - pkg/plugins/golang/v4/scaffolds/init.go | 2 + .../internal/templates/api/webhook.go | 1 + .../templates/api/webhook_suitetest.go | 3 +- .../scaffolds/internal/templates/golangci.go | 85 +++++++++++++++++++ .../internal/templates/golangci_gh_action.go | 69 +++++++++++++++ .../scaffolds/internal/templates/makefile.go | 15 ++++ test/e2e/externalplugin/generate_test.go | 3 - test/testdata/generate.sh | 2 +- testdata/project-v4-multigroup/.golangci.yml | 40 +++++++++ .../project-v4-multigroup/.workflows/lint.yml | 24 ++++++ testdata/project-v4-multigroup/Makefile | 15 ++++ .../api/crew/v1/captain_webhook.go | 1 + .../api/crew/v1/webhook_suite_test.go | 3 +- .../example.com/v1alpha1/memcached_webhook.go | 1 + .../v1alpha1/webhook_suite_test.go | 3 +- .../api/ship/v1/destroyer_webhook.go | 1 + .../api/ship/v1/webhook_suite_test.go | 3 +- .../api/ship/v1beta1/frigate_webhook.go | 1 + .../api/ship/v2alpha1/cruiser_webhook.go | 1 + .../api/ship/v2alpha1/webhook_suite_test.go | 3 +- .../api/v1/lakers_webhook.go | 1 + .../api/v1/webhook_suite_test.go | 3 +- .../example.com/busybox_controller.go | 1 - .../example.com/busybox_controller_test.go | 23 +++-- .../example.com/memcached_controller.go | 1 - .../example.com/memcached_controller_test.go | 23 +++-- .../.golangci.yml | 40 +++++++++ .../.workflows/lint.yml | 24 ++++++ .../project-v4-with-deploy-image/Makefile | 15 ++++ .../api/v1alpha1/memcached_webhook.go | 1 + .../api/v1alpha1/webhook_suite_test.go | 3 +- .../internal/controller/busybox_controller.go | 1 - .../controller/busybox_controller_test.go | 23 +++-- .../controller/memcached_controller.go | 1 - .../controller/memcached_controller_test.go | 23 +++-- .../project-v4-with-grafana/.golangci.yml | 40 +++++++++ .../.workflows/lint.yml | 24 ++++++ testdata/project-v4-with-grafana/Makefile | 15 ++++ testdata/project-v4/.golangci.yml | 40 +++++++++ testdata/project-v4/.workflows/lint.yml | 24 ++++++ testdata/project-v4/Makefile | 15 ++++ testdata/project-v4/api/v1/admiral_webhook.go | 1 + testdata/project-v4/api/v1/captain_webhook.go | 1 + .../project-v4/api/v1/firstmate_webhook.go | 1 + .../project-v4/api/v1/webhook_suite_test.go | 3 +- 55 files changed, 752 insertions(+), 57 deletions(-) create mode 100644 docs/book/src/component-config-tutorial/testdata/project/.golangci.yml create mode 100644 docs/book/src/component-config-tutorial/testdata/project/.workflows/lint.yml create mode 100644 docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml create mode 100644 docs/book/src/cronjob-tutorial/testdata/project/.workflows/lint.yml create mode 100644 pkg/plugins/golang/v4/scaffolds/internal/templates/golangci.go create mode 100644 pkg/plugins/golang/v4/scaffolds/internal/templates/golangci_gh_action.go create mode 100644 testdata/project-v4-multigroup/.golangci.yml create mode 100644 testdata/project-v4-multigroup/.workflows/lint.yml create mode 100644 testdata/project-v4-with-deploy-image/.golangci.yml create mode 100644 testdata/project-v4-with-deploy-image/.workflows/lint.yml create mode 100644 testdata/project-v4-with-grafana/.golangci.yml create mode 100644 testdata/project-v4-with-grafana/.workflows/lint.yml create mode 100644 testdata/project-v4/.golangci.yml create mode 100644 testdata/project-v4/.workflows/lint.yml diff --git a/docs/book/src/component-config-tutorial/testdata/project/.golangci.yml b/docs/book/src/component-config-tutorial/testdata/project/.golangci.yml new file mode 100644 index 00000000000..aed8644d11e --- /dev/null +++ b/docs/book/src/component-config-tutorial/testdata/project/.golangci.yml @@ -0,0 +1,40 @@ +run: + deadline: 5m + allow-parallel-runners: true + +issues: + # don't skip warning about doc comments + # don't exclude the default set of lint + exclude-use-default: false + # restore some of the defaults + # (fill in the rest as needed) + exclude-rules: + - path: "api/*" + linters: + - lll + - path: "internal/*" + linters: + - dupl + - lll +linters: + disable-all: true + enable: + - dupl + - errcheck + - exportloopref + - goconst + - gocyclo + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - prealloc + - staticcheck + - typecheck + - unconvert + - unparam + - unused diff --git a/docs/book/src/component-config-tutorial/testdata/project/.workflows/lint.yml b/docs/book/src/component-config-tutorial/testdata/project/.workflows/lint.yml new file mode 100644 index 00000000000..1e49331bebb --- /dev/null +++ b/docs/book/src/component-config-tutorial/testdata/project/.workflows/lint.yml @@ -0,0 +1,24 @@ +name: Lint + +# Trigger the workflow on pull requests and direct pushes to any branch +on: + push: + pull_request: + +jobs: + lint: + name: golangci-lint + runs-on: ubuntu-latest + # Pull requests from the same repository won't trigger this checks as they were already triggered by the push + if: (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository) + steps: + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '~1.20' + - name: Clone the code + uses: actions/checkout@v4 + - name: Run linter + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 diff --git a/docs/book/src/component-config-tutorial/testdata/project/Makefile b/docs/book/src/component-config-tutorial/testdata/project/Makefile index 751cdd77165..fc949b7f470 100644 --- a/docs/book/src/component-config-tutorial/testdata/project/Makefile +++ b/docs/book/src/component-config-tutorial/testdata/project/Makefile @@ -64,6 +64,21 @@ vet: ## Run go vet against code. test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out +GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint +golangci-lint: + @[ -f $(GOLANGCI_LINT) ] || { \ + set -e ;\ + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.54.2 ;\ + } + +.PHONY: lint +lint: golangci-lint ## Run golangci-lint linter & yamllint + $(GOLANGCI_LINT) run + +.PHONY: lint-fix +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes + $(GOLANGCI_LINT) run --fix + ##@ Build .PHONY: build diff --git a/docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml b/docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml new file mode 100644 index 00000000000..aed8644d11e --- /dev/null +++ b/docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml @@ -0,0 +1,40 @@ +run: + deadline: 5m + allow-parallel-runners: true + +issues: + # don't skip warning about doc comments + # don't exclude the default set of lint + exclude-use-default: false + # restore some of the defaults + # (fill in the rest as needed) + exclude-rules: + - path: "api/*" + linters: + - lll + - path: "internal/*" + linters: + - dupl + - lll +linters: + disable-all: true + enable: + - dupl + - errcheck + - exportloopref + - goconst + - gocyclo + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - prealloc + - staticcheck + - typecheck + - unconvert + - unparam + - unused diff --git a/docs/book/src/cronjob-tutorial/testdata/project/.workflows/lint.yml b/docs/book/src/cronjob-tutorial/testdata/project/.workflows/lint.yml new file mode 100644 index 00000000000..1e49331bebb --- /dev/null +++ b/docs/book/src/cronjob-tutorial/testdata/project/.workflows/lint.yml @@ -0,0 +1,24 @@ +name: Lint + +# Trigger the workflow on pull requests and direct pushes to any branch +on: + push: + pull_request: + +jobs: + lint: + name: golangci-lint + runs-on: ubuntu-latest + # Pull requests from the same repository won't trigger this checks as they were already triggered by the push + if: (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository) + steps: + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '~1.20' + - name: Clone the code + uses: actions/checkout@v4 + - name: Run linter + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 diff --git a/docs/book/src/cronjob-tutorial/testdata/project/Makefile b/docs/book/src/cronjob-tutorial/testdata/project/Makefile index 751cdd77165..fc949b7f470 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/Makefile +++ b/docs/book/src/cronjob-tutorial/testdata/project/Makefile @@ -64,6 +64,21 @@ vet: ## Run go vet against code. test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out +GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint +golangci-lint: + @[ -f $(GOLANGCI_LINT) ] || { \ + set -e ;\ + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.54.2 ;\ + } + +.PHONY: lint +lint: golangci-lint ## Run golangci-lint linter & yamllint + $(GOLANGCI_LINT) run + +.PHONY: lint-fix +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes + $(GOLANGCI_LINT) run --fix + ##@ Build .PHONY: build diff --git a/docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go b/docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go index 909469c09c7..e75120e120a 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go @@ -42,6 +42,7 @@ var cronjoblog = logf.Log.WithName("cronjob-resource") Then, we set up the webhook with the manager. */ +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *CronJob) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go b/docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go index eba3577726c..e82b72c2607 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go @@ -132,8 +132,7 @@ var _ = BeforeSuite(func() { if err != nil { return err } - conn.Close() - return nil + return conn.Close() }).Should(Succeed()) }) diff --git a/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go b/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go index 8ed8601ffd6..4ecfb708032 100644 --- a/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go +++ b/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go @@ -81,6 +81,7 @@ import ( "time" "fmt" + //nolint:golint . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" appsv1 "k8s.io/api/apps/v1" @@ -109,7 +110,10 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() { }, } - typeNamespaceName := types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Name} + typeNamespaceName := types.NamespacedName{ + Name: {{ .Resource.Kind }}Name, + Namespace: {{ .Resource.Kind }}Name, + } {{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{} BeforeEach(func() { @@ -190,13 +194,20 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() { By("Checking the latest Status Condition added to the {{ .Resource.Kind }} instance") Eventually(func() error { - if {{ lower .Resource.Kind }}.Status.Conditions != nil && len({{ lower .Resource.Kind }}.Status.Conditions) != 0 { + if {{ lower .Resource.Kind }}.Status.Conditions != nil && + len({{ lower .Resource.Kind }}.Status.Conditions) != 0 { latestStatusCondition := {{ lower .Resource.Kind }}.Status.Conditions[len({{ lower .Resource.Kind }}.Status.Conditions)-1] - expectedLatestStatusCondition := metav1.Condition{Type: typeAvailable{{ .Resource.Kind }}, - Status: metav1.ConditionTrue, Reason: "Reconciling", - Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", {{ lower .Resource.Kind }}.Name, {{ lower .Resource.Kind }}.Spec.Size)} + expectedLatestStatusCondition := metav1.Condition{ + Type: typeAvailable{{ .Resource.Kind }}, + Status: metav1.ConditionTrue, + Reason: "Reconciling", + Message: fmt.Sprintf( + "Deployment for custom resource (%s) with %d replicas created successfully", + {{ lower .Resource.Kind }}.Name, + {{ lower .Resource.Kind }}.Spec.Size), + } if latestStatusCondition != expectedLatestStatusCondition { - return fmt.Errorf("The latest status condition added to the {{ lower .Resource.Kind }} instance is not as expected") + return fmt.Errorf("The latest status condition added to the {{ .Resource.Kind }} instance is not as expected") } } return nil diff --git a/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go b/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go index 5284478c2ba..d7e6f9262af 100644 --- a/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go +++ b/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go @@ -137,7 +137,6 @@ type {{ .Resource.Kind }}Reconciler struct { // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. - // It is essential for the controller's reconciliation loop to be idempotent. By following the Operator // pattern you will create Controllers which provide a reconcile function // responsible for synchronizing resources until the desired state is reached on the cluster. diff --git a/pkg/plugins/golang/v4/scaffolds/init.go b/pkg/plugins/golang/v4/scaffolds/init.go index fbd8032dff5..b140ccdd4e0 100644 --- a/pkg/plugins/golang/v4/scaffolds/init.go +++ b/pkg/plugins/golang/v4/scaffolds/init.go @@ -139,5 +139,7 @@ func (s *initScaffolder) Scaffold() error { &templates.Dockerfile{}, &templates.DockerIgnore{}, &templates.Readme{}, + &templates.Golangci{}, + &templates.GolangciGHAction{}, ) } diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook.go index bde82f8f5c6..60ff9051a27 100644 --- a/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook.go +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook.go @@ -98,6 +98,7 @@ import ( // log is for logging in this package. var {{ lower .Resource.Kind }}log = logf.Log.WithName("{{ lower .Resource.Kind }}-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook_suitetest.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook_suitetest.go index ea9af73c683..e8e396deb16 100644 --- a/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook_suitetest.go +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook_suitetest.go @@ -249,8 +249,7 @@ var _ = BeforeSuite(func() { if err != nil { return err } - conn.Close() - return nil + return conn.Close(); }).Should(Succeed()) }) diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/golangci.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/golangci.go new file mode 100644 index 00000000000..f6b263dc674 --- /dev/null +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/golangci.go @@ -0,0 +1,85 @@ +/* +Copyright 2022 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package templates + +import ( + "sigs.k8s.io/kubebuilder/v3/pkg/machinery" +) + +var _ machinery.Template = &Golangci{} + +// Golangci scaffolds a file which define Golangci rules +type Golangci struct { + machinery.TemplateMixin + machinery.ProjectNameMixin +} + +// SetTemplateDefaults implements file.Template +func (f *Golangci) SetTemplateDefaults() error { + if f.Path == "" { + f.Path = ".golangci.yml" + } + + f.TemplateBody = golangciTemplate + + f.IfExistsAction = machinery.SkipFile + + return nil +} + +//nolint:lll +const golangciTemplate = `run: + deadline: 5m + allow-parallel-runners: true + +issues: + # don't skip warning about doc comments + # don't exclude the default set of lint + exclude-use-default: false + # restore some of the defaults + # (fill in the rest as needed) + exclude-rules: + - path: "api/*" + linters: + - lll + - path: "internal/*" + linters: + - dupl + - lll +linters: + disable-all: true + enable: + - dupl + - errcheck + - exportloopref + - goconst + - gocyclo + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - prealloc + - staticcheck + - typecheck + - unconvert + - unparam + - unused +` diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/golangci_gh_action.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/golangci_gh_action.go new file mode 100644 index 00000000000..aea74269def --- /dev/null +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/golangci_gh_action.go @@ -0,0 +1,69 @@ +/* +Copyright 2022 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package templates + +import ( + "sigs.k8s.io/kubebuilder/v3/pkg/machinery" +) + +var _ machinery.Template = &GolangciGHAction{} + +// GolangciGHAction scaffolds a file that defines Golangci GitHub Actions +type GolangciGHAction struct { + machinery.TemplateMixin + machinery.ProjectNameMixin +} + +// SetTemplateDefaults implements file.Template +func (f *GolangciGHAction) SetTemplateDefaults() error { + if f.Path == "" { + f.Path = ".workflows/lint.yml" + } + + f.TemplateBody = golangciGitHubActionTemplate + + f.IfExistsAction = machinery.SkipFile + + return nil +} + +//nolint:lll +const golangciGitHubActionTemplate = `name: Lint + +# Trigger the workflow on pull requests and direct pushes to any branch +on: + push: + pull_request: + +jobs: + lint: + name: golangci-lint + runs-on: ubuntu-latest + # Pull requests from the same repository won't trigger this checks as they were already triggered by the push + if: (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository) + steps: + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '~1.20' + - name: Clone the code + uses: actions/checkout@v4 + - name: Run linter + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 +` diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go index 9ad9268c5e7..28ffa442cb7 100644 --- a/pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go @@ -128,6 +128,21 @@ vet: ## Run go vet against code. test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out +GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint +golangci-lint: + @[ -f $(GOLANGCI_LINT) ] || { \ + set -e ;\ + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.54.2 ;\ + } + +.PHONY: lint +lint: golangci-lint ## Run golangci-lint linter & yamllint + $(GOLANGCI_LINT) run + +.PHONY: lint-fix +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes + $(GOLANGCI_LINT) run --fix + ##@ Build .PHONY: build diff --git a/test/e2e/externalplugin/generate_test.go b/test/e2e/externalplugin/generate_test.go index fa495485682..cdd39333638 100644 --- a/test/e2e/externalplugin/generate_test.go +++ b/test/e2e/externalplugin/generate_test.go @@ -24,9 +24,6 @@ import ( //nolint:golint //nolint:revive . "github.com/onsi/ginkgo/v2" - - //nolint:golint - //nolint:revive . "github.com/onsi/gomega" //nolint:golint diff --git a/test/testdata/generate.sh b/test/testdata/generate.sh index a6f7a8f2d8c..f37c007b63a 100755 --- a/test/testdata/generate.sh +++ b/test/testdata/generate.sh @@ -126,7 +126,7 @@ function scaffold_test_project { make generate manifests rm -f go.sum - + go mod tidy popd } diff --git a/testdata/project-v4-multigroup/.golangci.yml b/testdata/project-v4-multigroup/.golangci.yml new file mode 100644 index 00000000000..aed8644d11e --- /dev/null +++ b/testdata/project-v4-multigroup/.golangci.yml @@ -0,0 +1,40 @@ +run: + deadline: 5m + allow-parallel-runners: true + +issues: + # don't skip warning about doc comments + # don't exclude the default set of lint + exclude-use-default: false + # restore some of the defaults + # (fill in the rest as needed) + exclude-rules: + - path: "api/*" + linters: + - lll + - path: "internal/*" + linters: + - dupl + - lll +linters: + disable-all: true + enable: + - dupl + - errcheck + - exportloopref + - goconst + - gocyclo + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - prealloc + - staticcheck + - typecheck + - unconvert + - unparam + - unused diff --git a/testdata/project-v4-multigroup/.workflows/lint.yml b/testdata/project-v4-multigroup/.workflows/lint.yml new file mode 100644 index 00000000000..1e49331bebb --- /dev/null +++ b/testdata/project-v4-multigroup/.workflows/lint.yml @@ -0,0 +1,24 @@ +name: Lint + +# Trigger the workflow on pull requests and direct pushes to any branch +on: + push: + pull_request: + +jobs: + lint: + name: golangci-lint + runs-on: ubuntu-latest + # Pull requests from the same repository won't trigger this checks as they were already triggered by the push + if: (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository) + steps: + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '~1.20' + - name: Clone the code + uses: actions/checkout@v4 + - name: Run linter + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 diff --git a/testdata/project-v4-multigroup/Makefile b/testdata/project-v4-multigroup/Makefile index 751cdd77165..fc949b7f470 100644 --- a/testdata/project-v4-multigroup/Makefile +++ b/testdata/project-v4-multigroup/Makefile @@ -64,6 +64,21 @@ vet: ## Run go vet against code. test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out +GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint +golangci-lint: + @[ -f $(GOLANGCI_LINT) ] || { \ + set -e ;\ + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.54.2 ;\ + } + +.PHONY: lint +lint: golangci-lint ## Run golangci-lint linter & yamllint + $(GOLANGCI_LINT) run + +.PHONY: lint-fix +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes + $(GOLANGCI_LINT) run --fix + ##@ Build .PHONY: build diff --git a/testdata/project-v4-multigroup/api/crew/v1/captain_webhook.go b/testdata/project-v4-multigroup/api/crew/v1/captain_webhook.go index 7948f0387a6..ed99f7f0e55 100644 --- a/testdata/project-v4-multigroup/api/crew/v1/captain_webhook.go +++ b/testdata/project-v4-multigroup/api/crew/v1/captain_webhook.go @@ -27,6 +27,7 @@ import ( // log is for logging in this package. var captainlog = logf.Log.WithName("captain-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/testdata/project-v4-multigroup/api/crew/v1/webhook_suite_test.go b/testdata/project-v4-multigroup/api/crew/v1/webhook_suite_test.go index fb0518a6657..d58a13812d1 100644 --- a/testdata/project-v4-multigroup/api/crew/v1/webhook_suite_test.go +++ b/testdata/project-v4-multigroup/api/crew/v1/webhook_suite_test.go @@ -132,8 +132,7 @@ var _ = BeforeSuite(func() { if err != nil { return err } - conn.Close() - return nil + return conn.Close() }).Should(Succeed()) }) diff --git a/testdata/project-v4-multigroup/api/example.com/v1alpha1/memcached_webhook.go b/testdata/project-v4-multigroup/api/example.com/v1alpha1/memcached_webhook.go index 9dde8721abe..77993bc8d50 100644 --- a/testdata/project-v4-multigroup/api/example.com/v1alpha1/memcached_webhook.go +++ b/testdata/project-v4-multigroup/api/example.com/v1alpha1/memcached_webhook.go @@ -27,6 +27,7 @@ import ( // log is for logging in this package. var memcachedlog = logf.Log.WithName("memcached-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *Memcached) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/testdata/project-v4-multigroup/api/example.com/v1alpha1/webhook_suite_test.go b/testdata/project-v4-multigroup/api/example.com/v1alpha1/webhook_suite_test.go index 3fbb781f9f7..17d019e74f8 100644 --- a/testdata/project-v4-multigroup/api/example.com/v1alpha1/webhook_suite_test.go +++ b/testdata/project-v4-multigroup/api/example.com/v1alpha1/webhook_suite_test.go @@ -132,8 +132,7 @@ var _ = BeforeSuite(func() { if err != nil { return err } - conn.Close() - return nil + return conn.Close() }).Should(Succeed()) }) diff --git a/testdata/project-v4-multigroup/api/ship/v1/destroyer_webhook.go b/testdata/project-v4-multigroup/api/ship/v1/destroyer_webhook.go index 67798c6e1f0..4b0fb739d55 100644 --- a/testdata/project-v4-multigroup/api/ship/v1/destroyer_webhook.go +++ b/testdata/project-v4-multigroup/api/ship/v1/destroyer_webhook.go @@ -25,6 +25,7 @@ import ( // log is for logging in this package. var destroyerlog = logf.Log.WithName("destroyer-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *Destroyer) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/testdata/project-v4-multigroup/api/ship/v1/webhook_suite_test.go b/testdata/project-v4-multigroup/api/ship/v1/webhook_suite_test.go index fd04fa63923..1c6bb164c54 100644 --- a/testdata/project-v4-multigroup/api/ship/v1/webhook_suite_test.go +++ b/testdata/project-v4-multigroup/api/ship/v1/webhook_suite_test.go @@ -132,8 +132,7 @@ var _ = BeforeSuite(func() { if err != nil { return err } - conn.Close() - return nil + return conn.Close() }).Should(Succeed()) }) diff --git a/testdata/project-v4-multigroup/api/ship/v1beta1/frigate_webhook.go b/testdata/project-v4-multigroup/api/ship/v1beta1/frigate_webhook.go index 028189c9eab..e03bafd22b2 100644 --- a/testdata/project-v4-multigroup/api/ship/v1beta1/frigate_webhook.go +++ b/testdata/project-v4-multigroup/api/ship/v1beta1/frigate_webhook.go @@ -24,6 +24,7 @@ import ( // log is for logging in this package. var frigatelog = logf.Log.WithName("frigate-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *Frigate) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/testdata/project-v4-multigroup/api/ship/v2alpha1/cruiser_webhook.go b/testdata/project-v4-multigroup/api/ship/v2alpha1/cruiser_webhook.go index 030b6937640..14d5710a831 100644 --- a/testdata/project-v4-multigroup/api/ship/v2alpha1/cruiser_webhook.go +++ b/testdata/project-v4-multigroup/api/ship/v2alpha1/cruiser_webhook.go @@ -27,6 +27,7 @@ import ( // log is for logging in this package. var cruiserlog = logf.Log.WithName("cruiser-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *Cruiser) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/testdata/project-v4-multigroup/api/ship/v2alpha1/webhook_suite_test.go b/testdata/project-v4-multigroup/api/ship/v2alpha1/webhook_suite_test.go index 8f37bf1ee3d..38482d55ee7 100644 --- a/testdata/project-v4-multigroup/api/ship/v2alpha1/webhook_suite_test.go +++ b/testdata/project-v4-multigroup/api/ship/v2alpha1/webhook_suite_test.go @@ -132,8 +132,7 @@ var _ = BeforeSuite(func() { if err != nil { return err } - conn.Close() - return nil + return conn.Close() }).Should(Succeed()) }) diff --git a/testdata/project-v4-multigroup/api/v1/lakers_webhook.go b/testdata/project-v4-multigroup/api/v1/lakers_webhook.go index f1b3ac61a91..26041fdfc75 100644 --- a/testdata/project-v4-multigroup/api/v1/lakers_webhook.go +++ b/testdata/project-v4-multigroup/api/v1/lakers_webhook.go @@ -27,6 +27,7 @@ import ( // log is for logging in this package. var lakerslog = logf.Log.WithName("lakers-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *Lakers) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/testdata/project-v4-multigroup/api/v1/webhook_suite_test.go b/testdata/project-v4-multigroup/api/v1/webhook_suite_test.go index 5a88f849efe..250b42b87b3 100644 --- a/testdata/project-v4-multigroup/api/v1/webhook_suite_test.go +++ b/testdata/project-v4-multigroup/api/v1/webhook_suite_test.go @@ -132,8 +132,7 @@ var _ = BeforeSuite(func() { if err != nil { return err } - conn.Close() - return nil + return conn.Close() }).Should(Succeed()) }) diff --git a/testdata/project-v4-multigroup/internal/controller/example.com/busybox_controller.go b/testdata/project-v4-multigroup/internal/controller/example.com/busybox_controller.go index ce289946129..65756ff8f53 100644 --- a/testdata/project-v4-multigroup/internal/controller/example.com/busybox_controller.go +++ b/testdata/project-v4-multigroup/internal/controller/example.com/busybox_controller.go @@ -69,7 +69,6 @@ type BusyboxReconciler struct { // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. - // It is essential for the controller's reconciliation loop to be idempotent. By following the Operator // pattern you will create Controllers which provide a reconcile function // responsible for synchronizing resources until the desired state is reached on the cluster. diff --git a/testdata/project-v4-multigroup/internal/controller/example.com/busybox_controller_test.go b/testdata/project-v4-multigroup/internal/controller/example.com/busybox_controller_test.go index 0a3496134dd..1d06b715f88 100644 --- a/testdata/project-v4-multigroup/internal/controller/example.com/busybox_controller_test.go +++ b/testdata/project-v4-multigroup/internal/controller/example.com/busybox_controller_test.go @@ -22,6 +22,7 @@ import ( "os" "time" + //nolint:golint . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" appsv1 "k8s.io/api/apps/v1" @@ -48,7 +49,10 @@ var _ = Describe("Busybox controller", func() { }, } - typeNamespaceName := types.NamespacedName{Name: BusyboxName, Namespace: BusyboxName} + typeNamespaceName := types.NamespacedName{ + Name: BusyboxName, + Namespace: BusyboxName, + } busybox := &examplecomv1alpha1.Busybox{} BeforeEach(func() { @@ -126,13 +130,20 @@ var _ = Describe("Busybox controller", func() { By("Checking the latest Status Condition added to the Busybox instance") Eventually(func() error { - if busybox.Status.Conditions != nil && len(busybox.Status.Conditions) != 0 { + if busybox.Status.Conditions != nil && + len(busybox.Status.Conditions) != 0 { latestStatusCondition := busybox.Status.Conditions[len(busybox.Status.Conditions)-1] - expectedLatestStatusCondition := metav1.Condition{Type: typeAvailableBusybox, - Status: metav1.ConditionTrue, Reason: "Reconciling", - Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", busybox.Name, busybox.Spec.Size)} + expectedLatestStatusCondition := metav1.Condition{ + Type: typeAvailableBusybox, + Status: metav1.ConditionTrue, + Reason: "Reconciling", + Message: fmt.Sprintf( + "Deployment for custom resource (%s) with %d replicas created successfully", + busybox.Name, + busybox.Spec.Size), + } if latestStatusCondition != expectedLatestStatusCondition { - return fmt.Errorf("The latest status condition added to the busybox instance is not as expected") + return fmt.Errorf("The latest status condition added to the Busybox instance is not as expected") } } return nil diff --git a/testdata/project-v4-multigroup/internal/controller/example.com/memcached_controller.go b/testdata/project-v4-multigroup/internal/controller/example.com/memcached_controller.go index 9dd0faa9c7a..b17cbe01477 100644 --- a/testdata/project-v4-multigroup/internal/controller/example.com/memcached_controller.go +++ b/testdata/project-v4-multigroup/internal/controller/example.com/memcached_controller.go @@ -69,7 +69,6 @@ type MemcachedReconciler struct { // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. - // It is essential for the controller's reconciliation loop to be idempotent. By following the Operator // pattern you will create Controllers which provide a reconcile function // responsible for synchronizing resources until the desired state is reached on the cluster. diff --git a/testdata/project-v4-multigroup/internal/controller/example.com/memcached_controller_test.go b/testdata/project-v4-multigroup/internal/controller/example.com/memcached_controller_test.go index f96146c9b74..920db20dd05 100644 --- a/testdata/project-v4-multigroup/internal/controller/example.com/memcached_controller_test.go +++ b/testdata/project-v4-multigroup/internal/controller/example.com/memcached_controller_test.go @@ -22,6 +22,7 @@ import ( "os" "time" + //nolint:golint . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" appsv1 "k8s.io/api/apps/v1" @@ -48,7 +49,10 @@ var _ = Describe("Memcached controller", func() { }, } - typeNamespaceName := types.NamespacedName{Name: MemcachedName, Namespace: MemcachedName} + typeNamespaceName := types.NamespacedName{ + Name: MemcachedName, + Namespace: MemcachedName, + } memcached := &examplecomv1alpha1.Memcached{} BeforeEach(func() { @@ -127,13 +131,20 @@ var _ = Describe("Memcached controller", func() { By("Checking the latest Status Condition added to the Memcached instance") Eventually(func() error { - if memcached.Status.Conditions != nil && len(memcached.Status.Conditions) != 0 { + if memcached.Status.Conditions != nil && + len(memcached.Status.Conditions) != 0 { latestStatusCondition := memcached.Status.Conditions[len(memcached.Status.Conditions)-1] - expectedLatestStatusCondition := metav1.Condition{Type: typeAvailableMemcached, - Status: metav1.ConditionTrue, Reason: "Reconciling", - Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", memcached.Name, memcached.Spec.Size)} + expectedLatestStatusCondition := metav1.Condition{ + Type: typeAvailableMemcached, + Status: metav1.ConditionTrue, + Reason: "Reconciling", + Message: fmt.Sprintf( + "Deployment for custom resource (%s) with %d replicas created successfully", + memcached.Name, + memcached.Spec.Size), + } if latestStatusCondition != expectedLatestStatusCondition { - return fmt.Errorf("The latest status condition added to the memcached instance is not as expected") + return fmt.Errorf("The latest status condition added to the Memcached instance is not as expected") } } return nil diff --git a/testdata/project-v4-with-deploy-image/.golangci.yml b/testdata/project-v4-with-deploy-image/.golangci.yml new file mode 100644 index 00000000000..aed8644d11e --- /dev/null +++ b/testdata/project-v4-with-deploy-image/.golangci.yml @@ -0,0 +1,40 @@ +run: + deadline: 5m + allow-parallel-runners: true + +issues: + # don't skip warning about doc comments + # don't exclude the default set of lint + exclude-use-default: false + # restore some of the defaults + # (fill in the rest as needed) + exclude-rules: + - path: "api/*" + linters: + - lll + - path: "internal/*" + linters: + - dupl + - lll +linters: + disable-all: true + enable: + - dupl + - errcheck + - exportloopref + - goconst + - gocyclo + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - prealloc + - staticcheck + - typecheck + - unconvert + - unparam + - unused diff --git a/testdata/project-v4-with-deploy-image/.workflows/lint.yml b/testdata/project-v4-with-deploy-image/.workflows/lint.yml new file mode 100644 index 00000000000..1e49331bebb --- /dev/null +++ b/testdata/project-v4-with-deploy-image/.workflows/lint.yml @@ -0,0 +1,24 @@ +name: Lint + +# Trigger the workflow on pull requests and direct pushes to any branch +on: + push: + pull_request: + +jobs: + lint: + name: golangci-lint + runs-on: ubuntu-latest + # Pull requests from the same repository won't trigger this checks as they were already triggered by the push + if: (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository) + steps: + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '~1.20' + - name: Clone the code + uses: actions/checkout@v4 + - name: Run linter + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 diff --git a/testdata/project-v4-with-deploy-image/Makefile b/testdata/project-v4-with-deploy-image/Makefile index 751cdd77165..fc949b7f470 100644 --- a/testdata/project-v4-with-deploy-image/Makefile +++ b/testdata/project-v4-with-deploy-image/Makefile @@ -64,6 +64,21 @@ vet: ## Run go vet against code. test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out +GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint +golangci-lint: + @[ -f $(GOLANGCI_LINT) ] || { \ + set -e ;\ + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.54.2 ;\ + } + +.PHONY: lint +lint: golangci-lint ## Run golangci-lint linter & yamllint + $(GOLANGCI_LINT) run + +.PHONY: lint-fix +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes + $(GOLANGCI_LINT) run --fix + ##@ Build .PHONY: build diff --git a/testdata/project-v4-with-deploy-image/api/v1alpha1/memcached_webhook.go b/testdata/project-v4-with-deploy-image/api/v1alpha1/memcached_webhook.go index 9dde8721abe..77993bc8d50 100644 --- a/testdata/project-v4-with-deploy-image/api/v1alpha1/memcached_webhook.go +++ b/testdata/project-v4-with-deploy-image/api/v1alpha1/memcached_webhook.go @@ -27,6 +27,7 @@ import ( // log is for logging in this package. var memcachedlog = logf.Log.WithName("memcached-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *Memcached) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/testdata/project-v4-with-deploy-image/api/v1alpha1/webhook_suite_test.go b/testdata/project-v4-with-deploy-image/api/v1alpha1/webhook_suite_test.go index ad362da3011..d1abc45a294 100644 --- a/testdata/project-v4-with-deploy-image/api/v1alpha1/webhook_suite_test.go +++ b/testdata/project-v4-with-deploy-image/api/v1alpha1/webhook_suite_test.go @@ -132,8 +132,7 @@ var _ = BeforeSuite(func() { if err != nil { return err } - conn.Close() - return nil + return conn.Close() }).Should(Succeed()) }) diff --git a/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller.go b/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller.go index ca6cce659e2..12f6de5afc0 100644 --- a/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller.go +++ b/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller.go @@ -69,7 +69,6 @@ type BusyboxReconciler struct { // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. - // It is essential for the controller's reconciliation loop to be idempotent. By following the Operator // pattern you will create Controllers which provide a reconcile function // responsible for synchronizing resources until the desired state is reached on the cluster. diff --git a/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller_test.go b/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller_test.go index 146c380873b..e3aeacd7191 100644 --- a/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller_test.go +++ b/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller_test.go @@ -22,6 +22,7 @@ import ( "os" "time" + //nolint:golint . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" appsv1 "k8s.io/api/apps/v1" @@ -48,7 +49,10 @@ var _ = Describe("Busybox controller", func() { }, } - typeNamespaceName := types.NamespacedName{Name: BusyboxName, Namespace: BusyboxName} + typeNamespaceName := types.NamespacedName{ + Name: BusyboxName, + Namespace: BusyboxName, + } busybox := &examplecomv1alpha1.Busybox{} BeforeEach(func() { @@ -126,13 +130,20 @@ var _ = Describe("Busybox controller", func() { By("Checking the latest Status Condition added to the Busybox instance") Eventually(func() error { - if busybox.Status.Conditions != nil && len(busybox.Status.Conditions) != 0 { + if busybox.Status.Conditions != nil && + len(busybox.Status.Conditions) != 0 { latestStatusCondition := busybox.Status.Conditions[len(busybox.Status.Conditions)-1] - expectedLatestStatusCondition := metav1.Condition{Type: typeAvailableBusybox, - Status: metav1.ConditionTrue, Reason: "Reconciling", - Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", busybox.Name, busybox.Spec.Size)} + expectedLatestStatusCondition := metav1.Condition{ + Type: typeAvailableBusybox, + Status: metav1.ConditionTrue, + Reason: "Reconciling", + Message: fmt.Sprintf( + "Deployment for custom resource (%s) with %d replicas created successfully", + busybox.Name, + busybox.Spec.Size), + } if latestStatusCondition != expectedLatestStatusCondition { - return fmt.Errorf("The latest status condition added to the busybox instance is not as expected") + return fmt.Errorf("The latest status condition added to the Busybox instance is not as expected") } } return nil diff --git a/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller.go b/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller.go index d602cc86194..fb09c3095ff 100644 --- a/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller.go +++ b/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller.go @@ -69,7 +69,6 @@ type MemcachedReconciler struct { // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. - // It is essential for the controller's reconciliation loop to be idempotent. By following the Operator // pattern you will create Controllers which provide a reconcile function // responsible for synchronizing resources until the desired state is reached on the cluster. diff --git a/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller_test.go b/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller_test.go index 38e270374a4..00f8e7828d4 100644 --- a/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller_test.go +++ b/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller_test.go @@ -22,6 +22,7 @@ import ( "os" "time" + //nolint:golint . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" appsv1 "k8s.io/api/apps/v1" @@ -48,7 +49,10 @@ var _ = Describe("Memcached controller", func() { }, } - typeNamespaceName := types.NamespacedName{Name: MemcachedName, Namespace: MemcachedName} + typeNamespaceName := types.NamespacedName{ + Name: MemcachedName, + Namespace: MemcachedName, + } memcached := &examplecomv1alpha1.Memcached{} BeforeEach(func() { @@ -127,13 +131,20 @@ var _ = Describe("Memcached controller", func() { By("Checking the latest Status Condition added to the Memcached instance") Eventually(func() error { - if memcached.Status.Conditions != nil && len(memcached.Status.Conditions) != 0 { + if memcached.Status.Conditions != nil && + len(memcached.Status.Conditions) != 0 { latestStatusCondition := memcached.Status.Conditions[len(memcached.Status.Conditions)-1] - expectedLatestStatusCondition := metav1.Condition{Type: typeAvailableMemcached, - Status: metav1.ConditionTrue, Reason: "Reconciling", - Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", memcached.Name, memcached.Spec.Size)} + expectedLatestStatusCondition := metav1.Condition{ + Type: typeAvailableMemcached, + Status: metav1.ConditionTrue, + Reason: "Reconciling", + Message: fmt.Sprintf( + "Deployment for custom resource (%s) with %d replicas created successfully", + memcached.Name, + memcached.Spec.Size), + } if latestStatusCondition != expectedLatestStatusCondition { - return fmt.Errorf("The latest status condition added to the memcached instance is not as expected") + return fmt.Errorf("The latest status condition added to the Memcached instance is not as expected") } } return nil diff --git a/testdata/project-v4-with-grafana/.golangci.yml b/testdata/project-v4-with-grafana/.golangci.yml new file mode 100644 index 00000000000..aed8644d11e --- /dev/null +++ b/testdata/project-v4-with-grafana/.golangci.yml @@ -0,0 +1,40 @@ +run: + deadline: 5m + allow-parallel-runners: true + +issues: + # don't skip warning about doc comments + # don't exclude the default set of lint + exclude-use-default: false + # restore some of the defaults + # (fill in the rest as needed) + exclude-rules: + - path: "api/*" + linters: + - lll + - path: "internal/*" + linters: + - dupl + - lll +linters: + disable-all: true + enable: + - dupl + - errcheck + - exportloopref + - goconst + - gocyclo + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - prealloc + - staticcheck + - typecheck + - unconvert + - unparam + - unused diff --git a/testdata/project-v4-with-grafana/.workflows/lint.yml b/testdata/project-v4-with-grafana/.workflows/lint.yml new file mode 100644 index 00000000000..1e49331bebb --- /dev/null +++ b/testdata/project-v4-with-grafana/.workflows/lint.yml @@ -0,0 +1,24 @@ +name: Lint + +# Trigger the workflow on pull requests and direct pushes to any branch +on: + push: + pull_request: + +jobs: + lint: + name: golangci-lint + runs-on: ubuntu-latest + # Pull requests from the same repository won't trigger this checks as they were already triggered by the push + if: (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository) + steps: + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '~1.20' + - name: Clone the code + uses: actions/checkout@v4 + - name: Run linter + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 diff --git a/testdata/project-v4-with-grafana/Makefile b/testdata/project-v4-with-grafana/Makefile index 751cdd77165..fc949b7f470 100644 --- a/testdata/project-v4-with-grafana/Makefile +++ b/testdata/project-v4-with-grafana/Makefile @@ -64,6 +64,21 @@ vet: ## Run go vet against code. test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out +GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint +golangci-lint: + @[ -f $(GOLANGCI_LINT) ] || { \ + set -e ;\ + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.54.2 ;\ + } + +.PHONY: lint +lint: golangci-lint ## Run golangci-lint linter & yamllint + $(GOLANGCI_LINT) run + +.PHONY: lint-fix +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes + $(GOLANGCI_LINT) run --fix + ##@ Build .PHONY: build diff --git a/testdata/project-v4/.golangci.yml b/testdata/project-v4/.golangci.yml new file mode 100644 index 00000000000..aed8644d11e --- /dev/null +++ b/testdata/project-v4/.golangci.yml @@ -0,0 +1,40 @@ +run: + deadline: 5m + allow-parallel-runners: true + +issues: + # don't skip warning about doc comments + # don't exclude the default set of lint + exclude-use-default: false + # restore some of the defaults + # (fill in the rest as needed) + exclude-rules: + - path: "api/*" + linters: + - lll + - path: "internal/*" + linters: + - dupl + - lll +linters: + disable-all: true + enable: + - dupl + - errcheck + - exportloopref + - goconst + - gocyclo + - gofmt + - goimports + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - prealloc + - staticcheck + - typecheck + - unconvert + - unparam + - unused diff --git a/testdata/project-v4/.workflows/lint.yml b/testdata/project-v4/.workflows/lint.yml new file mode 100644 index 00000000000..1e49331bebb --- /dev/null +++ b/testdata/project-v4/.workflows/lint.yml @@ -0,0 +1,24 @@ +name: Lint + +# Trigger the workflow on pull requests and direct pushes to any branch +on: + push: + pull_request: + +jobs: + lint: + name: golangci-lint + runs-on: ubuntu-latest + # Pull requests from the same repository won't trigger this checks as they were already triggered by the push + if: (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository) + steps: + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '~1.20' + - name: Clone the code + uses: actions/checkout@v4 + - name: Run linter + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 diff --git a/testdata/project-v4/Makefile b/testdata/project-v4/Makefile index 751cdd77165..fc949b7f470 100644 --- a/testdata/project-v4/Makefile +++ b/testdata/project-v4/Makefile @@ -64,6 +64,21 @@ vet: ## Run go vet against code. test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out +GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint +golangci-lint: + @[ -f $(GOLANGCI_LINT) ] || { \ + set -e ;\ + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.54.2 ;\ + } + +.PHONY: lint +lint: golangci-lint ## Run golangci-lint linter & yamllint + $(GOLANGCI_LINT) run + +.PHONY: lint-fix +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes + $(GOLANGCI_LINT) run --fix + ##@ Build .PHONY: build diff --git a/testdata/project-v4/api/v1/admiral_webhook.go b/testdata/project-v4/api/v1/admiral_webhook.go index b809f0feb0e..26b2c9f44c6 100644 --- a/testdata/project-v4/api/v1/admiral_webhook.go +++ b/testdata/project-v4/api/v1/admiral_webhook.go @@ -25,6 +25,7 @@ import ( // log is for logging in this package. var admirallog = logf.Log.WithName("admiral-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *Admiral) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/testdata/project-v4/api/v1/captain_webhook.go b/testdata/project-v4/api/v1/captain_webhook.go index 7948f0387a6..ed99f7f0e55 100644 --- a/testdata/project-v4/api/v1/captain_webhook.go +++ b/testdata/project-v4/api/v1/captain_webhook.go @@ -27,6 +27,7 @@ import ( // log is for logging in this package. var captainlog = logf.Log.WithName("captain-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *Captain) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/testdata/project-v4/api/v1/firstmate_webhook.go b/testdata/project-v4/api/v1/firstmate_webhook.go index 941a86ed458..fda29007398 100644 --- a/testdata/project-v4/api/v1/firstmate_webhook.go +++ b/testdata/project-v4/api/v1/firstmate_webhook.go @@ -24,6 +24,7 @@ import ( // log is for logging in this package. var firstmatelog = logf.Log.WithName("firstmate-resource") +// SetupWebhookWithManager will setup the manager to manage the webhooks func (r *FirstMate) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(r). diff --git a/testdata/project-v4/api/v1/webhook_suite_test.go b/testdata/project-v4/api/v1/webhook_suite_test.go index bd1f7f75811..f15fd5f894a 100644 --- a/testdata/project-v4/api/v1/webhook_suite_test.go +++ b/testdata/project-v4/api/v1/webhook_suite_test.go @@ -135,8 +135,7 @@ var _ = BeforeSuite(func() { if err != nil { return err } - conn.Close() - return nil + return conn.Close() }).Should(Succeed()) })