-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubectx.py
executable file
·60 lines (48 loc) · 1.76 KB
/
kubectx.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
#!/usr/bin/env python3.7
#
# This will set an iterm2 variable "user.kubectx" with the name of the
# current kubectl context and namespace configured. This variable in turn
# can be referenced from the status bar controls using an interpolated
# string. It updates the variable every 5 seconds.
#
# See:
# https://www.iterm2.com/documentation-scripting-fundamentals.html
# https://github.com/renier/kubectx
import iterm2
import subprocess
import time
import json
async def main(connection):
async def set_kubectx(conn):
app = await iterm2.async_get_app(conn)
process = subprocess.Popen(
['/bin/bash', '-c', '/usr/local/bin/kubectl config view --minify --output json'], stdout=subprocess.PIPE)
output, error = process.communicate()
decoded = output.decode('utf-8')
json_dict = json.loads(decoded)
context_name = json_dict['contexts'][0]['name']
namespace = json_dict['contexts'][0]['context'].get(
'namespace', 'default')
kubectx = "%s %s" % (context_name, namespace)
window = app.current_window
if not window:
print("No windows found")
return
tab = window.current_tab
if not tab:
print("No tabs found")
return
for session in tab.sessions:
try:
await session.async_set_variable('user.kubectx', kubectx)
except Exception as e:
print(e)
# for window in app.windows:
# for tab in window.tabs:
# for session in tab.sessions:
# await session.async_set_variable('user.kubectx', kubectx)
return kubectx
while True:
time.sleep(5)
await set_kubectx(connection)
iterm2.run_forever(main)