chore: Bump dependency on kubernetes.core

Bump the dependency on kubernetes.core to versions >=3.0.1 and remove
the workaround introduced by 4429ac8c03.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
Felix Matouschek
2024-03-04 15:51:27 +01:00
parent f1c1d4825a
commit 48734484f9
4 changed files with 10 additions and 108 deletions

View File

@@ -1,3 +1,4 @@
---
ancestor: 1.0.0 ancestor: 1.0.0
releases: releases:
1.0.0: 1.0.0:
@@ -7,14 +8,14 @@ releases:
1.1.0: 1.1.0:
changes: changes:
major_changes: major_changes:
- Add kubevirt_vm_info module to describe existing VirtualMachines - Add kubevirt_vm_info module to describe existing VirtualMachines
minor_changes: minor_changes:
- 'inventory: Allow to control creation of additional groups' - 'inventory: Allow to control creation of additional groups'
- 'inventory: Drop creation of the namespace_vmis_group as it is redundant' - 'inventory: Drop creation of the namespace_vmis_group as it is redundant'
fragments: fragments:
- 0_flatten_groups.yml - 0_flatten_groups.yml
- 1_add_create_groups.yml - 1_add_create_groups.yml
- 3_add_kubevirt_vm_info.yml - 3_add_kubevirt_vm_info.yml
release_date: '2023-09-21' release_date: '2023-09-21'
1.2.0: 1.2.0:
release_date: '2024-03-04' release_date: '2024-03-04'

View File

@@ -6,7 +6,7 @@ readme: README.md
authors: authors:
- KubeVirt Project (kubevirt.io) - KubeVirt Project (kubevirt.io)
dependencies: dependencies:
kubernetes.core: '>=2.0.0' kubernetes.core: '>=3.0.1'
description: Lean Ansible bindings for KubeVirt description: Lean Ansible bindings for KubeVirt
license_file: LICENSE license_file: LICENSE
tags: tags:

View File

@@ -160,7 +160,6 @@ EXAMPLES = """
""" """
from dataclasses import dataclass from dataclasses import dataclass
from collections import defaultdict
from json import loads from json import loads
from typing import ( from typing import (
Any, Any,
@@ -173,19 +172,12 @@ from typing import (
# Handle import errors of python kubernetes client. # Handle import errors of python kubernetes client.
# HAS_K8S_MODULE_HELPER imported below will print a warning to the user if the client is missing. # HAS_K8S_MODULE_HELPER imported below will print a warning to the user if the client is missing.
try: try:
from kubernetes.dynamic.resource import Resource from kubernetes.dynamic.exceptions import DynamicApiError
from kubernetes.dynamic.exceptions import DynamicApiError, ServiceUnavailableError
except ImportError: except ImportError:
class Resource:
pass
class DynamicApiError(Exception): class DynamicApiError(Exception):
pass pass
class ServiceUnavailableError(Exception):
pass
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
@@ -194,29 +186,6 @@ from ansible_collections.kubernetes.core.plugins.module_utils.common import (
k8s_import_exception, k8s_import_exception,
) )
# Handle import errors of python kubernetes client.
# HAS_K8S_MODULE_HELPER imported above will print a warning to the user if the client is missing.
try:
from ansible_collections.kubernetes.core.plugins.module_utils.client.discovery import (
Discoverer,
)
except ImportError:
class Discoverer:
pass
# Handle import errors of python kubernetes client.
# HAS_K8S_MODULE_HELPER imported above will print a warning to the user if the client is missing.
try:
from ansible_collections.kubernetes.core.plugins.module_utils.client.resource import (
ResourceList,
)
except ImportError:
class ResourceList:
pass
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
get_api_client, get_api_client,
@@ -340,9 +309,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
super().__init__() super().__init__()
self.host_format = None self.host_format = None
# Monkey patch the Discoverers method, see end of file for an explanation.
Discoverer.get_resources_for_api_version = _get_resources_for_api_version
def verify_file(self, path: str) -> None: def verify_file(self, path: str) -> None:
""" """
verify_file ensures the inventory file is compatible with this plugin. verify_file ensures the inventory file is compatible with this plugin.
@@ -695,68 +661,3 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
ansible_host = ip_address ansible_host = ip_address
self.inventory.set_variable(vmi_name, "ansible_host", ansible_host) self.inventory.set_variable(vmi_name, "ansible_host", ansible_host)
# This function is copied from the kubernetes.core sources and a fix to handle apis of the format a/b/c was applied.
# To ensure compatibility with KubeVirt >=1.1.0 the kubernetes.core Discoverers original method is monkey patched with this one.
# TODO: Remove this once the fix was applied to the upstream kubernetes.core collection.
#
# See:
# https://github.com/ansible-collections/kubernetes.core/blob/main/plugins/module_utils/client/discovery.py
# https://github.com/ansible-collections/kubernetes.core/issues/685
# https://github.com/kubernetes-client/python/issues/2091
# https://github.com/kubernetes-client/python/pull/2095
def _get_resources_for_api_version(self, prefix, group, version, preferred):
"""returns a dictionary of resources associated with provided (prefix, group, version)"""
resources = defaultdict(list)
subresources = defaultdict(dict)
path = "/".join(filter(None, [prefix, group, version]))
try:
resources_response = self.client.request("GET", path).resources or []
except ServiceUnavailableError:
resources_response = []
resources_raw = list(
filter(lambda resource: "/" not in resource["name"], resources_response)
)
subresources_raw = list(
filter(lambda resource: "/" in resource["name"], resources_response)
)
for subresource in subresources_raw:
resource, name = subresource["name"].split("/", 1)
subresources[resource][name] = subresource
for resource in resources_raw:
# Prevent duplicate keys
for key in ("prefix", "group", "api_version", "client", "preferred"):
resource.pop(key, None)
resourceobj = Resource(
prefix=prefix,
group=group,
api_version=version,
client=self.client,
preferred=preferred,
subresources=subresources.get(resource["name"]),
**resource,
)
resources[resource["kind"]].append(resourceobj)
resource_lookup = {
"prefix": prefix,
"group": group,
"api_version": version,
"kind": resourceobj.kind,
"name": resourceobj.name,
}
resource_list = ResourceList(
self.client,
group=group,
api_version=version,
base_kind=resource["kind"],
base_resource_lookup=resource_lookup,
)
resources[resource_list.kind].append(resource_list)
return resources

View File

@@ -1,4 +1,4 @@
--- ---
collections: collections:
- name: kubernetes.core - name: kubernetes.core
version: '>=2.0.0' version: '>=3.0.1'