mirror of https://github.com/jumpserver/jumpserver
parent
69ef25666e
commit
ebbae36c49
|
@ -1,18 +1,14 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from urllib3.exceptions import MaxRetryError
|
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
|
from kubernetes import client
|
||||||
from kubernetes.client import api_client
|
from kubernetes.client import api_client
|
||||||
from kubernetes.client.api import core_v1_api
|
from kubernetes.client.api import core_v1_api
|
||||||
from kubernetes import client
|
|
||||||
from kubernetes.client.exceptions import ApiException
|
|
||||||
|
|
||||||
from rest_framework.generics import get_object_or_404
|
from rest_framework.generics import get_object_or_404
|
||||||
|
|
||||||
from common.utils import get_logger
|
|
||||||
from common.tree import TreeNode
|
|
||||||
from assets.models import SystemUser
|
from assets.models import SystemUser
|
||||||
|
from common.tree import TreeNode
|
||||||
|
from common.utils import get_logger
|
||||||
from .. import const
|
from .. import const
|
||||||
|
|
||||||
logger = get_logger(__file__)
|
logger = get_logger(__file__)
|
||||||
|
@ -23,7 +19,8 @@ class KubernetesClient:
|
||||||
self.url = url
|
self.url = url
|
||||||
self.token = token
|
self.token = token
|
||||||
|
|
||||||
def get_api(self):
|
@property
|
||||||
|
def api(self):
|
||||||
configuration = client.Configuration()
|
configuration = client.Configuration()
|
||||||
configuration.host = self.url
|
configuration.host = self.url
|
||||||
configuration.verify_ssl = False
|
configuration.verify_ssl = False
|
||||||
|
@ -32,63 +29,46 @@ class KubernetesClient:
|
||||||
api = core_v1_api.CoreV1Api(c)
|
api = core_v1_api.CoreV1Api(c)
|
||||||
return api
|
return api
|
||||||
|
|
||||||
def get_namespace_list(self):
|
def get_namespaces(self):
|
||||||
api = self.get_api()
|
namespaces = []
|
||||||
namespace_list = []
|
resp = self.api.list_namespace()
|
||||||
for ns in api.list_namespace().items:
|
for ns in resp.items:
|
||||||
namespace_list.append(ns.metadata.name)
|
namespaces.append(ns.metadata.name)
|
||||||
return namespace_list
|
return namespaces
|
||||||
|
|
||||||
def get_services(self):
|
def get_pods(self, namespace):
|
||||||
api = self.get_api()
|
pods = []
|
||||||
ret = api.list_service_for_all_namespaces(watch=False)
|
resp = self.api.list_namespaced_pod(namespace)
|
||||||
for i in ret.items:
|
for pd in resp.items:
|
||||||
print("%s \t%s \t%s \t%s \t%s \n" % (
|
pods.append(pd.metadata.name)
|
||||||
i.kind, i.metadata.namespace, i.metadata.name, i.spec.cluster_ip, i.spec.ports))
|
return pods
|
||||||
|
|
||||||
def get_pod_info(self, namespace, pod):
|
def get_containers(self, namespace, pod_name):
|
||||||
api = self.get_api()
|
containers = []
|
||||||
resp = api.read_namespaced_pod(namespace=namespace, name=pod)
|
resp = self.api.read_namespaced_pod(pod_name, namespace)
|
||||||
return resp
|
for container in resp.spec.containers:
|
||||||
|
containers.append(container.name)
|
||||||
|
return containers
|
||||||
|
|
||||||
def get_pod_logs(self, namespace, pod):
|
@classmethod
|
||||||
api = self.get_api()
|
def run(cls, asset, secret, tp='namespace'):
|
||||||
log_content = api.read_namespaced_pod_log(pod, namespace, pretty=True, tail_lines=200)
|
k8s_url = f'{asset.address}'
|
||||||
return log_content
|
k8s = cls(k8s_url, secret)
|
||||||
|
func_name = f'get_{tp}s'
|
||||||
|
if hasattr(k8s, func_name):
|
||||||
|
return getattr(k8s, func_name)()
|
||||||
|
return []
|
||||||
|
|
||||||
def get_pods(self):
|
@classmethod
|
||||||
api = self.get_api()
|
def get_kubernetes_data(cls, app_id, system_user_id, tp, *args):
|
||||||
try:
|
|
||||||
ret = api.list_pod_for_all_namespaces(watch=False, _request_timeout=(3, 3))
|
|
||||||
except MaxRetryError:
|
|
||||||
logger.warning('Kubernetes connection timed out')
|
|
||||||
return
|
|
||||||
except ApiException as e:
|
|
||||||
if e.status == 401:
|
|
||||||
logger.warning('Kubernetes User not authenticated')
|
|
||||||
else:
|
|
||||||
logger.warning(e)
|
|
||||||
return
|
|
||||||
data = {}
|
|
||||||
for i in ret.items:
|
|
||||||
namespace = i.metadata.namespace
|
|
||||||
pod_info = {
|
|
||||||
'pod_name': i.metadata.name,
|
|
||||||
'containers': [j.name for j in i.spec.containers]
|
|
||||||
}
|
|
||||||
if namespace in data:
|
|
||||||
data[namespace].append(pod_info)
|
|
||||||
else:
|
|
||||||
data[namespace] = [pod_info, ]
|
|
||||||
return data
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_kubernetes_data(app_id, system_user_id):
|
|
||||||
from ..models import Application
|
from ..models import Application
|
||||||
app = get_object_or_404(Application, id=app_id)
|
app = get_object_or_404(Application, id=app_id)
|
||||||
system_user = get_object_or_404(SystemUser, id=system_user_id)
|
system_user = get_object_or_404(SystemUser, id=system_user_id)
|
||||||
k8s = KubernetesClient(app.attrs['cluster'], system_user.token)
|
k8s = cls(app.attrs['cluster'], system_user.token)
|
||||||
return k8s.get_pods()
|
func_name = f'get_{tp}s'
|
||||||
|
if hasattr(k8s, func_name):
|
||||||
|
return getattr(k8s, func_name)(*args)
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
class KubernetesTree:
|
class KubernetesTree:
|
||||||
|
@ -118,11 +98,10 @@ class KubernetesTree:
|
||||||
)
|
)
|
||||||
return node
|
return node
|
||||||
|
|
||||||
def as_namespace_pod_tree_node(self, name, meta, type, counts=0, is_container=False):
|
def as_namespace_pod_tree_node(self, name, meta, type, is_container=False):
|
||||||
from ..models import ApplicationTreeNodeMixin
|
from ..models import ApplicationTreeNodeMixin
|
||||||
i = ApplicationTreeNodeMixin.create_tree_id(self.tree_id, type, name)
|
i = ApplicationTreeNodeMixin.create_tree_id(self.tree_id, type, name)
|
||||||
meta.update({type: name})
|
meta.update({type: name})
|
||||||
name = name if is_container else f'{name}({counts})'
|
|
||||||
node = self.create_tree_node(
|
node = self.create_tree_node(
|
||||||
i, self.tree_id, name, type, meta, icon='cloud', is_container=is_container
|
i, self.tree_id, name, type, meta, icon='cloud', is_container=is_container
|
||||||
)
|
)
|
||||||
|
@ -157,30 +136,30 @@ class KubernetesTree:
|
||||||
system_user_id = parent_info.get('system_user_id')
|
system_user_id = parent_info.get('system_user_id')
|
||||||
|
|
||||||
tree_nodes = []
|
tree_nodes = []
|
||||||
data = KubernetesClient.get_kubernetes_data(app_id, system_user_id)
|
|
||||||
if not data:
|
|
||||||
return tree_nodes
|
|
||||||
|
|
||||||
if pod_name:
|
if pod_name:
|
||||||
for container in next(
|
tp = 'container'
|
||||||
filter(
|
containers = KubernetesClient.get_kubernetes_data(
|
||||||
lambda x: x['pod_name'] == pod_name, data[namespace]
|
app_id, system_user_id, tp, namespace, pod_name
|
||||||
)
|
)
|
||||||
)['containers']:
|
for container in containers:
|
||||||
container_node = self.as_namespace_pod_tree_node(
|
container_node = self.as_namespace_pod_tree_node(
|
||||||
container, parent_info, 'container', is_container=True
|
container, parent_info, tp, is_container=True
|
||||||
)
|
)
|
||||||
tree_nodes.append(container_node)
|
tree_nodes.append(container_node)
|
||||||
elif namespace:
|
elif namespace:
|
||||||
for pod in data[namespace]:
|
tp = 'pod'
|
||||||
pod_nodes = self.as_namespace_pod_tree_node(
|
pods = KubernetesClient.get_kubernetes_data(app_id, system_user_id, tp, namespace)
|
||||||
pod['pod_name'], parent_info, 'pod', len(pod['containers'])
|
for pod in pods:
|
||||||
|
pod_node = self.as_namespace_pod_tree_node(
|
||||||
|
pod, parent_info, tp
|
||||||
)
|
)
|
||||||
tree_nodes.append(pod_nodes)
|
tree_nodes.append(pod_node)
|
||||||
elif system_user_id:
|
elif system_user_id:
|
||||||
for namespace, pods in data.items():
|
tp = 'namespace'
|
||||||
|
namespaces = KubernetesClient.get_kubernetes_data(app_id, system_user_id, tp)
|
||||||
|
for namespace in namespaces:
|
||||||
namespace_node = self.as_namespace_pod_tree_node(
|
namespace_node = self.as_namespace_pod_tree_node(
|
||||||
namespace, parent_info, 'namespace', len(pods)
|
namespace, parent_info, tp
|
||||||
)
|
)
|
||||||
tree_nodes.append(namespace_node)
|
tree_nodes.append(namespace_node)
|
||||||
return tree_nodes
|
return tree_nodes
|
||||||
|
|
Loading…
Reference in New Issue