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 example about using server-side apply with this client #1093

Closed
tdmalone opened this issue Mar 1, 2020 · 23 comments
Closed

Add example about using server-side apply with this client #1093

tdmalone opened this issue Mar 1, 2020 · 23 comments
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/feature Categorizes issue or PR as related to a new feature. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. wg/api-expression Categorizes an issue or PR as relevant to WG API Expression.

Comments

@tdmalone
Copy link

tdmalone commented Mar 1, 2020

What is the feature and why do you need it:

I would like to have an 'apply' (patch) mode for the helpful create_from_yaml/create_from_dict utility methods.

I currently use this to automate deployment of our application, however from time-to-time something goes wrong in the deployment and we need to deploy it again. At the moment we can't re-run the automation, because parts of it fail due to some objects having already been created.

I'm looking at catching the FailToCreateError and checking for reason: "AlreadyExists", but at this point it would be wonderful to be able to run create_from_yaml in an 'apply'/patch mode, rather than have to build out the full request myself.

Describe the solution you'd like to see:

Probably something as simple as an apply=True or patch=True argument on these methods could do the job?

@tdmalone tdmalone added the kind/feature Categorizes issue or PR as related to a new feature. label Mar 1, 2020
@tdmalone tdmalone changed the title 'apply' mode for utils.create_from_yaml/create_from_dict 'apply' (patch) mode for utils.create_from_yaml/create_from_dict Mar 2, 2020
@roycaihw
Copy link
Member

roycaihw commented Mar 9, 2020

Server-side apply has been beta since 1.15, we should add example/utility about how to use it with the 11.0 client.

@roycaihw roycaihw changed the title 'apply' (patch) mode for utils.create_from_yaml/create_from_dict Add example about using server-side apply with this client Mar 9, 2020
@roycaihw roycaihw added the help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. label Mar 9, 2020
@moshevayner
Copy link
Member

Hey @tdmalone & @roycaihw!
I'm willing to start contributing to this project, and this issue seems like it might be a great first one to begin with!
So I just wanted to make sure I have the right idea in mind- is the goal here to add an additional apply/patch logic to https://github.com/kubernetes-client/python/blob/master/kubernetes/utils/create_from_yaml.py? I didn't dive in too deep yet, but from a quick glimpse, it seems like adding a method apply_from_yaml_single_item and calling it when apply=True was passed could do it.
Alternatively, we can probably separate that entirely by creating a whole new module for apply_from_yaml.py, but I assume there will be a good bit of repetition if we take that route.
Either way, I'd love to take a stab at this! Please let me know how we can kick off a quick discussion around it.
Thanks much, and I'm looking forward to start working on that!

@roycaihw
Copy link
Member

roycaihw commented Apr 9, 2020

@MoShitrit Welcome and thanks for looking into this issue!

Yes I think the goal is to add an apply mode to the utility methods, either adding an apply=True option or automatically retrying when an AlreadyExists error is returned. To achieve that I think we should use the new server-side apply feature (implementing a client-side apply like kubectl apply from scratch will be expensive and doesn't make much sense when we have server-side apply almost ready).

To begin with, I think we can first add an example about how to use this API in the python client. According to the doc, a client requests a server-side apply by sending an API request to any PATCH endpoint with content type application/apply-patch+yaml and a partially specified object in the body.

Did a little research and I think there are currently two blockers for using this API in the python client:

  1. although server-side apply targeted to be beta in 1.15, it seems that it actually went beta in 1.16 (as the doc shows). For this client, we do code-generation based on the upstream openapi spec. The problem is that the 1.15 openapi spec didn't specify application/apply-patch+yaml as acceptable content type. And 1.16 openapi spec specified that. As a result, the generated client will only have application/apply-patch+yaml concept starting 1.16 (12.0.0a1+)
  2. there is a known issue about choosing content-type for PATCH methods. See content-type for patch commands #959

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 8, 2020
@tdmalone
Copy link
Author

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 10, 2020
@kwiesmueller
Copy link

Hey there,
we are currently investigating options for adding apply to client-go. If you need help with adding it to the python client, feel free to get in touch on Slack https://kubernetes.slack.com/archives/C0123CNN8F3
I'm adding our label to put this on our list as well.
/wg api-expression

@k8s-ci-robot k8s-ci-robot added the wg/api-expression Categorizes an issue or PR as relevant to WG API Expression. label Aug 5, 2020
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Nov 3, 2020
@fejta-bot
Copy link

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Dec 3, 2020
@fejta-bot
Copy link

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

@k8s-ci-robot
Copy link
Contributor

@fejta-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@themrmax
Copy link

themrmax commented Feb 8, 2021

/reopen

@k8s-ci-robot
Copy link
Contributor

@themrmax: You can't reopen an issue/PR unless you authored it or you are a collaborator.

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@themrmax
Copy link

themrmax commented Feb 8, 2021

@MoShitrit how did you go with this? i would very much like this feature

@moshevayner
Copy link
Member

@MoShitrit how did you go with this? i would very much like this feature

Hey @themrmax
I haven't really looked at it since. Might be a good time to re-evaluate, given that it's been about a year and we passed a few versions and iterations already.
If anyone's willing to take a stab at it- please feel free. Otherwise I'll try myself whenever I can put some time aside to do so.
Thanks!

/reopen

@k8s-ci-robot
Copy link
Contributor

@MoShitrit: Reopened this issue.

In response to this:

@MoShitrit how did you go with this? i would very much like this feature

Hey @themrmax
I haven't really looked at it since. Might be a good time to re-evaluate, given that it's been about a year and we passed a few versions and iterations already.
If anyone's willing to take a stab at it- please feel free. Otherwise I'll try myself whenever I can put some time aside to do so.
Thanks!

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot reopened this Feb 15, 2021
@Jc2k
Copy link

Jc2k commented Feb 16, 2021

I believe this PR from 2019 would enable the use of server side apply? #959. It enables you to set the content type, and AIUI that means you can do content_type=application/apply-patch+yaml when calling any patch API and enjoy SSA.

@fejta-bot
Copy link

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-contributor-experience at kubernetes/community.
/close

@k8s-ci-robot
Copy link
Contributor

@fejta-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-contributor-experience at kubernetes/community.
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@Buffer0x7cd
Copy link

Any idea if this is feature is still in plan ?

@zanieb
Copy link

zanieb commented Apr 27, 2022

It'd be great to know if this feature was available without subclassing the client. Are there any existing examples of using server-side apply from the Python client?

@Jc2k
Copy link

Jc2k commented Apr 27, 2022

I am still adding a subclass to every project, unfortunately.

@portswigger-tim
Copy link

Anyone who lands here... there's an example within the tests:

def test_server_side_apply_api(self):
client = DynamicClient(api_client.ApiClient(configuration=self.config))
api = client.resources.get(
api_version='v1', kind='Pod')
name = 'pod-' + short_uuid()
pod_manifest = {
'apiVersion': 'v1',
'kind': 'Pod',
'metadata': {'labels': {'name': name},
'name': name},
'spec': {'containers': [{
'image': 'nginx',
'name': 'nginx',
'ports': [{'containerPort': 80,
'protocol': 'TCP'}]}]}}
resp = api.server_side_apply(
namespace='default', body=pod_manifest,
field_manager='kubernetes-unittests', dry_run="All")
self.assertEqual('kubernetes-unittests', resp.metadata.managedFields[0].manager)

@jbnjohnathan
Copy link

2 years since Server-Side apply was introduced in Kubernetes 1.22. Would be great to get to use it fully in this kubernetes python client soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/feature Categorizes issue or PR as related to a new feature. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. wg/api-expression Categorizes an issue or PR as relevant to WG API Expression.
Projects
None yet
Development

No branches or pull requests