Skip to content

Commit 043a0f7

Browse files
committed
Add proxy support to Kubernetes client configuration in get_client
This update modifies the get_client function to support setting a proxy for the Kubernetes client configuration. The client_configuration argument is introduced to allow passing a client.Configuration object directly, and if not provided, it defaults to creating a new configuration instance. Additionally, if the HTTPS_PROXY or HTTP_PROXY environment variables are set, the proxy is applied to the Kubernetes client configuration. This enhancement enables easier integration of Kubernetes client setup when working behind a proxy. Signed-off-by: Shahaf Bahar <[email protected]>
1 parent 7f0771b commit 043a0f7

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

ocp_resources/resource.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from collections.abc import Callable, Generator
44
import contextlib
5+
from kubernetes import client
56

67
import copy
78
import json
@@ -79,7 +80,11 @@ def _get_api_version(dyn_client: DynamicClient, api_group: str, kind: str) -> st
7980

8081

8182
def get_client(
82-
config_file: str = "", config_dict: dict[str, Any] | None = None, context: str = "", **kwargs: Any
83+
config_file: str = "",
84+
config_dict: dict[str, Any] | None = None,
85+
client_configuration: client.Configuration | None = None,
86+
context: str = "",
87+
**kwargs: Any,
8388
) -> DynamicClient:
8489
"""
8590
Get a kubernetes client.
@@ -95,6 +100,7 @@ def get_client(
95100
Args:
96101
config_file (str): path to a kubeconfig file.
97102
config_dict (dict): dict with kubeconfig configuration.
103+
client_configuration (client.Configuration): The kubernetes.client.Configuration to set configs to.
98104
context (str): name of the context to use.
99105
100106
Returns:
@@ -115,8 +121,17 @@ def get_client(
115121
# If `KUBECONFIG` environment variable is set via code, the `KUBE_CONFIG_DEFAULT_LOCATION` will be None since
116122
# is populated during import which comes before setting the variable in code.
117123
config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config")
124+
client_configuration = client_configuration or client.Configuration()
125+
proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY")
126+
127+
if proxy:
128+
LOGGER.info(f"Trying to get client using proxy {proxy}")
129+
client_configuration.proxy = proxy
130+
118131
return kubernetes.dynamic.DynamicClient(
119-
client=kubernetes.config.new_client_from_config(config_file=config_file, context=context or None, **kwargs)
132+
client=kubernetes.config.new_client_from_config(
133+
config_file=config_file, client_configuration=client_configuration, context=context or None, **kwargs
134+
)
120135
)
121136
except MaxRetryError:
122137
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/incluster_config.py

0 commit comments

Comments
 (0)