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

Closed
Changes from 5 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
b61fb5c
created method to delete from yaml files
DiptoChakrabarty Jul 7, 2020
5fc0f53
function when namespaced delete not found
DiptoChakrabarty Jul 7, 2020
d339958
change syntax for delete operation
DiptoChakrabarty Jul 12, 2020
a8e5ac3
resolve kwargs error for python2.7
DiptoChakrabarty Jul 12, 2020
7e75c96
fixed typo errors
DiptoChakrabarty Jul 13, 2020
9605ff1
created method to delete from yaml files
DiptoChakrabarty Jul 7, 2020
f153cf1
function when namespaced delete not found
DiptoChakrabarty Jul 7, 2020
829ecc8
change syntax for delete operation
DiptoChakrabarty Jul 12, 2020
c86181b
resolve kwargs error for python2.7
DiptoChakrabarty Jul 12, 2020
1e3093b
fixed typo errors
DiptoChakrabarty Jul 13, 2020
424fb43
e2e tests of resource deletion
DiptoChakrabarty Aug 24, 2020
0108418
e2e tests of resource deletion
DiptoChakrabarty Aug 24, 2020
4e3d675
add e2e tests for multi resources and add method to delete of kind list
DiptoChakrabarty Aug 24, 2020
b010a19
changed propagation policy to background and updated comment
DiptoChakrabarty Aug 31, 2020
414946e
time limit exceeded case
DiptoChakrabarty Aug 31, 2020
ab1329c
Merge branch 'master' of https://github.com/kubernetes-client/python …
DiptoChakrabarty Sep 21, 2020
0a3cbcb
tests to verify resource deletion
DiptoChakrabarty Sep 22, 2020
399c166
error in multi resource test fix
DiptoChakrabarty Sep 23, 2020
704655f
Merge branch 'master' of https://github.com/kubernetes-client/python …
DiptoChakrabarty Sep 27, 2020
8c1967f
fix namespace deletion error and being deleted case
DiptoChakrabarty Sep 27, 2020
ec5db9c
minor change
DiptoChakrabarty Sep 28, 2020
6ff6e44
Merge branch 'master' of https://github.com/kubernetes-client/python …
DiptoChakrabarty Oct 19, 2020
3833f7e
Merge branch 'master' of https://github.com/kubernetes-client/python …
DiptoChakrabarty Nov 3, 2020
5c90c18
Update CHANGELOG and README to reflect v12.0.0 and v12.0.1
palnabarun Oct 15, 2020
92bf36b
Update constants to reflect Client release 17.0.0
palnabarun Nov 7, 2020
598d41b
Generate client 17.0.0
palnabarun Nov 7, 2020
e842ec9
Remove generated tests
palnabarun Nov 7, 2020
f1cc695
Fix custom objects API to preserve backward compatibility
palnabarun Jun 22, 2020
56ab983
Add kubernetes.client.apis as an alias to kubernetes.client.api
palnabarun Jun 22, 2020
0eb5f0f
Add test to ensure kubernetes client threadpool is cleaned up
fabianvf Feb 6, 2020
a80b3f5
add a test for default configuration behavior
roycaihw Nov 4, 2020
a9ad7d7
Update CHANGELOG with v17.0.0-snapshot
palnabarun Nov 7, 2020
bfb46ff
Update README
palnabarun Nov 7, 2020
0bd5e13
Merge branch 'master' of https://github.com/kubernetes-client/python
DiptoChakrabarty Nov 12, 2020
22d623b
Merge pull request #1308 from palnabarun/update-changelog
k8s-ci-robot Nov 13, 2020
b79ad68
Merge pull request #1307 from palnabarun/release-17.0-snapshot
k8s-ci-robot Nov 13, 2020
a63bf99
fix code style errors
DiptoChakrabarty Nov 15, 2020
32c5231
namespace condition error fix
DiptoChakrabarty Nov 15, 2020
ebd9864
created method to delete from yaml files
DiptoChakrabarty Jul 7, 2020
54377ea
function when namespaced delete not found
DiptoChakrabarty Jul 7, 2020
a20b3a1
change syntax for delete operation
DiptoChakrabarty Jul 12, 2020
92d50aa
resolve kwargs error for python2.7
DiptoChakrabarty Jul 12, 2020
89e06d1
fixed typo errors
DiptoChakrabarty Jul 13, 2020
1233244
created method to delete from yaml files
DiptoChakrabarty Jul 7, 2020
e0f4c5c
change syntax for delete operation
DiptoChakrabarty Jul 12, 2020
b03e37e
resolve kwargs error for python2.7
DiptoChakrabarty Jul 12, 2020
81374a3
fixed typo errors
DiptoChakrabarty Jul 13, 2020
4e81da5
e2e tests of resource deletion
DiptoChakrabarty Aug 24, 2020
0bf74fc
add e2e tests for multi resources and add method to delete of kind list
DiptoChakrabarty Aug 24, 2020
36838fc
changed propagation policy to background and updated comment
DiptoChakrabarty Aug 31, 2020
371d252
time limit exceeded case
DiptoChakrabarty Aug 31, 2020
c87c272
tests to verify resource deletion
DiptoChakrabarty Sep 22, 2020
3ce0c5a
error in multi resource test fix
DiptoChakrabarty Sep 23, 2020
e87ad34
fix namespace deletion error and being deleted case
DiptoChakrabarty Sep 27, 2020
95b1d2c
minor change
DiptoChakrabarty Sep 28, 2020
2e22a7b
fix code style errors
DiptoChakrabarty Nov 15, 2020
fa84691
namespace condition error fix
DiptoChakrabarty Nov 15, 2020
de8d44e
merge conflict fix
DiptoChakrabarty Nov 16, 2020
2bfdf96
tox test
DiptoChakrabarty Mar 18, 2021
b0c1eab
tox line break check
DiptoChakrabarty Mar 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions kubernetes/utils/delete_from_yaml.py
Original file line number Diff line number Diff line change
@@ -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

from os import path

import yaml

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.

k8s_client,
yaml_file,
verbose=False,
namespace="default",
**kwargs):
'''
Input:
yaml_file: string. Contains the path to yaml file.
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
resources inside. The namespace must preexist otherwise
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>:
: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

resources are included in the response.
:param str pretty: If 'true', then the output is pretty printed.
:param str dry_run: When present, indicates that modifications
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
Raises:
FailToDeleteError which holds list of `client.rest.ApiException`
instances for each object that failed to delete.
'''
# open yml file
with open(path.abspath(yaml_file)) as f:
#load all yml content
yml_document_all = yaml.safe_load_all(f)

failures=[]
for yml_document in yml_document_all:
try:
# call delete from dict function
delete_from_dict(k8s_client,yml_document,verbose,
namespace=namespace,
**kwargs)
except FailToDeleteError as failure:
# if error is returned add to failures list
failures.extend(failure.api_exceptions)
if failures:
#display the error
raise FailToDeleteError(failures)

def delete_from_dict(k8s_client,yml_document, verbose,namespace="default",**kwargs):
"""
Perform an action from a dictionary containing valid kubernetes
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

verbose: If True, print confirmation from the create action.
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
the yaml file already contains a namespace definition
this parameter has no effect.
Raises:
FailToDeleteError which holds list of `client.rest.ApiException`
instances for each object that failed to delete.
"""
api_exceptions = []
try:
# call function delete_from_yaml_single_item
delete_from_yaml_single_item(
k8s_client, yml_document, verbose, namespace=namespace, **kwargs
)
except client.rest.ApiException as api_exception:
api_exceptions.append(api_exception)

if api_exceptions:
raise FailToDeleteError(api_exceptions)


def delete_from_yaml_single_item(k8s_client, yml_document, verbose=False, **kwargs):
# get group and version from apiVersion
group,_,version = yml_document["apiVersion"].partition("/")
if version == "":
version = group
group = "core"
# Take care for the case e.g. api_type is "apiextensions.k8s.io"
group = "".join(group.rsplit(".k8s.io", 1))
# convert group name from DNS subdomain format to
# python class name convention
group = "".join(word.capitalize() for word in group.split('.'))
func = "{0}{1}Api".format(group, version.capitalize())
k8s_api = getattr(client, func)(k8s_client)
kind = yml_document["kind"]
kind = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', kind)
kind = re.sub('([a-z0-9])([A-Z])', r'\1_\2', kind).lower()

if getattr(k8s_api,"delete_namespaced_{}".format(kind)):
# load namespace if provided in yml file
if "namespace" in yml_document["metadata"]:
namespace = yml_document["metadata"]["namespace"]
kwargs["namespace"] = namespace
# take name input of kubernetes object
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


else:
# get name of object to delete
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.

if verbose:
msg = "{0} deleted.".format(kind)
if hasattr(res, 'status'):
msg += " status='{0}'".format(str(res.status))
print(msg)


class FailToDeleteError(Exception):
"""
An exception class for handling error if an error occurred when
handling a yaml file during deletion of the resource.
"""

def __init__(self, api_exceptions):
self.api_exceptions = api_exceptions

def __str__(self):
msg = ""
for api_exception in self.api_exceptions:
msg += "Error from server ({0}): {1}".format(
api_exception.reason, api_exception.body)
return msg