Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Handle relative paths in kubeconfig #55

Merged
merged 2 commits into from
Mar 29, 2020

Conversation

lukemassa
Copy link

Addresses: kelproject#130 from the original project.

Per k8s documentation (https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#file-references), if a relative path is specified in kube config, it should be resolved relative to the kubeconfig file, not (as is currently implemented) from the cwd.

Before:

luke@Lukes-MacBook-Air pykube % git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
luke@Lukes-MacBook-Air pykube % python3 -c 'import pykube; pykube.HTTPClient(pykube.KubeConfig.from_file("~/.kube/config"))'                     
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/luke/work/pykube/pykube/http.py", line 204, in __init__
    self.url = self.config.cluster["server"]
  File "/Users/luke/work/pykube/pykube/config.py", line 188, in cluster
    return self.clusters[self.contexts[self.current_context]["cluster"]]
  File "/Users/luke/work/pykube/pykube/config.py", line 151, in clusters
    BytesOrFile.maybe_set(c, "certificate-authority")
  File "/Users/luke/work/pykube/pykube/config.py", line 239, in maybe_set
    d[file_key] = cls(filename=d[file_key])
  File "/Users/luke/work/pykube/pykube/config.py", line 256, in __init__
    "'{}' file does not exist".format(filename)
pykube.exceptions.PyKubeError: 'certs/ndmad1-ca.pem' file does not exist
luke@Lukes-MacBook-Air pykube % python3 -c 'import pykube,os; os.chdir("/Users/luke/.kube"); pykube.HTTPClient(pykube.KubeConfig.from_file("~/.kube/config"))'
luke@Lukes-MacBook-Air pykube % 

After

luke@Lukes-MacBook-Air pykube % git checkout handle_relative_paths_in_kubeconfig
Switched to branch 'handle_relative_paths_in_kubeconfig'
luke@Lukes-MacBook-Air pykube % python3 -c 'import pykube; pykube.HTTPClient(pykube.KubeConfig.from_file("~/.kube/config"))'                                  
luke@Lukes-MacBook-Air pykube % python3 -c 'import pykube,os; os.chdir("/Users/luke/.kube"); pykube.HTTPClient(pykube.KubeConfig.from_file("~/.kube/config"))'
luke@Lukes-MacBook-Air pykube % 

Note that if PykubeConfig is created via a serviceaccount, there is no change to behavior (since there is no kubeconfig file to find)

luke@Lukes-MacBook-Air pykube % kubectl -n sandbox run --generator=run-pod/v1 --limits=cpu=200m,memory=512Mi -it hello-world-$USER --image techops-docker.maven.dev.tripadvisor.com/utils/pykube-upstream-tester:0.1 -- /bin/sh
If you don't see a command prompt, try pressing enter.
/ # python3 -c 'import pykube; pykube.HTTPClient(pykube.KubeConfig.from_service_account())'
/ #

@lukemassa
Copy link
Author

Fixed linting and re-pushed

@hjacobs
Copy link
Owner

hjacobs commented Mar 23, 2020

Thanks for the PR! We could change it to use pathlib.Path (Python 3+) instead of "old style" os.path.

@lukemassa
Copy link
Author

We could change it to use pathlib.Path (Python 3+) instead of "old style" os.path.

Sure, looking now

@lukemassa
Copy link
Author

It looks like there are a number of reference to os.path in config.py; do you want me to

  1. Change only the references that os.path that I introduced
  2. Change all references to os.path in this PR
  3. Leave it as is and follow up this PR with one that converts to pathlib

I'm fine doing any of those, I just know some people like to keep PRs single-purpose while others are OK w this sort of refactoring coming along for the ride.

@lukemassa
Copy link
Author

Looking into this a bit more, I think 3 is the way to go, since you'll probably want to change filename to filepath throughout the whole object for consistency, right? I started to work on this here, is this what you're looking for? lukemassa@0395eb1

@lukemassa
Copy link
Author

@hjacobs let me know which way you want to go with this, like I said I'm fine with any of the approaches

@hjacobs hjacobs merged commit 2ad94b3 into hjacobs:master Mar 29, 2020
@lukemassa lukemassa mentioned this pull request Mar 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants