mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-05-06 13:02:37 +00:00
handle aliases for lookup and inventory plugins for authentication options (#500)
Honor aliases for lookup and inventory plugins rebase and extend the following PR #71 ISSUE TYPE Bugfix Pull Request Reviewed-by: Mike Graves <mgraves@redhat.com>
This commit is contained in:
@@ -118,9 +118,10 @@ import json
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.common import (
|
||||
K8sAnsibleMixin,
|
||||
HAS_K8S_MODULE_HELPER,
|
||||
k8s_import_exception,
|
||||
)
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
|
||||
get_api_client,
|
||||
)
|
||||
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
|
||||
@@ -146,7 +147,7 @@ class K8sInventoryException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable, K8sAnsibleMixin):
|
||||
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||
NAME = "kubernetes.core.k8s"
|
||||
|
||||
connection_plugin = "kubernetes.core.kubectl"
|
||||
|
||||
@@ -180,10 +180,12 @@ from ansible.errors import AnsibleError
|
||||
from ansible.module_utils.common._collections_compat import KeysView
|
||||
from ansible.module_utils.common.validation import check_type_bool
|
||||
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.common import (
|
||||
K8sAnsibleMixin,
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
|
||||
get_api_client,
|
||||
)
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import (
|
||||
create_definitions,
|
||||
)
|
||||
|
||||
try:
|
||||
enable_turbo_mode = check_type_bool(os.environ.get("ENABLE_TURBO_MODE"))
|
||||
@@ -210,7 +212,7 @@ except ImportError as e:
|
||||
k8s_import_exception = e
|
||||
|
||||
|
||||
class KubernetesLookup(K8sAnsibleMixin):
|
||||
class KubernetesLookup(object):
|
||||
def __init__(self):
|
||||
|
||||
if not HAS_K8S_MODULE_HELPER:
|
||||
@@ -240,7 +242,7 @@ class KubernetesLookup(K8sAnsibleMixin):
|
||||
|
||||
cluster_info = kwargs.get("cluster_info")
|
||||
if cluster_info == "version":
|
||||
return [self.client.version]
|
||||
return [self.client.client.version]
|
||||
if cluster_info == "api_groups":
|
||||
if isinstance(self.client.resources.api_groups, KeysView):
|
||||
return [list(self.client.resources.api_groups)]
|
||||
@@ -257,7 +259,12 @@ class KubernetesLookup(K8sAnsibleMixin):
|
||||
resource_definition = kwargs.get("resource_definition")
|
||||
src = kwargs.get("src")
|
||||
if src:
|
||||
resource_definition = self.load_resource_definitions(src)[0]
|
||||
definitions = create_definitions(params=dict(src=src))
|
||||
if definitions:
|
||||
self.kind = definitions[0].kind
|
||||
self.name = definitions[0].name
|
||||
self.namespace = definitions[0].namespace
|
||||
self.api_version = definitions[0].api_version or "v1"
|
||||
if resource_definition:
|
||||
self.kind = resource_definition.get("kind", self.kind)
|
||||
self.api_version = resource_definition.get("apiVersion", self.api_version)
|
||||
@@ -272,14 +279,15 @@ class KubernetesLookup(K8sAnsibleMixin):
|
||||
"using the 'resource_definition' parameter."
|
||||
)
|
||||
|
||||
resource = self.find_resource(self.kind, self.api_version, fail=True)
|
||||
resource = self.client.resource(self.kind, self.api_version)
|
||||
try:
|
||||
k8s_obj = resource.get(
|
||||
params = dict(
|
||||
name=self.name,
|
||||
namespace=self.namespace,
|
||||
label_selector=self.label_selector,
|
||||
field_selector=self.field_selector,
|
||||
)
|
||||
k8s_obj = self.client.get(resource, **params)
|
||||
except NotFoundError:
|
||||
return []
|
||||
|
||||
|
||||
@@ -81,6 +81,9 @@ def _create_auth_spec(module=None, **kwargs) -> Dict:
|
||||
auth[true_name] = module.params.get(arg_name)
|
||||
elif arg_name in kwargs and kwargs.get(arg_name) is not None:
|
||||
auth[true_name] = kwargs.get(arg_name)
|
||||
elif true_name in kwargs and kwargs.get(true_name) is not None:
|
||||
# Aliases in kwargs
|
||||
auth[true_name] = kwargs.get(true_name)
|
||||
elif arg_name == "proxy_headers":
|
||||
# specific case for 'proxy_headers' which is a dictionary
|
||||
proxy_headers = {}
|
||||
@@ -131,7 +134,11 @@ def _create_configuration(auth: Dict):
|
||||
# Removing trailing slashes if any from hostname
|
||||
auth["host"] = auth.get("host").rstrip("/")
|
||||
|
||||
if auth_set("username", "password", "host") or auth_set("api_key", "host"):
|
||||
if (
|
||||
auth_set("username", "password", "host")
|
||||
or auth_set("api_key", "host")
|
||||
or auth_set("cert_file", "key_file", "host")
|
||||
):
|
||||
# We have enough in the parameters to authenticate, no need to load incluster or kubeconfig
|
||||
pass
|
||||
elif auth_set("kubeconfig") or auth_set("context"):
|
||||
@@ -346,10 +353,14 @@ def get_api_client(module=None, **kwargs: Optional[Any]) -> K8SClient:
|
||||
msg = "Could not create API client: {0}".format(e)
|
||||
raise CoreException(msg) from e
|
||||
|
||||
dry_run = False
|
||||
if module:
|
||||
dry_run = module.params.get("dry_run", False)
|
||||
|
||||
k8s_client = K8SClient(
|
||||
configuration=configuration,
|
||||
client=client,
|
||||
dry_run=module.params.get("dry_run", False),
|
||||
dry_run=dry_run,
|
||||
)
|
||||
|
||||
return k8s_client
|
||||
|
||||
Reference in New Issue
Block a user