From cf0b71f93eeb926fa6650f85219c934440e3bb4d Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Fri, 4 Jun 2021 10:23:21 +0530 Subject: [PATCH] lookup: return list even if single item found (#117) Always return list from k8s lookup plugin Fixes: #9 Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/9_lookup_k8s.yml | 3 ++ molecule/default/converge.yml | 8 ++++ molecule/default/tasks/lookup_k8s.yml | 54 +++++++++++++++++++++++++++ plugins/lookup/k8s.py | 17 +-------- 4 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 changelogs/fragments/9_lookup_k8s.yml create mode 100644 molecule/default/tasks/lookup_k8s.yml diff --git a/changelogs/fragments/9_lookup_k8s.yml b/changelogs/fragments/9_lookup_k8s.yml new file mode 100644 index 0000000000..9e370f3b1e --- /dev/null +++ b/changelogs/fragments/9_lookup_k8s.yml @@ -0,0 +1,3 @@ +--- +bugfixes: +- k8s - lookup should return list even if single item is found (https://github.com/ansible-collections/kubernetes.core/issues/9). diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index f389e905c7..730081e6a8 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -156,6 +156,14 @@ tags: - always + - name: Include lookup_k8s.yml + include_tasks: + file: tasks/lookup_k8s.yml + apply: + tags: [ lookup, k8s ] + tags: + - always + roles: - role: helm tags: diff --git a/molecule/default/tasks/lookup_k8s.yml b/molecule/default/tasks/lookup_k8s.yml new file mode 100644 index 0000000000..2ad21c0827 --- /dev/null +++ b/molecule/default/tasks/lookup_k8s.yml @@ -0,0 +1,54 @@ +--- +- block: + # https://github.com/ansible-collections/kubernetes.core/issues/9 + - name: Create a namespace with label + kubernetes.core.k8s: + definition: + apiVersion: v1 + kind: Namespace + metadata: + name: "app-development-one" + labels: + namespace_label: "app_development" + + - set_fact: + namespace_info: "{{ lookup('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development') }}" + + - name: Check if the returned value is list with a single element + assert: + that: + - namespace_info is iterable + - not namespace_info is string + - not namespace_info is mapping + - namespace_info | length == 1 + + - name: Create another namespace with label + kubernetes.core.k8s: + definition: + apiVersion: v1 + kind: Namespace + metadata: + name: "app-development-two" + labels: + namespace_label: "app_development" + + - set_fact: + namespace_info: "{{ lookup('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development') }}" + + - name: Check if the returned value is list with 2 elements + assert: + that: + - namespace_info is iterable + - not namespace_info is string + - not namespace_info is mapping + - namespace_info | length == 2 + + always: + - name: Ensure that namespace is removed + k8s: + kind: Namespace + name: "{{ item }}" + state: absent + with_items: + - app-development-one + - app-development-two diff --git a/plugins/lookup/k8s.py b/plugins/lookup/k8s.py index e363d1e30b..b4517fcdf9 100644 --- a/plugins/lookup/k8s.py +++ b/plugins/lookup/k8s.py @@ -1,20 +1,7 @@ # # Copyright 2018 Red Hat | Ansible # -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) @@ -277,7 +264,7 @@ def run(self, terms, variables=None, **kwargs): if self.name: return [k8s_obj.to_dict()] - return k8s_obj.to_dict().get('items') + return [k8s_obj.to_dict().get('items')] class LookupModule(LookupBase):