#!/usr/bin/env python # -*- coding: utf-8 -*- ''' @Descripttion: CSK集群信息查询 @Author: guohb65 @Email: guohb65@chinaunicom.cn @Date: 22022/7/11 10:21 @LastEditors: guohb65 @LastEditTime: 2022/7/11 10:21 ''' import json import os import requests import urllib3 from cucc_common_pkg.util_pkg.common_func import CommonFunc from cucc_common_pkg.util_pkg.const import RequestMethod, ConstGen, ResponseCode CSK_DOMAIN = "CSK_DOMAIN" CSK_SVC_TIMEOUT = "CSK_SVC_TIMEOUT" ACCESS_TOKEN = "accessToken" CSK_URL_REGIONS = "/regions/" CSK_URL_CLUSTER_URL = "/k8s/clusters" CSK_URL_VERSION = "/v1" CSK_URL_PREFIX = "/csk" CSK_URL_ADMIN_TOKEN = "adminToken" CSK_KEY_API_SERVER = "apiserver" CSK_KEY_VPC = "vpc" CSK_KEY_ADMIN_TOKEN = "AdminToken" class CskClusterInfo(object): def __init__(self, login_cookie=None): env_dist = os.environ common_mount_path = env_dist.get(ConstGen.COMMON_MOUNT_PATH) common_conf_file = open(common_mount_path, 'r') common_data_json = json.loads(common_conf_file.read()) common_conf_file.close() self.csk_domain = common_data_json.get(CSK_DOMAIN) if CSK_SVC_TIMEOUT in common_data_json: self.csk_svc_timeout = int(common_data_json.get(CSK_SVC_TIMEOUT)) else: self.csk_svc_timeout = 3 self.login_cookie = login_cookie def get_csk_list(self, regions): request_url = self.csk_domain + CSK_URL_PREFIX + CSK_URL_REGIONS + regions + CSK_URL_CLUSTER_URL urllib3.disable_warnings() try: resp = requests.request(RequestMethod.GET.value, request_url, cookies=self.login_cookie, timeout=self.csk_svc_timeout, verify=False) except Exception as e: print("CSK请求异常:" + e.__str__()) ret_data = {ConstGen.CODE: ResponseCode.SysError.value, ConstGen.MESSAGE: "CSK访问异常"} return False, json.dumps(ret_data, ensure_ascii=False) resp_code = resp.status_code if resp_code == 200: if ConstGen.CONTENT_TYPE_JSON in resp.headers.get(ConstGen.CONTENT_TYPE) and resp.text is not None: resp_json = json.loads(resp.text) if resp_json[ConstGen.CODE] == ResponseCode.OK.value: cluster_list = resp_json["data"] cluster_data = [] for cluster in cluster_list: post_handle_cluster = {"cluster_name": cluster.get("name"), "cluster_ip": "", "cluster_port": "", "cluster_type": "CSK", "cluster_apiserver": "", "cluster_apiservers": ""} cluster_data.append(post_handle_cluster) return True, cluster_data else: return False, resp.text else: ret_data = {ConstGen.CODE: ResponseCode.SysError.value, ConstGen.MESSAGE: resp.text} return False, json.dumps(ret_data, ensure_ascii=False) def list_csk_cluster(self, regions): ret_tuple = self.get_csk_list(regions) if ret_tuple[0]: return CommonFunc().fabricate_response_data(ConstGen.SUCCESS_CODE, ResponseCode.OK.value, ConstGen.SUCCESS_STR, None, ret_tuple[1]) else: return CommonFunc().fabricate_response_data(ConstGen.FAIL_CODE, ResponseCode.Failure.value, ret_tuple[1]) def get_csk_info(self, region_id, cluster_name): cluster_type = "CSK" csk_info = self.get_csk_rest_info(region_id, cluster_name) token = self.get_csk_token(region_id, cluster_name) if csk_info is not None and token is not None: api_servers = json.loads(csk_info).get(CSK_KEY_API_SERVER) apiserver_list = api_servers.split(";") api_server = apiserver_list[0] vpc = json.loads(csk_info).get(CSK_KEY_VPC) if type(api_server) is str and type(token) is str: apiserver = api_server.split(":") cluster_ip = apiserver[1][2:] cluster_port = apiserver[2] cluster_info = {"cluster_type": cluster_type, "cluster_ip": cluster_ip, "cluster_port": cluster_port, "cluster_token": token, "cluster_apiserver": api_server, "cluster_apiservers": apiserver_list} if type(vpc) is str: cluster_info["cluster_vpc"] = vpc else: cluster_info = {} else: cluster_info = {} print(cluster_info) return cluster_info def get_csk_apiserver(self, regions, cluster_name): request_url = self.csk_domain + CSK_URL_PREFIX + CSK_URL_REGIONS + regions + CSK_URL_CLUSTER_URL + "/" + cluster_name print(request_url) urllib3.disable_warnings() try: resp = requests.request(RequestMethod.GET.value, request_url, cookies=self.login_cookie, verify=False) except Exception as e: print("csk请求异常:" + e.__str__()) return e.__str__() resp_code = resp.status_code if resp_code == 200: if ConstGen.CONTENT_TYPE_JSON in resp.headers.get(ConstGen.CONTENT_TYPE) and resp.text is not None: resp_json = json.loads(resp.text) apiservers = resp_json.get(CSK_KEY_API_SERVER) apiserver_list = apiservers.split(";") return apiserver_list[0] else: return None else: return None def get_csk_rest_info(self, regions, cluster_name): if self.login_cookie is not None: request_url = self.csk_domain + CSK_URL_PREFIX + CSK_URL_REGIONS + regions + CSK_URL_CLUSTER_URL + "/" + cluster_name print(request_url) urllib3.disable_warnings() try: resp = requests.request(RequestMethod.GET.value, request_url, cookies=self.login_cookie, verify=False) print(resp.text) except Exception as e: print("csk请求异常:" + e.__str__()) return None resp_code = resp.status_code if resp_code == 200: if ConstGen.CONTENT_TYPE_JSON in resp.headers.get(ConstGen.CONTENT_TYPE) and resp.text is not None: return resp.text else: return None else: return None else: return None def get_csk_token(self, regions, cluster_name): if self.login_cookie is not None: request_url = self.csk_domain + CSK_URL_PREFIX + CSK_URL_REGIONS + regions + CSK_URL_CLUSTER_URL + "/" + cluster_name \ + "/" + CSK_URL_ADMIN_TOKEN urllib3.disable_warnings() try: resp = requests.request(RequestMethod.GET.value, request_url, cookies=self.login_cookie, verify=False) except Exception as e: print("csk请求异常:" + e.__str__()) return None resp_code = resp.status_code if resp_code == 200: if ConstGen.CONTENT_TYPE_JSON in resp.headers.get(ConstGen.CONTENT_TYPE) and resp.text is not None: resp_json = json.loads(resp.text) return resp_json.get(CSK_KEY_ADMIN_TOKEN) else: return None else: return None else: return None