Skip to content

Commit bdee88d

Browse files
(experimental) - Add kube-api custom lint
1 parent 202a630 commit bdee88d

File tree

72 files changed

+698
-298
lines changed

Some content is hidden

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

72 files changed

+698
-298
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/.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+
# This plugin is used 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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,18 @@ type CronJobStatus struct {
169169
type CronJob struct {
170170
/*
171171
*/
172-
metav1.TypeMeta `json:",inline"`
172+
metav1.TypeMeta `json:",inline"`
173+
174+
// objectMeta is a standard object metadata.
175+
// +optional
173176
metav1.ObjectMeta `json:"metadata,omitempty"`
174177

175-
Spec CronJobSpec `json:"spec,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
176184
Status CronJobStatus `json:"status,omitempty"`
177185
}
178186

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
@@ -85,7 +85,7 @@ cleanup-test-e2e: ## Tear down the Kind cluster used for e2e tests
8585
@$(KIND) delete cluster --name $(KIND_CLUSTER)
8686

8787
.PHONY: lint
88-
lint: golangci-lint ## Run golangci-lint linter
88+
lint: golangci-lint kube-api-linter ## Run golangci-lint linter
8989
$(GOLANGCI_LINT) run
9090

9191
.PHONY: lint-fix
@@ -215,6 +215,23 @@ golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
215215
$(GOLANGCI_LINT): $(LOCALBIN)
216216
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
217217

218+
# This plugin is used 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+
218235
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
219236
# $1 - target path with name of binary
220237
# $2 - package url which can be installed

docs/book/src/getting-started/testdata/project/api/v1alpha1/memcached_types.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ type MemcachedSpec struct {
3131
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
3232
// Important: Run "make" to regenerate code after modifying this file
3333

34-
// Size defines the number of Memcached instances
35-
// The following markers will use OpenAPI v3 schema to validate the value
36-
// More info: https://book.kubebuilder.io/reference/markers/crd-validation.html
34+
// size defines the number of Memcached instances
3735
// +kubebuilder:validation:Minimum=1
3836
// +kubebuilder:validation:Maximum=3
3937
// +kubebuilder:validation:ExclusiveMaximum=false
38+
// +optional
4039
Size int32 `json:"size,omitempty"`
4140
}
4241

@@ -59,10 +58,18 @@ type MemcachedStatus struct {
5958

6059
// Memcached is the Schema for the memcacheds API.
6160
type Memcached struct {
62-
metav1.TypeMeta `json:",inline"`
61+
metav1.TypeMeta `json:",inline"`
62+
63+
// objectMeta is a standard object metadata.
64+
// +optional
6365
metav1.ObjectMeta `json:"metadata,omitempty"`
6466

65-
Spec MemcachedSpec `json:"spec,omitempty"`
67+
// spec defines the desired state of Memcached.
68+
// +optional
69+
Spec MemcachedSpec `json:"spec,omitempty"`
70+
71+
// status defines the observed state of Memcached.
72+
// +optional
6673
Status MemcachedStatus `json:"status,omitempty"`
6774
}
6875

docs/book/src/getting-started/testdata/project/config/crd/bases/cache.example.com_memcacheds.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,17 @@ spec:
3737
metadata:
3838
type: object
3939
spec:
40-
description: MemcachedSpec defines the desired state of Memcached.
40+
description: spec defines the desired state of Memcached.
4141
properties:
4242
size:
43-
description: |-
44-
Size defines the number of Memcached instances
45-
The following markers will use OpenAPI v3 schema to validate the value
46-
More info: https://book.kubebuilder.io/reference/markers/crd-validation.html
43+
description: size defines the number of Memcached instances
4744
format: int32
4845
maximum: 3
4946
minimum: 1
5047
type: integer
5148
type: object
5249
status:
53-
description: MemcachedStatus defines the observed state of Memcached.
50+
description: status defines the observed state of Memcached.
5451
properties:
5552
conditions:
5653
items:

docs/book/src/getting-started/testdata/project/dist/chart/templates/crd/cache.example.com_memcacheds.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,17 @@ spec:
4343
metadata:
4444
type: object
4545
spec:
46-
description: MemcachedSpec defines the desired state of Memcached.
46+
description: spec defines the desired state of Memcached.
4747
properties:
4848
size:
49-
description: |-
50-
Size defines the number of Memcached instances
51-
The following markers will use OpenAPI v3 schema to validate the value
52-
More info: https://book.kubebuilder.io/reference/markers/crd-validation.html
49+
description: size defines the number of Memcached instances
5350
format: int32
5451
maximum: 3
5552
minimum: 1
5653
type: integer
5754
type: object
5855
status:
59-
description: MemcachedStatus defines the observed state of Memcached.
56+
description: status defines the observed state of Memcached.
6057
properties:
6158
conditions:
6259
items:

docs/book/src/getting-started/testdata/project/dist/install.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,17 @@ spec:
4545
metadata:
4646
type: object
4747
spec:
48-
description: MemcachedSpec defines the desired state of Memcached.
48+
description: spec defines the desired state of Memcached.
4949
properties:
5050
size:
51-
description: |-
52-
Size defines the number of Memcached instances
53-
The following markers will use OpenAPI v3 schema to validate the value
54-
More info: https://book.kubebuilder.io/reference/markers/crd-validation.html
51+
description: size defines the number of Memcached instances
5552
format: int32
5653
maximum: 3
5754
minimum: 1
5855
type: integer
5956
type: object
6057
status:
61-
description: MemcachedStatus defines the observed state of Memcached.
58+
description: status defines the observed state of Memcached.
6259
properties:
6360
conditions:
6461
items:

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$

docs/book/src/multiversion-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+
# This plugin is used 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/multiversion-tutorial/testdata/project/api/v1/cronjob_types.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,18 @@ type CronJobStatus struct {
133133
// +kubebuilder:storageversion
134134
// CronJob is the Schema for the cronjobs API.
135135
type CronJob struct {
136-
metav1.TypeMeta `json:",inline"`
136+
metav1.TypeMeta `json:",inline"`
137+
138+
// objectMeta is a standard object metadata.
139+
// +optional
137140
metav1.ObjectMeta `json:"metadata,omitempty"`
138141

139-
Spec CronJobSpec `json:"spec,omitempty"`
142+
// spec defines the desired state of CronJob.
143+
// +optional
144+
Spec CronJobSpec `json:"spec,omitempty"`
145+
146+
// status defines the observed state of CronJob.
147+
// +optional
140148
Status CronJobStatus `json:"status,omitempty"`
141149
}
142150

docs/book/src/multiversion-tutorial/testdata/project/api/v2/cronjob_types.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,18 @@ type CronJobStatus struct {
164164
// +versionName=v2
165165
// CronJob is the Schema for the cronjobs API.
166166
type CronJob struct {
167-
metav1.TypeMeta `json:",inline"`
167+
metav1.TypeMeta `json:",inline"`
168+
169+
// objectMeta is a standard object metadata.
170+
// +optional
168171
metav1.ObjectMeta `json:"metadata,omitempty"`
169172

170-
Spec CronJobSpec `json:"spec,omitempty"`
173+
// spec defines the desired state of CronJob.
174+
// +optional
175+
Spec CronJobSpec `json:"spec,omitempty"`
176+
177+
// status defines the observed state of CronJob.
178+
// +optional
171179
Status CronJobStatus `json:"status,omitempty"`
172180
}
173181

hack/docs/internal/cronjob-tutorial/generate_cronjob.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ func (sp *Sample) updateSpec() {
191191
`// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
192192
// Important: Run "make" to regenerate code after modifying this file
193193
194-
// Foo is an example field of CronJob. Edit cronjob_types.go to remove/update
194+
// foo is an example field of CronJob. Edit cronjob_types.go to remove/update
195+
// +optional
195196
Foo string`+" `"+`json:"foo,omitempty"`+"`", "")
196197
hackutils.CheckError("fixing cronjob_types.go", err)
197198

0 commit comments

Comments
 (0)