Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for KUBECONFIG and Ignore single quote error #57

Merged
merged 3 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions kubeshell/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
from kubernetes import client, config
from kubernetes.client.api_client import ApiException

import os
import logging
import urllib3

# disable warnings on stdout/stderr from urllib3 connection errors
ulogger = logging.getLogger("urllib3")
ulogger.setLevel("ERROR")
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
kubeconfig_filepath = os.getenv("KUBECONFIG") or "~/.kube/config"


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")

Expand All @@ -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:
Expand Down
13 changes: 8 additions & 5 deletions kubeshell/completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 10 additions & 9 deletions kubeshell/kubeshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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", "")
Expand All @@ -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")
Expand Down Expand Up @@ -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:
Expand Down