diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index b74c1af409..5f8b451358 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -13,6 +13,7 @@ from typing import Optional, Any, Dict, List import kubernetes +from kubernetes import client from kubernetes.dynamic import DynamicClient, ResourceInstance import yaml from benedict import benedict @@ -78,7 +79,11 @@ def _get_api_version(dyn_client: DynamicClient, api_group: str, kind: str) -> st def get_client( - config_file: str = "", config_dict: Dict[str, Any] | None = None, context: str = "", **kwargs: Any + config_file: str = "", + config_dict: Dict[str, Any] | None = None, + client_configuration: client.Configuration | None = None, + context: str = "", + **kwargs: Any, ) -> DynamicClient: """ Get a kubernetes client. @@ -94,6 +99,7 @@ def get_client( Args: config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. + client_configuration: The kubernetes.client.Configuration to set configs to. context (str): name of the context to use. Returns: @@ -107,15 +113,24 @@ def get_client( ) ) try: - # Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/__init__.py - LOGGER.info("Trying to get client via new_client_from_config") - # kubernetes.config.kube_config.load_kube_config sets KUBE_CONFIG_DEFAULT_LOCATION during module import. # If `KUBECONFIG` environment variable is set via code, the `KUBE_CONFIG_DEFAULT_LOCATION` will be None since # is populated during import which comes before setting the variable in code. config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config") + client_configuration = client_configuration or client.Configuration() + proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") + + if proxy: + LOGGER.info(f"Trying to get client using proxy {proxy}") + client_configuration.proxy = proxy + + # Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/__init__.py + LOGGER.info("Trying to get client via new_client_from_config") + return kubernetes.dynamic.DynamicClient( - client=kubernetes.config.new_client_from_config(config_file=config_file, context=context or None, **kwargs) + client=kubernetes.config.new_client_from_config( + config_file=config_file, client_configuration=client_configuration, context=context or None, **kwargs + ) ) except MaxRetryError: # Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/incluster_config.py