Skip to content

Commit df2c2dd

Browse files
ykskbLawouach
authored andcommitted
Fix name parameters to be used in field selector.
Signed-off-by: ykskb <yoheikusakabe@gmail.com>
1 parent a265480 commit df2c2dd

7 files changed

Lines changed: 47 additions & 33 deletions

File tree

chaosk8s/deployment/actions.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,25 @@ def create_deployment(spec_path: str, ns: str = "default",
3535
resp = v1.create_namespaced_deployment(ns, body=deployment)
3636

3737

38-
def delete_deployment(name: str, ns: str = "default",
39-
label_selector: str = "name in ({name})",
40-
secrets: Secrets = None):
38+
def delete_deployment(name: str = None, ns: str = "default",
39+
label_selector: str = None, secrets: Secrets = None):
4140
"""
42-
Delete a deployment by `name` in the namespace `ns`.
41+
Delete a deployment by `name` or `label_selector` in the namespace `ns`.
4342
4443
The deployment is deleted without a graceful period to trigger an abrupt
4544
termination.
4645
47-
The selected resources are matched by the given `label_selector`.
46+
If neither `name` nor `label_selector` is specified, all the deployments
47+
will be deleted in the namespace.
4848
"""
49-
label_selector = label_selector.format(name=name)
5049
api = create_k8s_api_client(secrets)
5150

5251
v1 = client.AppsV1Api(api)
53-
if label_selector:
52+
53+
if name:
54+
ret = v1.list_namespaced_deployment(
55+
ns, field_selector="metadata.name={}".format(name))
56+
elif label_selector:
5457
ret = v1.list_namespaced_deployment(ns, label_selector=label_selector)
5558
else:
5659
ret = v1.list_namespaced_deployment(ns)

chaosk8s/pod/actions.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,23 @@ def _select_pods(v1: client.CoreV1Api = None, label_selector: str = None,
225225
return pods
226226

227227

228-
def delete_pods(name: str, ns: str = "default",
229-
label_selector: str = "name in ({name})",
230-
secrets: Secrets = None):
228+
def delete_pods(name: str = None, ns: str = "default",
229+
label_selector: str = None, secrets: Secrets = None):
231230
"""
232-
Delete pods by `name` in the namespace `ns`.
231+
Delete pods by `name` or `label_selector` in the namespace `ns`.
233232
234233
The pods are deleted without a graceful period to trigger an abrupt
235234
termination.
236235
237-
The selected resources are matched by the given `label_selector`.
236+
If neither of `name` and `label_selector` is specified, all the pods will
237+
be deleted in the namespace.
238238
"""
239-
label_selector = label_selector.format(name=name)
240239
api = create_k8s_api_client(secrets)
241240
v1 = client.CoreV1Api(api)
242-
if label_selector:
241+
if name:
242+
ret = v1.list_namespaced_pod(
243+
ns, field_selector="metadata.name={}".format(name))
244+
elif label_selector:
243245
ret = v1.list_namespaced_pod(ns, label_selector=label_selector)
244246
else:
245247
ret = v1.list_namespaced_pod(ns)

chaosk8s/replicaset/actions.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@
77
__all__ = ["delete_replica_set"]
88

99

10-
def delete_replica_set(name: str, ns: str = "default",
11-
label_selector: str = "name in ({name})",
12-
secrets: Secrets = None):
10+
def delete_replica_set(name: str = None, ns: str = "default",
11+
label_selector: str = None, secrets: Secrets = None):
1312
"""
14-
Delete a replica set by `name` in the namespace `ns`.
13+
Delete a replica set by `name` or `label_selector` in the namespace `ns`.
1514
1615
The replica set is deleted without a graceful period to trigger an abrupt
1716
termination.
1817
19-
The selected resources are matched by the given `label_selector`.
18+
If neither `name` nor `label_selector` is specified, all the replica sets
19+
will be deleted in the namespace.
2020
"""
21-
label_selector = label_selector.format(name=name)
2221
api = create_k8s_api_client(secrets)
2322
v1 = client.ExtensionsV1beta1Api(api)
24-
if label_selector:
23+
if name:
24+
ret = v1.list_namespaced_replica_set(
25+
ns, field_selector="metadata.name={}".format(name))
26+
elif label_selector:
2527
ret = v1.list_namespaced_replica_set(ns, label_selector=label_selector)
2628
else:
2729
ret = v1.list_namespaced_replica_set(ns)

chaosk8s/statefulset/actions.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,25 @@ def scale_statefulset(name: str, replicas: int, ns: str = "default",
5757
def remove_statefulset(name: str = None, ns: str = "default",
5858
label_selector: str = None, secrets: Secrets = None):
5959
"""
60-
Remove a statefulset by `name` in the namespace `ns`.
60+
Remove a statefulset by `name` or `label_selector` in the namespace `ns`.
6161
6262
The statefulset is removed by deleting it without
6363
a graceful period to trigger an abrupt termination.
6464
65-
The selected resources are matched by the given `label_selector`.
65+
If neither `name` nor `label_selector` is specified, all the statefulsets
66+
will be deleted in the namespace.
6667
"""
67-
field_selector = "metadata.name={name}".format(name=name)
6868
api = create_k8s_api_client(secrets)
6969

7070
v1 = client.AppsV1Api(api)
71-
if label_selector:
71+
if name:
7272
ret = v1.list_namespaced_stateful_set(
73-
ns, field_selector=field_selector,
74-
label_selector=label_selector)
73+
ns, field_selector="metadata.name={}".format(name))
74+
elif label_selector:
75+
ret = v1.list_namespaced_stateful_set(
76+
ns, label_selector=label_selector)
7577
else:
76-
ret = v1.list_namespaced_stateful_set(ns,
77-
field_selector=field_selector)
78+
ret = v1.list_namespaced_stateful_set(ns)
7879

7980
logger.debug("Found {d} statefulset(s) named '{n}' in ns '{s}'".format(
8081
d=len(ret.items), n=name, s=ns))

tests/test_deployment.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def test_delete_deployment(client, api):
4646

4747
delete_deployment("fake_name", "fake_ns")
4848

49-
v1.list_namespaced_deployment.assert_called_with("fake_ns", label_selector=ANY)
49+
v1.list_namespaced_deployment.assert_called_with(
50+
"fake_ns", field_selector="metadata.name=fake_name")
5051
v1.delete_namespaced_deployment.assert_has_calls(
5152
calls=[
5253
call(depl1.metadata.name, "fake_ns", body=ANY),

tests/test_replicaset.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@patch('chaosk8s.replicaset.actions.create_k8s_api_client', autospec=True)
1010
@patch('chaosk8s.replicaset.actions.client', autospec=True)
11-
def test_create_deployment(client, api):
11+
def test_delete_replica_set(client, api):
1212
v1 = MagicMock()
1313
client.ExtensionsV1beta1Api.return_value = v1
1414

@@ -19,7 +19,8 @@ def test_create_deployment(client, api):
1919

2020
delete_replica_set("fake", "fake_ns")
2121

22-
v1.list_namespaced_replica_set.assert_called_with("fake_ns", label_selector="name in (fake)")
22+
v1.list_namespaced_replica_set.assert_called_with(
23+
"fake_ns", field_selector="metadata.name=fake")
2324
v1.delete_namespaced_replica_set.assert_has_calls(
2425
[
2526
call("repl1", "fake_ns", body=ANY),

tests/test_statefulset.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def test_removing_statefulset_with_name(cl, client, has_conf):
4242

4343
remove_statefulset("mystatefulset")
4444

45+
v1.list_namespaced_stateful_set.assert_called_with(
46+
"default", field_selector="metadata.name=mystatefulset")
4547
assert v1.delete_namespaced_stateful_set.call_count == 1
4648
v1.delete_namespaced_stateful_set.assert_called_with(
4749
"mystatefulset", "default", body=ANY)
@@ -63,8 +65,10 @@ def test_removing_statefulset_with_label_selector(cl, client, has_conf):
6365
v1.list_namespaced_stateful_set.return_value = result
6466

6567
label_selector = "app=my-super-app"
66-
remove_statefulset("mystatefulset", label_selector=label_selector)
68+
remove_statefulset(label_selector=label_selector)
6769

70+
v1.list_namespaced_stateful_set.assert_called_with(
71+
"default", label_selector=label_selector)
6872
assert v1.delete_namespaced_stateful_set.call_count == 1
6973
v1.delete_namespaced_stateful_set.assert_called_with(
7074
"mystatefulset", "default", body=ANY)

0 commit comments

Comments
 (0)