Skip to content

Commit

Permalink
Fix listing all namespaces when base URL is specified (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson authored Jul 1, 2024
1 parent 99556dd commit 5f11b89
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion kr8s/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions kr8s/_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,6 +100,14 @@ async def ssl_context(self):
sslcontext.load_verify_locations(cafile=self.server_ca_file)
return sslcontext

@property
def namespace(self) -> str:
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(
Expand Down Expand Up @@ -131,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"]:
Expand Down

0 comments on commit 5f11b89

Please sign in to comment.