From 2e1718eee6e5c96cf7e48ecccb25db781764ad5f Mon Sep 17 00:00:00 2001 From: Jacob Tomlinson Date: Mon, 1 Jul 2024 13:56:01 +0100 Subject: [PATCH 1/4] Fix when base URL is specified --- kr8s/_api.py | 3 ++- kr8s/_auth.py | 10 +++++++++- kr8s/tests/test_api.py | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/kr8s/_api.py b/kr8s/_api.py index 5575c58..1a41e1c 100644 --- a/kr8s/_api.py +++ b/kr8s/_api.py @@ -111,6 +111,7 @@ async def _create_session(self) -> None: headers=headers, verify=await self.auth.ssl_context(), timeout=self._timeout, + follow_redirects=True, ) def _construct_url( @@ -130,7 +131,7 @@ def _construct_url( parts = [base] if version: parts.append(version) - if namespace is not None: + if namespace: parts.extend(["namespaces", namespace]) parts.append(url) return "/".join(parts) diff --git a/kr8s/_auth.py b/kr8s/_auth.py index 9920b5b..a17ad1a 100644 --- a/kr8s/_auth.py +++ b/kr8s/_auth.py @@ -31,7 +31,7 @@ def __init__( self.client_key_file: Optional[PathType] = None self.server_ca_file: Optional[PathType] = None self.token: Optional[str] = None - self.namespace: Optional[str] = namespace + self._namespace: Optional[str] = namespace self.active_context: str = "" self.kubeconfig: KubeConfigSet self.tls_server_name: Optional[str] = None @@ -100,6 +100,14 @@ async def ssl_context(self): sslcontext.load_verify_locations(cafile=self.server_ca_file) return sslcontext + @property + def namespace(self): + return self._namespace if self._namespace else "default" + + @namespace.setter + def namespace(self, value: str): + self._namespace = value + async def _load_kubeconfig(self) -> None: """Load kubernetes auth from kubeconfig.""" if isinstance(self._kubeconfig_path_or_dict, str) or isinstance( diff --git a/kr8s/tests/test_api.py b/kr8s/tests/test_api.py index f98cc87..ea430e3 100644 --- a/kr8s/tests/test_api.py +++ b/kr8s/tests/test_api.py @@ -151,7 +151,7 @@ async def test_bad_api_version(): pass # pragma: no cover -@pytest.mark.parametrize("namespace", [kr8s.ALL, "kube-system"]) +@pytest.mark.parametrize("namespace", [kr8s.ALL, "kube-system", None]) async def test_get_pods(namespace): pods = await kr8s.asyncio.get("pods", namespace=namespace) assert isinstance(pods, list) From 872bb9e069eb3c064015cb6cf081800047cc7728 Mon Sep 17 00:00:00 2001 From: Jacob Tomlinson Date: Mon, 1 Jul 2024 13:57:34 +0100 Subject: [PATCH 2/4] Add type annotation --- kr8s/_auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kr8s/_auth.py b/kr8s/_auth.py index a17ad1a..db8d4c1 100644 --- a/kr8s/_auth.py +++ b/kr8s/_auth.py @@ -101,7 +101,7 @@ async def ssl_context(self): return sslcontext @property - def namespace(self): + def namespace(self) -> str: return self._namespace if self._namespace else "default" @namespace.setter From f46cb9402655eb0ba5921c954a099e03f6cfa238 Mon Sep 17 00:00:00 2001 From: Jacob Tomlinson Date: Mon, 1 Jul 2024 13:57:34 +0100 Subject: [PATCH 3/4] Add type annotation --- kr8s/_auth.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kr8s/_auth.py b/kr8s/_auth.py index a17ad1a..6747f97 100644 --- a/kr8s/_auth.py +++ b/kr8s/_auth.py @@ -101,7 +101,7 @@ async def ssl_context(self): return sslcontext @property - def namespace(self): + def namespace(self) -> str: return self._namespace if self._namespace else "default" @namespace.setter @@ -139,8 +139,8 @@ async def _load_kubeconfig(self) -> None: self.active_context = self.kubeconfig.contexts[0]["name"] # Load configuration options from the context - if self.namespace is None: - self.namespace = self.kubeconfig.current_namespace + if self._namespace is None: + self._namespace = self.kubeconfig.current_namespace # If no cluster is found in the context, assume it's a service account if not self._context["cluster"]: From bb6f5ce3b29948db0e59110ec69c7f11047fbdd9 Mon Sep 17 00:00:00 2001 From: Jacob Tomlinson Date: Mon, 1 Jul 2024 14:24:27 +0100 Subject: [PATCH 4/4] Remove None from test --- kr8s/tests/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kr8s/tests/test_api.py b/kr8s/tests/test_api.py index ea430e3..f98cc87 100644 --- a/kr8s/tests/test_api.py +++ b/kr8s/tests/test_api.py @@ -151,7 +151,7 @@ async def test_bad_api_version(): pass # pragma: no cover -@pytest.mark.parametrize("namespace", [kr8s.ALL, "kube-system", None]) +@pytest.mark.parametrize("namespace", [kr8s.ALL, "kube-system"]) async def test_get_pods(namespace): pods = await kr8s.asyncio.get("pods", namespace=namespace) assert isinstance(pods, list)