From 11a58dc7ad3fe76f940aee28bb31ec8c3efc003a Mon Sep 17 00:00:00 2001 From: feng <1304903146@qq.com> Date: Fri, 16 Dec 2022 10:52:08 +0800 Subject: [PATCH] perf: k8s tree proxy --- apps/assets/utils/k8s.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/apps/assets/utils/k8s.py b/apps/assets/utils/k8s.py index 2f66bf049..8e703d4b6 100644 --- a/apps/assets/utils/k8s.py +++ b/apps/assets/utils/k8s.py @@ -18,13 +18,15 @@ logger = get_logger(__file__) class KubernetesClient: - def __init__(self, url, token): + def __init__(self, url, token, proxy=None): self.url = url self.token = token + self.proxy = proxy def get_api(self): configuration = client.Configuration() configuration.host = self.url + configuration.proxy = self.proxy configuration.verify_ssl = False configuration.api_key = {"authorization": "Bearer " + self.token} c = api_client.ApiClient(configuration=configuration) @@ -81,11 +83,23 @@ class KubernetesClient: data[namespace] = [pod_info, ] return data - @staticmethod - def get_kubernetes_data(app_id, username): + @classmethod + def get_proxy_url(cls, asset): + if not asset.domain: + return None + + gateway = asset.domain.select_gateway() + if not gateway: + return None + return f'{gateway.address}:{gateway.port}' + + @classmethod + def get_kubernetes_data(cls, app_id, username): asset = get_object_or_404(Asset, id=app_id) account = get_object_or_404(Account, asset=asset, username=username) - k8s = KubernetesClient(asset.address, account.secret) + k8s_url = f'{asset.address}:{asset.port}' + proxy_url = cls.get_proxy_url(asset) + k8s = cls(k8s_url, account.secret, proxy=proxy_url) return k8s.get_pods()