kubernetes-client / python

Official Python client library for kubernetes
http://kubernetes.io/
Apache License 2.0
6.5k stars 3.22k forks source link

`format_quantity` function missing to convert Decimal values in to K8s quantity string #2205

Open rkschamer opened 3 months ago

rkschamer commented 3 months ago

What is the feature and why do you need it:

There is parse_quantity to convert a canonical K8s quantity into a Decimal value. However, an opposite function, like format_quantity is missing, making it hard to transform a Decimal into a K8s quantity.

My use case was that I needed to load the resource requirements of serval pods, finding the maximum value and setting it for all pods. Due to potentially different SI suffixes, I've used parse_quantity, however setting the resource requirements for all other Pods, requires me to convert back from a Decimal into a canonical K8s quantity.

Describe the solution you'd like to see:

So far, I've implemented the function in my project, but I think that might be something also others could benefit. For this reason, I'd like to open a pull request to contribute this function.

### Tasks
roycaihw commented 2 months ago

/help /assign @rkschamer

k8s-ci-robot commented 2 months ago

@roycaihw: This request has been marked as needing help from a contributor.

Guidelines

Please ensure that the issue body includes answers to the following questions:

For more details on the requirements of such an issue, please see here and ensure that they are met.

If this request no longer meets these requirements, the label can be removed by commenting with the /remove-help command.

In response to [this](https://github.com/kubernetes-client/python/issues/2205): >/help >/assign @rkschamer Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
rkschamer commented 1 month ago

Hi @roycaihw,

thanks for looking into my request. I'm sorry that it took me a while to come back to you.

I'm not entirely sure, on which aspect my help is needed. Maybe it my description for the desired function was not quite clear. So let me try again:

There is already utils.parse_quantity which I used in my project, but I also needed a way to convert a Decimal number back into a Kubernetes quantity. I propose to add this functionality to the Python K8s client.

I opened the issue to ask if such a function is desired and since, I've already implemented it for my own project, I want to contribute. To illustrate my request a bit more, I've no created a draft PR for the function, so that you can have a look (there is also an example how to use the function): https://github.com/kubernetes-client/python/pull/2216

Please let me know if you're in favor of the change. Then I'll convert into an actual PR and sign the CLA.

I'm removing the 'help wanted', since to indicate that I've replied to your request.

/remove-help

roycaihw commented 1 month ago

Hi @rkschamer, the feature sounds good! Please feel free to convert the draft to an actual PR and we can review and merge it

rkschamer commented 1 month ago

Hi @rkschamer, the feature sounds good! Please feel free to convert the draft to an actual PR and we can review and merge it

Thanks, @roycaihw! I've now added tests and converted to an actual PR. Please feel free to review the PR.