-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes.py
64 lines (48 loc) · 1.51 KB
/
kubernetes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from powerline_shell.utils import BasicSegment
from kubernetes import K8sConfig
import os
_KUBE_SYMBOL = u'\U00002388 '
## Set the following environment variables to configure:
## K8S_ALERT_NAMESPACES:
## - comma-separated list of namespaces to highlight if we are using them
ALERT_FG = 9
ALERT_BG = 235
SAFE_FG = 10
SAFE_BG = 235
NORMAL_FG = 69
NORMAL_BG = 237
CLUSTER_FG = 117
CLUSTER_BG = 235
class Segment(BasicSegment):
def add_to_powerline(self):
if not os.getenv('KUBECONFIG'):
return
try:
alert_clusters = os.getenv("K8S_ALERT_CLUSTERS").split(',')
except:
alert_clusters = []
try:
safe_clusters = os.getenv("K8S_SAFE_CLUSTERS").split(',')
except:
safe_clusters = []
context = K8sConfig().current_context_dict
# user = context.get('user')
# seg = f'{_KUBE_SYMBOL}{user}@'
# seg = f'{_KUBE_SYMBOL}'
# self.powerline.append(seg, NORMAL_FG, NORMAL_BG, separator='')
cluster = context.get('cluster')
namespace = context.get('namespace')
if not namespace:
namespace = 'default'
ns = f'({namespace})'
if cluster in alert_clusters:
fg = ALERT_FG
bg = ALERT_BG
elif cluster in safe_clusters:
fg = SAFE_FG
bg = SAFE_BG
else:
fg = CLUSTER_FG
bg = CLUSTER_BG
self.powerline.append(cluster, fg, bg)
self.powerline.append(ns, fg, bg)