Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix listing all namespaces when base URL is specified #424

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a baseurl is set we may get redirected. By default httpx doesn't follow redirects, but we probably always want to.

)

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:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using a URL with a base url component and setting namespace=kr8s.ALL this can end up as an empty string. So modifying the if statement to catch that case.

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
Loading