From 1c73bf13d607b7494b8c3c67e8bb5de456aa6bba Mon Sep 17 00:00:00 2001 From: cherys Date: Thu, 30 Jul 2020 00:48:40 +0800 Subject: [PATCH 1/3] =?UTF-8?q?[op]1.=E4=BF=AE=E5=A4=8D=E4=BA=86yaml.load(?= =?UTF-8?q?input)=20=E6=8A=A5=E9=94=99=202.=20=E4=BF=AE=E5=A4=8D=E4=BA=86?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=98=BE=E7=A4=BA=E5=BD=93=E5=89=8D=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87=E7=9A=84=20cluster=20=E7=AD=89=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kubeshell/kubeshell.py | 58 ++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/kubeshell/kubeshell.py b/kubeshell/kubeshell.py index 7bbc8f2..081b72d 100644 --- a/kubeshell/kubeshell.py +++ b/kubeshell/kubeshell.py @@ -18,6 +18,7 @@ import subprocess import yaml import logging + logger = logging.getLogger(__name__) inline_help = True @@ -27,7 +28,6 @@ class KubeConfig(object): - clustername = user = "" namespace = "default" current_context_index = 0 @@ -39,22 +39,21 @@ def parse_kubeconfig(): return ("", "", "") 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", "") - contexts = doc.get("contexts") - if contexts: - for index, context in enumerate(contexts): - if context['name'] == current_context: - KubeConfig.current_context_index = index - KubeConfig.current_context_name = context['name'] - if 'cluster' in context['context']: - KubeConfig.clustername = context['context']['cluster'] - if 'namespace' in context['context']: - KubeConfig.namespace = context['context']['namespace'] - if 'user' in context['context']: - KubeConfig.user = context['context']['user'] - return (KubeConfig.clustername, KubeConfig.user, KubeConfig.namespace) + docs = yaml.load(fd, Loader=yaml.FullLoader) + current_context = docs.get("current-context", "") + contexts = docs.get("contexts") + if contexts: + for index, context in enumerate(contexts): + if context['name'] == current_context: + KubeConfig.current_context_index = index + KubeConfig.current_context_name = context['name'] + if 'cluster' in context['context']: + KubeConfig.clustername = context['context']['cluster'] + if 'namespace' in context['context']: + KubeConfig.namespace = context['context']['namespace'] + if 'user' in context['context']: + KubeConfig.user = context['context']['user'] + return (KubeConfig.clustername, KubeConfig.user, KubeConfig.namespace) return ("", "", "") @staticmethod @@ -67,7 +66,7 @@ def switch_to_next_cluster(): for doc in docs: contexts = doc.get("contexts") if contexts: - KubeConfig.current_context_index = (KubeConfig.current_context_index+1) % len(contexts) + KubeConfig.current_context_index = (KubeConfig.current_context_index + 1) % len(contexts) cluster_name = contexts[KubeConfig.current_context_index]['name'] kubectl_config_use_context = "kubectl config use-context " + cluster_name cmd_process = subprocess.Popen(kubectl_config_use_context, shell=True, stdout=subprocess.PIPE) @@ -87,7 +86,6 @@ def switch_to_next_namespace(current_namespace): class Kubeshell(object): - clustername = user = "" namespace = "default" @@ -144,7 +142,7 @@ def get_title(): logger.info("running kube-shell event loop") 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), + 'Server side completion functionality may not work.'.format(kubeconfig_filepath), fg='red', blink=True, bold=True) while True: global inline_help @@ -156,16 +154,16 @@ def get_title(): try: user_input = prompt('kube-shell> ', - history=self.history, - auto_suggest=AutoSuggestFromHistory(), - style=StyleFactory("vim").style, - lexer=KubectlLexer, - get_title=get_title, - enable_history_search=False, - get_bottom_toolbar_tokens=self.toolbar.handler, - vi_mode=True, - key_bindings_registry=registry, - completer=completer) + history=self.history, + auto_suggest=AutoSuggestFromHistory(), + style=StyleFactory("vim").style, + lexer=KubectlLexer, + get_title=get_title, + enable_history_search=False, + get_bottom_toolbar_tokens=self.toolbar.handler, + vi_mode=True, + key_bindings_registry=registry, + completer=completer) except (EOFError, KeyboardInterrupt): sys.exit() From a5c3ca5fd0696bf1ba25e0fac85d5874e1b92e5f Mon Sep 17 00:00:00 2001 From: cherys Date: Sun, 2 Aug 2020 11:46:26 +0800 Subject: [PATCH 2/3] =?UTF-8?q?[op]=E5=86=8D=E4=B8=80=E6=AC=A1=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E4=BA=86=E7=82=B9=E5=87=BBF4=E6=97=B6=E5=80=99?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E7=9A=84yaml.load(input)=20=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kubeshell/kubeshell.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/kubeshell/kubeshell.py b/kubeshell/kubeshell.py index 081b72d..3a5fc58 100644 --- a/kubeshell/kubeshell.py +++ b/kubeshell/kubeshell.py @@ -62,15 +62,15 @@ def switch_to_next_cluster(): return with open(os.path.expanduser(kubeconfig_filepath), "r") as fd: - docs = yaml.load_all(fd) - for doc in docs: - contexts = doc.get("contexts") - if contexts: - KubeConfig.current_context_index = (KubeConfig.current_context_index + 1) % len(contexts) - cluster_name = contexts[KubeConfig.current_context_index]['name'] - kubectl_config_use_context = "kubectl config use-context " + cluster_name - cmd_process = subprocess.Popen(kubectl_config_use_context, shell=True, stdout=subprocess.PIPE) - cmd_process.wait() + # docs = yaml.load_all(fd) # 已经弃用 + docs = yaml.load(fd, Loader=yaml.FullLoader) + contexts = docs.get("contexts") + if contexts: + KubeConfig.current_context_index = (KubeConfig.current_context_index + 1) % len(contexts) + cluster_name = contexts[KubeConfig.current_context_index]['name'] + kubectl_config_use_context = "kubectl config use-context " + cluster_name + cmd_process = subprocess.Popen(kubectl_config_use_context, shell=True, stdout=subprocess.PIPE) + cmd_process.wait() return @staticmethod From 37e9697705d2a4e42a76602f8d2008821de7728b Mon Sep 17 00:00:00 2001 From: cherys Date: Sun, 2 Aug 2020 12:11:33 +0800 Subject: [PATCH 3/3] =?UTF-8?q?[op]=E4=BC=98=E5=8C=96=E4=BA=86=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E7=9A=84=E6=89=8E=E7=9C=BC=E7=9A=84=E7=99=BD=E8=89=B2?= =?UTF-8?q?,=E6=94=B9=E7=94=A8LightGray?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kubeshell/style.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kubeshell/style.py b/kubeshell/style.py index d02e1ce..0906c7f 100644 --- a/kubeshell/style.py +++ b/kubeshell/style.py @@ -50,9 +50,9 @@ def style_factory(self, style_name): t = Token styles.update({ t.Menu.Completions.Completion.Current: 'bg:#00aaaa #000000', - t.Menu.Completions.Completion: 'bg:#008888 #ffffff', + t.Menu.Completions.Completion: 'bg:#008888 #D3D3D3', t.Menu.Completions.Meta.Current: 'bg:#00aaaa #000000', - t.Menu.Completions.Meta: 'bg:#00aaaa #ffffff', + t.Menu.Completions.Meta: 'bg:#00aaaa #D3D3D3', t.Scrollbar.Button: 'bg:#003333', t.Scrollbar: 'bg:#00aaaa', t.Toolbar: 'bg:#222222 #cccccc',