diff --git a/kubeshell/client.py b/kubeshell/client.py index 360f337..a6304bb 100644 --- a/kubeshell/client.py +++ b/kubeshell/client.py @@ -3,6 +3,7 @@ from kubernetes import client, config from kubernetes.client.api_client import ApiException +import os import logging import urllib3 @@ -10,6 +11,7 @@ ulogger = logging.getLogger("urllib3") ulogger.setLevel("ERROR") urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) +kubeconfig_filepath = os.getenv("KUBECONFIG") or "~/.kube/config" class KubernetesClient(object): @@ -17,7 +19,8 @@ class KubernetesClient(object): def __init__(self): self.logger = logging.getLogger(__name__) try: - config.load_kube_config() + config_file = os.path.expanduser(kubeconfig_filepath) + config.load_kube_config(config_file=config_file) except: self.logger.warning("unable to load kube-config") @@ -29,7 +32,6 @@ def __init__(self): self.batchV1Api = client.BatchV1Api() self.batchV2Api = client.BatchV2alpha1Api() - def get_resource(self, resource, namespace="all"): ret, resources = None, list() try: diff --git a/kubeshell/completer.py b/kubeshell/completer.py index 066cb0c..d710ce9 100644 --- a/kubeshell/completer.py +++ b/kubeshell/completer.py @@ -38,8 +38,11 @@ def set_namespace(self, namespace): def get_completions(self, document, complete_event, smart_completion=None): word_before_cursor = document.get_word_before_cursor(WORD=True) cmdline = document.text_before_cursor.strip() - tokens = shlex.split(cmdline) - _, _, suggestions = self.parser.parse_tokens(tokens) - valid_keys = fuzzyfinder(word_before_cursor, suggestions.keys()) - for key in valid_keys: - yield Completion(key, -len(word_before_cursor), display=key, display_meta=suggestions[key]) + try: + tokens = shlex.split(cmdline) + _, _, suggestions = self.parser.parse_tokens(tokens) + valid_keys = fuzzyfinder(word_before_cursor, suggestions.keys()) + for key in valid_keys: + yield Completion(key, -len(word_before_cursor), display=key, display_meta=suggestions[key]) + except ValueError: + pass diff --git a/kubeshell/kubeshell.py b/kubeshell/kubeshell.py index d63800e..7bbc8f2 100644 --- a/kubeshell/kubeshell.py +++ b/kubeshell/kubeshell.py @@ -10,7 +10,7 @@ from kubeshell.completer import KubectlCompleter from kubeshell.lexer import KubectlLexer from kubeshell.toolbar import Toolbar -from kubeshell.client import KubernetesClient +from kubeshell.client import KubernetesClient, kubeconfig_filepath import os import click @@ -35,10 +35,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 +59,10 @@ def parse_kubeconfig(): @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 +142,16 @@ def get_title(): 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: