2022-12-07 15:55:56 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-12-21 09:17:54 +00:00
|
|
|
from urllib.parse import urlencode
|
2022-12-07 15:55:56 +00:00
|
|
|
|
|
|
|
from kubernetes import client
|
|
|
|
from kubernetes.client import api_client
|
|
|
|
from kubernetes.client.api import core_v1_api
|
|
|
|
|
|
|
|
from common.utils import get_logger
|
|
|
|
from ..const import CloudTypes, Category
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
class KubernetesClient:
|
2022-12-16 02:52:08 +00:00
|
|
|
def __init__(self, url, token, proxy=None):
|
2022-12-07 15:55:56 +00:00
|
|
|
self.url = url
|
|
|
|
self.token = token
|
2022-12-16 02:52:08 +00:00
|
|
|
self.proxy = proxy
|
2022-12-07 15:55:56 +00:00
|
|
|
|
2023-03-01 09:04:17 +00:00
|
|
|
@property
|
|
|
|
def api(self):
|
2022-12-07 15:55:56 +00:00
|
|
|
configuration = client.Configuration()
|
|
|
|
configuration.host = self.url
|
2022-12-16 02:52:08 +00:00
|
|
|
configuration.proxy = self.proxy
|
2022-12-07 15:55:56 +00:00
|
|
|
configuration.verify_ssl = False
|
|
|
|
configuration.api_key = {"authorization": "Bearer " + self.token}
|
|
|
|
c = api_client.ApiClient(configuration=configuration)
|
|
|
|
api = core_v1_api.CoreV1Api(c)
|
|
|
|
return api
|
|
|
|
|
2023-03-01 09:04:17 +00:00
|
|
|
def get_namespaces(self):
|
|
|
|
namespaces = []
|
|
|
|
resp = self.api.list_namespace()
|
|
|
|
for ns in resp.items:
|
|
|
|
namespaces.append(ns.metadata.name)
|
|
|
|
return namespaces
|
|
|
|
|
|
|
|
def get_pods(self, namespace):
|
|
|
|
pods = []
|
|
|
|
resp = self.api.list_namespaced_pod(namespace)
|
|
|
|
for pd in resp.items:
|
|
|
|
pods.append(pd.metadata.name)
|
|
|
|
return pods
|
|
|
|
|
|
|
|
def get_containers(self, namespace, pod_name):
|
|
|
|
containers = []
|
|
|
|
resp = self.api.read_namespaced_pod(pod_name, namespace)
|
|
|
|
for container in resp.spec.containers:
|
|
|
|
containers.append(container.name)
|
|
|
|
return containers
|
2022-12-07 15:55:56 +00:00
|
|
|
|
2023-03-01 09:04:17 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_proxy_url(asset):
|
2022-12-16 02:52:08 +00:00
|
|
|
if not asset.domain:
|
|
|
|
return None
|
|
|
|
|
|
|
|
gateway = asset.domain.select_gateway()
|
|
|
|
if not gateway:
|
|
|
|
return None
|
|
|
|
return f'{gateway.address}:{gateway.port}'
|
|
|
|
|
|
|
|
@classmethod
|
2023-03-01 10:26:54 +00:00
|
|
|
def run(cls, asset, secret, tp, *args):
|
2023-02-10 07:56:34 +00:00
|
|
|
k8s_url = f'{asset.address}'
|
2022-12-16 02:52:08 +00:00
|
|
|
proxy_url = cls.get_proxy_url(asset)
|
2022-12-21 09:17:54 +00:00
|
|
|
k8s = cls(k8s_url, secret, proxy=proxy_url)
|
2023-03-01 09:04:17 +00:00
|
|
|
func_name = f'get_{tp}s'
|
|
|
|
if hasattr(k8s, func_name):
|
|
|
|
return getattr(k8s, func_name)(*args)
|
|
|
|
return []
|
2022-12-07 15:55:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KubernetesTree:
|
2022-12-21 09:17:54 +00:00
|
|
|
def __init__(self, asset, secret):
|
|
|
|
self.asset = asset
|
|
|
|
self.secret = secret
|
2022-12-07 15:55:56 +00:00
|
|
|
|
2022-12-21 09:17:54 +00:00
|
|
|
def as_asset_tree_node(self):
|
|
|
|
i = str(self.asset.id)
|
|
|
|
name = str(self.asset)
|
2022-12-07 15:55:56 +00:00
|
|
|
node = self.create_tree_node(
|
2022-12-21 09:17:54 +00:00
|
|
|
i, i, name, 'asset', is_open=True,
|
2022-12-07 15:55:56 +00:00
|
|
|
)
|
|
|
|
return node
|
|
|
|
|
2023-03-01 09:04:17 +00:00
|
|
|
def as_namespace_node(self, name, tp):
|
2022-12-21 09:17:54 +00:00
|
|
|
i = urlencode({'namespace': name})
|
|
|
|
pid = str(self.asset.id)
|
|
|
|
node = self.create_tree_node(i, pid, name, tp, icon='cloud')
|
2022-12-07 15:55:56 +00:00
|
|
|
return node
|
|
|
|
|
2023-03-01 09:04:17 +00:00
|
|
|
def as_pod_tree_node(self, namespace, name, tp):
|
2022-12-21 09:17:54 +00:00
|
|
|
pid = urlencode({'namespace': namespace})
|
|
|
|
i = urlencode({'namespace': namespace, 'pod': name})
|
|
|
|
node = self.create_tree_node(i, pid, name, tp, icon='cloud')
|
2022-12-07 15:55:56 +00:00
|
|
|
return node
|
|
|
|
|
2022-12-21 09:17:54 +00:00
|
|
|
def as_container_tree_node(self, namespace, pod, name, tp):
|
|
|
|
pid = urlencode({'namespace': namespace, 'pod': pod})
|
|
|
|
i = urlencode({'namespace': namespace, 'pod': pod, 'container': name})
|
2022-12-07 15:55:56 +00:00
|
|
|
node = self.create_tree_node(
|
2022-12-21 09:17:54 +00:00
|
|
|
i, pid, name, tp, icon='cloud', is_container=True
|
2022-12-07 15:55:56 +00:00
|
|
|
)
|
|
|
|
return node
|
|
|
|
|
|
|
|
@staticmethod
|
2022-12-15 08:08:19 +00:00
|
|
|
def create_tree_node(id_, pid, name, identity, icon='', is_container=False, is_open=False):
|
2022-12-07 15:55:56 +00:00
|
|
|
node = {
|
|
|
|
'id': id_,
|
|
|
|
'name': name,
|
|
|
|
'title': name,
|
|
|
|
'pId': pid,
|
|
|
|
'isParent': not is_container,
|
2022-12-15 08:08:19 +00:00
|
|
|
'open': is_open,
|
2022-12-07 15:55:56 +00:00
|
|
|
'iconSkin': icon,
|
|
|
|
'meta': {
|
|
|
|
'type': 'k8s',
|
|
|
|
'data': {
|
|
|
|
'category': Category.CLOUD,
|
|
|
|
'type': CloudTypes.K8S,
|
|
|
|
'identity': identity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
|
2022-12-21 09:17:54 +00:00
|
|
|
def async_tree_node(self, namespace, pod):
|
2022-12-07 15:55:56 +00:00
|
|
|
tree = []
|
2022-12-21 09:17:54 +00:00
|
|
|
if pod:
|
2023-03-01 09:04:17 +00:00
|
|
|
tp = 'container'
|
|
|
|
containers = KubernetesClient.run(
|
|
|
|
self.asset, self.secret, tp, namespace, pod
|
|
|
|
)
|
|
|
|
for container in containers:
|
2022-12-21 09:17:54 +00:00
|
|
|
container_node = self.as_container_tree_node(
|
2023-03-01 09:04:17 +00:00
|
|
|
namespace, pod, container, tp
|
2022-12-07 15:55:56 +00:00
|
|
|
)
|
|
|
|
tree.append(container_node)
|
|
|
|
elif namespace:
|
2023-03-01 09:04:17 +00:00
|
|
|
tp = 'pod'
|
|
|
|
pods = KubernetesClient.run(self.asset, self.secret, tp, namespace)
|
|
|
|
for pod in pods:
|
|
|
|
pod_node = self.as_pod_tree_node(namespace, pod, tp)
|
|
|
|
tree.append(pod_node)
|
2022-12-21 09:17:54 +00:00
|
|
|
else:
|
2023-03-01 09:04:17 +00:00
|
|
|
tp = 'namespace'
|
|
|
|
namespaces = KubernetesClient.run(self.asset, self.secret, tp)
|
|
|
|
for namespace in namespaces:
|
|
|
|
namespace_node = self.as_namespace_node(namespace, tp)
|
2022-12-07 15:55:56 +00:00
|
|
|
tree.append(namespace_node)
|
|
|
|
return tree
|