From 0ed97d6b534e9fd06ab004ba0914cb60111d44d9 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 09:39:15 +0200 Subject: [PATCH 01/33] 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 --- README.md | 10 ++++++++++ ocp_resources/resource.py | 27 +++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2b4f34aa5f..3474346aa8 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,16 @@ export OPENSHIFT_PYTHON_WRAPPER_LOG_LEVEL= # can be: "DEBUG", "INFO", export OPENSHIFT_PYTHON_WRAPPER_HASH_LOG_DATA="false" ``` +## Dynamic Proxy Enablement + +To dynamically enable proxy configuration: + +Set the environment variable OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=1. +This will automatically enable the proxy without needing to set the use_proxy argument when creating the client. + +Make sure to define the HTTPS_PROXY or HTTP_PROXY environment variable for the proxy URL. +If neither is set, a ValueError will be raised. + ## Code check We use pre-commit for code check. diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 848fe599d0..c7e76fddc9 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -79,7 +79,12 @@ 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: kubernetes.client.Configuration | None = None, + use_proxy: bool = False, + context: str = "", + **kwargs: Any, ) -> DynamicClient: """ Get a kubernetes client. @@ -95,6 +100,10 @@ def get_client( Args: config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. + client_configuration (kubernetes.client.Configuration): The kubernetes.client.Configuration to set configs to. + use_proxy (bool): If True, automatically retrieves and sets the proxy from the environment + variables (`HTTPS_PROXY` or `HTTP_PROXY`). If proxy is not found, raises an error. + Default is False, meaning no proxy is used. context (str): name of the context to use. Returns: @@ -115,8 +124,22 @@ def get_client( # 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 kubernetes.client.Configuration() + + if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY") == "1": + use_proxy = True + + if use_proxy: + proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") + if not proxy: + raise ValueError("Proxy not found. Please set the HTTPS_PROXY or HTTP_PROXY environment variable.") + LOGGER.info(f"Setting proxy in client configuration: {proxy}") + client_configuration.proxy = proxy + 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 From e58e6c852daf921e659c005a9e0a6500ca137bac Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 12:34:05 +0200 Subject: [PATCH 02/33] Simplify docstring by removing redundant default value explanation --- ocp_resources/resource.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index c7e76fddc9..31e8e9d18d 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -103,7 +103,6 @@ def get_client( client_configuration (kubernetes.client.Configuration): The kubernetes.client.Configuration to set configs to. use_proxy (bool): If True, automatically retrieves and sets the proxy from the environment variables (`HTTPS_PROXY` or `HTTP_PROXY`). If proxy is not found, raises an error. - Default is False, meaning no proxy is used. context (str): name of the context to use. Returns: From 705372cf8d6812c80f891c3bf7e917fdb0f22279 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 12:37:15 +0200 Subject: [PATCH 03/33] Update proxy configuration logic and README documentation --- README.md | 2 +- ocp_resources/resource.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3474346aa8..bf706a6652 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ export OPENSHIFT_PYTHON_WRAPPER_LOG_LEVEL= # can be: "DEBUG", "INFO", To dynamically enable proxy configuration: -Set the environment variable OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=1. +Set the environment variable OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=. This will automatically enable the proxy without needing to set the use_proxy argument when creating the client. Make sure to define the HTTPS_PROXY or HTTP_PROXY environment variable for the proxy URL. diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 31e8e9d18d..b189650ce9 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -125,7 +125,7 @@ def get_client( config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config") client_configuration = client_configuration or kubernetes.client.Configuration() - if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY") == "1": + if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): use_proxy = True if use_proxy: From 0f89b004b49ebdab1345acdce74ebf93a2e420e8 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 12:41:12 +0200 Subject: [PATCH 04/33] Simplify use_proxy docstring in get_client method --- ocp_resources/resource.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index b189650ce9..4be3d2efc2 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -101,8 +101,7 @@ def get_client( config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. client_configuration (kubernetes.client.Configuration): The kubernetes.client.Configuration to set configs to. - use_proxy (bool): If True, automatically retrieves and sets the proxy from the environment - variables (`HTTPS_PROXY` or `HTTP_PROXY`). If proxy is not found, raises an error. + use_proxy (bool): If True, retrieves HTTPS_PROXY or HTTP_PROXY from OS environment and use it. context (str): name of the context to use. Returns: From 0fb97021a8872f7152861b775475eb1f6ec5aee3 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 13:07:50 +0200 Subject: [PATCH 05/33] Add support for enabling proxy with use_proxy flag --- ocp_resources/resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 4be3d2efc2..7056634cf6 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -124,7 +124,7 @@ def get_client( config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config") client_configuration = client_configuration or kubernetes.client.Configuration() - if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): + if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY") or use_proxy: use_proxy = True if use_proxy: From a9b4c6f68312091546ffc719c40df592a429d1c0 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 13:23:49 +0200 Subject: [PATCH 06/33] Remove client_configuration from get_client and initialize only for proxy usage Signed-off-by: Shahaf Bahar --- ocp_resources/resource.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 7056634cf6..572b9332cf 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -81,7 +81,6 @@ 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, - client_configuration: kubernetes.client.Configuration | None = None, use_proxy: bool = False, context: str = "", **kwargs: Any, @@ -100,7 +99,6 @@ def get_client( Args: config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. - client_configuration (kubernetes.client.Configuration): The kubernetes.client.Configuration to set configs to. use_proxy (bool): If True, retrieves HTTPS_PROXY or HTTP_PROXY from OS environment and use it. context (str): name of the context to use. @@ -122,7 +120,7 @@ def get_client( # 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 kubernetes.client.Configuration() + client_configuration = kubernetes.client.Configuration() if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY") or use_proxy: use_proxy = True From 479f42fe075f0854992438b9b828c44e26486675 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 14:17:53 +0200 Subject: [PATCH 07/33] Refactor proxy handling in get_client function --- ocp_resources/resource.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 572b9332cf..eb0b555aac 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -120,7 +120,7 @@ def get_client( # 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 = kubernetes.client.Configuration() + client_configuration = None if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY") or use_proxy: use_proxy = True @@ -130,11 +130,15 @@ def get_client( if not proxy: raise ValueError("Proxy not found. Please set the HTTPS_PROXY or HTTP_PROXY environment variable.") LOGGER.info(f"Setting proxy in client configuration: {proxy}") + client_configuration = kubernetes.client.Configuration() client_configuration.proxy = proxy return kubernetes.dynamic.DynamicClient( client=kubernetes.config.new_client_from_config( - config_file=config_file, client_configuration=client_configuration, context=context or None, **kwargs + config_file=config_file, + client_configuration=client_configuration if client_configuration else None, + context=context or None, + **kwargs, ) ) except MaxRetryError: From 251eee6c963cae28f4acbbd6c9867a181d5e4165 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 14:24:14 +0200 Subject: [PATCH 08/33] Refactor docstring to clarify proxy behavior --- ocp_resources/resource.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index eb0b555aac..584c0d81fe 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -100,6 +100,7 @@ def get_client( config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. use_proxy (bool): If True, retrieves HTTPS_PROXY or HTTP_PROXY from OS environment and use it. + If the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY` is set, it will override the value of `use_proxy`. context (str): name of the context to use. Returns: From 6d43194c0f9adce6bee2c43d1b9a46cf14b677c9 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 14:26:13 +0200 Subject: [PATCH 09/33] Simplify proxy configuration logic in get_client --- ocp_resources/resource.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 584c0d81fe..7fbcad5db3 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -123,10 +123,7 @@ def get_client( config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config") client_configuration = None - if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY") or use_proxy: - use_proxy = True - - if use_proxy: + if use_proxy or os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if not proxy: raise ValueError("Proxy not found. Please set the HTTPS_PROXY or HTTP_PROXY environment variable.") From 6bf30f65c13bb804fbc56f565df9b0776c6d36ae Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 14:26:52 +0200 Subject: [PATCH 10/33] Improve error message for missing proxy configuration in get_client --- ocp_resources/resource.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 7fbcad5db3..39a4e7015e 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -126,7 +126,9 @@ def get_client( if use_proxy or os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if not proxy: - raise ValueError("Proxy not found. Please set the HTTPS_PROXY or HTTP_PROXY environment variable.") + raise ValueError( + "Proxy configuration requested but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set" + ) LOGGER.info(f"Setting proxy in client configuration: {proxy}") client_configuration = kubernetes.client.Configuration() client_configuration.proxy = proxy From 948fac03325a0d11c95d591b86fa71d6a9949449 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 14:53:24 +0200 Subject: [PATCH 11/33] Remove redundant dot in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bf706a6652..7cf314a219 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ export OPENSHIFT_PYTHON_WRAPPER_LOG_LEVEL= # can be: "DEBUG", "INFO", To dynamically enable proxy configuration: -Set the environment variable OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=. +Set the environment variable OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY= This will automatically enable the proxy without needing to set the use_proxy argument when creating the client. Make sure to define the HTTPS_PROXY or HTTP_PROXY environment variable for the proxy URL. From 90d5bbb766d3cc920cfc19d09d497426128a19ad Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Sun, 26 Jan 2025 14:57:03 +0200 Subject: [PATCH 12/33] Remove unnecessary check for client_configuration in get_client --- ocp_resources/resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 39a4e7015e..a08323a0b6 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -136,7 +136,7 @@ def get_client( return kubernetes.dynamic.DynamicClient( client=kubernetes.config.new_client_from_config( config_file=config_file, - client_configuration=client_configuration if client_configuration else None, + client_configuration=client_configuration, context=context or None, **kwargs, ) From 595cea710827aeb43f983fd17ffdf708e34edf0b Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Mon, 27 Jan 2025 08:57:44 +0200 Subject: [PATCH 13/33] Update README to clarify proxy enablement instructions --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7cf314a219..bdce69baa1 100644 --- a/README.md +++ b/README.md @@ -93,14 +93,14 @@ export OPENSHIFT_PYTHON_WRAPPER_LOG_LEVEL= # can be: "DEBUG", "INFO", export OPENSHIFT_PYTHON_WRAPPER_HASH_LOG_DATA="false" ``` -## Dynamic Proxy Enablement +## Proxy Enablement -To dynamically enable proxy configuration: +To enable proxy configuration for the client: Set the environment variable OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY= -This will automatically enable the proxy without needing to set the use_proxy argument when creating the client. +This enables the proxy without requiring the use_proxy argument when creating the client. -Make sure to define the HTTPS_PROXY or HTTP_PROXY environment variable for the proxy URL. +Define the HTTPS_PROXY or HTTP_PROXY environment variable for the proxy URL. If neither is set, a ValueError will be raised. ## Code check From 6bea2c9ae17a7fe3a3d72c9f1eeab451d9510a6c Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Mon, 27 Jan 2025 09:18:11 +0200 Subject: [PATCH 14/33] Clarify proxy enablement in README --- README.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bdce69baa1..035e5a15ea 100644 --- a/README.md +++ b/README.md @@ -97,11 +97,16 @@ export OPENSHIFT_PYTHON_WRAPPER_LOG_LEVEL= # can be: "DEBUG", "INFO", To enable proxy configuration for the client: -Set the environment variable OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY= -This enables the proxy without requiring the use_proxy argument when creating the client. +1. Set the `use_proxy` argument when creating the client or set the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=` +This enables the proxy without requiring the `use_proxy` argument when creating the client. -Define the HTTPS_PROXY or HTTP_PROXY environment variable for the proxy URL. -If neither is set, a ValueError will be raised. +2. Define either `HTTPS_PROXY` or `HTTP_PROXY` environment variable with your proxy URL: +```bash +export HTTPS_PROXY="http://proxy.example.com:8080" +# or +export HTTP_PROXY="http://proxy.example.com:8080" +``` +If neither variable is set when proxy is enabled, a `ValueError` will be raised. ## Code check From 84b9023cc47ce527bc9b29a785983924e22320eb Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Mon, 27 Jan 2025 09:21:10 +0200 Subject: [PATCH 15/33] Clarify use_proxy argument type in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 035e5a15ea..30ab9e22dd 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ export OPENSHIFT_PYTHON_WRAPPER_LOG_LEVEL= # can be: "DEBUG", "INFO", To enable proxy configuration for the client: -1. Set the `use_proxy` argument when creating the client or set the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=` +1. Set the `use_proxy` argument to `True` when creating the client or set the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=` This enables the proxy without requiring the `use_proxy` argument when creating the client. 2. Define either `HTTPS_PROXY` or `HTTP_PROXY` environment variable with your proxy URL: From 1898f5c947644831633c60d90fdc5ff1e1a4bbe7 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Mon, 27 Jan 2025 10:27:44 +0200 Subject: [PATCH 16/33] Retrieve client_configuration from kwargs if provided --- ocp_resources/resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index a08323a0b6..bb1969eada 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -121,7 +121,7 @@ def get_client( # 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 = None + client_configuration = kwargs.get('client_configuration', None) if use_proxy or os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") From 59367f477ddfb22122ad00301d7ca35e993c0e44 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 27 Jan 2025 08:33:42 +0000 Subject: [PATCH 17/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ocp_resources/resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index bb1969eada..db977a47da 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -121,7 +121,7 @@ def get_client( # 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 = kwargs.get('client_configuration', None) + client_configuration = kwargs.get("client_configuration", None) if use_proxy or os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") From 776c4477ce686043fee97de2f4f4eb38516f0c0c Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Tue, 28 Jan 2025 08:10:41 +0200 Subject: [PATCH 18/33] Ensure environment variable overrides use_proxy --- ocp_resources/resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index db977a47da..812bf36423 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -123,7 +123,7 @@ def get_client( config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config") client_configuration = kwargs.get("client_configuration", None) - if use_proxy or os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): + if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY") or use_proxy: proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if not proxy: raise ValueError( From 4a92fbb9bf87dfca9fafff62541f898640263318 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Tue, 28 Jan 2025 08:11:01 +0200 Subject: [PATCH 19/33] Remove redundant proxy configuration log --- ocp_resources/resource.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 812bf36423..4bf0c5a74d 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -129,7 +129,6 @@ def get_client( raise ValueError( "Proxy configuration requested but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set" ) - LOGGER.info(f"Setting proxy in client configuration: {proxy}") client_configuration = kubernetes.client.Configuration() client_configuration.proxy = proxy From f6673b2a5a64b059a7c2e314d31510ff4b3c968a Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Tue, 28 Jan 2025 08:27:00 +0200 Subject: [PATCH 20/33] Preserve client_configuration from kwargs when setting proxy --- ocp_resources/resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 4bf0c5a74d..7692e83957 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -129,7 +129,7 @@ def get_client( raise ValueError( "Proxy configuration requested but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set" ) - client_configuration = kubernetes.client.Configuration() + client_configuration = client_configuration or kubernetes.client.Configuration() client_configuration.proxy = proxy return kubernetes.dynamic.DynamicClient( From f375dad27743c1722e467d1a64ca5484bd798f1c Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Tue, 28 Jan 2025 09:46:07 +0200 Subject: [PATCH 21/33] Update docstring to clarify proxy conf behavior with env var --- ocp_resources/resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 7692e83957..af0bcba91c 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -100,7 +100,7 @@ def get_client( config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. use_proxy (bool): If True, retrieves HTTPS_PROXY or HTTP_PROXY from OS environment and use it. - If the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY` is set, it will override the value of `use_proxy`. + If the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY` is set and not empty, it will override the value of `use_proxy`. context (str): name of the context to use. Returns: From 040a1c9d67d3c9cd2a4a4a4cbfe9bc31367b85b8 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Tue, 28 Jan 2025 11:49:58 +0200 Subject: [PATCH 22/33] Refactor proxy configuration logic for clarity --- ocp_resources/resource.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index af0bcba91c..67303e6ea9 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -100,7 +100,8 @@ def get_client( config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. use_proxy (bool): If True, retrieves HTTPS_PROXY or HTTP_PROXY from OS environment and use it. - If the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY` is set and not empty, it will override the value of `use_proxy`. + If `use_proxy` is not set, the `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY` environment variable will be used + if it is set and not empty. context (str): name of the context to use. Returns: @@ -123,7 +124,7 @@ def get_client( config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config") client_configuration = kwargs.get("client_configuration", None) - if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY") or use_proxy: + if use_proxy or os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if not proxy: raise ValueError( From 3f8643326046a4e76caf8562829240880b11944c Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Tue, 28 Jan 2025 11:54:12 +0200 Subject: [PATCH 23/33] Update README with proxy enablement details --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 30ab9e22dd..96afb7807f 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,9 @@ export OPENSHIFT_PYTHON_WRAPPER_LOG_LEVEL= # can be: "DEBUG", "INFO", ## Proxy Enablement +This configuration allows the client to route traffic through a specified proxy server. +It can be enabled via the `use_proxy` argument or environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY`. + To enable proxy configuration for the client: 1. Set the `use_proxy` argument to `True` when creating the client or set the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=` From 10ee9f2d88e1b18538289744f7005b1d46fdb662 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Tue, 28 Jan 2025 14:10:07 +0200 Subject: [PATCH 24/33] Refactor client_configuration retrieval in get_client --- ocp_resources/resource.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 67303e6ea9..d8a29fa4e2 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -114,6 +114,7 @@ def get_client( config_dict=config_dict, context=context or None, **kwargs ) ) + client_configuration = kwargs.get("client_configuration") 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") @@ -122,7 +123,6 @@ def get_client( # 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 = kwargs.get("client_configuration", None) if use_proxy or os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") @@ -130,8 +130,9 @@ def get_client( raise ValueError( "Proxy configuration requested but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set" ) - client_configuration = client_configuration or kubernetes.client.Configuration() - client_configuration.proxy = proxy + if not client_configuration: + client_configuration = kubernetes.client.Configuration() + client_configuration.proxy = proxy return kubernetes.dynamic.DynamicClient( client=kubernetes.config.new_client_from_config( @@ -146,7 +147,7 @@ def get_client( LOGGER.info("Trying to get client via incluster_config") return kubernetes.dynamic.DynamicClient( client=kubernetes.config.incluster_config.load_incluster_config( - client_configuration=kwargs.get("client_configuration"), + client_configuration=client_configuration, try_refresh_token=kwargs.get("try_refresh_token", True), ) ) From cbf9f0d6e88e36da5b1814b686fd510d2eeda4af Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Tue, 28 Jan 2025 14:55:43 +0200 Subject: [PATCH 25/33] Refactor proxy configuration in get_client for improved clarity --- ocp_resources/resource.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index d8a29fa4e2..718ced84d8 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -100,8 +100,8 @@ def get_client( config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. use_proxy (bool): If True, retrieves HTTPS_PROXY or HTTP_PROXY from OS environment and use it. - If `use_proxy` is not set, the `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY` environment variable will be used - if it is set and not empty. + The `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY` environment variable, if set and not empty, will be used + in addition to the `use_proxy` parameter. context (str): name of the context to use. Returns: @@ -114,7 +114,7 @@ def get_client( config_dict=config_dict, context=context or None, **kwargs ) ) - client_configuration = kwargs.get("client_configuration") + client_configuration = kwargs.get("client_configuration", kubernetes.client.Configuration()) 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") @@ -128,10 +128,9 @@ def get_client( proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if not proxy: raise ValueError( - "Proxy configuration requested but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set" + "Proxy configuration is enabled but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set." ) - if not client_configuration: - client_configuration = kubernetes.client.Configuration() + if not kwargs.get("client_configuration"): client_configuration.proxy = proxy return kubernetes.dynamic.DynamicClient( From cae73aa0c52e43b3a2dff42b8019bb6dc094e99d Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Wed, 29 Jan 2025 11:56:35 +0200 Subject: [PATCH 26/33] Refactor use_proxy argument in get_client to support None as default value --- ocp_resources/resource.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 718ced84d8..482e88f645 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -81,7 +81,7 @@ 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, - use_proxy: bool = False, + use_proxy: bool | None = None, context: str = "", **kwargs: Any, ) -> DynamicClient: @@ -99,9 +99,11 @@ def get_client( Args: config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. - use_proxy (bool): If True, retrieves HTTPS_PROXY or HTTP_PROXY from OS environment and use it. - The `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY` environment variable, if set and not empty, will be used - in addition to the `use_proxy` parameter. + use_proxy (bool, default=None): + - `True`: retrieves HTTPS_PROXY or HTTP_PROXY from OS environment and use it. + - `False`: Disables proxy. + - `None`: (default): Proxy is determined by the `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY` environment + variable. non-empty value enables proxy. context (str): name of the context to use. Returns: @@ -124,7 +126,7 @@ def get_client( # is populated during import which comes before setting the variable in code. config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config") - if use_proxy or os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): + if use_proxy or (use_proxy is None and os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY")): proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if not proxy: raise ValueError( From b010e7b2c5be96a31dfb0b903e620ce03d91352a Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 30 Jan 2025 08:11:02 +0200 Subject: [PATCH 27/33] Remove use_proxy argument from get_client and update README --- README.md | 5 ++--- ocp_resources/resource.py | 8 +------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 96afb7807f..f77d95cc6f 100644 --- a/README.md +++ b/README.md @@ -96,12 +96,11 @@ export OPENSHIFT_PYTHON_WRAPPER_LOG_LEVEL= # can be: "DEBUG", "INFO", ## Proxy Enablement This configuration allows the client to route traffic through a specified proxy server. -It can be enabled via the `use_proxy` argument or environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY`. +It can be enabled via the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY`. To enable proxy configuration for the client: -1. Set the `use_proxy` argument to `True` when creating the client or set the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=` -This enables the proxy without requiring the `use_proxy` argument when creating the client. +1. Set the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=` 2. Define either `HTTPS_PROXY` or `HTTP_PROXY` environment variable with your proxy URL: ```bash diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 1c9359f7fe..babb007120 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -78,7 +78,6 @@ 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, - use_proxy: bool | None = None, context: str = "", **kwargs: Any, ) -> DynamicClient: @@ -96,11 +95,6 @@ def get_client( Args: config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. - use_proxy (bool, default=None): - - `True`: retrieves HTTPS_PROXY or HTTP_PROXY from OS environment and use it. - - `False`: Disables proxy. - - `None`: (default): Proxy is determined by the `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY` environment - variable. non-empty value enables proxy. context (str): name of the context to use. Returns: @@ -123,7 +117,7 @@ def get_client( # is populated during import which comes before setting the variable in code. config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config") - if use_proxy or (use_proxy is None and os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY")): + if os.environ.get("OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"): proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if not proxy: raise ValueError( From 148d83458c810b35cde1785e654b6092ed7a7d85 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 30 Jan 2025 09:09:32 +0200 Subject: [PATCH 28/33] Ensure proxy settings are always applied when enabled --- ocp_resources/resource.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index babb007120..1977ea690d 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -123,8 +123,7 @@ def get_client( raise ValueError( "Proxy configuration is enabled but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set." ) - if not kwargs.get("client_configuration"): - client_configuration.proxy = proxy + client_configuration.proxy = proxy return kubernetes.dynamic.DynamicClient( client=kubernetes.config.new_client_from_config( From aa638916d2795006aad098dd4b843532204a596d Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 30 Jan 2025 09:40:03 +0200 Subject: [PATCH 29/33] Check for proxy conflicts in client_configuration and environment variable --- ocp_resources/resource.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 1977ea690d..c744161b15 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -123,6 +123,11 @@ def get_client( raise ValueError( "Proxy configuration is enabled but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set." ) + if client_configuration.proxy and client_configuration.proxy != proxy: + raise ValueError( + f"Conflicting proxy settings: client_configuration.proxy={client_configuration.proxy}, " + f"but the environment variable 'OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY' defines proxy as {proxy}." + ) client_configuration.proxy = proxy return kubernetes.dynamic.DynamicClient( From de9c6a86697c2e1aa8a4505476b8cc1bfc87c386 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 30 Jan 2025 10:19:55 +0200 Subject: [PATCH 30/33] Add test to verify proxy conflict raises ValueError for mismatched proxy settings --- tests/test_resources.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_resources.py b/tests/test_resources.py index 7f7487cdb5..22748ee8ab 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -1,5 +1,7 @@ import pytest import yaml +import kubernetes +import os from testcontainers.k3s import K3SContainer from ocp_resources.exceptions import ResourceTeardownError @@ -103,3 +105,15 @@ def test_resource_context_manager_exit(self, client): with pytest.raises(ResourceTeardownError): with TestSecretExit(name="test-context-manager-exit", namespace="default", client=client): pass + + def test_proxy_conflict_raises_value_error(self): + os.environ["OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"] = "1" + os.environ["HTTPS_PROXY"] = "http://env-proxy.com" + client_configuration = kubernetes.client.Configuration() + client_configuration.proxy = "http://not-env-proxy.com" + with pytest.raises( + ValueError, + match="Conflicting proxy settings: client_configuration.proxy=http://not-env-proxy.com, " + "but the environment variable 'OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY' defines proxy as http://env-proxy.com.", + ): + get_client(client_configuration=client_configuration) From 85965caa0fb6b20a0413ecbffb6d7b8444530113 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 30 Jan 2025 11:12:44 +0200 Subject: [PATCH 31/33] Refactor test for proxy conflict in get_client method Signed-off-by: Shahaf Bahar --- tests/test_resources.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/test_resources.py b/tests/test_resources.py index 22748ee8ab..0d498f1102 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -1,7 +1,6 @@ import pytest import yaml import kubernetes -import os from testcontainers.k3s import K3SContainer from ocp_resources.exceptions import ResourceTeardownError @@ -106,14 +105,16 @@ def test_resource_context_manager_exit(self, client): with TestSecretExit(name="test-context-manager-exit", namespace="default", client=client): pass - def test_proxy_conflict_raises_value_error(self): - os.environ["OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY"] = "1" - os.environ["HTTPS_PROXY"] = "http://env-proxy.com" + def test_proxy_conflict_raises_value_error(self, monkeypatch): + monkeypatch.setenv(name="OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY", value="1") + monkeypatch.setenv(name="HTTPS_PROXY", value="http://env-proxy.com") + client_configuration = kubernetes.client.Configuration() client_configuration.proxy = "http://not-env-proxy.com" - with pytest.raises( - ValueError, - match="Conflicting proxy settings: client_configuration.proxy=http://not-env-proxy.com, " - "but the environment variable 'OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY' defines proxy as http://env-proxy.com.", - ): + + with pytest.raises(ValueError) as exc_info: get_client(client_configuration=client_configuration) + + assert "Conflicting proxy settings" in str(exc_info.value) + assert "http://not-env-proxy.com" in str(exc_info.value) + assert "http://env-proxy.com" in str(exc_info.value) From b617c53b4276abd4affe3cd1364d0568d5e2d5b9 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 30 Jan 2025 13:15:58 +0200 Subject: [PATCH 32/33] Refactor test for proxy conflict to use pytest.raises match exp --- tests/test_resources.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_resources.py b/tests/test_resources.py index 0d498f1102..92afde2047 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -112,9 +112,9 @@ def test_proxy_conflict_raises_value_error(self, monkeypatch): client_configuration = kubernetes.client.Configuration() client_configuration.proxy = "http://not-env-proxy.com" - with pytest.raises(ValueError) as exc_info: + with pytest.raises( + ValueError, + match="Conflicting proxy settings: client_configuration.proxy=http://not-env-proxy.com, " + "but the environment variable 'OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY' defines proxy as http://env-proxy.com.", + ): get_client(client_configuration=client_configuration) - - assert "Conflicting proxy settings" in str(exc_info.value) - assert "http://not-env-proxy.com" in str(exc_info.value) - assert "http://env-proxy.com" in str(exc_info.value) From 798ef9e83377ad08bb6d4c4899cefe62d3e506ce Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 30 Jan 2025 13:19:46 +0200 Subject: [PATCH 33/33] Add test for proxy enabled without proxy env variables set --- tests/test_resources.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_resources.py b/tests/test_resources.py index 92afde2047..403f76e81a 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -105,6 +105,15 @@ def test_resource_context_manager_exit(self, client): with TestSecretExit(name="test-context-manager-exit", namespace="default", client=client): pass + def test_proxy_enabled_but_no_proxy_set(self, monkeypatch): + monkeypatch.setenv(name="OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY", value="1") + + with pytest.raises( + ValueError, + match="Proxy configuration is enabled but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set.", + ): + get_client() + def test_proxy_conflict_raises_value_error(self, monkeypatch): monkeypatch.setenv(name="OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY", value="1") monkeypatch.setenv(name="HTTPS_PROXY", value="http://env-proxy.com")