Closed LeoLiuYan closed 1 year ago
@LeoLiuYan
In KCL, numeric units exist as a special type, which does not allow arithmetic operations, because str(1024Mi) == "1024Mi"
can be written directly, but when they are calculated and then use the str
function, KCL compiler don't know what unit to take.
Therefore, If you really want to support resource assignment as a string type, you can directly extend the definition of Resource
as follows:
import units
type Unit = units.NumberMultiplier
schema Resource:
cpu?: int | Unit = 1
memory?: str | Unit = 1024Mi
disk?: str | Unit = 10Gi
Where, the type of memory
is str | Unit
@Peefy
OK. But this is a common operation when set the value of resources.limits
and resources.requests
, will the KCL
to support an easy way to do this operation.
OK. But this is a common operation when set the value of
resources.limits
andresources.requests
, will theKCL
to support a easy way to do this operation.
Got it. We'll consider an easier way to deal with resource
.
However, we can convert the numeric unit literal value into an integer through the int()
function, and then call the functions in the units package or spliced into the corresponding unit string.
import units
import math
a: units.NumberMultiplier = 2048Mi
ratio: float = 0.3
cpu: int | str = 1
res = {
cpu = str(int(int(cpu) * ratio * 1000)) + "m"
memory = units.to_Mi(int(int(a) * ratio))
}
@LeoLiuYan
@LeoLiuYan In KCL, numeric units exist as a special type, which does not allow arithmetic operations, because
str(1024Mi) == "1024Mi"
can be written directly, but when they are calculated and then use thestr
function, KCL compiler don't know what unit to take.Therefore, If you really want to support resource assignment as a string type, you can directly extend the definition of
Resource
as follows:import units type Unit = units.NumberMultiplier schema Resource: cpu?: int | Unit = 1 memory?: str | Unit = 1024Mi disk?: str | Unit = 10Gi
Where, the type of
memory
isstr | Unit
In addition, it is not complete to only support the operation of numerical unit types and integers. For addition, 1Mi + 1Ki is ambiguous, and the calculation result unit is uncertain.
General Question
When users want to use 2048Mi memory, for example, and we set
resources.memroy.limits = 2048Mi
, but setresources.memroy.requests = 2048Mi * (a float ratio).
@Peefy @chai2010