kubeblocks
kubeblocks copied to clipboard
[Feature]: Support standard storage unit suffixes (MB, MiB, GiB, etc.) in CUE validation attributes
Problem Description
Currently, the CUE attributes used for parameter validation in KubeBlocks have inconsistent and incomplete support for common storage unit suffixes. This makes it impossible to validate strings like 256MiB or 256MB reliably using a single attribute.
@k8sResource(quantity): Uses Kubernetes'resource.ParseQuantity.- Success:
256Mi(Binary),256M(Decimal). - Failure:
256MB,256MiB,256GB. (Fails because it does not recognize the trailingB).
- Success:
@storeResource(): Uses a custom internal unit map.- Success:
256MB,256M(Both treated as binary 1024^2). - Failure:
256Mi,256MiB,256Gi. (Fails because it does not recognize theisuffix).
- Success:
There is currently no single attribute that supports the full range of standard suffixes that users frequently use in configuration (especially the B and iB variants).
Root Cause Analysis
In pkg/parameters/validate/cuelang_expansion.go:
handleK8sQuantityTypepasses the string directly to the Kubernetes parser without handling theBsuffix common in user inputs.bytesSizeTableused by@storeResourcelacks definitions for binary units containingi(e.g.,Mi,MiB,Gi,GiB).
Proposed Solution
Enhance the parsing logic in cuelang_expansion.go to provide consistent unit support:
- Update
handleK8sQuantityType: Pre-process the string to trim a trailingBif it follows a unit character (e.g., convertMiBtoMiorMBtoM) before callingresource.ParseQuantity. - Update
bytesSizeTable: Expand the internal map to include binary variants such as:KI,MI,GI,TI,PI,EIKIB,MIB,GIB,TIB,PIB,EIB
Expected Behavior
After the fix, a CUE template like:
"memory.allowedBytes"?: int @k8sResource(quantity)
should successfully parse and validate all of the following inputs: 256, 256M, 256Mi, 256MB, and 256MiB.