Created
May 11, 2022 06:25
-
-
Save girirahayu/792f3af0cd96665deb8a5d6a1707c073 to your computer and use it in GitHub Desktop.
Switching Kube Config Contexts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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