def get_cluster_hpa_list(request, project_id, cluster_id, cluster_env, cluster_name, namespace=None): """获取基础hpa列表""" access_token = request.user.token.access_token project_code = request.project.english_name hpa_list = [] try: if request.project.kind == ProjectKind.MESOS.value: client = mesos.MesosClient(access_token, project_id, cluster_id, env=cluster_env) hpa = client.list_hpa(namespace).get("data") or [] hpa_list = slz_mesos_hpa_info(hpa, project_code, cluster_name, cluster_env, cluster_id) else: ctx_cluster = CtxCluster.create( token=request.user.token.access_token, project_id=project_id, id=cluster_id) client = hpa_client.HPA(ctx_cluster) formatter = HPAFormatter(cluster_id, project_code, cluster_name, cluster_env) hpa_list = client.list(formatter=formatter) except Exception as error: logger.error("get hpa list error, %s", error) return hpa_list
def delete_hpa(request, project_id, cluster_id, ns_name, namespace_id, name): if request.project.kind == ProjectKind.K8S.value: ctx_cluster = CtxCluster.create(token=request.user.token.access_token, project_id=project_id, id=cluster_id) client = hpa_client.HPA(ctx_cluster) try: client.delete_ignore_nonexistent(name=name, namespace=ns_name) except Exception as error: logger.error( "delete hpa error, namespace: %s, name: %s, error: %s", ns_name, name, error) raise hpa_exceptions.DeleteHPAError(_("删除HPA资源失败")) # 删除成功则更新状态 InstanceConfig.objects.filter( namespace=namespace_id, category=K8sResourceName.K8sHPA.value, name=name).update( updator=request.user.username, oper_type=application_constants.DELETE_INSTANCE, deleted_time=timezone.now(), is_deleted=True, is_bcs_success=True, ) else: delete_mesos_hpa(request, project_id, cluster_id, ns_name, namespace_id, name)
def cobj_client(self, project_id, cluster_id): return get_cobj_client_by_crd( CtxCluster.create(token='token', project_id=project_id, id=cluster_id), crd_name=getitems(sample_crd, "metadata.name"), )
def crd_client(self, project_id, cluster_id): return CustomResourceDefinition( CtxCluster.create(token='token', project_id=project_id, id=cluster_id), api_version=sample_crd["apiVersion"], )
def _set_ctx_project_cluster(self, request, project_id: str, cluster_id: str): access_token = request.user.token.access_token request.ctx_project = CtxProject.create(token=access_token, id=project_id) if cluster_id: request.ctx_cluster = CtxCluster.create(token=access_token, id=cluster_id, project_id=project_id) else: request.ctx_cluster = None
def handler_k8shpa(self, ns, cluster_id, spec): """下发HPA配置""" ctx_cluster = CtxCluster.create(token=self.access_token, project_id=self.project_id, id=cluster_id) client = hpa_client.HPA(ctx_cluster) name = spec["metadata"]["name"] spec['apiVersion'] = hpa_client.PREFERRED_API_VERSION try: result = client.update_or_create(spec, name, ns) logger.debug("deploy hpa success, %s", result) except Exception as error: logger.exception('deploy hpa error, %s', error) raise Rollback({})
def client(self, project_id, cluster_id): try: client = hpa_client.HPA(CtxCluster.create(token='token', project_id=project_id, id=cluster_id)) except ResourceNotFoundError: pytest.skip('Can not initialize HPA client, skip') return client