ansible-collections / kubernetes.core

The collection includes a variety of Ansible content to help automate the management of applications in Kubernetes and OpenShift clusters, as well as the provisioning and maintenance of clusters themselves.
Other
216 stars 135 forks source link

Unable to list resources due to KeyError: 'kind' on Kubernetes v1.24.3 #636

Open faisalchishtii opened 1 year ago

faisalchishtii commented 1 year ago
SUMMARY

Unable to list resources due to KeyError: 'kind' on Kubernetes v1.24.3. Works on older versions like v1.20.4.

Error:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: KeyError: 'kind' fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/root/.ansible/tmp/ansible-tmp-1688559049.12-8819-73623407880326/AnsiballZ_k8s_info.py\", line 102, in \n _ansiballz_main()\n File \"/root/.ansible/tmp/ansible-tmp-1688559049.12-8819-73623407880326/AnsiballZ_k8s_info.py\", line 94, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/root/.ansible/tmp/ansible-tmp-1688559049.12-8819-73623407880326/AnsiballZ_k8s_info.py\", line 40, in invoke_module\n runpy.run_module(mod_name='ansible_collections.kubernetes.core.plugins.modules.k8s_info', init_globals=None, run_name='main', alter_sys=True)\n File \"/usr/lib/python3.10/runpy.py\", line 224, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File \"/usr/lib/python3.10/runpy.py\", line 96, in _run_module_code\n _run_code(code, mod_globals, init_globals,\n File \"/usr/lib/python3.10/runpy.py\", line 86, in _run_code\n exec(code, run_globals)\n File \"/tmp/ansible_kubernetes.core.k8s_info_payload_osvhd0sq/ansible_kubernetes.core.k8s_info_payload.zip/ansible_collections/kubernetes/core/plugins/modules/k8s_info.py\", line 206, in \n File \"/tmp/ansible_kubernetes.core.k8s_info_payload_osvhd0sq/ansible_kubernetes.core.k8s_info_payload.zip/ansible_collections/kubernetes/core/plugins/modules/k8s_info.py\", line 202, in main\n File \"/tmp/ansible_kubernetes.core.k8s_info_payload_osvhd0sq/ansible_kubernetes.core.k8s_info_payload.zip/ansible_collections/kubernetes/core/plugins/modules/k8s_info.py\", line 173, in execute_module\n File \"/tmp/ansible_kubernetes.core.k8s_info_payload_osvhd0sq/ansible_kubernetes.core.k8s_info_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/common.py\", line 312, in kubernetes_facts\n File \"/tmp/ansible_kubernetes.core.k8s_info_payload_osvhd0sq/ansible_kubernetes.core.k8s_info_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/common.py\", line 301, in find_resource\n File \"/usr/local/lib/python3.10/dist-packages/openshift/dynamic/discovery.py\", line 170, in get\n results = self.search(kwargs)\n File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 246, in search\n results = self.search(self.build_search(kwargs), self.resources, [])\n File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 294, in search\n matches.extend(self.search([key] + parts[1:], resources, reqParams))\n File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 280, in search\n return self.search(parts[1:], resourcePart, reqParams + [part] )\n File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 294, in search\n matches.extend(self.search([key] + parts[1:], resources, reqParams))\n File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 280, in search\n return self.search(parts[1:], resourcePart, reqParams + [part] )\n File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 269, in search\n resourcePart.resources = self.get_resources_for_api_version(\n File \"/usr/local/lib/python3.10/dist-packages/openshift/dynamic/discovery.py\", line 126, in get_resources_for_api_version\n resources_response = self.client.request('GET', path).resources or []\n File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/client.py\", line 62, in inner\n return serializer(self, json.loads(resp.data.decode('utf8')))\n File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/resource.py\", line 291, in init\n kind = instance['kind']\nKeyError: 'kind'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

ISSUE TYPE
COMPONENT NAME

kubernetes.core.k8s_info

ANSIBLE VERSION
ansible --version
/usr/local/lib/python2.7/dist-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.
  from cryptography.exceptions import InvalidSignature
ansible 2.10.8
  config file = None
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.18 (default, Jul  1 2022, 10:30:50) [GCC 11.2.0]
COLLECTION VERSION
ansible-galaxy collection list  | grep kubernetes
/usr/local/lib/python2.7/dist-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.
  from cryptography.exceptions import InvalidSignature
community.kubernetes          1.2.1  
kubernetes.core               1.2.1  
CONFIGURATION
ansible-config dump --only-changed
/usr/local/lib/python2.7/dist-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.
  from cryptography.exceptions import InvalidSignature
OS / ENVIRONMENT

Linux

STEPS TO REPRODUCE
- hosts: 127.0.0.1
  connection: local
  tasks:
    - name: Search for all Pods labelled app=web
      kubernetes.core.k8s_info:
        api_version: v1
        kind: Service
        kubeconfig: "/root/.kube/config1"
      register: pod_list2

    - debug:
        msg: "{{ pod_list2 }}"
EXPECTED RESULTS

Expect the list of resources in json format

ACTUAL RESULTS

PLAY [127.0.0.1] **********************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
[DEPRECATION WARNING]: Distribution ubuntu 22.04 on host localhost should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release 
will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.10/reference_appendices/interpreter_discovery.html for more information. This feature will be 
removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [localhost]

TASK [Search for all Pods labelled app=web] *******************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: KeyError: 'kind'
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1688559793.54-9050-157682426982778/AnsiballZ_k8s_info.py\", line 102, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1688559793.54-9050-157682426982778/AnsiballZ_k8s_info.py\", line 94, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1688559793.54-9050-157682426982778/AnsiballZ_k8s_info.py\", line 40, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.kubernetes.core.plugins.modules.k8s_info', init_globals=None, run_name='__main__', alter_sys=True)\n  File \"/usr/lib/python3.10/runpy.py\", line 224, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib/python3.10/runpy.py\", line 96, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib/python3.10/runpy.py\", line 86, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_kubernetes.core.k8s_info_payload_6g0dtr78/ansible_kubernetes.core.k8s_info_payload.zip/ansible_collections/kubernetes/core/plugins/modules/k8s_info.py\", line 206, in <module>\n  File \"/tmp/ansible_kubernetes.core.k8s_info_payload_6g0dtr78/ansible_kubernetes.core.k8s_info_payload.zip/ansible_collections/kubernetes/core/plugins/modules/k8s_info.py\", line 202, in main\n  File \"/tmp/ansible_kubernetes.core.k8s_info_payload_6g0dtr78/ansible_kubernetes.core.k8s_info_payload.zip/ansible_collections/kubernetes/core/plugins/modules/k8s_info.py\", line 173, in execute_module\n  File \"/tmp/ansible_kubernetes.core.k8s_info_payload_6g0dtr78/ansible_kubernetes.core.k8s_info_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/common.py\", line 312, in kubernetes_facts\n  File \"/tmp/ansible_kubernetes.core.k8s_info_payload_6g0dtr78/ansible_kubernetes.core.k8s_info_payload.zip/ansible_collections/kubernetes/core/plugins/module_utils/common.py\", line 301, in find_resource\n  File \"/usr/local/lib/python3.10/dist-packages/openshift/dynamic/discovery.py\", line 170, in get\n    results = self.search(**kwargs)\n  File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 246, in search\n    results = self.__search(self.__build_search(**kwargs), self.__resources, [])\n  File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 294, in __search\n    matches.extend(self.__search([key] + parts[1:], resources, reqParams))\n  File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 280, in __search\n    return self.__search(parts[1:], resourcePart, reqParams + [part] )\n  File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 294, in __search\n    matches.extend(self.__search([key] + parts[1:], resources, reqParams))\n  File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 280, in __search\n    return self.__search(parts[1:], resourcePart, reqParams + [part] )\n  File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/discovery.py\", line 269, in __search\n    resourcePart.resources = self.get_resources_for_api_version(\n  File \"/usr/local/lib/python3.10/dist-packages/openshift/dynamic/discovery.py\", line 126, in get_resources_for_api_version\n    resources_response = self.client.request('GET', path).resources or []\n  File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/client.py\", line 62, in inner\n    return serializer(self, json.loads(resp.data.decode('utf8')))\n  File \"/usr/local/lib/python3.10/dist-packages/kubernetes/dynamic/resource.py\", line 291, in __init__\n    kind = instance['kind']\nKeyError: 'kind'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
gravesm commented 1 year ago

@faisalchishtii You are using very old versions of things. The latest version of this collection is 2.4.0 an the latest version of Ansible is 2.15.1. Also, note that Python 2 is no longer supported. Could you please try upgrading?