#!/usr/bin/env python # -*- coding: utf-8 -*- ''' @Descripttion: CKE集群信息查询 @Author: guohb65 @Email: guohb65@chinaunicom.cn @Date: 2020/4/15 10:21 @LastEditors: guohb65 @LastEditTime: 2020/4/15 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 CKE_DOMAIN = "CKE_DOMAIN" CKE_SVC_TIMEOUT = "CKE_SVC_TIMEOUT" ACCESS_TOKEN = "accessToken" CKE_URL_REGIONS = "/regions/" CKE_URL_CLUSTER_URL = "/k8s/v2/clusters" CKE_URL_ADMIN_TOKEN = "adminToken" CKE_KEY_API_SERVER = "apiserver" CKE_KEY_NETWORK = "network" CKE_KEY_VPC = "vpc" CKE_KEY_ADMIN_TOKEN = "AdminToken" class CkeClusterInfo(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.cke_domain = common_data_json.get(CKE_DOMAIN) if CKE_SVC_TIMEOUT in common_data_json: self.cke_svc_timeout = int(common_data_json.get(CKE_SVC_TIMEOUT)) else: self.cke_svc_timeout = 3 self.login_cookie = login_cookie def get_cke_list(self, regions): request_url = self.cke_domain + CKE_URL_REGIONS + regions + CKE_URL_CLUSTER_URL urllib3.disable_warnings() try: resp = requests.request(RequestMethod.GET.value, request_url, cookies=self.login_cookie, timeout=self.cke_svc_timeout, verify=False) except Exception as e: print("CKE请求异常:" + e.__str__()) return False, e.__str__(), 404 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) else: resp_json = [] cluster_data = [] for cluster in resp_json: apiservers = cluster.get(CKE_KEY_API_SERVER) apiserver_list = apiservers.split(";") apiserver = apiserver_list[0] if apiserver is not "": apiserver_tuple = apiserver.split(":") cluster_ip = apiserver_tuple[1][2:] cluster_port = apiserver_tuple[2] post_handle_cluster = {"cluster_name": cluster.get("name"), "cluster_ip": cluster_ip, "cluster_port": cluster_port, "cluster_type": "CKE", "cluster_apiserver": apiserver, "cluster_apiservers": apiserver_list} cluster_data.append(post_handle_cluster) return True, cluster_data else: return False, resp.text def list_cke_cluster(self, regions): ret_tuple = self.get_cke_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_cke_info(self, region_id, cluster_name): cluster_type = "CKE" cke_info = self.get_cke_rest_info(region_id, cluster_name) token = self.get_cke_token(region_id, cluster_name) if cke_info is not None and token is not None: api_servers = json.loads(cke_info).get(CKE_KEY_API_SERVER) apiserver_list = api_servers.split(";") api_server = apiserver_list[0] network = json.loads(cke_info).get(CKE_KEY_NETWORK) vpc = json.loads(cke_info).get(CKE_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(network) is str: cluster_info["cluster_network"] = network if type(vpc) is str: cluster_info["cluster_vpc"] = vpc else: cluster_info = {} else: cluster_info = {} print(cluster_info) return cluster_info def get_cke_apiserver(self, regions, cluster_name): request_url = self.cke_domain + CKE_URL_REGIONS + regions + CKE_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("CKE请求异常:" + 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(CKE_KEY_API_SERVER) apiserver_list = apiservers.split(";") return apiserver_list[0] else: return None else: return None def get_cke_rest_info(self, regions, cluster_name): if self.login_cookie is not None: request_url = self.cke_domain + CKE_URL_REGIONS + regions + CKE_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("CKE请求异常:" + 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_cke_token(self, regions, cluster_name): if self.login_cookie is not None: request_url = self.cke_domain + CKE_URL_REGIONS + regions + CKE_URL_CLUSTER_URL + "/" + cluster_name \ + "/" + CKE_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("CKE请求异常:" + 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(CKE_KEY_ADMIN_TOKEN) else: return None else: return None else: return None