kr8s icon indicating copy to clipboard operation
kr8s copied to clipboard

KUBECONFIG env var with empty path item leads to "IsADirectoryError: [Errno 21] Is a directory: '.'"

Open marcodlk opened this issue 9 months ago • 1 comments

Which project are you reporting a bug for?

kr8s

What happened?

My KUBECONFIG env var has a leading empty path segment, i.e. ":/path/to/config1:/path/to/config2". kubectl ignores this empty path segment, but trying out the following simple kr8s example resulted in an error:

import kr8s

for pod in kr8s.get("pods", namespace="machina"):
    print(pod.status.podIP, pod.metadata.namespace, pod.metadata.name)

Error (trimmed):

[ . . . ]
  File "/[...]/.venv/lib/python3.9/site-packages/kr8s/_api.py", line 73, in f
    await self.auth
  File "/[...]/.venv/lib/python3.9/site-packages/kr8s/_auth.py", line 53, in f
    await self.reauthenticate()
  File "/[...]/.venv/lib/python3.9/site-packages/kr8s/_auth.py", line 65, in reauthenticate
    await self._load_kubeconfig()
  File "/[...]/.venv/lib/python3.9/site-packages/kr8s/_auth.py", line 100, in _load_kubeconfig
    self.kubeconfig = await KubeConfigSet(
  File "/[...]/.venv/lib/python3.9/site-packages/kr8s/_config.py", line 35, in f
    await config
  File "/[...]/.venv/lib/python3.9/site-packages/kr8s/_config.py", line 197, in f
    async with await anyio.open_file(self.path) as fh:
[ . . . ]
IsADirectoryError: [Errno 21] Is a directory: '.'

Side note: This is due to the way my bash_profile scripts were setup to always append to existing KUBECONFIG var and initial one is always going to be empty. I have since fixed this, but thinking that other users may have a similar setup and run into this issue.

Anything else?

Potential solution

Update kr8s._auth.KubeAuth._load_kubeconfig to check for empty path segments and ignore them. Could also go a step further and ignore any paths that are directories also as they will cause an error, but that would be inconsistent with kubectl, which does return error if a config path is a directory.

I would be happy to create a PR for this. Is there a Contributing guide I should follow?

marcodlk avatar May 11 '24 22:05 marcodlk

Totally agree we should handle this gracefully. I think we should strive to copy the behaviour of kubectl so we should handle empty paths, but raise an error on badly configured paths. Although if kubectl's error is not helpful that doesn't mean ours can't be.

Yep contributing guide is here.

jacobtomlinson avatar May 13 '24 07:05 jacobtomlinson