mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-05-07 13:32:37 +00:00
Move module dependency functions outside of module (#342)
Move module dependency functions outside of module SUMMARY This moves the has_at_least and requires functions that had been on the module to top level functions. The functions on the module now call these with a few added bits of functionality. Moving these functions to the top level and removing their requirement on having a module makes them usable in situations where we may not yet have a module, such as during client creation. ISSUE TYPE COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Alina Buzachis <None> Reviewed-by: None <None>
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
|
||||
import os
|
||||
import hashlib
|
||||
from distutils.version import LooseVersion
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from ansible.module_utils.six import iteritems, string_types
|
||||
@@ -13,6 +12,7 @@ from ansible_collections.kubernetes.core.plugins.module_utils.args_common import
|
||||
AUTH_ARG_SPEC,
|
||||
AUTH_PROXY_HEADERS_SPEC,
|
||||
)
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import requires
|
||||
|
||||
try:
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils import (
|
||||
@@ -40,20 +40,9 @@ except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
module = None
|
||||
_pool = {}
|
||||
|
||||
|
||||
def _requires_kubernetes_at_least(version: str):
|
||||
if module:
|
||||
module.requires("kubernetes", version)
|
||||
else:
|
||||
if LooseVersion(kubernetes.__version__) < LooseVersion(version):
|
||||
raise Exception(
|
||||
f"kubernetes >= {version} is required to use in-memory kubeconfig."
|
||||
)
|
||||
|
||||
|
||||
def _create_auth_spec(module=None, **kwargs) -> Dict:
|
||||
auth: Dict = {}
|
||||
# If authorization variables aren't defined, look for them in environment variables
|
||||
@@ -97,7 +86,6 @@ def _load_config(auth: Dict) -> None:
|
||||
if isinstance(kubeconfig, string_types):
|
||||
kubernetes.config.load_kube_config(config_file=kubeconfig, **optional_arg)
|
||||
elif isinstance(kubeconfig, dict):
|
||||
_requires_kubernetes_at_least("17.17.0")
|
||||
kubernetes.config.load_kube_config_from_dict(
|
||||
config_dict=kubeconfig, **optional_arg
|
||||
)
|
||||
@@ -241,6 +229,11 @@ class K8SClient:
|
||||
|
||||
def get_api_client(module=None, **kwargs: Optional[Any]) -> K8SClient:
|
||||
auth_spec = _create_auth_spec(module, **kwargs)
|
||||
if isinstance(auth_spec.get("kubeconfig"), dict):
|
||||
if module:
|
||||
module.requires("kubernetes", "17.17.0", "to use in-memory config")
|
||||
else:
|
||||
requires("kubernetes", "17.17.0", "to use in-memory config")
|
||||
configuration = _create_configuration(auth_spec)
|
||||
client = create_api_client(configuration)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user