-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tyler Gu <[email protected]>
- Loading branch information
Showing
7 changed files
with
247 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import subprocess | ||
from typing import Optional | ||
|
||
|
||
class Helm: | ||
"""Helm client class""" | ||
|
||
def __init__(self, kubeconfig: str, context_name: str) -> None: | ||
self.kubeconfig = kubeconfig | ||
self.context_name = context_name | ||
|
||
def helm(self, args: list) -> subprocess.CompletedProcess: | ||
"""Executes a helm command""" | ||
cmd = ["helm"] | ||
cmd.extend(args) | ||
cmd.extend(["--kubeconfig", self.kubeconfig]) | ||
cmd.extend(["--kube-context", self.context_name]) | ||
return subprocess.run(cmd, capture_output=True, text=True, check=False) | ||
|
||
def repo_add(self, name: str, url: str) -> subprocess.CompletedProcess: | ||
"""Adds a helm repository""" | ||
cmd = ["repo", "add", name, url] | ||
return self.helm(cmd) | ||
|
||
def install( | ||
self, | ||
release_name: str, | ||
chart: str, | ||
namespace: str, | ||
repo: Optional[str] = None, | ||
args: Optional[list] = None, | ||
) -> subprocess.CompletedProcess: | ||
"""Installs a helm chart""" | ||
cmd = [ | ||
"install", | ||
release_name, | ||
chart, | ||
"--namespace", | ||
namespace, | ||
"--create-namespace", | ||
] | ||
if repo: | ||
cmd.extend(["--repo", repo]) | ||
if args: | ||
cmd.extend(args) | ||
return self.helm(cmd) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,95 @@ | ||
import logging | ||
import subprocess | ||
from typing import Optional | ||
|
||
|
||
class KubectlClient: | ||
"""Kubectl client class""" | ||
|
||
def __init__(self, kubeconfig: str, context_name: str): | ||
|
||
if not kubeconfig: | ||
raise ValueError('kubeconfig is required') | ||
raise ValueError("kubeconfig is required") | ||
if not context_name: | ||
raise ValueError('context_name is required') | ||
raise ValueError("context_name is required") | ||
|
||
self.kubeconfig = kubeconfig | ||
self.context_name = context_name | ||
|
||
def exec(self, | ||
pod: str, | ||
namespace: str, | ||
commands: list, | ||
capture_output=False, | ||
text=False) -> subprocess.CompletedProcess: | ||
'''Executes a command in a pod''' | ||
cmd = ['exec'] | ||
def exec( | ||
self, | ||
pod: str, | ||
namespace: str, | ||
commands: list, | ||
capture_output=False, | ||
text=False, | ||
) -> subprocess.CompletedProcess: | ||
"""Executes a command in a pod""" | ||
cmd = ["exec"] | ||
cmd.extend([pod]) | ||
cmd.extend(['--namespace', namespace]) | ||
cmd.extend(['--']) | ||
cmd.extend(["--namespace", namespace]) | ||
cmd.extend(["--"]) | ||
cmd.extend(commands) | ||
|
||
return self.kubectl(cmd, capture_output, text) | ||
|
||
def kubectl(self, | ||
args: list, | ||
capture_output=False, | ||
text=False, | ||
timeout: int = 600) -> subprocess.CompletedProcess: | ||
'''Executes a kubectl command''' | ||
cmd = ['kubectl'] | ||
cmd.extend(['--kubeconfig', self.kubeconfig]) | ||
cmd.extend(['--context', self.context_name]) | ||
def kubectl( | ||
self, args: list, capture_output=False, text=False, timeout: int = 600 | ||
) -> subprocess.CompletedProcess: | ||
"""Executes a kubectl command""" | ||
cmd = ["kubectl"] | ||
cmd.extend(["--kubeconfig", self.kubeconfig]) | ||
cmd.extend(["--context", self.context_name]) | ||
|
||
cmd.extend(args) | ||
|
||
p = subprocess.run(cmd, capture_output=capture_output, text=text, timeout=timeout) | ||
return p | ||
logging.info("Running kubectl command: %s", " ".join(cmd)) | ||
p = subprocess.run( | ||
cmd, | ||
capture_output=capture_output, | ||
text=text, | ||
timeout=timeout, | ||
check=False, | ||
) | ||
return p | ||
|
||
def wait( | ||
self, | ||
file: str, | ||
for_condition: str, | ||
timeout: int = 600, | ||
namespace: Optional[str] = None, | ||
) -> subprocess.CompletedProcess: | ||
"""Waits for a condition to be true""" | ||
cmd = [ | ||
"wait", | ||
"-f", | ||
file, | ||
"--for", | ||
for_condition, | ||
"--timeout", | ||
f"{timeout}s", | ||
] | ||
if namespace: | ||
cmd.extend(["-n", namespace]) | ||
else: | ||
cmd.extend(["--all-namespaces"]) | ||
return self.kubectl(cmd, capture_output=True, text=True) | ||
|
||
def wait_for_all_pods( | ||
self, timeout: int = 600, namespace: Optional[str] = None | ||
) -> subprocess.CompletedProcess: | ||
"""Waits for all pods to be ready""" | ||
cmd = [ | ||
"wait", | ||
"--for=condition=Ready", | ||
"--timeout", | ||
f"{timeout}s", | ||
"pods", | ||
"--all", | ||
] | ||
if namespace: | ||
cmd.extend(["-n", namespace]) | ||
else: | ||
cmd.extend(["--all-namespaces"]) | ||
return self.kubectl(cmd, capture_output=True, text=True) |
This file contains 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
This file contains 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
Oops, something went wrong.