def compare_metrics(self, deploy, m_type, cur_data, min_data, max_data): """ :param m_type: :param cur_data: :param min_data: :param max_data: """ cur_replicas = deploy.get('hpa', {}).get('replicas') min_replicas = deploy.get('hpa', {}).get('minReplicas') max_replicas = deploy.get('hpa', {}).get('maxReplicas') if max_replicas == 0: max_replicas = 10000000 Log(3, "elastic compare_metrics deploy:{}, type:{}, current data:{}, set_min:{}, set_max:{}, cur_replicas:{},\ min_replicas:{}, max_replicas:{}".format(deploy.get('name'), m_type, cur_data, min_data, max_data, cur_replicas, min_replicas, max_replicas)) if cur_data < min_data and cur_replicas - 1 >= min_replicas: # elastic -1 r = DeployClient.instance().service_up(deploy.get('name'), deploy.get('group', ''), deploy.get('workspace', ''), -1) if r: Log(3, u"deploy {} , 缩容 -1 成功".format(deploy.get('name'))) else: Log(1, u"deploy {}, 缩容 -1 失败".format(deploy.get('name'))) return if cur_data > max_data and cur_replicas + 1 <= max_replicas: # elastic +1 r = DeployClient.instance().service_up(deploy.get('name'), deploy.get('group', ''), deploy.get('workspace', ''), 1) if r: Log(3, u"deploy {}, 扩容 1 成功".format(deploy.get('name'))) else: Log(1, u"deploy {}, 扩容 1 失败".format(deploy.get('deploy', ''))) return return
def delete(self, **kwargs): cluster_name = kwargs.get('cluster_name') name = kwargs.get('name') if not name or not cluster_name: return Result('', 400, 'param error', 400) return DeployClient.instance().del_clusterrolebinding( name, cluster_name)
def list(self, **kwargs): """ list :param kwargs: "cluster_name" : cluster name "name": clusterrole name :return: Result object """ cluster_name = kwargs.get('cluster_name') if not cluster_name: return Result('', 400, 'param error', 400) return DeployClient.instance().get_clusterrole(cluster_name)
def timeout_before(self): # all ns's all deployments deploys = {} all_deploys = DeployClient.instance().all_deploy() if not all_deploys: return Log(4, 'all_deploys:{}'.format(all_deploys)) for deploy in all_deploys: ns = deploy.get('workspace') name = deploy.get('name') if ns and name: deploys.setdefault('%s-%s' % (ns, name), deploy) clu_nodes = self.get_clu_nodes() if not clu_nodes: return for clu_name, nodes in clu_nodes.items(): if deploys and nodes: self.create_task(clu_name, nodes, deploys)
def syn_apply_num(self, clu_name, clu_gws, apply_num): deploy = DeployClient.instance().get_apply_num(clu_name, clu_gws) if apply_num != deploy.content: return Clusterdb.instance().update_apply_num(clu_name, deploy.content) return Result('')
def get_pod_info(self, cluster_name, namespace, group): url = '/namespaces/' + namespace + '/pods' try: r = self.client.request(method='GET', url=url, timeout=self.timeout) except Exception as e: Log(3, "get_pod_info from apiserver:{}".format(e.message)) return [] if r.status_code != 200: return [] content_list = [] data = r.json() items = data.get('items', []) if items: for i in items: pod = { 'cluster_name': cluster_name, # pod所在集群 'api': self.client, # 连接集群的apiserver 'group': group, # 所属group 'namespace': namespace, # pod所在的namespace 'app': i.get('metadata', {}).get('annotations', {}).get('com.appsoar.ufleet.app', ''), # app name 'deploy': i.get('metadata', {}).get('annotations', {}).get('com.appsoar.ufleet.deploy', ''), # deploy name 'is_can_scale': i.get('metadata', {}).get('annotations', {}).get('com.appsoar.ufleet.autoscale', ''), # 是否支持弹性伸缩 # pod所属deployment 'status': i.get('status', {}).get('phase'), 'service_threshold': '', # 服务阈值 'node_name': i.get('spec').get('nodeName', ''), # pod所属主机名 'pod_name': i.get('metadata', {}).get('name', ''), # pod名称 'container_list': i.get('status', {}).get('containerStatuses', []), # 容器列表 'host_ip': i.get('status', {}).get('hostIP', ''), # 主机ip 'pod_uid': i.get('metadata', {}).get('uid', ''), 'container_resouse': i.get('spec', {}).get('containers', []) } Log(4, "elastic pod:{}".format(pod)) if pod['group'] and pod['namespace'] and pod['deploy'] and pod[ 'status'] == 'Running': check_threshold = DeployClient.instance().get_threshold( pod['deploy'], pod['group'], pod['namespace']) Log(4, "get_threashold:{}".format(check_threshold)) if check_threshold and check_threshold.get('deployed', '') is True: pod['service_threshold'] = check_threshold if pod['service_threshold']: content_list.append(pod) content_1 = [] re_content = [] deploy_name_list = [] for j in content_list: # Log(3, "service_name:{}".format(j.get('service', ''))) deploy_name = j.get('deploy', '') if deploy_name: deploy_name_list.append(deploy_name) deploy_name_list = set(deploy_name_list) for k in deploy_name_list: temp_dic = {'deploy_name': k, 'pod_list': []} for m in content_list: if k == m.get('deploy', ''): temp_dic['pod_list'].append(m) re_content.append(temp_dic) for m in re_content: content_1.append(random.sample(m['pod_list'], 1)[0]) return content_1
#! /usr/bin/env python # -*- coding:utf-8 -*- from core.deployclient import DeployClient if __name__ == '__main__': r = DeployClient.instance().get_clusterrole('clu1') print r
def list(self, **kwargs): clu_name = kwargs.get('cluster_name') if not clu_name: return Result('', 400, 'param error', 400) return DeployClient.instance().get_clusterrolebinding(clu_name)