Skip to content

Commit 7b93ca6

Browse files
Add KAL custom linter
1 parent 6d91914 commit 7b93ca6

File tree

23 files changed

+240
-8
lines changed

23 files changed

+240
-8
lines changed

.github/workflows/lint-sample.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ jobs:
3333
- name: Prepare ${{ matrix.folder }}
3434
working-directory: ${{ matrix.folder }}
3535
run: go mod tidy
36+
- name: Build kubeapilinter
37+
working-directory: ${{ matrix.folder }}
38+
run: make kube-api-linter
39+
env:
40+
CGO_ENABLED: 1
3641
- name: Check linter configuration
3742
working-directory: ${{ matrix.folder }}
3843
run: make lint-config
@@ -41,6 +46,7 @@ jobs:
4146
with:
4247
version: v2.2.2
4348
working-directory: ${{ matrix.folder }}
49+
install-mode: goinstall
4450
- name: Run linter via makefile target
4551
working-directory: ${{ matrix.folder }}
4652
run: make lint

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.2.2
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
@@ -95,7 +95,7 @@ cleanup-test-e2e: ## Tear down the Kind cluster used for e2e tests
9595
@$(KIND) delete cluster --name $(KIND_CLUSTER)
9696

9797
.PHONY: lint
98-
lint: golangci-lint ## Run golangci-lint linter
98+
lint: golangci-lint kube-api-linter ## Run golangci-lint linter
9999
$(GOLANGCI_LINT) run
100100

101101
.PHONY: lint-fix
@@ -225,6 +225,23 @@ golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
225225
$(GOLANGCI_LINT): $(LOCALBIN)
226226
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
227227

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

docs/book/src/getting-started.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ we will allow configuring the number of instances with the following:
8383
```go
8484
type MemcachedSpec struct {
8585
...
86+
// +kubebuilder:validation:Minimum=0
87+
// +required
8688
Size int32 `json:"size,omitempty"`
8789
}
8890
```
@@ -97,7 +99,21 @@ similar to how we do with any resource from the Kubernetes API.
9799
```go
98100
// MemcachedStatus defines the observed state of Memcached
99101
type MemcachedStatus struct {
100-
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
102+
// For Kubernetes API conventions, see:
103+
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties
104+
105+
// conditions represent the current state of the {{ .Resource.Kind }} resource.
106+
// Each condition has a unique type and reflects the status of a specific aspect of the resource.
107+
//
108+
// Standard condition types include:
109+
// - "Available": the resource is fully functional.
110+
// - "Progressing": the resource is being created or updated.
111+
// - "Degraded": the resource failed to reach or maintain its desired state.
112+
//
113+
// The status of each condition is one of True, False, or Unknown.
114+
// +listType=map
115+
// +listMapKey=type
116+
Conditions []metav1.Condition `json:"conditions,omitempty"`
101117
}
102118
```
103119

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.2.2
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$

docs/book/src/getting-started/testdata/project/Makefile

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

9393
.PHONY: lint
94-
lint: golangci-lint ## Run golangci-lint linter
94+
lint: golangci-lint kube-api-linter ## Run golangci-lint linter
9595
$(GOLANGCI_LINT) run
9696

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

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

docs/book/src/multiversion-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.2.2
24+
install-mode: goinstall

docs/book/src/multiversion-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$

0 commit comments

Comments
 (0)