From bac6f99608d370e9bf4b0971de24e0abd8a6971d Mon Sep 17 00:00:00 2001 From: Will Thames Date: Tue, 23 Oct 2018 14:05:31 +1000 Subject: [PATCH] Ensure that k8s_facts always returns resources key (#46733) (#47253) * Ensure that k8s_facts always returns resources key (#46733) Fix bug returning `items` key if NotFound exception is hit (cherry picked from commit b772485d97168722c241aca13906ab016b332cdd) * Add changelog for k8s_facts fix --- changelogs/fragments/k8s_facts_fix.yaml | 3 +++ lib/ansible/module_utils/k8s/common.py | 2 +- .../k8s/playbooks/roles/k8s/tasks/main.yml | 15 ++++++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/k8s_facts_fix.yaml diff --git a/changelogs/fragments/k8s_facts_fix.yaml b/changelogs/fragments/k8s_facts_fix.yaml new file mode 100644 index 00000000000..bfef56a34ec --- /dev/null +++ b/changelogs/fragments/k8s_facts_fix.yaml @@ -0,0 +1,3 @@ +--- +bugfixes: +- k8s_facts now returns a resources key in all situations diff --git a/lib/ansible/module_utils/k8s/common.py b/lib/ansible/module_utils/k8s/common.py index e05f1ee7ccb..f60c232fe9a 100644 --- a/lib/ansible/module_utils/k8s/common.py +++ b/lib/ansible/module_utils/k8s/common.py @@ -204,7 +204,7 @@ class K8sAnsibleMixin(object): label_selector=','.join(label_selectors), field_selector=','.join(field_selectors)).to_dict() except openshift.dynamic.exceptions.NotFoundError: - return dict(items=[]) + return dict(resources=[]) if 'items' in result: return dict(resources=result['items']) diff --git a/test/integration/targets/k8s/playbooks/roles/k8s/tasks/main.yml b/test/integration/targets/k8s/playbooks/roles/k8s/tasks/main.yml index beb66b423bd..13ef50fdc4b 100644 --- a/test/integration/targets/k8s/playbooks/roles/k8s/tasks/main.yml +++ b/test/integration/targets/k8s/playbooks/roles/k8s/tasks/main.yml @@ -15,6 +15,19 @@ debug: var: output + - name: k8s_facts works with empty resources + k8s_facts: + kind: Deployment + namespace: testing + api_version: extensions/v1beta1 + register: k8s_facts + + - name: assert that k8s_facts is in correct format + assert: + that: + - "'resources' in k8s_facts" + - not k8s_facts.resources + - name: Create a service k8s: state: present @@ -85,7 +98,7 @@ k8s: state: present inline: &deployment - apiVersion: apps/v1beta1 + apiVersion: extensions/v1beta1 kind: Deployment metadata: name: elastic