Skip to content

Commit

Permalink
Add debug logging to troubleshoot auth issues (#542)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
jacobtomlinson and pre-commit-ci[bot] authored Jan 6, 2025
1 parent dc7a159 commit 40fbaea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions kr8s/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ async def call_api(
# If we get a 401 or 403 our credentials may have expired so we
# reauthenticate and try again a few times before giving up.
if e.response.status_code in (401, 403) and auth_attempts < 3:
logger.debug(
f"Unauthorized {e.response.status_code} error, reauthenticating attempt {auth_attempts}"
)
auth_attempts += 1
await self.auth.reauthenticate()
await self._create_session()
Expand Down
6 changes: 6 additions & 0 deletions kr8s/_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import base64
import ipaddress
import json
import logging
import os
import pathlib
import ssl
Expand All @@ -14,6 +15,8 @@
from ._config import KubeConfigSet
from ._types import PathType

logger = logging.getLogger(__name__)


class KubeAuth:
"""Load kubernetes auth from kubeconfig, service account, or url."""
Expand Down Expand Up @@ -69,6 +72,7 @@ async def reauthenticate(self) -> None:
"""Reauthenticate with the server."""
async with self.__auth_lock:
if self._url:
logger.debug("URL specified manually")
self.server = self._url
else:
if self._kubeconfig_path_or_dict is not False:
Expand Down Expand Up @@ -110,6 +114,7 @@ def namespace(self, value: str):

async def _load_kubeconfig(self) -> None:
"""Load kubernetes auth from kubeconfig."""
logger.debug("Loading kubeconfig")
if isinstance(self._kubeconfig_path_or_dict, str) or isinstance(
self._kubeconfig_path_or_dict, pathlib.Path
):
Expand Down Expand Up @@ -254,6 +259,7 @@ async def _load_kubeconfig(self) -> None:

async def _load_service_account(self) -> None:
"""Load credentials from service account."""
logger.debug("Loading service account")
self._serviceaccount = os.path.expanduser(self._serviceaccount)
if not os.path.isdir(self._serviceaccount):
return
Expand Down

0 comments on commit 40fbaea

Please sign in to comment.