Skip to content

Instantly share code, notes, and snippets.

@girirahayu
Created May 11, 2022 06:25
Show Gist options
  • Save girirahayu/792f3af0cd96665deb8a5d6a1707c073 to your computer and use it in GitHub Desktop.
Save girirahayu/792f3af0cd96665deb8a5d6a1707c073 to your computer and use it in GitHub Desktop.
Switching Kube Config Contexts
#!/usr/bin/env python3
import subprocess
import sys
def command(s):
proc = subprocess.Popen([s], stdout=subprocess.PIPE, shell=True)
out, _ = proc.communicate()
return out.decode()
def getContext(s):
context = command("kubectl config get-contexts | grep "+s+" | awk '{print $2}'")
return context
def response():
s = """Simplify switch kubernetes context!
Commands Available:
get show list contexts config kubernetes
Usage:
connect get
connect <type string connection>
Notes:
<string connection> define using 'grep' so you can type similar name context.
"""
return s
try:
if sys.argv[1] == "get":
print(command("kubectl config get-contexts"))
elif (sys.argv[1] == "--help" or sys.argv[1] == "help"):
print(response())
else:
context = getContext(sys.argv[1])
print(command("kubectl config use-context "+context))
except:
print(response())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment