kubeblocks icon indicating copy to clipboard operation
kubeblocks copied to clipboard

[Feature]: Support standard storage unit suffixes (MB, MiB, GiB, etc.) in CUE validation attributes

Open zero2hj opened this issue 6 months ago • 0 comments

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.

  1. @k8sResource(quantity): Uses Kubernetes' resource.ParseQuantity.
    • Success: 256Mi (Binary), 256M (Decimal).
    • Failure: 256MB, 256MiB, 256GB. (Fails because it does not recognize the trailing B).
  2. @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 the i suffix).

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:

  • handleK8sQuantityType passes the string directly to the Kubernetes parser without handling the B suffix common in user inputs.
  • bytesSizeTable used by @storeResource lacks definitions for binary units containing i (e.g., Mi, MiB, Gi, GiB).

Proposed Solution

Enhance the parsing logic in cuelang_expansion.go to provide consistent unit support:

  1. Update handleK8sQuantityType: Pre-process the string to trim a trailing B if it follows a unit character (e.g., convert MiB to Mi or MB to M) before calling resource.ParseQuantity.
  2. Update bytesSizeTable: Expand the internal map to include binary variants such as:
    • KI, MI, GI, TI, PI, EI
    • KIB, 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.

zero2hj avatar May 11 '26 06:05 zero2hj