Skip to content

Commit b1d56e5

Browse files
committed
Set proxy on the right client
1 parent 1b45ed2 commit b1d56e5

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

ocp_resources/resource.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ def _get_api_version(dyn_client: DynamicClient, api_group: str, kind: str) -> st
7777

7878

7979
def get_client(
80-
config_file: str = "",
80+
config_file: str | None = None,
8181
config_dict: dict[str, Any] | None = None,
82-
context: str = "",
83-
**kwargs: Any,
82+
context: str | None = None,
83+
client_configuration: kubernetes.client.Configuration | None = None,
84+
persist_config: bool = True,
85+
temp_file_path: str | None = None,
86+
try_refresh_token: bool = True,
8487
) -> DynamicClient:
8588
"""
8689
Get a kubernetes client.
@@ -97,24 +100,21 @@ def get_client(
97100
config_file (str): path to a kubeconfig file.
98101
config_dict (dict): dict with kubeconfig configuration.
99102
context (str): name of the context to use.
103+
persist_config (bool): whether to persist config file.
104+
temp_file_path (str): path to a temporary kubeconfig file.
105+
try_refresh_token (bool): try to refresh token
100106
101107
Returns:
102108
DynamicClient: a kubernetes client.
103109
"""
104-
client_configuration = kwargs.get("client_configuration", kubernetes.client.Configuration())
105-
106-
# If the proxy is not set, set it from the environment
107-
if not client_configuration.proxy:
108-
proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY")
109-
if proxy:
110-
client_configuration.proxy = proxy
111-
112-
kwargs["client_configuration"] = client_configuration
113-
114110
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/kube_config.py
115111
if config_dict:
116112
_client = kubernetes.config.new_client_from_config_dict(
117-
config_dict=config_dict, context=context or None, **kwargs
113+
config_dict=config_dict,
114+
context=context,
115+
client_configuration=client_configuration,
116+
persist_config=persist_config,
117+
temp_file_path=temp_file_path,
118118
)
119119
else:
120120
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/__init__.py
@@ -127,20 +127,26 @@ def get_client(
127127

128128
_client = kubernetes.config.new_client_from_config(
129129
config_file=config_file,
130-
context=context or None,
131-
**kwargs,
130+
context=context,
131+
client_configuration=client_configuration,
132+
persist_config=persist_config,
132133
)
133134

135+
proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY")
136+
137+
if not _client.configuration.proxy and proxy:
138+
LOGGER.info(f"Setting proxy from environment variable: {proxy}")
139+
_client.configuration.proxy = proxy
140+
134141
try:
135142
return kubernetes.dynamic.DynamicClient(client=_client)
136143
except MaxRetryError:
137144
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/incluster_config.py
138145
LOGGER.info("Trying to get client via incluster_config")
139146
return kubernetes.dynamic.DynamicClient(
140147
client=kubernetes.config.incluster_config.load_incluster_config(
141-
client_configuration=client_configuration,
142-
try_refresh_token=kwargs.get("try_refresh_token", True),
143-
)
148+
client_configuration=client_configuration, try_refresh_token=try_refresh_token
149+
),
144150
)
145151

146152

tests/test_resources.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
import yaml
33
from docker.errors import DockerException
4-
from kubernetes.config.config_exception import ConfigException
54
from testcontainers.k3s import K3SContainer
65

76
from ocp_resources.exceptions import ResourceTeardownError
@@ -119,5 +118,5 @@ def test_client_with_proxy(monkeypatch, k3scontainer_config):
119118
proxy = "http://env-proxy.com"
120119
monkeypatch.setenv(name="HTTPS_PROXY", value=proxy)
121120

122-
with pytest.raises(ConfigException, match=r"Service host/port is not set."):
123-
get_client(config_dict=k3scontainer_config)
121+
client = get_client(config_dict=k3scontainer_config)
122+
assert client.configuration.proxy == proxy

0 commit comments

Comments
 (0)