Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do units.NumberMultiplier * float operator? #30

Closed
LeoLiuYan opened this issue Aug 22, 2022 · 6 comments
Closed

How to do units.NumberMultiplier * float operator? #30

LeoLiuYan opened this issue Aug 22, 2022 · 6 comments

Comments

@LeoLiuYan
Copy link
Contributor

LeoLiuYan commented Aug 22, 2022

General Question

When users want to use 2048Mi memory, for example, and we set resources.memroy.limits = 2048Mi, but set resources.memroy.requests = 2048Mi * (a float ratio).

import units
import base.pkg.kusion_models.kube.frontend.resource

a: units.NumberMultiplier = 2048Mi
ratio: float = 0.01

res: resource.Resource = {
    cpu = 1 * ratio        # How to convert to "10m"?
    memory = a * ratio  # error: unsupport
    #memory = units.to_Mi(float(int(a)) * ratio) # error: to_Mi unsupport a float argument
}

@Peefy @chai2010

@Peefy
Copy link
Contributor

Peefy commented Aug 22, 2022

@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

@LeoLiuYan
Copy link
Contributor Author

LeoLiuYan commented Aug 22, 2022

@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.

@Peefy
Copy link
Contributor

Peefy commented Aug 22, 2022

OK. But this is a common operation when set the value of resources.limits and resources.requests, will the KCL to support a easy way to do this operation.

Got it. We'll consider an easier way to deal with resource.

@Peefy
Copy link
Contributor

Peefy commented Sep 19, 2022

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.

@Peefy
Copy link
Contributor

Peefy commented Sep 19, 2022

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

@Peefy
Copy link
Contributor

Peefy commented Sep 19, 2022

@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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants