Skip to content

Commit 8a94dfc

Browse files
feat: add kube api linter in kagent (#1759)
Add kube-api-linter: https://github.com/kubernetes-sigs/kube-api-linter ## Fix * Added the `// +kubebuilder:optional` marker to the `Conditions` field in the status struct to comply with Kubernetes API conventions and pass kube-apilinter validation. * Removed the conflicting `+kubebuilder:required` marker. * Kept `+kubebuilder:default:=xxx` and added the explicit `+kubebuilder:optional` marker to comply with Kubebuilder validation rules. --------- Signed-off-by: dongjiang <dongjiang1989@126.com> Co-authored-by: Eitan Yarmush <eitan.yarmush@solo.io>
1 parent 2f6dc28 commit 8a94dfc

28 files changed

Lines changed: 925 additions & 285 deletions

.github/workflows/ci.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,9 @@ jobs:
304304
go-version: "1.26"
305305
cache: true
306306
cache-dependency-path: go/go.sum
307-
- name: golangci-lint
308-
uses: golangci/golangci-lint-action@v9
309-
with:
310-
version: v2.11.3
311-
working-directory: go
307+
- name: golangci-lint
308+
working-directory: go
309+
run: make lint
312310

313311
python-test:
314312
env:

go/.golangci.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ linters:
1717
- importas
1818
- ineffassign
1919
- iotamixing
20+
- kubeapilinter
2021
- makezero
2122
- misspell
2223
- modernize
@@ -26,6 +27,43 @@ linters:
2627
- unused
2728
- whitespace
2829
settings:
30+
custom:
31+
kubeapilinter:
32+
path: "bin/kube-api-linter.so"
33+
description: Kube API Linter lints Kube like APIs based on API conventions and best practices.
34+
original-url: sigs.k8s.io/kube-api-linter
35+
settings:
36+
linters:
37+
enable:
38+
- "conflictingmarkers"
39+
- "duplicatemarkers"
40+
- "nofloats"
41+
- "optionalorrequired"
42+
- "statussubresource"
43+
- "uniquemarkers"
44+
- "statusoptional"
45+
- "nophase"
46+
- "nonullable"
47+
- "forbiddenmarkers"
48+
- "nomaps"
49+
disable:
50+
- "*"
51+
lintersConfig:
52+
conflictingmarkers:
53+
conflicts:
54+
- name: "default_vs_required"
55+
sets:
56+
- ["default", "kubebuilder:default"]
57+
- ["required", "kubebuilder:validation:Required", "k8s:required"]
58+
description: "A field with a default value cannot be required"
59+
forbiddenmarkers:
60+
markers:
61+
- identifier: "+kubebuilder:pruning:PreserveUnknownFields"
62+
- identifier: "+kubebuilder:validation:XPreserveUnknownFields"
63+
- identifier: "+kubebuilder:validation:items:XPreserveUnknownFields"
64+
- identifier: "+kubebuilder:validation:EmbeddedResource"
65+
- identifier: "+kubebuilder:validation:XEmbeddedResource"
66+
- identifier: "+kubebuilder:validation:items:XEmbeddedResource"
2967
depguard:
3068
rules:
3169
forbid-pkg-errors:
@@ -88,6 +126,10 @@ linters:
88126
paths:
89127
- zz_generated.*\.go$
90128
- .*conversion.*\.go$
129+
rules:
130+
- linters:
131+
- kubeapilinter
132+
path-except: "api/v1alpha"
91133
issues:
92134
max-issues-per-linter: 0
93135
max-same-issues: 0

go/Makefile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,29 @@ fmt: ## Run go fmt.
4343
vet: ## Run go vet.
4444
go vet ./...
4545

46+
KAL_SO = $(LOCALBIN)/kube-api-linter.so
47+
4648
.PHONY: lint
47-
lint: golangci-lint ## Run golangci-lint.
49+
lint: golangci-lint $(KAL_SO) ## Run golangci-lint.
4850
$(GOLANGCI_LINT) run
4951

5052
.PHONY: lint-fix
51-
lint-fix: golangci-lint ## Run golangci-lint with auto-fix.
53+
lint-fix: golangci-lint $(KAL_SO) ## Run golangci-lint with auto-fix.
5254
$(GOLANGCI_LINT) run --fix
5355

5456
.PHONY: lint-config
5557
lint-config: golangci-lint ## Verify golangci-lint linter configuration.
5658
$(GOLANGCI_LINT) config verify
5759

60+
$(KAL_SO):
61+
@mkdir -p $(LOCALBIN)
62+
@echo "Building kube-api-linter plugin..."
63+
@rm -rf $(LOCALBIN)/golangci-lint-src
64+
@git clone --depth 1 --branch $(GOLANGCI_LINT_VERSION) --quiet https://github.com/golangci/golangci-lint.git $(LOCALBIN)/golangci-lint-src
65+
@cd $(LOCALBIN)/golangci-lint-src && go get sigs.k8s.io/kube-api-linter@$(KAL_VERSION)
66+
@cd $(LOCALBIN)/golangci-lint-src && go build -buildmode=plugin -o $(KAL_SO) sigs.k8s.io/kube-api-linter/pkg/plugin
67+
@rm -rf $(LOCALBIN)/golangci-lint-src
68+
5869
.PHONY: govulncheck
5970
govulncheck: ## Run govulncheck.
6071
$(call go-install-tool,bin/govulncheck,golang.org/x/vuln/cmd/govulncheck,latest)
@@ -99,6 +110,8 @@ core/bin/kagent-windows-amd64.exe.sha256: core/bin/kagent-windows-amd64.exe
99110
.PHONY: clean
100111
clean:
101112
rm -f core/bin/kagent* && mkdir -p core/bin
113+
rm -f $(LOCALBIN)/kube-api-linter.so
114+
rm -rf $(LOCALBIN)/golangci-lint-src
102115

103116
.PHONY: build
104117
build: core/bin/kagent-linux-amd64.sha256 core/bin/kagent-linux-arm64.sha256 core/bin/kagent-darwin-amd64.sha256 core/bin/kagent-darwin-arm64.sha256 core/bin/kagent-windows-amd64.exe.sha256
@@ -136,6 +149,7 @@ ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller
136149
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
137150
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
138151
GOLANGCI_LINT_VERSION ?= v2.11.3
152+
KAL_VERSION ?= v0.0.0-20260423112246-3fa174937a6b
139153
SQLC_VERSION ?= v1.30.0
140154
BUF ?= $(LOCALBIN)/buf
141155
BUF_VERSION ?= v1.52.1

go/api/config/crd/bases/kagent.dev_agents.yaml

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,14 +2257,10 @@ spec:
22572257
type: string
22582258
type: object
22592259
type:
2260-
allOf:
2261-
- enum:
2262-
- McpServer
2263-
- Agent
2264-
- enum:
2265-
- McpServer
2266-
- Agent
22672260
description: ToolProviderType represents the tool provider type
2261+
enum:
2262+
- McpServer
2263+
- Agent
22682264
type: string
22692265
type: object
22702266
x-kubernetes-validations:
@@ -2347,9 +2343,6 @@ spec:
23472343
observedGeneration:
23482344
format: int64
23492345
type: integer
2350-
required:
2351-
- configHash
2352-
- observedGeneration
23532346
type: object
23542347
type: object
23552348
served: true
@@ -13052,19 +13045,15 @@ spec:
1305213045
type: array
1305313046
type: object
1305413047
runtime:
13055-
allOf:
13056-
- enum:
13057-
- python
13058-
- go
13059-
- enum:
13060-
- python
13061-
- go
1306213048
default: python
1306313049
description: |-
1306413050
Runtime specifies which ADK implementation to use for this agent.
1306513051
- "python": Uses the Python ADK (default, slower startup, full feature set)
1306613052
- "go": Uses the Go ADK (faster startup, most features supported)
1306713053
The runtime determines both the container image and readiness probe configuration.
13054+
enum:
13055+
- python
13056+
- go
1306813057
type: string
1306913058
stream:
1307013059
description: |-
@@ -13215,15 +13204,11 @@ spec:
1321513204
rule: '!has(self.requireApproval) || self.requireApproval.all(x,
1321613205
has(self.toolNames) && x in self.toolNames)'
1321713206
type:
13218-
allOf:
13219-
- enum:
13220-
- McpServer
13221-
- Agent
13222-
- enum:
13223-
- McpServer
13224-
- Agent
1322513207
description: ToolProviderType represents the tool provider
1322613208
type
13209+
enum:
13210+
- McpServer
13211+
- Agent
1322713212
type: string
1322813213
type: object
1322913214
x-kubernetes-validations:
@@ -13553,18 +13538,12 @@ spec:
1355313538
type: array
1355413539
type: object
1355513540
type:
13556-
allOf:
13557-
- enum:
13558-
- Declarative
13559-
- BYO
13560-
- enum:
13561-
- Declarative
13562-
- BYO
1356313541
default: Declarative
1356413542
description: AgentType represents the agent type
13543+
enum:
13544+
- Declarative
13545+
- BYO
1356513546
type: string
13566-
required:
13567-
- type
1356813547
type: object
1356913548
x-kubernetes-validations:
1357013549
- message: type must be specified
@@ -13637,8 +13616,6 @@ spec:
1363713616
observedGeneration:
1363813617
format: int64
1363913618
type: integer
13640-
required:
13641-
- observedGeneration
1364213619
type: object
1364313620
type: object
1364413621
served: true

go/api/config/crd/bases/kagent.dev_memories.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ spec:
8686
enum:
8787
- Pinecone
8888
type: string
89-
required:
90-
- provider
9189
type: object
9290
status:
9391
description: MemoryStatus defines the observed state of Memory.
@@ -151,9 +149,6 @@ spec:
151149
observedGeneration:
152150
format: int64
153151
type: integer
154-
required:
155-
- conditions
156-
- observedGeneration
157152
type: object
158153
type: object
159154
served: true

go/api/config/crd/bases/kagent.dev_modelconfigs.yaml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ spec:
259259
type: string
260260
required:
261261
- model
262-
- provider
263262
type: object
264263
x-kubernetes-validations:
265264
- message: provider.openAI must be nil if the provider is not OpenAI
@@ -340,9 +339,6 @@ spec:
340339
observedGeneration:
341340
format: int64
342341
type: integer
343-
required:
344-
- conditions
345-
- observedGeneration
346342
type: object
347343
type: object
348344
served: true
@@ -606,13 +602,10 @@ spec:
606602
- audience
607603
type: object
608604
type:
609-
allOf:
610-
- enum:
611-
- GDCHServiceAccount
612-
- enum:
613-
- GDCHServiceAccount
614605
description: TokenExchangeType identifies the token exchange
615606
mechanism
607+
enum:
608+
- GDCHServiceAccount
616609
type: string
617610
required:
618611
- type
@@ -691,7 +684,6 @@ spec:
691684
type: object
692685
required:
693686
- model
694-
- provider
695687
type: object
696688
x-kubernetes-validations:
697689
- message: provider.openAI must be nil if the provider is not OpenAI
@@ -821,9 +813,6 @@ spec:
821813
this model config detect changes to these secrets and restart if
822814
necessary.
823815
type: string
824-
required:
825-
- conditions
826-
- observedGeneration
827816
type: object
828817
type: object
829818
served: true

go/api/config/crd/bases/kagent.dev_remotemcpservers.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,6 @@ spec:
262262
Important: Run "make" to regenerate code after modifying this file
263263
format: int64
264264
type: integer
265-
required:
266-
- conditions
267-
- observedGeneration
268265
type: object
269266
type: object
270267
served: true

go/api/config/crd/bases/kagent.dev_sandboxagents.yaml

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10703,19 +10703,15 @@ spec:
1070310703
type: array
1070410704
type: object
1070510705
runtime:
10706-
allOf:
10707-
- enum:
10708-
- python
10709-
- go
10710-
- enum:
10711-
- python
10712-
- go
1071310706
default: python
1071410707
description: |-
1071510708
Runtime specifies which ADK implementation to use for this agent.
1071610709
- "python": Uses the Python ADK (default, slower startup, full feature set)
1071710710
- "go": Uses the Go ADK (faster startup, most features supported)
1071810711
The runtime determines both the container image and readiness probe configuration.
10712+
enum:
10713+
- python
10714+
- go
1071910715
type: string
1072010716
stream:
1072110717
description: |-
@@ -10866,15 +10862,11 @@ spec:
1086610862
rule: '!has(self.requireApproval) || self.requireApproval.all(x,
1086710863
has(self.toolNames) && x in self.toolNames)'
1086810864
type:
10869-
allOf:
10870-
- enum:
10871-
- McpServer
10872-
- Agent
10873-
- enum:
10874-
- McpServer
10875-
- Agent
1087610865
description: ToolProviderType represents the tool provider
1087710866
type
10867+
enum:
10868+
- McpServer
10869+
- Agent
1087810870
type: string
1087910871
type: object
1088010872
x-kubernetes-validations:
@@ -11204,18 +11196,12 @@ spec:
1120411196
type: array
1120511197
type: object
1120611198
type:
11207-
allOf:
11208-
- enum:
11209-
- Declarative
11210-
- BYO
11211-
- enum:
11212-
- Declarative
11213-
- BYO
1121411199
default: Declarative
1121511200
description: AgentType represents the agent type
11201+
enum:
11202+
- Declarative
11203+
- BYO
1121611204
type: string
11217-
required:
11218-
- type
1121911205
type: object
1122011206
x-kubernetes-validations:
1122111207
- message: type must be specified
@@ -11288,8 +11274,6 @@ spec:
1128811274
observedGeneration:
1128911275
format: int64
1129011276
type: integer
11291-
required:
11292-
- observedGeneration
1129311277
type: object
1129411278
type: object
1129511279
served: true

go/api/config/crd/bases/kagent.dev_toolservers.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,6 @@ spec:
321321
Important: Run "make" to regenerate code after modifying this file
322322
format: int64
323323
type: integer
324-
required:
325-
- conditions
326-
- observedGeneration
327324
type: object
328325
type: object
329326
served: true

0 commit comments

Comments
 (0)