Skip to content

Commit 1b45ed2

Browse files
committed
Use HTTP_PROXY and HTTPS_PROXY environment variables for proxy
Use the same proxy configuration as the `http` and `https` protocols.
1 parent 8515dbb commit 1b45ed2

File tree

3 files changed

+38
-56
lines changed

3 files changed

+38
-56
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,17 @@ export OPENSHIFT_PYTHON_WRAPPER_LOG_LEVEL=<LOG_LEVEL> # can be: "DEBUG", "INFO",
9696
## Proxy Enablement
9797

9898
This configuration allows the client to route traffic through a specified proxy server.
99-
It can be enabled via the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY`.
10099

101100
To enable proxy configuration for the client:
102101

103-
1. Set the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=<any value>`
102+
1. Define either `HTTPS_PROXY` or `HTTP_PROXY` environment variable with your proxy URL:
104103

105-
2. Define either `HTTPS_PROXY` or `HTTP_PROXY` environment variable with your proxy URL:
106104
```bash
107105
export HTTPS_PROXY="http://proxy.example.com:8080"
108106
# or
109107
export HTTP_PROXY="http://proxy.example.com:8080"
110108
```
109+
111110
If neither variable is set when proxy is enabled, a `ValueError` will be raised.
112111

113112
## Code check

ocp_resources/resource.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,22 @@ def get_client(
101101
Returns:
102102
DynamicClient: a kubernetes client.
103103
"""
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+
104114
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/kube_config.py
105115
if config_dict:
106-
return kubernetes.dynamic.DynamicClient(
107-
client=kubernetes.config.new_client_from_config_dict(
108-
config_dict=config_dict, context=context or None, **kwargs
109-
)
116+
_client = kubernetes.config.new_client_from_config_dict(
117+
config_dict=config_dict, context=context or None, **kwargs
110118
)
111-
client_configuration = kwargs.get("client_configuration", kubernetes.client.Configuration())
112-
try:
119+
else:
113120
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/__init__.py
114121
LOGGER.info("Trying to get client via new_client_from_config")
115122

@@ -118,28 +125,14 @@ def get_client(
118125
# is populated during import which comes before setting the variable in code.
119126
config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config")
120127

121-
if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"):
122-
proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY")
123-
if not proxy:
124-
raise ValueError(
125-
"Proxy configuration is enabled but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set."
126-
)
127-
if client_configuration.proxy and client_configuration.proxy != proxy:
128-
raise ValueError(
129-
f"Conflicting proxy settings: client_configuration.proxy={client_configuration.proxy}, "
130-
f"but the environment variable 'HTTPS_PROXY/HTTP_PROXY' defines proxy as {proxy}."
131-
)
132-
client_configuration.proxy = proxy
133-
134-
kwargs["client_configuration"] = client_configuration
135-
136-
return kubernetes.dynamic.DynamicClient(
137-
client=kubernetes.config.new_client_from_config(
138-
config_file=config_file,
139-
context=context or None,
140-
**kwargs,
141-
)
128+
_client = kubernetes.config.new_client_from_config(
129+
config_file=config_file,
130+
context=context or None,
131+
**kwargs,
142132
)
133+
134+
try:
135+
return kubernetes.dynamic.DynamicClient(client=_client)
143136
except MaxRetryError:
144137
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/incluster_config.py
145138
LOGGER.info("Trying to get client via incluster_config")

tests/test_resources.py

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

77
from ocp_resources.exceptions import ResourceTeardownError
@@ -19,13 +19,18 @@ def clean_up(self, wait: bool = True, timeout: int | None = None) -> bool:
1919
return False
2020

2121

22-
@pytest.fixture(scope="session")
23-
def client():
22+
@pytest.fixture(scope="class")
23+
def k3scontainer_config():
2424
try:
2525
with K3SContainer() as k3s:
26-
yield get_client(config_dict=yaml.safe_load(k3s.config_yaml()))
27-
except DockerException:
28-
pytest.skip("K3S container not available")
26+
yield yaml.safe_load(k3s.config_yaml())
27+
except DockerException as ex:
28+
pytest.skip(f"K3S container not available. {ex}")
29+
30+
31+
@pytest.fixture(scope="class")
32+
def client(k3scontainer_config):
33+
yield get_client(config_dict=k3scontainer_config)
2934

3035

3136
@pytest.fixture(scope="class")
@@ -109,25 +114,10 @@ def test_resource_context_manager_exit(self, client):
109114
with SecretTestExit(name="test-context-manager-exit", namespace="default", client=client):
110115
pass
111116

112-
def test_proxy_enabled_but_no_proxy_set(self, monkeypatch):
113-
monkeypatch.setenv(name="OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY", value="1")
114-
115-
with pytest.raises(
116-
ValueError,
117-
match="Proxy configuration is enabled but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set.",
118-
):
119-
get_client()
120-
121-
def test_proxy_conflict_raises_value_error(self, monkeypatch):
122-
monkeypatch.setenv(name="OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY", value="1")
123-
monkeypatch.setenv(name="HTTPS_PROXY", value="http://env-proxy.com")
124117

125-
client_configuration = kubernetes.client.Configuration()
126-
client_configuration.proxy = "http://not-env-proxy.com"
118+
def test_client_with_proxy(monkeypatch, k3scontainer_config):
119+
proxy = "http://env-proxy.com"
120+
monkeypatch.setenv(name="HTTPS_PROXY", value=proxy)
127121

128-
with pytest.raises(
129-
ValueError,
130-
match="Conflicting proxy settings: client_configuration.proxy=http://not-env-proxy.com, "
131-
"but the environment variable 'HTTPS_PROXY/HTTP_PROXY' defines proxy as http://env-proxy.com.",
132-
):
133-
get_client(client_configuration=client_configuration)
122+
with pytest.raises(ConfigException, match=r"Service host/port is not set."):
123+
get_client(config_dict=k3scontainer_config)

0 commit comments

Comments
 (0)