2022-12-07 15:55:56 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-04-18 03:32:59 +00:00
|
|
|
from urllib.parse import urlencode, urlparse
|
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
|
2023-04-18 03:32:59 +00:00
|
|
|
from sshtunnel import SSHTunnelForwarder, BaseSSHTunnelForwarderError
|
2022-12-07 15:55:56 +00:00
|
|
|
|
|
|
|
from common.utils import get_logger
|
|
|
|
from ..const import CloudTypes, Category
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
class KubernetesClient:
|
2023-04-18 03:32:59 +00:00
|
|
|
def __init__(self, asset, token):
|
|
|
|
self.url = asset.address
|
2023-04-18 07:31:47 +00:00
|
|
|
self.token = token or ''
|
2023-04-18 03:32:59 +00:00
|
|
|
self.server = self.get_gateway_server(asset)
|
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()
|
2023-08-02 07:07:22 +00:00
|
|
|
scheme = urlparse(self.url).scheme
|
|
|
|
if not self.server:
|
|
|
|
host = self.url
|
|
|
|
else:
|
|
|
|
host = f'{scheme}://127.0.0.1:{self.server.local_bind_port}'
|
|
|
|
configuration.host = host
|
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
|
2023-04-18 03:32:59 +00:00
|
|
|
def get_gateway_server(asset):
|
|
|
|
gateway = None
|
|
|
|
if not asset.is_gateway and asset.domain:
|
|
|
|
gateway = asset.domain.select_gateway()
|
2022-12-16 02:52:08 +00:00
|
|
|
|
|
|
|
if not gateway:
|
2023-04-18 03:32:59 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
remote_bind_address = (
|
|
|
|
urlparse(asset.address).hostname,
|
2023-08-24 08:10:29 +00:00
|
|
|
urlparse(asset.address).port or 443
|
2023-04-18 03:32:59 +00:00
|
|
|
)
|
|
|
|
server = SSHTunnelForwarder(
|
|
|
|
(gateway.address, gateway.port),
|
|
|
|
ssh_username=gateway.username,
|
|
|
|
ssh_password=gateway.password,
|
|
|
|
ssh_pkey=gateway.private_key_path,
|
|
|
|
remote_bind_address=remote_bind_address
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
server.start()
|
|
|
|
except BaseSSHTunnelForwarderError:
|
|
|
|
err_msg = 'Gateway is not active: %s' % asset.get('name', '')
|
|
|
|
print('\033[31m %s \033[0m\n' % err_msg)
|
|
|
|
return server
|
|
|
|
|
|
|
|
def run(self, tp, *args):
|
2023-03-01 09:04:17 +00:00
|
|
|
func_name = f'get_{tp}s'
|
2023-04-14 08:57:08 +00:00
|
|
|
data = []
|
2023-04-18 03:32:59 +00:00
|
|
|
if hasattr(self, func_name):
|
2023-04-14 08:57:08 +00:00
|
|
|
try:
|
2023-04-18 03:32:59 +00:00
|
|
|
data = getattr(self, func_name)(*args)
|
2024-01-17 10:41:29 +00:00
|
|
|
except Exception as e:
|
2024-04-19 09:41:41 +00:00
|
|
|
logger.error(f'K8S tree get {tp} error: {e}')
|
2023-04-18 03:32:59 +00:00
|
|
|
|
|
|
|
if self.server:
|
|
|
|
self.server.stop()
|
2023-04-14 08:57:08 +00:00
|
|
|
return data
|
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(
|
2023-08-16 03:42:36 +00:00
|
|
|
i, i, name, 'asset', icon='k8s', 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 = []
|
2023-04-18 03:32:59 +00:00
|
|
|
k8s_client = KubernetesClient(self.asset, self.secret)
|
2022-12-21 09:17:54 +00:00
|
|
|
if pod:
|
2023-03-01 09:04:17 +00:00
|
|
|
tp = 'container'
|
2023-04-18 03:32:59 +00:00
|
|
|
containers = k8s_client.run(
|
|
|
|
tp, namespace, pod
|
2023-03-01 09:04:17 +00:00
|
|
|
)
|
|
|
|
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'
|
2023-04-18 03:32:59 +00:00
|
|
|
pods = k8s_client.run(tp, namespace)
|
2023-03-01 09:04:17 +00:00
|
|
|
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'
|
2023-04-18 03:32:59 +00:00
|
|
|
namespaces = k8s_client.run(tp)
|
2023-03-01 09:04:17 +00:00
|
|
|
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
|