summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcos Amorim <mamorim@rkh.co.uk>2018-06-04 11:44:27 +0100
committerMarcos Amorim <mamorim@rkh.co.uk>2018-06-04 11:44:27 +0100
commit550141074026f46892cd20e4c946040b740696f0 (patch)
tree25397430d83c757a478413c3a63fd5a59cef6395
parentb7d167c1f5ca480545262b8d844bf262791fbf7f (diff)
Enabling the option to use KUBECONFIG instead of a hard coded path
-rw-r--r--kubeshell/kubeshell.py18
1 files changed, 10 insertions, 8 deletions
diff --git a/kubeshell/kubeshell.py b/kubeshell/kubeshell.py
index d63800e..4d67d23 100644
--- a/kubeshell/kubeshell.py
+++ b/kubeshell/kubeshell.py
@@ -24,6 +24,7 @@ inline_help = True
registry = load_key_bindings_for_prompt()
completer = KubectlCompleter()
client = KubernetesClient()
+kubeconfig_filepath = os.getenv("KUBECONFIG") or "~/.kube/config"
class KubeConfig(object):
@@ -35,10 +36,10 @@ class KubeConfig(object):
@staticmethod
def parse_kubeconfig():
- if not os.path.exists(os.path.expanduser("~/.kube/config")):
+ if not os.path.exists(os.path.expanduser(kubeconfig_filepath)):
return ("", "", "")
- with open(os.path.expanduser("~/.kube/config"), "r") as fd:
+ with open(os.path.expanduser(kubeconfig_filepath), "r") as fd:
docs = yaml.load_all(fd)
for doc in docs:
current_context = doc.get("current-context", "")
@@ -59,10 +60,10 @@ class KubeConfig(object):
@staticmethod
def switch_to_next_cluster():
- if not os.path.exists(os.path.expanduser("~/.kube/config")):
+ if not os.path.exists(os.path.expanduser(kubeconfig_filepath)):
return
- with open(os.path.expanduser("~/.kube/config"), "r") as fd:
+ with open(os.path.expanduser(kubeconfig_filepath), "r") as fd:
docs = yaml.load_all(fd)
for doc in docs:
contexts = doc.get("contexts")
@@ -142,15 +143,16 @@ class Kubeshell(object):
return "kube-shell"
logger.info("running kube-shell event loop")
- if not os.path.exists(os.path.expanduser("~/.kube/config")):
- click.secho('Kube-shell uses ~/.kube/config for server side completion. Could not find ~/.kube/config. '
- 'Server side completion functionality may not work.', fg='red', blink=True, bold=True)
+ if not os.path.exists(os.path.expanduser(kubeconfig_filepath)):
+ click.secho('Kube-shell uses {0} for server side completion. Could not find {0}. '
+ 'Server side completion functionality may not work.'.format(kubeconfig_filepath),
+ fg='red', blink=True, bold=True)
while True:
global inline_help
try:
Kubeshell.clustername, Kubeshell.user, Kubeshell.namespace = KubeConfig.parse_kubeconfig()
except:
- logger.error("unable to parse ~/.kube/config %s", exc_info=1)
+ logger.error("unable to parse {} %s".format(kubeconfig_filepath), exc_info=1)
completer.set_namespace(self.namespace)
try: