Currently we have two method in config module to load config. One from kube config and the other from in-cluster configs. Either add in-cluster config load to load kube config or add a new method (maybe call it load_config or load for short) that tries in-cluster first and then kube-config.
@mbohlool : Looks good to me, Just a small questions: Would the differentiation be done on the basis of whether config file is present or not?
That is a good question. I think it should be on success of loading config not existance of the files only. If loading in-cluster config raised an exception, we can try kube-config. However, I don't have strong opinion on this.
I think we should create a new function, which will try first in-cluster and the kube-config and document this order.
For people that do not want to rely on default settings they can use the current existing functions.
@mbohlool I imagine you plan to update all the examples to use this new load method, don't you?
I'd love to see this as part of the API. I implemented a version of this already; I'm happy to put this into a PR if you like:
# Prereqs:
# - import os
# - 'config' is the instance of kubernetes.client.Configuration being initialized.
# Code:
# Load credentials from the environment.
base_creds_path = '/var/run/secrets/kubernetes.io/serviceaccount'
with open(os.path.join(base_creds_path, 'token')) as token_file:
config.api_key['authorization'] = 'Bearer {}'.format(token_file.read())
config.ssl_ca_cert = os.path.join(base_creds_path, 'ca.crt')
# Load the "kubernetes" service items.
service_host = os.environ['KUBERNETES_SERVICE_HOST']
service_port = os.environ['KUBERNETES_SERVICE_PORT']
config.host = 'https://{}:{}'.format(service_host, service_port)
@jkinkead We already have an in cluster config loader. The goal of this issue is to merge the experience of load in-cluster config and kube config (we have both separately).
@pokoli Yes, I will work on examples soon.
Awesome. Where is the in-cluster loader? I'd love to not have to reimplement this.
from kubernetes import config
config.load_incluster_config()
...
Somebody should add an example for this in examples/ folder.
Would be nice since this is just accidental complexity for the normal case, and for all _our_ kube python projects.
People don't develop and run initial tests in a pod. Running the code however in prod, or doing SIT happens mostly in pods for us, so we're forced to use the try-except _boilerplate_.
Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.
If this issue is safe to close now please do so with /close.
Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale
Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.
If this issue is safe to close now please do so with /close.
Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten
Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.
Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close
@fejta-bot: Closing this issue.
In response to this:
Rotten issues close after 30d of inactivity.
Reopen the issue with/reopen.
Mark the issue as fresh with/remove-lifecycle rotten.Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.
Most helpful comment
Somebody should add an example for this in examples/ folder.