def parse_kubeconfig():
if not os.path.exists(os.path.expanduser("~/.kube/config")):
return ("", "", "")
with open(os.path.expanduser("~/.kube/config"), "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)
return ("", "", "")
评论列表
文章目录