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 conventions about primitive types. #17377

Merged
merged 2 commits into from
Nov 19, 2015

Conversation

bgrant0607
Copy link
Member

No description provided.

#### Primitive types
* Avoid floating-point values as much as possible, and never use them in spec. Floating-point values cannot be reliably round-tripped (encoded and re-decoded) without changing, and have varying precision and representations across languages and architectures.
* Do not use unsigned integers. Similarly, not all languages (e.g., Javascript) support unsigned integers.
Copy link
Member

Choose a reason for hiding this comment

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

uint32 should be ok, since float64 can represent it

@k8s-github-robot
Copy link

Labelling this PR as size/XS

@k8s-github-robot k8s-github-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Nov 17, 2015
* Avoid floating-point values as much as possible, and never use them in spec. Floating-point values cannot be reliably round-tripped (encoded and re-decoded) without changing, and have varying precision and representations across languages and architectures.
* Do not use unsigned integers. Similarly, not all languages (e.g., Javascript) support unsigned integers.
* int64 is converted to float by Javascript and some other languages, so they also need to be accepted as strings.
Copy link
Member

Choose a reason for hiding this comment

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

all numbers are converted to float64 by [...], so any field which is expected to exceed that either in magnitude or in precision (specifically int values > 53 bits) should be serialized and accepted as strings

@k8s-bot
Copy link

k8s-bot commented Nov 17, 2015

GCE e2e build/test failed for commit 0141c53.

@ncdc
Copy link
Member

ncdc commented Nov 18, 2015

cc @kubernetes/rh-cluster-infra

@bgrant0607
Copy link
Member Author

Thanks. Addressed feedback, PTAL.


* Avoid floating-point values as much as possible, and never use them in spec. Floating-point values cannot be reliably round-tripped (encoded and re-decoded) without changing, and have varying precision and representations across languages and architectures.
* All numbers (e.g., uint32, int64) are converted to float64 by Javascript and some other languages, so any field which is expected to exceed that either in magnitude or in precision (specifically integer values > 53 bits) should be serialized and accepted as strings.
* Do not use unsigned integers, due to inconsistent support across languages and libraries. Just validate that the integer is non-negative if that's the case.
Copy link
Member

Choose a reason for hiding this comment

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

@smarterclayton I sort of like this... int64 validated to be 0 <= x <= 2^32 instead of uint32? what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I could be ok with this - @thockin if you agree I'll use int64 internally
for UID/GID and then we'll validate to the range

On Wed, Nov 18, 2015 at 1:05 PM, Jordan Liggitt notifications@github.com
wrote:

In docs/devel/api-conventions.md
#17377 (comment)
:

@@ -247,6 +248,14 @@ ports:

This rule maintains the invariant that all JSON/YAML keys are fields in API objects. The only exceptions are pure maps in the API (currently, labels, selectors, annotations, data), as opposed to sets of subobjects.

+#### Primitive types
+
+* Avoid floating-point values as much as possible, and never use them in spec. Floating-point values cannot be reliably round-tripped (encoded and re-decoded) without changing, and have varying precision and representations across languages and architectures.
+* All numbers (e.g., uint32, int64) are converted to float64 by Javascript and some other languages, so any field which is expected to exceed that either in magnitude or in precision (specifically integer values > 53 bits) should be serialized and accepted as strings.
+* Do not use unsigned integers, due to inconsistent support across languages and libraries. Just validate that the integer is non-negative if that's the case.

@smarterclayton https://github.com/smarterclayton I sort of like
this... int64 validated to be 0 <= x <= 2^32 instead of uint32? what do
you think?


Reply to this email directly or view it on GitHub
https://github.com/kubernetes/kubernetes/pull/17377/files#r45235687.

Copy link
Member Author

Choose a reason for hiding this comment

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

We could make exceptions, if compelling, but I'd rather not. We haven't run tests in Javascript, Python, PHP, Ruby, Java, etc., nor in various parsing/printing libraries, nor alternate encodings (e.g., yaml, ubjson), but it's definitely the case that some don't natively support unsigned ints. For instance, it looks like Java would need to parse unit32 as a double (http://www.json.org/javadoc/org/json/JSONObject.html), in which case it would have to validate sign and size, anyway.

We don't have any examples of bitmasks yet, but I'd be inclined to say they should be strings.

Copy link
Member

Choose a reason for hiding this comment

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

I can live with this, I think

Copy link
Contributor

Choose a reason for hiding this comment

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

We kind of do - the range allocators are base64 encoded bitmask strings.
Not publicly exposed yet.

On Nov 19, 2015, at 12:22 AM, Brian Grant notifications@github.com wrote:

In docs/devel/api-conventions.md
#17377 (comment):

@@ -247,6 +248,14 @@ ports:

This rule maintains the invariant that all JSON/YAML keys are fields in API objects. The only exceptions are pure maps in the API (currently, labels, selectors, annotations, data), as opposed to sets of subobjects.

+#### Primitive types
+
+* Avoid floating-point values as much as possible, and never use them in spec. Floating-point values cannot be reliably round-tripped (encoded and re-decoded) without changing, and have varying precision and representations across languages and architectures.
+* All numbers (e.g., uint32, int64) are converted to float64 by Javascript and some other languages, so any field which is expected to exceed that either in magnitude or in precision (specifically integer values > 53 bits) should be serialized and accepted as strings.
+* Do not use unsigned integers, due to inconsistent support across languages and libraries. Just validate that the integer is non-negative if that's the case.

We could make exceptions, if compelling, but I'd rather not. We haven't run
tests in Javascript, Python, PHP, Ruby, Java, etc., nor in various
parsing/printing libraries, nor alternate encodings (e.g., yaml, ubjson),
but it's definitely the case that some don't natively support unsigned
ints. For instance, it looks like Java would need to parse unit32 as a
double (http://www.json.org/javadoc/org/json/JSONObject.html), in which
case it would have to validate sign and size, anyway.

We don't have any examples of bitmasks yet, but I'd be inclined to say they
should be strings.


Reply to this email directly or view it on GitHub
https://github.com/kubernetes/kubernetes/pull/17377/files#r45301195.

@k8s-bot
Copy link

k8s-bot commented Nov 18, 2015

GCE e2e build/test failed for commit d625217.

@pmorie
Copy link
Member

pmorie commented Nov 18, 2015

@kubernetes/rh-cluster-infra

* Avoid floating-point values as much as possible, and never use them in spec. Floating-point values cannot be reliably round-tripped (encoded and re-decoded) without changing, and have varying precision and representations across languages and architectures.
* All numbers (e.g., uint32, int64) are converted to float64 by Javascript and some other languages, so any field which is expected to exceed that either in magnitude or in precision (specifically integer values > 53 bits) should be serialized and accepted as strings.
* Do not use unsigned integers, due to inconsistent support across languages and libraries. Just validate that the integer is non-negative if that's the case.
* Do not use enums. Use aliases for string instead (e.g., `NodeConditionType`).
Copy link
Member

Choose a reason for hiding this comment

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

Tangent: we don't validate the fields typed as string aliases are one of the defined constants for that type. Should we? Go string aliases can take any value and aren't bound by const blocks.

Copy link
Member

Choose a reason for hiding this comment

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

validation logic should check that values are valid. I know we do for a lot of fields (I wrote it). We might not be 100%

@liggitt
Copy link
Member

liggitt commented Nov 19, 2015

LGTM

@liggitt liggitt added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 19, 2015
@wojtek-t
Copy link
Member

@k8s-bot e2e test this please

@k8s-bot
Copy link

k8s-bot commented Nov 19, 2015

GCE e2e test build/test passed for commit d625217.

@k8s-github-robot
Copy link

Automatic merge from submit-queue

k8s-github-robot pushed a commit that referenced this pull request Nov 19, 2015
Auto commit by PR queue bot
@k8s-github-robot k8s-github-robot merged commit 8ce3f15 into kubernetes:master Nov 19, 2015
xingzhou pushed a commit to xingzhou/kubernetes that referenced this pull request Dec 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.