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
214 stars 133 forks source link

Dynamic discovery cannot handle APIs of format a/b/c #685

Closed 0xFelix closed 7 months ago

0xFelix commented 7 months ago
SUMMARY

The get_resources_for_api_version() method of module_utils.client.discovery.Discoverer expects sub-resource names to be of the format a/b, which breaks with newly added APIs in KubeVirt that are of the format a/b/c.

See:

https://github.com/kubernetes-client/python/issues/2091 https://github.com/kubernetes-client/python/pull/2095

ISSUE TYPE
COMPONENT NAME

module_utils.client.discovery.Discoverer

ANSIBLE VERSION
ansible [core 2.16.2]
  config file = /home/felix/.ansible.cfg
  configured module search path = ['/home/felix/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.12/site-packages/ansible
  ansible collection location = /home/felix/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.12.2 (main, Feb  7 2024, 00:00:00) [GCC 13.2.1 20231205 (Red Hat 13.2.1-6)] (/usr/bin/python3)
  jinja version = 3.1.3
  libyaml = True
COLLECTION VERSION
# /home/felix/.ansible/collections/ansible_collections
Collection      Version
--------------- -------
kubernetes.core 3.0.0
STEPS TO REPRODUCE

With https://github.com/kubevirt/kubevirt.core try to use the inventory plugin against a cluster running KubeVirt >=1.1.0.

ansible-inventory -i examples/inventory.kubevirt.yml -y --list -vvvv
EXPECTED RESULTS

Inventory should be able to list VMs on cluster.

ACTUAL RESULTS

Inventory fails because it fails to discover APIs on the cluster.

felix@felix-p1 kubevirt/kubevirt.core (bump-version-e2e *) » ansible-inventory -i examples/inventory.kubevirt.yml -y --list -vvvv
ansible-inventory [core 2.16.2]
  config file = /home/felix/.ansible.cfg
  configured module search path = ['/home/felix/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.12/site-packages/ansible
  ansible collection location = /home/felix/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-inventory
  python version = 3.12.2 (main, Feb  7 2024, 00:00:00) [GCC 13.2.1 20231205 (Red Hat 13.2.1-6)] (/usr/bin/python3)
  jinja version = 3.1.3
  libyaml = True
Using /home/felix/.ansible.cfg as config file
setting up inventory plugins
Loading collection kubevirt.core from /home/felix/.ansible/collections/ansible_collections/kubevirt/core
Loading collection ansible.builtin from 
Loading collection kubernetes.core from /home/felix/.ansible/collections/ansible_collections/kubernetes/core
[WARNING]:  * Failed to parse /home/felix/Dokumente/kubevirt/kubevirt.core/examples/inventory.kubevirt.yml with ansible_collections.kubevirt.core.plugins.inventory.kubevirt plugin: too many values to unpack (expected 2)
  File "/usr/lib/python3.12/site-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/home/felix/.ansible/collections/ansible_collections/kubevirt/core/plugins/inventory/kubevirt.py", line 346, in parse
    self.setup(config_data, cache, cache_key)
  File "/home/felix/.ansible/collections/ansible_collections/kubevirt/core/plugins/inventory/kubevirt.py", line 370, in setup
    self.fetch_objects(connections)
  File "/home/felix/.ansible/collections/ansible_collections/kubevirt/core/plugins/inventory/kubevirt.py", line 405, in fetch_objects
    self.get_vmis_for_namespace(client, name, namespace, opts)
  File "/home/felix/.ansible/collections/ansible_collections/kubevirt/core/plugins/inventory/kubevirt.py", line 469, in get_vmis_for_namespace
    services = self.get_ssh_services_for_namespace(client, namespace)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/felix/.ansible/collections/ansible_collections/kubevirt/core/plugins/inventory/kubevirt.py", line 623, in get_ssh_services_for_namespace
    v1_service = client.resources.get(api_version="v1", kind="Service")
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/felix/.ansible/collections/ansible_collections/kubernetes/core/plugins/module_utils/client/discovery.py", line 158, in get
    results = self.search(**kwargs)
              ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/kubernetes/dynamic/discovery.py", line 246, in search
    results = self.__search(self.__build_search(**kwargs), self.__resources, [])
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/kubernetes/dynamic/discovery.py", line 294, in __search
    matches.extend(self.__search([key] + parts[1:], resources, reqParams))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/kubernetes/dynamic/discovery.py", line 280, in __search
    return self.__search(parts[1:], resourcePart, reqParams + [part] )
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/kubernetes/dynamic/discovery.py", line 294, in __search
    matches.extend(self.__search([key] + parts[1:], resources, reqParams))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/kubernetes/dynamic/discovery.py", line 280, in __search
    return self.__search(parts[1:], resourcePart, reqParams + [part] )
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/kubernetes/dynamic/discovery.py", line 269, in __search
    resourcePart.resources = self.get_resources_for_api_version(
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/felix/.ansible/collections/ansible_collections/kubevirt/core/plugins/inventory/kubevirt.py", line 731, in __get_resources_for_api_version
    resource, name = subresource["name"].split("/")
    ^^^^^^^^^^^^^^
[WARNING]: Unable to parse /home/felix/Dokumente/kubevirt/kubevirt.core/examples/inventory.kubevirt.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{}
gravesm commented 7 months ago

Duplicate of #659.