Skip to content

Delete from yaml #1197

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

Conversation

DiptoChakrabarty
Copy link
Contributor

@DiptoChakrabarty DiptoChakrabarty commented Jul 7, 2020

This is a method to delete kubernetes resources using the yaml files . It is similar to the method create_from_yaml but for deleting kubernetes resources .
It can be used to any type pf resource deployment , pod , service etc.
Fixes #940

Resolved issue for tests failing for python2.7

create_from_yaml creates kubernetes objects like deployments,serivces,ingress etc from the given yml files , the delete_from_yaml method can be used to remove/delete those objects from the same yml files in the given cluster for any namespace

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please sign in with your organization's credentials at https://identity.linuxfoundation.org/projects/cncf to be authorized.
  • If you have done the above and are still having issues with the CLA being reported as unsigned, please log a ticket with the Linux Foundation Helpdesk: https://support.linuxfoundation.org/
  • Should you encounter any issues with the Linux Foundation Helpdesk, send a message to the backup e-mail support address at: [email protected]

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. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Jul 7, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @DiptoChakrabarty!

It looks like this is your first PR to kubernetes-client/python 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/python has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot requested review from roycaihw and yliaog July 7, 2020 19:43
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 7, 2020
@moshevayner
Copy link
Member

Hey @DiptoChakrabarty
Thanks for your PR!
Looks like you need to sign the CLA, can you please follow these instructions and sign?

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jul 8, 2020
@DiptoChakrabarty
Copy link
Contributor Author

Hey @DiptoChakrabarty
Thanks for your PR!
Looks like you need to sign the CLA, can you please follow these instructions and sign?

Yes I have done it

@DiptoChakrabarty
Copy link
Contributor Author

HI @roycaihw , @yliaog can you please review this PR

print(msg)


class FailToCreateError(Exception):
Copy link
Contributor

Choose a reason for hiding this comment

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

should it be FailToDeleteError?

# convert group name from DNS subdomain format to
# python class name convention
group = "".join(word.capitalize() for word in group.split('.'))
group = "".join(word.capitalize() for word in group.split('.'))
Copy link
Contributor

Choose a reason for hiding this comment

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

looks the two lines above are the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes resolving these small issues

from kubernetes import client


def delete_from_yaml(
Copy link
Contributor

Choose a reason for hiding this comment

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

overall, what is the difference between this and create_from_yaml? could you please list the differences in the PR description? is it possible to reuse to avoid dups?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I will do it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have written the difference between the methods create_from_yaml and delete_from_yaml editing the PR description and also fixed the issues as stated earlier.

Copy link
Member

@roycaihw roycaihw left a comment

Choose a reason for hiding this comment

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

@@ -0,0 +1,146 @@
import re
Copy link
Member

Choose a reason for hiding this comment

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

please add copyright boilerplate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure Ill add the boilerplate and try writing the tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added the boilerplate and added tests also modifying delete_from_dict function

this parameter has no effect.
Available parameters for creating <kind>:
:param async_req bool
:param bool include_uninitialized: If true, partially initialized
Copy link
Member

Choose a reason for hiding this comment

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

the initializer feature has been removed

API object (i.e. List, Service, etc).
Input:
k8s_client: an ApiClient object, initialized with the client args.
data: a dictionary holding valid kubernetes objects
Copy link
Member

Choose a reason for hiding this comment

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

the comment doesn't match the parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed the comment with correct parameter

name = yml_document["metadata"]["name"]
#call function to delete from namespace
res = getattr(k8s_api,"delete_namespaced_{}".format(kind))(
name=name,body=client.V1DeleteOptions(propagation_policy="Foreground", grace_period_seconds=5),**kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

if we want to achieve parity with kubectl delete -f, we should probably use Background policy by default with a Cascade option: https://github.com/kubernetes/kubernetes/blob/0c642b6ef01071219b9b9e34ad6d89cb9b4b1971/staging/src/k8s.io/kubectl/pkg/cmd/delete/delete.go#L304-L308

Copy link
Contributor Author

Choose a reason for hiding this comment

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

have updated this to use background policy

name = yml_document["metadata"]["name"]
kwargs.pop('namespace', None)
res = getattr(k8s_api,"delete_{}".format(kind))(
name=name,body=client.V1DeleteOptions(propagation_policy="Foreground", grace_period_seconds=5),**kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

A 200 response from a delete request doesn't guarantee the resource being deleted. We should wait until the resource is no longer visible from the server.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How should the e2e tests work for objects being deleted , after deletion if they are search (the object) it gets timed out resulting in an error ,however in the create_from_yaml tests in the end all resources are being deleted using multiple separate commands could we delete those using the delete_from_yaml method and that would be sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

/assign @yliaog

Copy link
Contributor

Choose a reason for hiding this comment

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

in e2e, after delete, try listing the object again, if it does not exist anymore, it means the object is successfully deleted.

@psikka1
Copy link

psikka1 commented Aug 4, 2020

@DiptoChakrabarty @roycaihw - Does the API support non-namespaced resources such as ClusterRole? I am failing to delete the cluster role with this error -

line 115, in delete_from_yaml_single_item
    if getattr(k8s_api, "delete_namespaced_{}".format(kind)):
AttributeError: 'RbacAuthorizationV1Api' object has no attribute 'delete_namespaced_cluster_role'

I feel they should also be supported. Same seems to be the case with create_from_yaml.
Another likely bug I noticed was that when the yaml doc has a blank item, this fails.

...
---

---
...
...

Adding a check
if yml_document:
before this line would do the job.

@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 24, 2020
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 16, 2020
@palnabarun
Copy link
Member

/hold

(for resolving the issues with the commit history in this branch)

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 23, 2020
@DiptoChakrabarty
Copy link
Contributor Author

hey can you tell me how can I resolve this commit history issue .
About the pycodestyle test I ran the bash script locally as suggested and this was the error kubernetes/utils/quantity.py:24:80: E501 line too long (91 > 79 characters)
which is this commented line 'See https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go'
I did not do any changes in that file hence

@liavyona
Copy link

Can you please merge?

@DiptoChakrabarty
Copy link
Contributor Author

Hey can I send a new PR again with the same content , then can close this PR

@roycaihw
Copy link
Member

roycaihw commented Mar 1, 2021

@DiptoChakrabarty Could you open a new PR? That would help with rebasing and squashing the commits. Thanks!

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: DiptoChakrabarty
To complete the pull request process, please ask for approval from yliaog after the PR has been reviewed.

The full list of commands accepted by this bot can be found 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

@palnabarun
Copy link
Member

Looks like this is superseded by #1392.

Closing this. Please reopen if it's not the case.

@palnabarun
Copy link
Member

/close

@k8s-ci-robot
Copy link
Contributor

@palnabarun: Closed this PR.

In response to this:

/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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

delete_from_yaml
9 participants