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

Make Api::create and Api::replace take type-safe KubeObjects #143

Closed

Conversation

nightkr
Copy link
Member

@nightkr nightkr commented Feb 20, 2020

Vec<u8>s are still supported, but deprecated. Sadly, #[deprecated] doesn't work for trait impls, so a doccomment was used instead.

Fixes #142.

This is mostly a spike to get feedback on whether it'd be a good idea. The rest of the examples should probably be ported before merging...

`Vec<u8>`s are still supported, but deprecated. Sadly, `#[deprecated]` doesn't
work for trait impls, so a doccomment was used instead.

Fixes kube-rs#142.
"image": "alpine:latest"
spec: JobSpec {
template: PodTemplateSpec {
metadata: Some(OpenApiObjectMeta {
Copy link
Member Author

Choose a reason for hiding this comment

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

This seems like a pretty big API wart to expose.. :/

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the json! macro is pretty nice to avoid having to explicitly type out all the defaults.

We could maybe do some magic with TryFrom json::Value -> relevant K, but that would only give us runtime type-safety, which only saves us from having to actually post it to the k8s api.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was mostly talking about the ObjectMeta vs OpenApiObjectMeta distinction.

The Default proliferation sounds like it could be worked around with a macro that implicitly adds a ..Default::default() member to every object declaration? Alternatively, builders..

Copy link
Member

@clux clux Feb 21, 2020

Choose a reason for hiding this comment

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

Ah. Yeah, fair enough, that is a bit ugly.

Btw keep in mind that we need to let people create incomplete objects anyway for server-side-apply (we set the fields we care about, api server handles the rest). I think it would be a lot easier if they could just use the json! macro (or something similar for easy yaml if it exists) for it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah.. I considered partial updates (apply, patch) out of scope for this PR.

I think it would be a lot easier if they could just use the json! macro (or something similar for easy yaml if it exists) for it.

Not a huge fan of telling users to send untyped blobs.. I guess we could impl<K> SerializeKubeObject<K> for serde_json::Value, so they at least get types that last that far...

Copy link
Member Author

Choose a reason for hiding this comment

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

I should probably have clarified.. OwnedSubset itself doesn't care about the data itself, it's just a glorified From<K>. The macro would generate the (equivalent of) into, enforcing that all fields are OwnedSubsets of their corresponding fields in the base. That should work as long as we also blanket impl<K> OwnedSubset<K> for K (for the leaves).

Copy link
Member Author

Choose a reason for hiding this comment

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

Then again, k8s-openapi seems to go out of their way to avoid serializing empty fields, so all of this extra machinery probably wouldn't actually help. From what I can tell, just Api::apply(serde_json::to_vec(&deployment)) should actually do the right thing? Then again, I'm probably missing something and would have to experiment more to get anywhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

I should also clarify my own needs before kicking off a huge over-engineering project: at the moment I mostly care about controllers that fully own their resources, create, replace, and watch are what I really care about.

Copy link
Member

Choose a reason for hiding this comment

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

Then again, k8s-openapi seems to go out of their way to avoid serializing empty fields, so all of this extra machinery probably wouldn't actually help. From what I can tell, just Api::apply(serde_json::to_vec(&deployment)) should actually do the right thing? Then again, I'm probably missing something and would have to experiment more to get anywhere.

Oh wow. k8s-openapi really have gotten a lot smarter.

Yeah, if with had Api::apply as an alias for patch with server-side apply, then that should do the right thing. Particularly since we get to take advantage over that serde impl in k8s-openapi.

Copy link
Member

Choose a reason for hiding this comment

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

I should also clarify my own needs before kicking off a huge over-engineering project: at the moment I mostly care about controllers that fully own their resources, create, replace, and watch are what I really care about.

If you have any defaulting behaviour from the openapi schema in the CRD, or any mutating admission controllers, then using server-side Api::apply instead of both replace and create would probably be the best thing (as that preserves the behaviour of those). But in your simpler case, you can just use Api::create and Api::replace in the simple case.

@clux
Copy link
Member

clux commented Feb 20, 2020

Btw, I wouldn't worry about being so well-behaved with breaking changes atm. It's pretty-well advertised that things are in flux at the top of the readme.

@clux
Copy link
Member

clux commented Mar 8, 2020

I think there are some good ideas in here that's worth salvaging, but as you said, given #173 we should rethink them a bit.

I think the owned subset makes a lot of sense for Api::apply (upcoming) and Api::patch even if we only do it to ensure that the input is actually a subset of the type. I'll raise an issue for one potential approach.

@nightkr
Copy link
Member Author

nightkr commented May 7, 2020

Closing this, since it's obsoleted by #175 and #173.

@nightkr nightkr closed this May 7, 2020
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

Successfully merging this pull request may close these issues.

Should Api handle serialization too?
2 participants