Skip to content

Instantly share code, notes, and snippets.

@mtgto
Created April 5, 2019 23:04
Show Gist options
  • Save mtgto/924a8043cc3848a6ab147bbd759074fa to your computer and use it in GitHub Desktop.
Save mtgto/924a8043cc3848a6ab147bbd759074fa to your computer and use it in GitHub Desktop.
Show Kubernetes context in Powerline segment (need kubernetes-py)
# -*- coding: utf-8 -*-
# I made it because below related libraries doesn't work with python 3.7.
#
# - https://github.com/zcmarine/powerkube
# - https://github.com/so0k/powerline-kubernetes
#
# NOTE: I don't understand completely how to use powerline watcher.
from kubernetes_py import K8sConfig
from powerline.theme import requires_filesystem_watcher
from os.path import expanduser
watcher = None
context = ''
namespace = ''
@requires_filesystem_watcher
def kubernetes(pl, create_watcher):
global watcher, context, namespace
if watcher is None:
watcher = create_watcher()
config = K8sConfig()
context = config.current_context
namespace = config.namespace
elif watcher("{0}/.kube/config".format(expanduser("~"))):
config = K8sConfig()
context = config.current_context
namespace = config.namespace
return [{
'contents': '{}/{}'.format(context, namespace),
'highlight_groups': ['information:regular']
}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment