diff --git a/docs/book/src/cronjob-tutorial/testdata/project/.custom-gcl.yml b/docs/book/src/cronjob-tutorial/testdata/project/.custom-gcl.yml new file mode 100644 index 00000000000..fd284c65732 --- /dev/null +++ b/docs/book/src/cronjob-tutorial/testdata/project/.custom-gcl.yml @@ -0,0 +1,7 @@ +version: v2.7.2 +name: golangci-lint-kube-api +destination: ./bin + +plugins: + - module: sigs.k8s.io/kube-api-linter + version: latest diff --git a/docs/book/src/cronjob-tutorial/testdata/project/.github/workflows/lint.yml b/docs/book/src/cronjob-tutorial/testdata/project/.github/workflows/lint.yml index 2681d30f7a9..6e1b6aa5802 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/.github/workflows/lint.yml +++ b/docs/book/src/cronjob-tutorial/testdata/project/.github/workflows/lint.yml @@ -17,7 +17,13 @@ jobs: with: go-version-file: go.mod + - name: Check linter configuration + run: make lint-config + - name: Run linter uses: golangci/golangci-lint-action@v8 with: version: v2.7.2 + + - name: Run lint target + run: make lint diff --git a/docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml b/docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml index a8e84c1f3ef..45bb0da5b36 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml +++ b/docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml @@ -22,6 +22,7 @@ linters: - unconvert - unparam - unused + - kubeapilinter settings: revive: rules: @@ -30,6 +31,17 @@ linters: modernize: disable: - omitzero + custom: + kubeapilinter: + type: module + description: "Kube API Linter plugin" + original-url: "sigs.k8s.io/kube-api-linter" + settings: + linters: {} + lintersConfig: + optionalfields: + pointers: + preference: WhenRequired exclusions: generated: lax rules: @@ -40,6 +52,9 @@ linters: - dupl - lll path: internal/* + - path-except: "^api/" + linters: + - kubeapilinter paths: - third_party$ - builtin$ diff --git a/docs/book/src/cronjob-tutorial/testdata/project/Makefile b/docs/book/src/cronjob-tutorial/testdata/project/Makefile index 16d4fd06cc3..339e28a8da6 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/Makefile +++ b/docs/book/src/cronjob-tutorial/testdata/project/Makefile @@ -188,7 +188,8 @@ KIND ?= kind KUSTOMIZE ?= $(LOCALBIN)/kustomize CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen ENVTEST ?= $(LOCALBIN)/setup-envtest -GOLANGCI_LINT = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT_BASE = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-kube-api ## Tool Versions KUSTOMIZE_VERSION ?= v5.7.1 @@ -230,8 +231,17 @@ $(ENVTEST): $(LOCALBIN) .PHONY: golangci-lint golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. -$(GOLANGCI_LINT): $(LOCALBIN) - $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT_BASE): $(LOCALBIN) + $(call go-install-tool,$(GOLANGCI_LINT_BASE),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT): $(GOLANGCI_LINT_BASE) .custom-gcl.yml + @echo "Running golangci-lint custom..." + @$(GOLANGCI_LINT_BASE) custom || { \ + echo "golangci-lint failed. Cleaning up..."; \ + rm -f $(GOLANGCI_LINT_BASE); \ + exit 1; \ + } # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist # $1 - target path with name of binary diff --git a/docs/book/src/getting-started/testdata/project/.custom-gcl.yml b/docs/book/src/getting-started/testdata/project/.custom-gcl.yml new file mode 100644 index 00000000000..fd284c65732 --- /dev/null +++ b/docs/book/src/getting-started/testdata/project/.custom-gcl.yml @@ -0,0 +1,7 @@ +version: v2.7.2 +name: golangci-lint-kube-api +destination: ./bin + +plugins: + - module: sigs.k8s.io/kube-api-linter + version: latest diff --git a/docs/book/src/getting-started/testdata/project/.github/workflows/lint.yml b/docs/book/src/getting-started/testdata/project/.github/workflows/lint.yml index 2681d30f7a9..6e1b6aa5802 100644 --- a/docs/book/src/getting-started/testdata/project/.github/workflows/lint.yml +++ b/docs/book/src/getting-started/testdata/project/.github/workflows/lint.yml @@ -17,7 +17,13 @@ jobs: with: go-version-file: go.mod + - name: Check linter configuration + run: make lint-config + - name: Run linter uses: golangci/golangci-lint-action@v8 with: version: v2.7.2 + + - name: Run lint target + run: make lint diff --git a/docs/book/src/getting-started/testdata/project/.golangci.yml b/docs/book/src/getting-started/testdata/project/.golangci.yml index a8e84c1f3ef..45bb0da5b36 100644 --- a/docs/book/src/getting-started/testdata/project/.golangci.yml +++ b/docs/book/src/getting-started/testdata/project/.golangci.yml @@ -22,6 +22,7 @@ linters: - unconvert - unparam - unused + - kubeapilinter settings: revive: rules: @@ -30,6 +31,17 @@ linters: modernize: disable: - omitzero + custom: + kubeapilinter: + type: module + description: "Kube API Linter plugin" + original-url: "sigs.k8s.io/kube-api-linter" + settings: + linters: {} + lintersConfig: + optionalfields: + pointers: + preference: WhenRequired exclusions: generated: lax rules: @@ -40,6 +52,9 @@ linters: - dupl - lll path: internal/* + - path-except: "^api/" + linters: + - kubeapilinter paths: - third_party$ - builtin$ diff --git a/docs/book/src/getting-started/testdata/project/Makefile b/docs/book/src/getting-started/testdata/project/Makefile index d3c87c32861..b0b993ac83e 100644 --- a/docs/book/src/getting-started/testdata/project/Makefile +++ b/docs/book/src/getting-started/testdata/project/Makefile @@ -184,7 +184,8 @@ KIND ?= kind KUSTOMIZE ?= $(LOCALBIN)/kustomize CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen ENVTEST ?= $(LOCALBIN)/setup-envtest -GOLANGCI_LINT = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT_BASE = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-kube-api ## Tool Versions KUSTOMIZE_VERSION ?= v5.7.1 @@ -226,8 +227,17 @@ $(ENVTEST): $(LOCALBIN) .PHONY: golangci-lint golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. -$(GOLANGCI_LINT): $(LOCALBIN) - $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT_BASE): $(LOCALBIN) + $(call go-install-tool,$(GOLANGCI_LINT_BASE),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT): $(GOLANGCI_LINT_BASE) .custom-gcl.yml + @echo "Running golangci-lint custom..." + @$(GOLANGCI_LINT_BASE) custom || { \ + echo "golangci-lint failed. Cleaning up..."; \ + rm -f $(GOLANGCI_LINT_BASE); \ + exit 1; \ + } # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist # $1 - target path with name of binary diff --git a/docs/book/src/multiversion-tutorial/testdata/project/.custom-gcl.yml b/docs/book/src/multiversion-tutorial/testdata/project/.custom-gcl.yml new file mode 100644 index 00000000000..fd284c65732 --- /dev/null +++ b/docs/book/src/multiversion-tutorial/testdata/project/.custom-gcl.yml @@ -0,0 +1,7 @@ +version: v2.7.2 +name: golangci-lint-kube-api +destination: ./bin + +plugins: + - module: sigs.k8s.io/kube-api-linter + version: latest diff --git a/docs/book/src/multiversion-tutorial/testdata/project/.github/workflows/lint.yml b/docs/book/src/multiversion-tutorial/testdata/project/.github/workflows/lint.yml index 2681d30f7a9..6e1b6aa5802 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/.github/workflows/lint.yml +++ b/docs/book/src/multiversion-tutorial/testdata/project/.github/workflows/lint.yml @@ -17,7 +17,13 @@ jobs: with: go-version-file: go.mod + - name: Check linter configuration + run: make lint-config + - name: Run linter uses: golangci/golangci-lint-action@v8 with: version: v2.7.2 + + - name: Run lint target + run: make lint diff --git a/docs/book/src/multiversion-tutorial/testdata/project/.golangci.yml b/docs/book/src/multiversion-tutorial/testdata/project/.golangci.yml index a8e84c1f3ef..45bb0da5b36 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/.golangci.yml +++ b/docs/book/src/multiversion-tutorial/testdata/project/.golangci.yml @@ -22,6 +22,7 @@ linters: - unconvert - unparam - unused + - kubeapilinter settings: revive: rules: @@ -30,6 +31,17 @@ linters: modernize: disable: - omitzero + custom: + kubeapilinter: + type: module + description: "Kube API Linter plugin" + original-url: "sigs.k8s.io/kube-api-linter" + settings: + linters: {} + lintersConfig: + optionalfields: + pointers: + preference: WhenRequired exclusions: generated: lax rules: @@ -40,6 +52,9 @@ linters: - dupl - lll path: internal/* + - path-except: "^api/" + linters: + - kubeapilinter paths: - third_party$ - builtin$ diff --git a/docs/book/src/multiversion-tutorial/testdata/project/Makefile b/docs/book/src/multiversion-tutorial/testdata/project/Makefile index 16d4fd06cc3..339e28a8da6 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/Makefile +++ b/docs/book/src/multiversion-tutorial/testdata/project/Makefile @@ -188,7 +188,8 @@ KIND ?= kind KUSTOMIZE ?= $(LOCALBIN)/kustomize CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen ENVTEST ?= $(LOCALBIN)/setup-envtest -GOLANGCI_LINT = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT_BASE = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-kube-api ## Tool Versions KUSTOMIZE_VERSION ?= v5.7.1 @@ -230,8 +231,17 @@ $(ENVTEST): $(LOCALBIN) .PHONY: golangci-lint golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. -$(GOLANGCI_LINT): $(LOCALBIN) - $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT_BASE): $(LOCALBIN) + $(call go-install-tool,$(GOLANGCI_LINT_BASE),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT): $(GOLANGCI_LINT_BASE) .custom-gcl.yml + @echo "Running golangci-lint custom..." + @$(GOLANGCI_LINT_BASE) custom || { \ + echo "golangci-lint failed. Cleaning up..."; \ + rm -f $(GOLANGCI_LINT_BASE); \ + exit 1; \ + } # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist # $1 - target path with name of binary diff --git a/pkg/plugins/golang/v4/scaffolds/init.go b/pkg/plugins/golang/v4/scaffolds/init.go index beb50fabe04..a08be0f175c 100644 --- a/pkg/plugins/golang/v4/scaffolds/init.go +++ b/pkg/plugins/golang/v4/scaffolds/init.go @@ -176,6 +176,7 @@ func (s *initScaffolder) Scaffold() error { &templates.DockerIgnore{}, &templates.Readme{CommandName: s.commandName}, &templates.Golangci{}, + &templates.CustomGcl{GolangciLintVersion: GolangciLintVersion}, &e2e.Test{}, &e2e.WebhookTestUpdater{WireWebhook: false}, &e2e.SuiteTest{}, diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/custom-gcl.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/custom-gcl.go new file mode 100644 index 00000000000..0efda5231e6 --- /dev/null +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/custom-gcl.go @@ -0,0 +1,54 @@ +/* +Copyright 2025 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/v4/pkg/machinery" +) + +var _ machinery.Template = &CustomGcl{} + +// CustomGcl scaffolds a file ..custom-gcl.yaml which define KAL configuration to install +type CustomGcl struct { + machinery.TemplateMixin + machinery.ProjectNameMixin + + // GolangciLintVersion is the golangci-lint version used to build the custom binary + GolangciLintVersion string +} + +// SetTemplateDefaults implements machinery.Template +func (f *CustomGcl) SetTemplateDefaults() error { + if f.Path == "" { + f.Path = ".custom-gcl.yml" + } + + f.TemplateBody = customGCLTemplate + + f.IfExistsAction = machinery.SkipFile + + return nil +} + +const customGCLTemplate = `version: {{ .GolangciLintVersion }} +name: golangci-lint-kube-api +destination: ./bin + +plugins: + - module: sigs.k8s.io/kube-api-linter + version: latest +` diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/github/lint.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/github/lint.go index 730eba48ab4..b0605ce646d 100644 --- a/pkg/plugins/golang/v4/scaffolds/internal/templates/github/lint.go +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/github/lint.go @@ -65,8 +65,14 @@ jobs: with: go-version-file: go.mod + - name: Check linter configuration + run: make lint-config + - name: Run linter uses: golangci/golangci-lint-action@v8 with: version: {{ .GolangciLintVersion }} + + - name: Run lint target + run: make lint ` diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/golangci.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/golangci.go index d0f448f3d64..a282a58110a 100644 --- a/pkg/plugins/golang/v4/scaffolds/internal/templates/golangci.go +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/golangci.go @@ -65,6 +65,7 @@ linters: - unconvert - unparam - unused + - kubeapilinter settings: revive: rules: @@ -73,6 +74,17 @@ linters: modernize: disable: - omitzero + custom: + kubeapilinter: + type: module + description: "Kube API Linter plugin" + original-url: "sigs.k8s.io/kube-api-linter" + settings: + linters: {} + lintersConfig: + optionalfields: + pointers: + preference: WhenRequired exclusions: generated: lax rules: @@ -83,6 +95,9 @@ linters: - dupl - lll path: internal/* + - path-except: "^api/" + linters: + - kubeapilinter paths: - third_party$ - builtin$ diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go index 4ee5fbc7ff7..c411d9efd91 100644 --- a/pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go @@ -263,7 +263,8 @@ KIND ?= kind KUSTOMIZE ?= $(LOCALBIN)/kustomize CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen ENVTEST ?= $(LOCALBIN)/setup-envtest -GOLANGCI_LINT = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT_BASE = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-kube-api ## Tool Versions KUSTOMIZE_VERSION ?= {{ .KustomizeVersion }} @@ -305,8 +306,17 @@ $(ENVTEST): $(LOCALBIN) .PHONY: golangci-lint golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. -$(GOLANGCI_LINT): $(LOCALBIN) - $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT_BASE): $(LOCALBIN) + $(call go-install-tool,$(GOLANGCI_LINT_BASE),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT): $(GOLANGCI_LINT_BASE) .custom-gcl.yml + @echo "Running golangci-lint custom..." + @$(GOLANGCI_LINT_BASE) custom || { \ + echo "golangci-lint failed. Cleaning up..."; \ + rm -f $(GOLANGCI_LINT_BASE); \ + exit 1; \ + } # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist # $1 - target path with name of binary diff --git a/testdata/project-v4-multigroup/.custom-gcl.yml b/testdata/project-v4-multigroup/.custom-gcl.yml new file mode 100644 index 00000000000..fd284c65732 --- /dev/null +++ b/testdata/project-v4-multigroup/.custom-gcl.yml @@ -0,0 +1,7 @@ +version: v2.7.2 +name: golangci-lint-kube-api +destination: ./bin + +plugins: + - module: sigs.k8s.io/kube-api-linter + version: latest diff --git a/testdata/project-v4-multigroup/.github/workflows/lint.yml b/testdata/project-v4-multigroup/.github/workflows/lint.yml index 2681d30f7a9..6e1b6aa5802 100644 --- a/testdata/project-v4-multigroup/.github/workflows/lint.yml +++ b/testdata/project-v4-multigroup/.github/workflows/lint.yml @@ -17,7 +17,13 @@ jobs: with: go-version-file: go.mod + - name: Check linter configuration + run: make lint-config + - name: Run linter uses: golangci/golangci-lint-action@v8 with: version: v2.7.2 + + - name: Run lint target + run: make lint diff --git a/testdata/project-v4-multigroup/.golangci.yml b/testdata/project-v4-multigroup/.golangci.yml index a8e84c1f3ef..45bb0da5b36 100644 --- a/testdata/project-v4-multigroup/.golangci.yml +++ b/testdata/project-v4-multigroup/.golangci.yml @@ -22,6 +22,7 @@ linters: - unconvert - unparam - unused + - kubeapilinter settings: revive: rules: @@ -30,6 +31,17 @@ linters: modernize: disable: - omitzero + custom: + kubeapilinter: + type: module + description: "Kube API Linter plugin" + original-url: "sigs.k8s.io/kube-api-linter" + settings: + linters: {} + lintersConfig: + optionalfields: + pointers: + preference: WhenRequired exclusions: generated: lax rules: @@ -40,6 +52,9 @@ linters: - dupl - lll path: internal/* + - path-except: "^api/" + linters: + - kubeapilinter paths: - third_party$ - builtin$ diff --git a/testdata/project-v4-multigroup/Makefile b/testdata/project-v4-multigroup/Makefile index 2043b9587fe..8af071c72a5 100644 --- a/testdata/project-v4-multigroup/Makefile +++ b/testdata/project-v4-multigroup/Makefile @@ -184,7 +184,8 @@ KIND ?= kind KUSTOMIZE ?= $(LOCALBIN)/kustomize CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen ENVTEST ?= $(LOCALBIN)/setup-envtest -GOLANGCI_LINT = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT_BASE = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-kube-api ## Tool Versions KUSTOMIZE_VERSION ?= v5.7.1 @@ -226,8 +227,17 @@ $(ENVTEST): $(LOCALBIN) .PHONY: golangci-lint golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. -$(GOLANGCI_LINT): $(LOCALBIN) - $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT_BASE): $(LOCALBIN) + $(call go-install-tool,$(GOLANGCI_LINT_BASE),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT): $(GOLANGCI_LINT_BASE) .custom-gcl.yml + @echo "Running golangci-lint custom..." + @$(GOLANGCI_LINT_BASE) custom || { \ + echo "golangci-lint failed. Cleaning up..."; \ + rm -f $(GOLANGCI_LINT_BASE); \ + exit 1; \ + } # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist # $1 - target path with name of binary diff --git a/testdata/project-v4-with-plugins/.custom-gcl.yml b/testdata/project-v4-with-plugins/.custom-gcl.yml new file mode 100644 index 00000000000..fd284c65732 --- /dev/null +++ b/testdata/project-v4-with-plugins/.custom-gcl.yml @@ -0,0 +1,7 @@ +version: v2.7.2 +name: golangci-lint-kube-api +destination: ./bin + +plugins: + - module: sigs.k8s.io/kube-api-linter + version: latest diff --git a/testdata/project-v4-with-plugins/.github/workflows/lint.yml b/testdata/project-v4-with-plugins/.github/workflows/lint.yml index 2681d30f7a9..6e1b6aa5802 100644 --- a/testdata/project-v4-with-plugins/.github/workflows/lint.yml +++ b/testdata/project-v4-with-plugins/.github/workflows/lint.yml @@ -17,7 +17,13 @@ jobs: with: go-version-file: go.mod + - name: Check linter configuration + run: make lint-config + - name: Run linter uses: golangci/golangci-lint-action@v8 with: version: v2.7.2 + + - name: Run lint target + run: make lint diff --git a/testdata/project-v4-with-plugins/.golangci.yml b/testdata/project-v4-with-plugins/.golangci.yml index a8e84c1f3ef..45bb0da5b36 100644 --- a/testdata/project-v4-with-plugins/.golangci.yml +++ b/testdata/project-v4-with-plugins/.golangci.yml @@ -22,6 +22,7 @@ linters: - unconvert - unparam - unused + - kubeapilinter settings: revive: rules: @@ -30,6 +31,17 @@ linters: modernize: disable: - omitzero + custom: + kubeapilinter: + type: module + description: "Kube API Linter plugin" + original-url: "sigs.k8s.io/kube-api-linter" + settings: + linters: {} + lintersConfig: + optionalfields: + pointers: + preference: WhenRequired exclusions: generated: lax rules: @@ -40,6 +52,9 @@ linters: - dupl - lll path: internal/* + - path-except: "^api/" + linters: + - kubeapilinter paths: - third_party$ - builtin$ diff --git a/testdata/project-v4-with-plugins/Makefile b/testdata/project-v4-with-plugins/Makefile index 80f155fac70..94fbffce480 100644 --- a/testdata/project-v4-with-plugins/Makefile +++ b/testdata/project-v4-with-plugins/Makefile @@ -184,7 +184,8 @@ KIND ?= kind KUSTOMIZE ?= $(LOCALBIN)/kustomize CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen ENVTEST ?= $(LOCALBIN)/setup-envtest -GOLANGCI_LINT = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT_BASE = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-kube-api ## Tool Versions KUSTOMIZE_VERSION ?= v5.7.1 @@ -226,8 +227,17 @@ $(ENVTEST): $(LOCALBIN) .PHONY: golangci-lint golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. -$(GOLANGCI_LINT): $(LOCALBIN) - $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT_BASE): $(LOCALBIN) + $(call go-install-tool,$(GOLANGCI_LINT_BASE),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT): $(GOLANGCI_LINT_BASE) .custom-gcl.yml + @echo "Running golangci-lint custom..." + @$(GOLANGCI_LINT_BASE) custom || { \ + echo "golangci-lint failed. Cleaning up..."; \ + rm -f $(GOLANGCI_LINT_BASE); \ + exit 1; \ + } # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist # $1 - target path with name of binary diff --git a/testdata/project-v4/.custom-gcl.yml b/testdata/project-v4/.custom-gcl.yml new file mode 100644 index 00000000000..fd284c65732 --- /dev/null +++ b/testdata/project-v4/.custom-gcl.yml @@ -0,0 +1,7 @@ +version: v2.7.2 +name: golangci-lint-kube-api +destination: ./bin + +plugins: + - module: sigs.k8s.io/kube-api-linter + version: latest diff --git a/testdata/project-v4/.github/workflows/lint.yml b/testdata/project-v4/.github/workflows/lint.yml index 2681d30f7a9..6e1b6aa5802 100644 --- a/testdata/project-v4/.github/workflows/lint.yml +++ b/testdata/project-v4/.github/workflows/lint.yml @@ -17,7 +17,13 @@ jobs: with: go-version-file: go.mod + - name: Check linter configuration + run: make lint-config + - name: Run linter uses: golangci/golangci-lint-action@v8 with: version: v2.7.2 + + - name: Run lint target + run: make lint diff --git a/testdata/project-v4/.golangci.yml b/testdata/project-v4/.golangci.yml index a8e84c1f3ef..45bb0da5b36 100644 --- a/testdata/project-v4/.golangci.yml +++ b/testdata/project-v4/.golangci.yml @@ -22,6 +22,7 @@ linters: - unconvert - unparam - unused + - kubeapilinter settings: revive: rules: @@ -30,6 +31,17 @@ linters: modernize: disable: - omitzero + custom: + kubeapilinter: + type: module + description: "Kube API Linter plugin" + original-url: "sigs.k8s.io/kube-api-linter" + settings: + linters: {} + lintersConfig: + optionalfields: + pointers: + preference: WhenRequired exclusions: generated: lax rules: @@ -40,6 +52,9 @@ linters: - dupl - lll path: internal/* + - path-except: "^api/" + linters: + - kubeapilinter paths: - third_party$ - builtin$ diff --git a/testdata/project-v4/Makefile b/testdata/project-v4/Makefile index c1e4c5d1fff..30b49ac447d 100644 --- a/testdata/project-v4/Makefile +++ b/testdata/project-v4/Makefile @@ -184,7 +184,8 @@ KIND ?= kind KUSTOMIZE ?= $(LOCALBIN)/kustomize CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen ENVTEST ?= $(LOCALBIN)/setup-envtest -GOLANGCI_LINT = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT_BASE = $(LOCALBIN)/golangci-lint +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-kube-api ## Tool Versions KUSTOMIZE_VERSION ?= v5.7.1 @@ -226,8 +227,17 @@ $(ENVTEST): $(LOCALBIN) .PHONY: golangci-lint golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. -$(GOLANGCI_LINT): $(LOCALBIN) - $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT_BASE): $(LOCALBIN) + $(call go-install-tool,$(GOLANGCI_LINT_BASE),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) + +$(GOLANGCI_LINT): $(GOLANGCI_LINT_BASE) .custom-gcl.yml + @echo "Running golangci-lint custom..." + @$(GOLANGCI_LINT_BASE) custom || { \ + echo "golangci-lint failed. Cleaning up..."; \ + rm -f $(GOLANGCI_LINT_BASE); \ + exit 1; \ + } # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist # $1 - target path with name of binary