feat(inventory): Use OCP projects if available

If no namespaces were specified in the inventory config try to get all
available namespaces by trying to list OCP projects first. If the resource
was not found (no OCP cluster) fall back to regular namespaces.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
Felix Matouschek
2025-04-28 15:41:31 +02:00
parent 0b542ddced
commit 4a8b4ead2f
3 changed files with 54 additions and 2 deletions

View File

@@ -141,7 +141,7 @@ from typing import (
# Set HAS_K8S_MODULE_HELPER and k8s_import exception accordingly to
# potentially print a warning to the user if the client is missing.
try:
from kubernetes.dynamic.exceptions import DynamicApiError
from kubernetes.dynamic.exceptions import DynamicApiError, ResourceNotFoundError
HAS_K8S_MODULE_HELPER = True
K8S_IMPORT_EXCEPTION = None
@@ -152,6 +152,11 @@ except ImportError as e:
Dummy class, mainly used for ansible-test sanity.
"""
class ResourceNotFoundError(Exception):
"""
Dummy class, mainly used for ansible-test sanity.
"""
HAS_K8S_MODULE_HELPER = False
K8S_IMPORT_EXCEPTION = e
@@ -575,9 +580,18 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
_get_available_namespaces lists all namespaces accessible with the
configured credentials and returns them.
"""
namespaces = []
try:
namespaces = self._get_resources(
client, "project.openshift.io/v1", "Project"
)
except ResourceNotFoundError:
namespaces = self._get_resources(client, "v1", "Namespace")
return [
namespace["metadata"]["name"]
for namespace in self._get_resources(client, "v1", "Namespace")
for namespace in namespaces
if "metadata" in namespace and "name" in namespace["metadata"]
]