Description
Link to the issue (please include a link to the specific documentation or example):
See the examples in the README:
https://github.com/kubernetes-client/python#examples
Description of the issue (please include outputs or screenshots if possible):
The examples of how to set up the module all (except for in_cluster_config.py
) look like this:
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
# Do stuff with Kubernetes
This gives the impression that this is all you need to do to pick up "the" Kubernetes configuration that your user is going to expect you to use (i.e. whatever kubectl
would use). However, this is not the case.
If you are running in a pod, and you want to use the configuration that kubectl
picks up (for the pod's service account, talking to the current Kubertnetes), you need to run config.load_incluster_config()
if/when config.load_kube_config()
fails. Since, outside of very specialized situations, you don't really know where your user's will run your software or which method will produce the actual Kubernetes credentials in advance, the Right Way to connect to Kubernetes is not a single method call but a try/except, something like this:
try:
config.load_kube_config()
except:
# load_kube_config throws if there is no config, but does not document what it throws, so I can't rely on any particular type here
config.load_incluster_config()
The examples in the README, and possibly in the examples folder, should be changed to demonstrate credential loading that works like kubectl
and pulls from either of these sources as available.
Ideally, the two utility methods should be merged/wrapped in a utility method that loads whichever config is available.
It looks like a similar proposal (with the order reversed) was made as part of #487, but that was part of a larger request, and it was killed by the stale bot without anyone actually solving this particular problem.