def delete_hpa(request, project_id, cluster_id, ns_name, namespace_id, name): if request.project.kind == ProjectKind.K8S.value: cluster_auth = ClusterAuth(request.user.token.access_token, project_id, cluster_id) client = hpa_client.HPA(cluster_auth) 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 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: cluster_auth = ClusterAuth(request.user.token.access_token, project_id, cluster_id) client = hpa_client.HPA(cluster_auth) 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 client(self, project_id, cluster_id): try: client = hpa_client.HPA( ClusterAuth('token', project_id, cluster_id)) except ResourceNotFoundError: pytest.skip('Can not initialize HPA client, skip') return client
def get_custom_object(self, request, project_id, cluster_id, crd_name, name): cobj_client = get_cobj_client_by_crd( ClusterAuth(request.user.token.access_token, project_id, cluster_id), crd_name) cobj_dict = cobj_client.get( namespace=request.query_params.get("namespace"), name=name) return Response(cobj_dict)
def list_custom_objects(self, request, project_id, cluster_id, crd_name): cluster_auth = ClusterAuth(request.user.token.access_token, project_id, cluster_id) crd_client = CustomResourceDefinition(cluster_auth) crd = crd_client.get(name=crd_name, is_format=False) if not crd: raise error_codes.ResNotFoundError(_("集群({})中未注册自定义资源({})").format(cluster_id, crd_name)) cobj_client = get_cobj_client_by_crd(cluster_auth, crd_name) cobj_list = cobj_client.list(namespace=request.query_params.get("namespace")) return Response(to_table_format(crd.to_dict(), cobj_list, cluster_id=cluster_id))
def handler_k8shpa(self, ns, cluster_id, spec): """下发HPA配置""" cluster_auth = ClusterAuth(self.access_token, self.project_id, cluster_id) client = hpa_client.HPA(cluster_auth) 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 patch_custom_object(self, request, project_id, cluster_id, crd_name, name): serializer = PatchCustomObjectSLZ(data=request.data) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data cobj_client = get_cobj_client_by_crd( ClusterAuth(request.user.token.access_token, project_id, cluster_id), crd_name ) cobj_client.patch( name=name, namespace=validated_data.get("namespace"), body=validated_data["body"], content_type=validated_data["patch_type"], ) return Response()
def batch_delete_custom_objects(self, request, project_id, cluster_id, crd_name): serializer = BatchDeleteCustomObjectsSLZ(data=request.data) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data cobj_client = get_cobj_client_by_crd( ClusterAuth(request.user.token.access_token, project_id, cluster_id), crd_name ) failed_list = [] namespace = validated_data["namespace"] for name in validated_data["cobj_name_list"]: try: cobj_client.delete_ignore_nonexistent(namespace=namespace, name=name) except Exception: failed_list.append(name) if failed_list: raise error_codes.APIError(_("部分资源删除失败,失败列表: {}").format(",".join(failed_list))) return Response()
def list(self, request, project_id, cluster_id): crd_client = CustomResourceDefinition(ClusterAuth(request.user.token.access_token, project_id, cluster_id)) return Response(crd_client.list())
def client(self, project_id, cluster_id): client = hpa_client.HPA(ClusterAuth('token', project_id, cluster_id)) return client
def cobj_client(self, project_id, cluster_id): return get_cobj_client_by_crd( ClusterAuth('token', project_id, cluster_id), crd_name=getitems(sample_crd, "metadata.name") )
def crd_client(self, project_id, cluster_id): return CustomResourceDefinition( ClusterAuth('token', project_id, cluster_id), api_version=sample_crd["apiVersion"] )