-
Notifications
You must be signed in to change notification settings - Fork 7
/
kube_terraform.py
51 lines (46 loc) · 1.79 KB
/
kube_terraform.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
from kubernetes import config as kubeconfig
from kubernetes.client.api import core_v1_api
from kubernetes.client.rest import ApiException
from kubernetes.stream import stream
kubeconfig.load_kube_config()
api = core_v1_api.CoreV1Api()
class KubeTerraform():
def __init__(self, deployment_name, namespace="micado-system"):
"""Initialise kubernetes sdk from kubeconfig"""
self.deployment = deployment_name
self.namespace = namespace
def _get_pod(self):
"""Get the pod from the deployment"""
if not api:
raise NameError("Kube API not initialised!")
try:
return [
pod.metadata.name
for pod in api.list_namespaced_pod(self.namespace).items
if pod.metadata.name.startswith(self.deployment)
][0]
except IndexError as e:
logger.error(f"Could not find {self.deployment} pod in {self.namespace}!")
raise e from None
def exec_run(self, command, success=None):
"""Exec a shell command in the pod, optionally check success"""
pod_name = self._get_pod()
exec_command = ["/bin/sh", "-c"]
exec_command.append(command)
try:
resp = stream(
api.connect_get_namespaced_pod_exec,
pod_name,
self.namespace,
command=exec_command,
stderr=True,
stdin=False,
stdout=True,
tty=False,
)
if success and success not in resp:
logger.error(f"{pod_name} exec error: {resp}")
raise AdaptorCritical(f"Error: {resp}")
except ApiException as e:
logger.error(f"K8s API error: {e}")
raise AdaptorCritical(f"Error: {e}")