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

add function to parse canonical quantities (e.g. resources) #855

Merged
merged 1 commit into from
Aug 26, 2019

Conversation

juliantaylor
Copy link
Contributor

This utility function is useful to parse values like the 200m or 300Gi
memory and cpu resources stored in kubernetes manifests.
The function only supports the "canonical form" which has no fractional
digits.
Currently no support for negative numbers as those to my knowledge don't appear in manifests, though it could be added for completeness.

A concern is the return value of the function, currently it is a float.
Though maybe a Decimal would be better as that represents the intention of the number somewhat better (exact representation for typically used numbers, typically arithmetic is more exact etc.) but using a somewhat rarely used type as a return value could have unforeseen consequences elsewhere (.e.g. type float checks would fail, other libraries may not support decimals properly etc.)

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 20, 2019
@juliantaylor
Copy link
Contributor Author

I don't think the test failures are related to my change

@roycaihw
Copy link
Member

/assign

test failure is unrelated #795 (comment)

@juliantaylor
Copy link
Contributor Author

rebased on master, tests are succeeding now

@juliantaylor
Copy link
Contributor Author

any chance for a review?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 24, 2019
@juliantaylor
Copy link
Contributor Author

rebased

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 24, 2019
@roycaihw
Copy link
Member

roycaihw commented Aug 2, 2019

LGTM in general. I'm also concerned about the floating number output which might defeat the purpose of the canonical quantity which guarantees no precision lost

given it's on client side and currently only in parsing canonical quantities (instead of writing), perhaps it's okay to use floating numbers

/approve
/cc @yliaog
WDYT?

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 2, 2019
@scottilee
Copy link
Contributor

@yliaog can you take a look when you get a moment?

import re


def parse_quantity(quantity):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@juliantaylor juliantaylor Aug 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this function currently only parse the canonical form of the quantities, which is what you get in the api objects. It is not intended to re-implement the quantities in kubernetes.

it is probably reasonably straightforward to do by returning decimal objects instead of floats.
Imo that is not unreasonable, but it means user have to work fully in decimals or convert to float/integer as you cannot mix both in computations.

Should I change it to decimals and be able to parse all number representations, also those that will never be in api objects?

@juliantaylor juliantaylor force-pushed the parse-quantity branch 2 times, most recently from e5b3e6a to e825f56 Compare August 24, 2019 14:43
@juliantaylor
Copy link
Contributor Author

Updated to use Decimal as output, add more testcases and added nano(n) and micro(u) which is apparently supported by Quantities (but not listed in its documentation)


def parse_quantity(quantity):
"""
Parse kubernetes canonical form quantity like 200Mi to a decimal number.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a link to the quantity code in k/k repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

This utility function is useful to parse values like the 200m or 300Gi
memory and cpu resources stored in kubernetes manifests.
It uses Decimal as output format as it usually represents typical input
values more accurately and reduces rounding errors.
@yliaog
Copy link
Contributor

yliaog commented Aug 26, 2019

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 26, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: juliantaylor, roycaihw, yliaog

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit a5cb3d7 into kubernetes-client:master Aug 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants