Skip to content

Commit e6d27e6

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 e6d27e6

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

ocp_resources/resource.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ def _get_api_version(dyn_client: DynamicClient, api_group: str, kind: str) -> st
7979

8080

8181
def get_client(
82-
config_file: str = "", config_dict: dict[str, Any] | None = None, context: str = "", **kwargs: Any
82+
config_file: str = "",
83+
config_dict: dict[str, Any] | None = None,
84+
client_configuration: kubernetes.client.Configuration | None = None,
85+
context: str = "",
86+
**kwargs: Any,
8387
) -> DynamicClient:
8488
"""
8589
Get a kubernetes client.
@@ -95,6 +99,7 @@ def get_client(
9599
Args:
96100
config_file (str): path to a kubeconfig file.
97101
config_dict (dict): dict with kubeconfig configuration.
102+
client_configuration (kubernetes.client.Configuration): The kubernetes.client.Configuration to set configs to.
98103
context (str): name of the context to use.
99104
100105
Returns:
@@ -115,8 +120,17 @@ def get_client(
115120
# If `KUBECONFIG` environment variable is set via code, the `KUBE_CONFIG_DEFAULT_LOCATION` will be None since
116121
# is populated during import which comes before setting the variable in code.
117122
config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config")
123+
client_configuration = client_configuration or kubernetes.client.Configuration()
124+
proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY")
125+
126+
if proxy:
127+
LOGGER.info(f"Setting proxy in client configuration: {proxy}")
128+
client_configuration.proxy = proxy
129+
118130
return kubernetes.dynamic.DynamicClient(
119-
client=kubernetes.config.new_client_from_config(config_file=config_file, context=context or None, **kwargs)
131+
client=kubernetes.config.new_client_from_config(
132+
config_file=config_file, client_configuration=client_configuration, context=context or None, **kwargs
133+
)
120134
)
121135
except MaxRetryError:
122136
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/incluster_config.py

0 commit comments

Comments
 (0)