cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.06k stars 288 forks source link

Support 'm' as a numeric suffix #1311

Open verdverm opened 2 years ago

verdverm commented 2 years ago

Is your feature request related to a problem? Please describe.

Kubernetes supports setting cpu lim/req with millis such as 100m. Creating schemas and constraints for these numeric values does not seem possible.

Describe the solution you'd like

Support m as a numeric suffix. It's value is 1m == 0.001 or 1000m == 1.

Maybe there are more suffixes for other numerical values <1 that would follow naturally?

Describe alternatives you've considered

string parsing and computations via a struct

mpvl commented 2 years ago

Interesting. Not very high priority, it seems, but may be worth considering.

seh commented 2 years ago

Funny, but not funny, just this week several of us spent a while trying to interpret a pod container's memory request value that the Kubernetes API server had converted to 'm' units—1/1,000 of a byte—but that we were mistakenly reading as megabytes.

mpvl commented 2 years ago

@verdverm: can you support to the specific schema and documentation that supports this feature?

verdverm commented 2 years ago

@mpvl https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes

mpvl commented 2 years ago

FTR: to me 1m reads as a duration value, meaning 1 minute.

myitcv commented 2 years ago

@verdverm is this any generally-accepted precedent outside of Kubernetes?

verdverm commented 2 years ago

hmm, both m and kubernetes seem to have their quirks.

I guess the higher level question is how does one apply bound type constraints to values which look like strings?

For example, as an operator, I may want to limit devs by having the following in a definition they must unify against.

This sounds a bit like the discussion in #1368

Perhaps the answer is in the stdlib?

Another thing that could be helpful is something like

cpu: <= 2 @error(type=string,msg="cpu must be a numerical value and cannot be of the form '100m") <- making this up

niemeyer commented 2 years ago

Indeed m also reads to me as either minutes or meters, depending on context. Milliseconds is typically ms, per the actual standard unit.

Will try to keep this in mind when looking at Kubernetes resources.

verdverm commented 2 years ago

@niemeyer I think the k8s unit of measurement is non-standard for 1/1000 of a CPU core. In my example I'm using m for a duration and this magic k8s unit.

I think there are a couple of issues being discussed from that, so probably best if I break out the new ones.

More generally here, there are some fields which accept these modified numbers, but which don't translate into numeric numbers, and thus it is a difficult exercise for the reader to create constraints for such values. Port values from k8s would be another thorny one.

slewiskelly commented 2 years ago

This hasn't been noted yet, but consider that CUE currently supports some SI multipliers, but not any SI divisors, of which m is 10^-3.^1

niemeyer commented 2 years ago

This hasn't been noted yet, but consider that CUE currently supports some SI multipliers, but not any SI divisors, of which m is 10^-3.1

I'm just a bystander here, but I would support neither of these as a rule. These are prefixes rather than units, and most are meaningless to Cue's audience.

slewiskelly commented 2 years ago

Yes, but as mentioned, some symbols are already supported for multipliers, as per the spec: ^1

multiplier = ( "K" | "M" | "G" | "T" | "P" ) [ "i" ]