Skip to content

Commit b2c3dbb

Browse files
(go/v4,deploy-image/alpha-v1) feat: integrate kube-api-linter and fix scaffolds
- Add support for kube-api-linter to validate Kubernetes API definitions More info: https://github.com/kubernetes-sigs/kube-api-linter - Fix all tutorials (getting-started, cronjob, multiversion) to comply with lint rules - Update all plugins scaffolds to according to rules - Adjust GitHub Actions to build and run the new custom linter in CI
1 parent 202a630 commit b2c3dbb

File tree

122 files changed

+1234
-475
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+1234
-475
lines changed

.github/workflows/lint-sample.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ jobs:
3232
- name: Prepare ${{ matrix.folder }}
3333
working-directory: ${{ matrix.folder }}
3434
run: go mod tidy
35+
- name: Build kubeapilinter
36+
working-directory: ${{ matrix.folder }}
37+
run: make kube-api-linter
38+
env:
39+
CGO_ENABLED: 1
3540
- name: Check linter configuration
3641
working-directory: ${{ matrix.folder }}
3742
run: make lint-config
@@ -40,6 +45,7 @@ jobs:
4045
with:
4146
version: v2.1.6
4247
working-directory: ${{ matrix.folder }}
48+
install-mode: goinstall
4349
args: --config .golangci.yml ./...
4450
- name: Run linter via makefile target
4551
working-directory: ${{ matrix.folder }}

docs/book/src/cronjob-tutorial/testdata/project/.github/workflows/lint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ jobs:
2121
uses: golangci/golangci-lint-action@v8
2222
with:
2323
version: v2.1.6
24+
install-mode: goinstall

docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,20 @@ linters:
2121
- unconvert
2222
- unparam
2323
- unused
24+
- kubeapilinter
2425
settings:
2526
revive:
2627
rules:
2728
- name: comment-spacings
2829
- name: import-shadowing
30+
custom:
31+
kubeapilinter:
32+
path: "./bin/kube-api-linter.so"
33+
description: "Kube API Linter plugin"
34+
original-url: "sigs.k8s.io/kube-api-linter"
35+
settings:
36+
linters: { }
37+
lintersConfig: { }
2938
exclusions:
3039
generated: lax
3140
rules:
@@ -36,6 +45,9 @@ linters:
3645
- dupl
3746
- lll
3847
path: internal/*
48+
- path-except: "^api/"
49+
linters:
50+
- kubeapilinter
3951
paths:
4052
- third_party$
4153
- builtin$

docs/book/src/cronjob-tutorial/testdata/project/Makefile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ cleanup-test-e2e: ## Tear down the Kind cluster used for e2e tests
8989
@$(KIND) delete cluster --name $(KIND_CLUSTER)
9090

9191
.PHONY: lint
92-
lint: golangci-lint ## Run golangci-lint linter
92+
lint: golangci-lint kube-api-linter ## Run golangci-lint linter
9393
$(GOLANGCI_LINT) run
9494

9595
.PHONY: lint-fix
@@ -219,6 +219,23 @@ golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
219219
$(GOLANGCI_LINT): $(LOCALBIN)
220220
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
221221

222+
# To lint Kubernetes API definitions.
223+
# More info: https://github.com/kubernetes-sigs/kube-api-linter
224+
KUBE_API_LINTER_PLUGIN := $(LOCALBIN)/kube-api-linter.so
225+
KUBE_API_LINTER_BUILD_DIR := ./hack/kube-api-linter
226+
227+
.PHONY: kube-api-linter
228+
kube-api-linter: $(KUBE_API_LINTER_PLUGIN) ## Build the kube-api-linter plugin
229+
$(KUBE_API_LINTER_PLUGIN): $(KUBE_API_LINTER_BUILD_DIR)/go.mod
230+
cd $(KUBE_API_LINTER_BUILD_DIR) && \
231+
go build -buildmode=plugin -o "$(abspath $(KUBE_API_LINTER_PLUGIN))" sigs.k8s.io/kube-api-linter/pkg/plugin
232+
$(KUBE_API_LINTER_BUILD_DIR)/go.mod:
233+
@echo "Setting up local module for kube-api-linter plugin..."
234+
mkdir -p $(KUBE_API_LINTER_BUILD_DIR)
235+
cd $(KUBE_API_LINTER_BUILD_DIR) && \
236+
go mod init local-kube-api-linter && \
237+
go get sigs.k8s.io/kube-api-linter@latest
238+
222239
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
223240
# $1 - target path with name of binary
224241
# $2 - package url which can be installed

docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_types.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import (
6666
type CronJobSpec struct {
6767
// +kubebuilder:validation:MinLength=0
6868

69-
// The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
69+
// schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
7070
Schedule string `json:"schedule"`
7171

7272
// +kubebuilder:validation:Minimum=0
@@ -84,12 +84,12 @@ type CronJobSpec struct {
8484
// +optional
8585
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
8686

87-
// This flag tells the controller to suspend subsequent executions, it does
87+
// suspend tells the controller to suspend subsequent executions, it does
8888
// not apply to already started executions. Defaults to false.
8989
// +optional
9090
Suspend *bool `json:"suspend,omitempty"`
9191

92-
// Specifies the job that will be created when executing a CronJob.
92+
// jobTemplate defines the job that will be created when executing a CronJob.
9393
JobTemplate batchv1.JobTemplateSpec `json:"jobTemplate"`
9494

9595
// +kubebuilder:validation:Minimum=0
@@ -169,11 +169,19 @@ type CronJobStatus struct {
169169
type CronJob struct {
170170
/*
171171
*/
172-
metav1.TypeMeta `json:",inline"`
172+
metav1.TypeMeta `json:",inline"`
173+
174+
// metadata is a standard object metadata.
175+
// +optional
173176
metav1.ObjectMeta `json:"metadata,omitempty"`
174177

175-
Spec CronJobSpec `json:"spec,omitempty"`
176-
Status CronJobStatus `json:"status,omitempty"`
178+
// spec defines the desired state of CronJob.
179+
// +optional
180+
Spec *CronJobSpec `json:"spec,omitempty"`
181+
182+
// status defines the observed state of CronJob.
183+
// +optional
184+
Status *CronJobStatus `json:"status,omitempty"`
177185
}
178186

179187
// +kubebuilder:object:root=true

docs/book/src/cronjob-tutorial/testdata/project/api/v1/zz_generated.deepcopy.go

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ var _ = Describe("CronJob controller", func() {
7575
Name: CronjobName,
7676
Namespace: CronjobNamespace,
7777
},
78-
Spec: cronjobv1.CronJobSpec{
78+
Spec: &cronjobv1.CronJobSpec{
7979
Schedule: "1 * * * *",
8080
JobTemplate: batchv1.JobTemplateSpec{
8181
Spec: batchv1.JobSpec{

docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var _ = Describe("CronJob Webhook", func() {
3737

3838
BeforeEach(func() {
3939
obj = &batchv1.CronJob{
40-
Spec: batchv1.CronJobSpec{
40+
Spec: &batchv1.CronJobSpec{
4141
Schedule: schedule,
4242
ConcurrencyPolicy: batchv1.AllowConcurrent,
4343
SuccessfulJobsHistoryLimit: new(int32),
@@ -48,7 +48,7 @@ var _ = Describe("CronJob Webhook", func() {
4848
*obj.Spec.FailedJobsHistoryLimit = 1
4949

5050
oldObj = &batchv1.CronJob{
51-
Spec: batchv1.CronJobSpec{
51+
Spec: &batchv1.CronJobSpec{
5252
Schedule: schedule,
5353
ConcurrencyPolicy: batchv1.AllowConcurrent,
5454
SuccessfulJobsHistoryLimit: new(int32),

docs/book/src/getting-started/testdata/project/.github/workflows/lint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ jobs:
2121
uses: golangci/golangci-lint-action@v8
2222
with:
2323
version: v2.1.6
24+
install-mode: goinstall

docs/book/src/getting-started/testdata/project/.golangci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,20 @@ linters:
2121
- unconvert
2222
- unparam
2323
- unused
24+
- kubeapilinter
2425
settings:
2526
revive:
2627
rules:
2728
- name: comment-spacings
2829
- name: import-shadowing
30+
custom:
31+
kubeapilinter:
32+
path: "./bin/kube-api-linter.so"
33+
description: "Kube API Linter plugin"
34+
original-url: "sigs.k8s.io/kube-api-linter"
35+
settings:
36+
linters: { }
37+
lintersConfig: { }
2938
exclusions:
3039
generated: lax
3140
rules:
@@ -36,6 +45,9 @@ linters:
3645
- dupl
3746
- lll
3847
path: internal/*
48+
- path-except: "^api/"
49+
linters:
50+
- kubeapilinter
3951
paths:
4052
- third_party$
4153
- builtin$

0 commit comments

Comments
 (0)