Skip to content

Commit 7f7008f

Browse files
authored
k8s_log - fix module traceback when resource not found (#493)
k8s_log - fix module traceback when resource not found Depends-on: #495 SUMMARY closes #479 ISSUE TYPE Bugfix Pull Request COMPONENT NAME k8s_log Reviewed-by: Mike Graves <[email protected]>
1 parent 09d5491 commit 7f7008f

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
bugfixes:
3+
- k8s_log - Fix module traceback when no resource found (https://github.com/ansible-collections/kubernetes.core/issues/479).

plugins/modules/k8s_log.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@
152152
K8sService,
153153
)
154154

155+
try:
156+
from kubernetes.client.exceptions import ApiException
157+
except ImportError:
158+
# ImportError are managed by the common module already.
159+
pass
160+
155161

156162
def argspec():
157163
args = copy.deepcopy(AUTH_ARG_SPEC)
@@ -217,9 +223,17 @@ def execute_module(svc, params):
217223
{"tailLines": params["tail_lines"]}
218224
)
219225

220-
response = resource.log.get(
221-
name=name, namespace=namespace, serialize=False, **kwargs
222-
)
226+
try:
227+
response = resource.log.get(
228+
name=name, namespace=namespace, serialize=False, **kwargs
229+
)
230+
except ApiException as exc:
231+
if exc.reason == "Not Found":
232+
raise CoreException("Pod {0}/{1} not found.".format(namespace, name))
233+
raise CoreException(
234+
"Unable to retrieve log from Pod due to: {0}".format(exc.reason)
235+
)
236+
223237
log = response.data.decode("utf8")
224238

225239
return {"changed": False, "log": log, "log_lines": log.split("\n")}

tests/integration/targets/k8s_log/tasks/main.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
---
22
- block:
3+
- name: Retrieve log from unexisting Pod
4+
k8s_log:
5+
namespace: "{{ test_namespace }}"
6+
name: "this_pod_does_exist"
7+
ignore_errors: true
8+
register: fake_pod
9+
10+
- name: Assert that task failed with proper message
11+
assert:
12+
that:
13+
- fake_pod is failed
14+
- 'fake_pod.msg == "Pod {{ test_namespace }}/this_pod_does_exist not found."'
15+
316
- name: create hello-world deployment
417
k8s:
518
wait: yes

0 commit comments

Comments
 (0)