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
@@ -481,65 +481,34 @@ linterConfig:
481
481
482
482
## NumericBounds
483
483
484
-
The `numericbounds` linter checks that numeric fields (`int32` and `int64`) have appropriate bounds validation markers.
484
+
The `numericbounds` linter checks that numeric fields (`int32`, `int64`, `float32`, `float64`) have appropriate bounds validation markers.
485
485
486
486
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.
487
487
488
488
This linter ensures that:
489
-
- `int32`and `int64` fields have both `+kubebuilder:validation:Minimum` and `+kubebuilder:validation:Maximum` markers
490
-
- `int64`fields with bounds outside the JavaScript safe integer range are flagged
489
+
- Numeric fields have both `+kubebuilder:validation:Minimum` and `+kubebuilder:validation:Maximum` markers
490
+
- K8s declarative validation markers (`+k8s:minimum` and `+k8s:maximum`) are also supported
491
+
- Bounds values are within the type's valid range (e.g., int32 bounds must fit in int32)
492
+
- `int64`fields with bounds outside the JavaScript safe integer range (±2^53-1) are flagged
491
493
492
-
### JavaScript Safe Integer Range
494
+
**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.
493
495
494
-
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`).
495
-
496
-
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.
497
-
498
-
When an `int64` field has bounds that exceed this range, the linter will suggest using a string type instead to avoid precision loss.
499
-
500
-
### Examples
501
-
502
-
**Valid:** Numeric field with proper bounds markers
503
-
```go
504
-
type Example struct {
505
-
// +kubebuilder:validation:Minimum=0
506
-
// +kubebuilder:validation:Maximum=100
507
-
Count int32
508
-
}
509
-
```
510
-
511
-
**Valid:** Int64 field with JavaScript-safe bounds
Count int32 // want: should have minimum and maximum bounds validation markers
524
-
}
525
-
```
498
+
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.
526
499
527
-
**Invalid:** Only one bound specified
528
-
```go
529
-
type Example struct {
530
-
// +kubebuilder:validation:Minimum=0
531
-
Count int32 // want: has minimum but is missing maximum bounds validation marker
532
-
}
500
+
To enable in `.golangci.yml`:
501
+
```yaml
502
+
linters-settings:
503
+
custom:
504
+
kubeapilinter:
505
+
settings:
506
+
linters:
507
+
enable:
508
+
- numericbounds
533
509
```
534
510
535
-
**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