Skip to content

Commit c7cc7ef

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 c7cc7ef

File tree

138 files changed

+45243
-564
lines changed

Some content is hidden

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

138 files changed

+45243
-564
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: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ help: ## Display this help.
4343

4444
.PHONY: manifests
4545
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
46-
# Note that the option maxDescLen=0 was added in the default scaffold in order to sort out the issue
47-
# Too long: must have at most 262144 bytes. By using kubectl apply to create / update resources an annotation
48-
# is created by K8s API to store the latest version of the resource ( kubectl.kubernetes.io/last-applied-configuration).
49-
# However, it has a size limit and if the CRD is too big with so many long descriptions as this one it will cause the failure.
50-
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:maxDescLen=0 webhook paths="./..." output:crd:artifacts:config=config/crd/bases
46+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
5147

5248
.PHONY: generate
5349
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
@@ -89,7 +85,7 @@ cleanup-test-e2e: ## Tear down the Kind cluster used for e2e tests
8985
@$(KIND) delete cluster --name $(KIND_CLUSTER)
9086

9187
.PHONY: lint
92-
lint: golangci-lint ## Run golangci-lint linter
88+
lint: golangci-lint kube-api-linter ## Run golangci-lint linter
9389
$(GOLANGCI_LINT) run
9490

9591
.PHONY: lint-fix
@@ -219,6 +215,23 @@ golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
219215
$(GOLANGCI_LINT): $(LOCALBIN)
220216
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
221217

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

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ import (
6565
// CronJobSpec defines the desired state of CronJob.
6666
type CronJobSpec struct {
6767
// +kubebuilder:validation:MinLength=0
68+
// +kubebuilder:validation:Required
6869

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

7273
// +kubebuilder:validation:Minimum=0
@@ -82,14 +83,17 @@ type CronJobSpec struct {
8283
// - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet;
8384
// - "Replace": cancels currently running job and replaces it with a new one
8485
// +optional
85-
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
86+
// +kubebuilder:default:=Allow
87+
ConcurrencyPolicy *ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
8688

87-
// This flag tells the controller to suspend subsequent executions, it does
89+
// suspend tells the controller to suspend subsequent executions, it does
8890
// not apply to already started executions. Defaults to false.
8991
// +optional
9092
Suspend *bool `json:"suspend,omitempty"`
9193

92-
// Specifies the job that will be created when executing a CronJob.
94+
// +kubebuilder:validation:Required
95+
96+
// jobTemplate defines the job that will be created when executing a CronJob.
9397
JobTemplate batchv1.JobTemplateSpec `json:"jobTemplate"`
9498

9599
// +kubebuilder:validation:Minimum=0
@@ -169,11 +173,19 @@ type CronJobStatus struct {
169173
type CronJob struct {
170174
/*
171175
*/
172-
metav1.TypeMeta `json:",inline"`
176+
metav1.TypeMeta `json:",inline"`
177+
178+
// metadata is a standard object metadata.
179+
// +optional
173180
metav1.ObjectMeta `json:"metadata,omitempty"`
174181

175-
Spec CronJobSpec `json:"spec,omitempty"`
176-
Status CronJobStatus `json:"status,omitempty"`
182+
// spec defines the desired state of CronJob.
183+
// +optional
184+
Spec *CronJobSpec `json:"spec,omitempty"`
185+
186+
// status defines the observed state of CronJob.
187+
// +optional
188+
Status *CronJobStatus `json:"status,omitempty"`
177189
}
178190

179191
// +kubebuilder:object:root=true

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

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

0 commit comments

Comments
 (0)