From a055619fb2625cd710fcf6edc76b56a54ae97c3b Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 22 Aug 2024 09:43:49 +0300 Subject: [PATCH 01/11] Add support to set client with cluster proxy --- ocp_resources/resource.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index b74c1af409..4aedf1274a 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -13,6 +13,7 @@ from typing import Optional, Any, Dict, List import kubernetes +from kubernetes import config, client from kubernetes.dynamic import DynamicClient, ResourceInstance import yaml from benedict import benedict @@ -107,13 +108,26 @@ def get_client( ) ) 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") - # kubernetes.config.kube_config.load_kube_config sets KUBE_CONFIG_DEFAULT_LOCATION during module import. # 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") + proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") + + if proxy: + LOGGER.info("Trying to get client using proxy %s", proxy) + client_configuration = client.Configuration() + config.load_kube_config( + config_file=config_file, client_configuration=client_configuration, persist_config=True + ) + client_configuration.proxy = proxy + api_client = client.ApiClient(configuration=client_configuration) + + return kubernetes.dynamic.DynamicClient(client=api_client) + + # 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") + return kubernetes.dynamic.DynamicClient( client=kubernetes.config.new_client_from_config(config_file=config_file, context=context or None, **kwargs) ) From aeaa34077ad4320498f53e170937ee093edc53dc Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 22 Aug 2024 10:51:38 +0300 Subject: [PATCH 02/11] Use f-string format --- 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 4aedf1274a..889c9607b6 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -115,7 +115,7 @@ def get_client( proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if proxy: - LOGGER.info("Trying to get client using proxy %s", proxy) + LOGGER.info(f"Trying to get client using proxy {proxy}") client_configuration = client.Configuration() config.load_kube_config( config_file=config_file, client_configuration=client_configuration, persist_config=True From b8934d99a501bf74f4b4fa03bd2b289f4aff9b6b Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 22 Aug 2024 14:45:40 +0300 Subject: [PATCH 03/11] Refactor client initialization --- 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 889c9607b6..9ebabfd89c 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -121,9 +121,10 @@ def get_client( config_file=config_file, client_configuration=client_configuration, persist_config=True ) client_configuration.proxy = proxy - api_client = client.ApiClient(configuration=client_configuration) - return kubernetes.dynamic.DynamicClient(client=api_client) + return kubernetes.dynamic.DynamicClient( + client=kubernetes.config.new_client_from_config(config_file=config_file, client_configuration=client_configuration, context=context or None, **kwargs) + ) # 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") From b279107431822d7129e5b76b9606f4a32bce9ce9 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Thu, 22 Aug 2024 14:48:25 +0300 Subject: [PATCH 04/11] Apply pre-commit changes --- ocp_resources/resource.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 9ebabfd89c..fa63d7dc82 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -123,7 +123,12 @@ def get_client( 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) + client=kubernetes.config.new_client_from_config( + config_file=config_file, + client_configuration=client_configuration, + context=context or None, + **kwargs, + ) ) # Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/__init__.py From 7c616871c7e34fc430dbd370876df1b44ed7005a Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Mon, 26 Aug 2024 09:57:48 +0300 Subject: [PATCH 05/11] Add additional argument for 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 fa63d7dc82..41fda34a42 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -112,7 +112,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") - proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") + proxy = kwargs.pop("proxy", None) or os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if proxy: LOGGER.info(f"Trying to get client using proxy {proxy}") From dc55809741d498151a9e96cb3d160f05f89eb3a1 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Mon, 26 Aug 2024 11:28:44 +0300 Subject: [PATCH 06/11] Add type hint for proxy variable --- 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 41fda34a42..7a88eeba61 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -112,7 +112,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") - proxy = kwargs.pop("proxy", None) or os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") + proxy: Optional[str] = kwargs.pop("proxy", None) or os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if proxy: LOGGER.info(f"Trying to get client using proxy {proxy}") From 114b02a929f341cfb0452f9e5c775262039a4f0d Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Mon, 26 Aug 2024 15:54:06 +0300 Subject: [PATCH 07/11] Refactor proxy to be a function argument instead of kwargs --- 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 7a88eeba61..19747b2c47 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -79,7 +79,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, context: str = "", **kwargs: Any + config_file: str = "", config_dict: Dict[str, Any] | None = None, context: str = "", proxy: str = "", **kwargs: Any ) -> DynamicClient: """ Get a kubernetes client. @@ -96,6 +96,7 @@ def get_client( config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. context (str): name of the context to use. + proxy (str): the URL to the proxy to be used for all requests made by this client. Returns: DynamicClient: a kubernetes client. @@ -112,7 +113,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") - proxy: Optional[str] = kwargs.pop("proxy", None) or os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") + proxy = proxy or os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if proxy: LOGGER.info(f"Trying to get client using proxy {proxy}") From dedada65182c018ef2e7f8344bec52e50a70d6bf Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Mon, 26 Aug 2024 16:00:03 +0300 Subject: [PATCH 08/11] Remove redundant function call --- ocp_resources/resource.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 19747b2c47..760fbf8f32 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -118,9 +118,6 @@ def get_client( if proxy: LOGGER.info(f"Trying to get client using proxy {proxy}") client_configuration = client.Configuration() - config.load_kube_config( - config_file=config_file, client_configuration=client_configuration, persist_config=True - ) client_configuration.proxy = proxy return kubernetes.dynamic.DynamicClient( From 0ef82fb6921d399f8c86182f3e49b8142acb37a9 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Mon, 26 Aug 2024 16:08:50 +0300 Subject: [PATCH 09/11] Refactor to use one return statement --- ocp_resources/resource.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index 760fbf8f32..f1ef0c0d65 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -114,26 +114,17 @@ 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") proxy = proxy or os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") + client_configuration = client.Configuration() if proxy: LOGGER.info(f"Trying to get client using proxy {proxy}") - client_configuration = 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, - ) - ) - # 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") 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 3109d0004e35a67e8bbb59c7bc04d2ea2175844f Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Mon, 26 Aug 2024 16:09:53 +0300 Subject: [PATCH 10/11] Apply pre-commit changes Signed-off-by: Shahaf Bahar --- ocp_resources/resource.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index f1ef0c0d65..aff3626de6 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -13,7 +13,7 @@ from typing import Optional, Any, Dict, List import kubernetes -from kubernetes import config, client +from kubernetes import client from kubernetes.dynamic import DynamicClient, ResourceInstance import yaml from benedict import benedict @@ -79,7 +79,11 @@ 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 = "", proxy: str = "", **kwargs: Any + config_file: str = "", + config_dict: Dict[str, Any] | None = None, + context: str = "", + proxy: Optional[str] = "", + **kwargs: Any, ) -> DynamicClient: """ Get a kubernetes client. @@ -124,7 +128,9 @@ def get_client( LOGGER.info("Trying to get client via new_client_from_config") return kubernetes.dynamic.DynamicClient( - client=kubernetes.config.new_client_from_config(config_file=config_file, client_configuration=client_configuration, 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 500ee4fdfb2ef64ca424c4b18bab824072add856 Mon Sep 17 00:00:00 2001 From: Shahaf Bahar Date: Tue, 27 Aug 2024 11:27:56 +0300 Subject: [PATCH 11/11] Refactor function argument to use configuration object Signed-off-by: Shahaf Bahar --- ocp_resources/resource.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ocp_resources/resource.py b/ocp_resources/resource.py index aff3626de6..5f8b451358 100644 --- a/ocp_resources/resource.py +++ b/ocp_resources/resource.py @@ -81,8 +81,8 @@ 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: client.Configuration | None = None, context: str = "", - proxy: Optional[str] = "", **kwargs: Any, ) -> DynamicClient: """ @@ -99,8 +99,8 @@ def get_client( Args: config_file (str): path to a kubeconfig file. config_dict (dict): dict with kubeconfig configuration. + client_configuration: The kubernetes.client.Configuration to set configs to. context (str): name of the context to use. - proxy (str): the URL to the proxy to be used for all requests made by this client. Returns: DynamicClient: a kubernetes client. @@ -117,8 +117,8 @@ 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") - proxy = proxy or os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") - client_configuration = client.Configuration() + client_configuration = client_configuration or client.Configuration() + proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") if proxy: LOGGER.info(f"Trying to get client using proxy {proxy}")