Skip to content

Commit 5e3eb41

Browse files
Add CLAUDE command and new check to review and lint the API changes
1 parent 90cd55a commit 5e3eb41

File tree

2 files changed

+582
-0
lines changed

2 files changed

+582
-0
lines changed

.claude/commands/api-lint-diff.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
description: Validate API issues using kube-api-linter with diff-aware analysis
3+
---
4+
5+
# API Lint Diff
6+
7+
Validates API issues in `api/` directory using kube-api-linter with diff-aware analysis that distinguishes between NEW and PRE-EXISTING issues.
8+
9+
## Instructions for Claude AI
10+
11+
When this command is invoked, you MUST:
12+
13+
**CRITICAL:** The final output MUST be a comprehensive analysis report displayed directly in the conversation for the user to read. Do NOT just create temp files - output the full report as your response.
14+
15+
1. **Execute the shell script**
16+
```bash
17+
bash hack/api-lint-diff/run.sh
18+
```
19+
20+
2. **Understand the shell script's output**:
21+
- **False positives (IGNORED)**: Standard CRD scaffolding patterns that kube-api-linter incorrectly flags
22+
- **NEW issues (ERRORS)**: Introduced in current branch → MUST fix
23+
- **PRE-EXISTING issues (WARNINGS)**: Existed before changes → Can fix separately
24+
25+
3. **Filter false positives** - Operator projects scaffold standard Kubernetes CRD patterns that kube-api-linter incorrectly flags as errors, even with WhenRequired configuration.
26+
27+
**Scenario 1: optionalfields on Status field**
28+
```go
29+
Status MyResourceStatus `json:"status,omitzero"`
30+
```
31+
**Error reported:**
32+
```
33+
optionalfields: field Status has a valid zero value ({}), but the validation
34+
is not complete (e.g. min properties/adding required fields). The field should
35+
be a pointer to allow the zero value to be set. If the zero value is not a
36+
valid use case, complete the validation and remove the pointer.
37+
```
38+
**Why it's a FALSE POSITIVE:**
39+
- Status is NEVER a pointer in any Kubernetes API
40+
- Works correctly with `omitzero` tag
41+
- Validation incompleteness is expected - Status is controller-managed, not user-provided
42+
- **ACTION: IGNORE this error**
43+
44+
**Scenario 2: requiredfields on Spec field**
45+
```go
46+
Spec MyResourceSpec `json:"spec"`
47+
```
48+
**Error reported:**
49+
```
50+
requiredfields: field Spec has a valid zero value ({}), but the validation is
51+
not complete (e.g. min properties/adding required fields). The field should be
52+
a pointer to allow the zero value to be set. If the zero value is not a valid
53+
use case, complete the validation and remove the pointer.
54+
```
55+
**Why it's a FALSE POSITIVE:**
56+
- Spec is NEVER a pointer in Kubernetes APIs
57+
- Scaffolds are starting points - users add validation when they implement their business logic
58+
- **ACTION: IGNORE this error**
59+
60+
**Scenario 3: conditions markers on metav1.Condition**
61+
```go
62+
Conditions []metav1.Condition `json:"conditions,omitempty"`
63+
```
64+
**Error reported:**
65+
```
66+
conditions: Conditions field is missing the following markers:
67+
patchStrategy=merge, patchMergeKey=type
68+
```
69+
**Why it's a FALSE POSITIVE:**
70+
- `metav1.Condition` already handles patches correctly
71+
- Adding these markers is redundant for this standard Kubernetes type
72+
- **ACTION: IGNORE this error**
73+
74+
4. **For reported issues, provide intelligent analysis**:
75+
76+
a. **Categorize by fix complexity**:
77+
- NON-BREAKING: Marker replacements, adding listType, adding +required/+optional
78+
- BREAKING: Pointer conversions, type changes (int→int32)
79+
80+
b. **Search for actual usage** (REQUIRED FOR ALL ISSUES - NOT OPTIONAL):
81+
- **CRITICAL:** Do NOT just look at JSON tags - analyze actual code usage patterns
82+
- **Exception:** Deprecated marker replacements (`+kubebuilder:validation:Required``+required`) are straightforward - no usage analysis needed
83+
- **For all other issues:** MUST analyze actual usage before making recommendations
84+
- Use Grep to find ALL occurrences where each field is:
85+
* **Read/accessed**: `obj.Spec.FieldName`, `cat.Spec.Priority`
86+
* **Written/set**: `obj.Spec.FieldName = value`
87+
* **Checked for zero/nil**: `if obj.Spec.FieldName == ""`, `if ptr != nil`
88+
* **Used in conditionals**: Understand semantic meaning of zero values
89+
- Search in: controllers, reconcilers, internal packages, tests, examples
90+
- **Smart assessment based on usage patterns**:
91+
* Field ALWAYS set in code? → Should be **required**, no omitempty
92+
* Field SOMETIMES set? → Should be **optional** with omitempty
93+
* Code checks `if field == zero`? → May need **pointer** to distinguish zero vs unset
94+
* Zero value semantically valid? → Keep as value type with omitempty
95+
* Zero value semantically invalid? → Use pointer OR mark required
96+
* Field never read but only set by controller? → Likely Status field
97+
- **Example analysis workflow for a field**:
98+
```
99+
1. Grep for field usage: `CatalogFilter.Version`
100+
2. Found 5 occurrences:
101+
- controllers/extension.go:123: if filter.Version != "" { ... }
102+
- controllers/extension.go:456: result.Version = bundle.Version
103+
- tests/filter_test.go:89: Version: "1.2.3"
104+
3. Analysis: Version is checked for empty, sometimes set, sometimes omitted
105+
4. Recommendation: Optional with omitempty (current usage supports this)
106+
```
107+
108+
c. **Generate EXACT code fixes** grouped by file:
109+
- Show current code
110+
- Show replacement code (copy-pasteable)
111+
- **Explain why based on actual usage analysis** (not just JSON tags):
112+
* Include usage summary: "Found N occurrences"
113+
* Cite specific examples: "Used in resolve/catalog.go:163 as direct int32"
114+
* Explain semantic meaning: "Field distinguishes priority 0 vs unset"
115+
* Justify recommendation: "Since code checks for empty, should be optional"
116+
- Note breaking change impact with reasoning
117+
- **Each fix MUST include evidence from code usage**
118+
119+
d. **Prioritize recommendations**:
120+
- NEW issues first (must fix)
121+
- Group PRE-EXISTING by NON-BREAKING vs BREAKING
122+
123+
5. **Present actionable report directly to user**:
124+
- **IMPORTANT:** Output the full comprehensive analysis in the conversation (not just to a temp file)
125+
- Summary: False positives filtered, NEW count, PRE-EXISTING count
126+
- Group issues by file and fix type
127+
- Provide code snippets ready to apply (current code → fixed code)
128+
- **DO NOT include "Next Steps" or "Conclusion" sections** - just present the analysis
129+
130+
**Report Structure:**
131+
```
132+
# API Lint Diff Analysis Report
133+
134+
**Generated:** [date]
135+
**Baseline:** main branch
136+
**Current:** [branch name]
137+
**Status:** [status icon and message based on logic below]
138+
139+
**Status Logic:**
140+
- ✅ PASSED: 0 real issues (after filtering false positives)
141+
- ⚠️ WARN: 0 new issues but has pre-existing issues
142+
- ❌ FAIL: Has new issues that must be fixed
143+
144+
## Executive Summary
145+
- Total issues: X
146+
- False positives (IGNORED): Y
147+
- Real issues (NEED FIXING): Z
148+
- NEW issues: N
149+
- PRE-EXISTING issues: P
150+
151+
## REAL ISSUES - FIXES NEEDED (Z issues)
152+
153+
### Category 1: [Issue Type] (N issues) - [BREAKING/NON-BREAKING]
154+
155+
#### File: [filename]
156+
157+
**[Issue #]. Line X - [Field Name]**
158+
```go
159+
// CURRENT:
160+
[current code]
161+
162+
// FIX:
163+
[fixed code]
164+
```
165+
**Usage Analysis:**
166+
- Found N occurrences in codebase
167+
- [Specific usage example 1]: path/file.go:123
168+
- [Specific usage example 2]: path/file.go:456
169+
- Pattern: [always set / sometimes set / checked for zero / etc.]
170+
171+
**Why:** [Recommendation based on usage analysis with evidence]
172+
**Breaking:** [YES/NO] ([detailed reason with impact])
173+
174+
[Repeat for all issues]
175+
176+
## Summary of Breaking Changes
177+
[Table of breaking changes if any]
178+
```
179+
180+
## Related Documentation
181+
182+
- [Kubernetes API Conventions](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md)
183+
- [kube-api-linter](https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/code-generator/cmd/kube-api-linter)
184+
- AGENTS.md in this repository for understanding operator patterns

0 commit comments

Comments
 (0)