Extend maxlength linter to support kubernetes declarative validation markers - #245
Extend maxlength linter to support kubernetes declarative validation markers #245itzPranshul wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: itzPranshul The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
0730e76 to
92b1b98
Compare
There was a problem hiding this comment.
Those were accidental changes introduced by my local environment while addressing the lint issues.I have reverted them. Sorry about that!
|
|
||
| // TestMaxLength_DVMarkers tests that the linter (default config) accepts DV | ||
| // markers as satisfying the max-length/max-items/max-properties constraints. | ||
| func TestMaxLength_DVMarkers(t *testing.T) { |
There was a problem hiding this comment.
This test looks duplicating TestMaxLength. Why did you add this test ?
There was a problem hiding this comment.
I've removed it now
| ci, ok := maxlength.Initializer().(initializer.ConfigurableAnalyzerInitializer) | ||
| if !ok { | ||
| t.Fatal("maxlength.Initializer() does not implement ConfigurableAnalyzerInitializer") | ||
| } | ||
|
|
||
| a, err := ci.Init(&maxlength.MaxLengthConfig{ | ||
| PreferredMaxLengthMarker: markers.K8sMaxLengthMarker, | ||
| PreferredMaxItemsMarker: markers.K8sMaxItemsMarker, | ||
| PreferredMaxPropertiesMarker: markers.K8sMaxPropertiesMarker, | ||
| }) |
There was a problem hiding this comment.
| ci, ok := maxlength.Initializer().(initializer.ConfigurableAnalyzerInitializer) | |
| if !ok { | |
| t.Fatal("maxlength.Initializer() does not implement ConfigurableAnalyzerInitializer") | |
| } | |
| a, err := ci.Init(&maxlength.MaxLengthConfig{ | |
| PreferredMaxLengthMarker: markers.K8sMaxLengthMarker, | |
| PreferredMaxItemsMarker: markers.K8sMaxItemsMarker, | |
| PreferredMaxPropertiesMarker: markers.K8sMaxPropertiesMarker, | |
| }) | |
| ci, err := maxlength.Initializer().Initialize(&maxlength.MaxLengthConfig{ | |
| PreferredMaxLengthMarker: markers.K8sMaxLengthMarker, | |
| PreferredMaxItemsMarker: markers.K8sMaxItemsMarker, | |
| PreferredMaxPropertiesMarker: markers.K8sMaxPropertiesMarker, | |
| }) |
I like this style.
| // Run only against c.go-style testdata when DV markers are preferred. | ||
| // The want-comments in a.go cite kubebuilder markers; for this run we use a | ||
| // separate package that has no want-comments (happy-path only). | ||
| // For now we verify the analyzer initialises and runs without panicking. | ||
| _ = a | ||
| _ = testdata |
There was a problem hiding this comment.
Why don't you test dv-style marker?
If you don't test dv-style, we can't confirm this implementation works correctly.
There was a problem hiding this comment.
I've Added a new testdata/src/b/b.go with the analyzer initialized in DV preferred mode.
DV-style markers are now tested across two layers:
- Acceptance in default (Kubebuilder-preferred) config - verifies that +k8s markers suppress diagnostics even when the linter is configured to prefer kubebuilder markers.
2.End to end DV-preferred config
All tests pass. Thanks for the review!
92b1b98 to
615ec6e
Compare
| MapWithNoMaxProperties map[string]string // want `field DVMaxLength.MapWithNoMaxProperties must have a maximum properties, add kubebuilder:validation:MaxProperties marker` | ||
|
|
||
| // Non-string-keyed map — should NOT lint. | ||
| // DV +k8s:maxProperties only supports string-keyed maps; linter mirrors this constraint. |
There was a problem hiding this comment.
What about +kubebuilder:validation:maxProperties?
There was a problem hiding this comment.
The restriction actually applies to both. I've updated the comments to be generic so it's clear why it's skipped in both modes.
| // +k8s:maxLength (counts chars) is not the correct DV tag for []byte; | ||
| // +k8s:maxBytes (counts bytes) should be used instead. |
There was a problem hiding this comment.
What's the parsing doing for these comments, they look like markers? Maybe remove the + at the beginning?
| // DV +k8s:maxProperties only supports string-keyed maps. | ||
| // Skip non-string-keyed maps (e.g. map[int]string) to avoid false positives. | ||
| if keyIdent, ok := mapType.Key.(*ast.Ident); !ok || keyIdent.Name != "string" { | ||
| return | ||
| } |
There was a problem hiding this comment.
Won't this break CRD use cases? Or are non string keyed maps not possible in CRDs?
There was a problem hiding this comment.
As far as I understood, non-string-keyed maps are not valid CRD fields. So, this will not break CRD use case
| // needsByteSliceMaxLength is like needsStringMaxLength but enforces that for DV markers, | ||
| // +k8s:maxBytes is used instead of +k8s:maxLength (which counts characters). |
There was a problem hiding this comment.
How does this work for CRDs though where there is no byte length check, it is always character based?
There was a problem hiding this comment.
For CRDs, the linter remains character-based and only-handles byte based validation +k8s:maxBytes when explicitly configured for DV.
OpenAPI v3 does not have native byte-length constraints; instead, byte[] fields are represented as base64-encoded json strings, so the linter expects +kubebuilder :validation:MaxLength for []byte fields for CRDs
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: itzPranshul The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
f0c8801 to
77257d4
Compare
This PR extends the maxlength linter to support kubernetes DV markers:
Key chages:
MaxLengthConfigto specify whether the linter should prefer kubebuilder or k8s DV markersNewConfigurableInitializer.Fixes #223