You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/linters.md
+18-49Lines changed: 18 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -479,65 +479,34 @@ linterConfig:
479
479
480
480
## NumericBounds
481
481
482
-
The `numericbounds` linter checks that numeric fields (`int32` and `int64`) have appropriate bounds validation markers.
482
+
The `numericbounds` linter checks that numeric fields (`int32`, `int64`, `float32`, `float64`) have appropriate bounds validation markers.
483
483
484
484
According to Kubernetes API conventions, numeric fields should have bounds checking to prevent values that are too small, negative (when not intended), or too large.
485
485
486
486
This linter ensures that:
487
-
- `int32`and `int64` fields have both `+kubebuilder:validation:Minimum` and `+kubebuilder:validation:Maximum` markers
488
-
- `int64`fields with bounds outside the JavaScript safe integer range are flagged
487
+
- Numeric fields have both `+kubebuilder:validation:Minimum` and `+kubebuilder:validation:Maximum` markers
488
+
- K8s declarative validation markers (`+k8s:minimum` and `+k8s:maximum`) are also supported
489
+
- Bounds values are within the type's valid range (e.g., int32 bounds must fit in int32)
490
+
- `int64`fields with bounds outside the JavaScript safe integer range (±2^53-1) are flagged
489
491
490
-
### JavaScript Safe Integer Range
492
+
**Note:** While `+k8s:minimum` is documented in the official Kubernetes declarative validation spec, `+k8s:maximum` is not yet officially documented but is supported by this linter for forward compatibility and consistency.
491
493
492
-
For `int64` fields, the linter checks if the bounds exceed the JavaScript safe integer range of `-(2^53)` to `(2^53)` (specifically, `-9007199254740991` to `9007199254740991`).
493
-
494
-
JavaScript represents all numbers as IEEE 754 double-precision floating-point values, which can only safely represent integers in this range. Values outside this range may lose precision when processed by JavaScript clients.
495
-
496
-
When an `int64` field has bounds that exceed this range, the linter will suggest using a string type instead to avoid precision loss.
497
-
498
-
### Examples
499
-
500
-
**Valid:** Numeric field with proper bounds markers
501
-
```go
502
-
type Example struct {
503
-
// +kubebuilder:validation:Minimum=0
504
-
// +kubebuilder:validation:Maximum=100
505
-
Count int32
506
-
}
507
-
```
508
-
509
-
**Valid:** Int64 field with JavaScript-safe bounds
Count int32 // want: should have minimum and maximum bounds validation markers
522
-
}
523
-
```
496
+
This linter is **not enabled by default** as it is primarily focused on CRD validation with kubebuilder markers. It can be enabled in your configuration.
524
497
525
-
**Invalid:** Only one bound specified
526
-
```go
527
-
type Example struct {
528
-
// +kubebuilder:validation:Minimum=0
529
-
Count int32 // want: has minimum but is missing maximum bounds validation marker
530
-
}
498
+
To enable in `.golangci.yml`:
499
+
```yaml
500
+
linters-settings:
501
+
custom:
502
+
kubeapilinter:
503
+
settings:
504
+
linters:
505
+
enable:
506
+
- numericbounds
531
507
```
532
508
533
-
**Invalid:** Int64 with bounds exceeding JavaScript safe range
// Extract the actual type name from typeDesc (e.g., "array element type of int32" -> "int32")
236
+
typeName:=extractTypeName(typeDesc)
237
+
238
+
switchtypeName {
239
+
case"int32":
240
+
ifminimum<minInt32||minimum>maxInt32 {
241
+
pass.Reportf(field.Pos(), "field %s %s has minimum bound %v that is outside the valid int32 range [%d, %d]", fieldName, typeDesc, minimum, minInt32, maxInt32)
242
+
}
243
+
ifmaximum<minInt32||maximum>maxInt32 {
244
+
pass.Reportf(field.Pos(), "field %s %s has maximum bound %v that is outside the valid int32 range [%d, %d]", fieldName, typeDesc, maximum, minInt32, maxInt32)
245
+
}
246
+
case"float32":
247
+
ifminimum<minFloat32||minimum>maxFloat32 {
248
+
pass.Reportf(field.Pos(), "field %s %s has minimum bound %v that is outside the valid float32 range", fieldName, typeDesc, minimum)
249
+
}
250
+
ifmaximum<minFloat32||maximum>maxFloat32 {
251
+
pass.Reportf(field.Pos(), "field %s %s has maximum bound %v that is outside the valid float32 range", fieldName, typeDesc, maximum)
252
+
}
253
+
}
206
254
}
207
255
208
256
// checkJavaScriptSafeBounds checks if int64 bounds are within JavaScript safe integer range.
"field %s of type int64 has bounds [%d, %d] that exceed safe integer range [%d, %d]. Consider using a string type to avoid precision loss in JavaScript clients",
0 commit comments