Skip to content

Delete kubernetes resources from yaml files , solves #940 #1392

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

Closed

Conversation

DiptoChakrabarty
Copy link
Contributor

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

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 k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 16, 2021
@k8s-ci-robot k8s-ci-robot requested review from roycaihw and yliaog March 16, 2021 03:36
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 16, 2021
@DiptoChakrabarty
Copy link
Contributor Author

hey all the tests are passing for my branch in my repo
image
However in the master branch the test_portforward_raw.py test seems to be failing only causing the travis build to fail , I havent done any changes to that file
image
What can I do since I havent edited that file

@yliaog
Copy link
Contributor

yliaog commented Mar 19, 2021

test_portforward_raw is flaky, we have an issue to track fixing it.

closing to trigger CI
/close

@k8s-ci-robot
Copy link
Contributor

@yliaog: Closed this PR.

In response to this:

test_portforward_raw is flaky, we have an issue to track fixing it.

closing to trigger CI
/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.

@yliaog
Copy link
Contributor

yliaog commented Mar 19, 2021

/reopen

@k8s-ci-robot k8s-ci-robot reopened this Mar 19, 2021
@k8s-ci-robot
Copy link
Contributor

@yliaog: Reopened this PR.

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.

@roycaihw
Copy link
Member

/assign @yliaog

Copy link
Contributor

@yliaog yliaog left a comment

Choose a reason for hiding this comment

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

it is mostly duplicates from create_from_yaml, better to refactor it into a common file, then have create_from_yaml, and delete_from_yaml share the common util, instead of having duplicates between them.

@@ -0,0 +1,146 @@
# Copyright 2018 The Kubernetes Authors.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/2018/2021/


def delete_from_yaml(k8s_client, yaml_file, verbose=False,
namespace="default", **kwargs):
"""Input:
Copy link
Contributor

Choose a reason for hiding this comment

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

better to describe what the function does before the Input:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay will modify and push

k8s_client: an ApiClient object, initialized with the client args.
verbose: If True, print confirmation from the create action.
Default is False.
namespace: string. Contains the namespace to create all
Copy link
Contributor

Choose a reason for hiding this comment

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

s/create/delete/

Default is False.
namespace: string. Contains the namespace to create all
resources inside. The namespace must preexist otherwise
the resource creation will fail. If the API object in
Copy link
Contributor

Choose a reason for hiding this comment

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

s/creation/deletion/ ?

the resource creation will fail. If the API object in
the yaml file already contains a namespace definition
this parameter has no effect.
Available parameters for creating <kind>:
Copy link
Contributor

Choose a reason for hiding this comment

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

s/creating/deleting/

should not be persisted. An invalid or unrecognized dryRun
directive will result in an error response and no further
processing of the request.
Valid values are: - All: all dry run stages will be processed
Copy link
Contributor

Choose a reason for hiding this comment

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

Returns:?

does it return anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no it does not return anything , just deletes the resource

failures = []
for yml_document in yml_document_all:
try:
# call delete from dict function
Copy link
Contributor

Choose a reason for hiding this comment

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

the comment above is not very useful



def delete_from_dict(k8s_client, yml_document, verbose,
namespace="default", **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

add comments about what it does, Input, Returns etc

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 will do

except client.rest.ApiException as api_exception:
api_exceptions.append(api_exception)
else:

Copy link
Contributor

Choose a reason for hiding this comment

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

why extra blank line?

@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

@DiptoChakrabarty
Copy link
Contributor Author

hey since this is solving the issue of #940 deleting resources using yaml maintainers can this be merged pleased

@yliaog
Copy link
Contributor

yliaog commented May 22, 2021

it is mostly duplicates from create_from_yaml, better to refactor it into a common file, then have create_from_yaml, and delete_from_yaml share the common util.

@DiptoChakrabarty
Copy link
Contributor Author

it is mostly duplicates from create_from_yaml, better to refactor it into a common file, then have create_from_yaml, and delete_from_yaml share the common util.

Can this not be a new issue as this PR is solving the issue of deleting using yamls feature and a common file would be a new enhancement

@palnabarun
Copy link
Member

@DiptoChakrabarty -- I agree with @yliaog. It's better to address the duplication of logic in this PR itself. I wouldn't say that deduplication of logic is a new enhancement.

@palnabarun palnabarun mentioned this pull request Jun 6, 2021
@DiptoChakrabarty
Copy link
Contributor Author

@DiptoChakrabarty -- I agree with @yliaog. It's better to address the duplication of logic in this PR itself. I wouldn't say that deduplication of logic is a new enhancement.

Okay I am working on it

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 8, 2021
@DiptoChakrabarty
Copy link
Contributor Author

I made a new file operate_from_yaml which has both the create and delete features as requested. Please tell me if I have to add anything more in it.

@DiptoChakrabarty
Copy link
Contributor Author

hey I have rebased it

@mohammedi-haroune
Copy link

mohammedi-haroune commented Jun 28, 2021

Hi, any plans to merge this and release it?

@DiptoChakrabarty
Copy link
Contributor Author

Hey @reviewers can you please review this and tell what I have to do next or any suggestions

@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 20, 2021
@DiptoChakrabarty DiptoChakrabarty requested a review from yliaog July 21, 2021 11:26
@k8s-ci-robot k8s-ci-robot added the lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. label Jun 2, 2022
@DiptoChakrabarty
Copy link
Contributor Author

/remove-lifecycle rotten

@k8s-ci-robot k8s-ci-robot removed the lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. label Jun 3, 2022

def delete_k8s_object(k8s_api, yml_document, kind, **kwargs):

if hasattr(k8s_api, "create_namespaced_{0}".format(kind)):
Copy link

Choose a reason for hiding this comment

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

Is the "create_namespaceD_{0}" here intentional? Should it not be "delete_namespaced_{0}"?

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/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 Sep 13, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/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 Oct 13, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closed this PR.

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

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

@dofmind
Copy link

dofmind commented Nov 16, 2022

Any update?

@dofmind
Copy link

dofmind commented Nov 16, 2022

/reopen

@k8s-ci-robot
Copy link
Contributor

@dofmind: 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.

@modzilla99
Copy link

This feature would definitely be very welcome! Please reopen!

@charlesmelby
Copy link

Any plans to finish this? It would be quite useful.

@stefan-falk-dt
Copy link

@DiptoChakrabarty @roycaihw is this PR still on the table?

@modzilla99
Copy link

Jup it would be very nice to have!

@jamesjallorina
Copy link

Why this PR has not been merged? It would be nice to have this feature. Thank you!

@jamesjallorina
Copy link

/reopen

@k8s-ci-robot
Copy link
Contributor

@jamesjallorina: 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.

@jamesjallorina
Copy link

Anyway, I was able to use the author's implementation. Just create a package (e.g kubernetes_workaround) and copy the changes, no need to do some extra tweaking. Thanks!

@chrisabbott
Copy link

chrisabbott commented Mar 5, 2024

Will this feature ever be supported? It has been requested numerous times over the past three years, seems like a logical counterpart to create_from_yaml, and blocks many Python client use-cases. I would be happy to open another PR for this if there's interest from others.

@dgimeno777
Copy link

Will this feature ever be supported? It has been requested numerous times over the past three years, seems like a logical counterpart to create_from_yaml, and blocks many Python client use-cases. I would be happy to open another PR for this if there's interest from others.

Seconding this. Without the mirrored ability to tear down resources the create_from_yaml function and Python client itself feels very limited.

kocarba added a commit to kocarba/k8s-python that referenced this pull request Mar 25, 2025
…l by DiptoChakrabarty) and update to latest
kocarba added a commit to kocarba/k8s-python that referenced this pull request Mar 25, 2025
…l by DiptoChakrabarty) and update to latest

Revive PR kubernetes-client#1392: Implement delete_from_yaml, solves kubernetes-client#940 (original by DiptoChakrabarty) and update to latest
kocarba added a commit to kocarba/k8s-python that referenced this pull request Mar 26, 2025
…date to latest

(original by DiptoChakrabarty)

- Implement delete_from_yaml functionality
- Combine create_from_yaml and delete_from_yaml into yaml_processor, preserving existing signatures for create
kocarba added a commit to kocarba/k8s-python that referenced this pull request Mar 26, 2025
…date to latest

(original by @DiptoChakrabarty)

- Implement delete_from_yaml functionality
- Combine create_from_yaml and delete_from_yaml into yaml_processor, preserving existing signatures for create
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. 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. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

delete_from_yaml