Skip to content

Commit 5f486fb

Browse files
committed
Resolve DynamicClient delete function only takes the resource name
1 parent b18352f commit 5f486fb

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

kubernetes/e2e_test/test_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,39 @@ def test_delete_from_list_in_multi_resource_yaml(self):
883883
self.assertFalse(pod1_status)
884884
self.assertFalse(deploy_status)
885885

886+
def test_delete_apps_deployment_from_yaml_with_apply(self):
887+
"""
888+
Should be able to create and delete an apps/v1 deployment using apply.
889+
"""
890+
k8s_client = client.api_client.ApiClient(configuration=self.config)
891+
# Create the deployment
892+
utils.process_from_yaml(
893+
k8s_client, self.path_prefix + "apps-deployment.yaml", apply=True)
894+
app_api = client.AppsV1Api(k8s_client)
895+
dep = app_api.read_namespaced_deployment(name="nginx-app",
896+
namespace="default")
897+
self.assertIsNotNone(dep)
898+
self.assertEqual("nginx-app", dep.metadata.name)
899+
self.assertEqual(
900+
"nginx:1.15.4", dep.spec.template.spec.containers[0].image)
901+
self.assertEqual(
902+
80, dep.spec.template.spec.containers[0].ports[0].container_port)
903+
self.assertEqual(
904+
"nginx", dep.spec.template.spec.containers[0].name)
905+
self.assertEqual("nginx", dep.spec.template.metadata.labels["app"])
906+
self.assertEqual(3, dep.spec.replicas)
907+
908+
# Delete the deployment using apply
909+
utils.process_from_yaml(
910+
k8s_client, self.path_prefix + "apps-deployment.yaml", apply=True, action="delete")
911+
time.sleep(10) # Wait for the deletion to propagate
912+
913+
# Verify the deployment is deleted
914+
with self.assertRaises(ApiException) as cm:
915+
app_api.read_namespaced_deployment(name="nginx-app",
916+
namespace="default")
917+
self.assertEqual(cm.exception.status, 404)
918+
886919

887920
class TestUtilsUnitTests(unittest.TestCase):
888921

kubernetes/utils/yaml_processor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ def process_from_yaml_single_item(
280280
body=yml_object, field_manager="python-client", **kwargs
281281
)
282282
elif action == "delete":
283+
name = yml_object["metadata"]["name"]
283284
resp = apply_client.delete(
284-
body=yml_object, field_manager="python-client", **kwargs
285+
name=name, **kwargs
285286
)
286287
if verbose:
287288
msg = "{0} {1}d.".format(kind, action)
@@ -483,8 +484,9 @@ def create_from_dict(k8s_client, data, verbose=False, namespace='default',
483484
"""
484485
return process_from_dict(k8s_client, data, verbose, namespace, action="create", **kwargs)
485486

487+
486488
class FailToCreateError(FailToProcessError):
487489
"""
488490
An exception class for handling error if an error occurred when
489491
handling a yaml file.
490-
"""
492+
"""

0 commit comments

Comments
 (0)