def get_asi_node_info_from_k8s(self, asi_apiclient): # asi_apiclient = config.new_client_from_config(asi_config) v1 = client.CoreV1Api(asi_apiclient) try: api_response = v1.list_node(watch=False) except ApiException as e: print("Exception when calling CoreV1Api->list_node: %s\n" % e) asi_node_info = {} for item in api_response.items: # asi_node_info[] # pprint(item.metadata.name) asi_node_info[item.metadata.name] = {} item_info = { item.metadata.name: { 'memoryGB': self.Byte_to_GB( parse_quantity(item.status.capacity['memory'])), 'cpu': int(parse_quantity(item.status.capacity['cpu'])), } } ## node status: Ready or Not Ready ## node unschedulable : True or False item_info['status'] = 'Ready' item_info['unschedulable'] = False for c in item.status.conditions: if c.type == 'Ready' and c.status != 'True': item_info['status'] = 'Not Ready' if item.spec.unschedulable: item_info['unschedulable'] = True asi_node_info[item.metadata.name] = item_info return asi_node_info
def get_requested_resources(pod: V1Pod) -> Resources: r = pod.spec.containers[0].resources.requests disk_str = r.get(SPEC_RESOURCE_DISK_KEY, '') if disk_str == '': disk_str = r[SPEC_RESOURCE_DISK_DEPRECATED_KEY] gpu = 0 gpu_str = r.get(SPEC_RESOURCE_GPU_KEY, '') if gpu_str != '': gpu = int(parse_quantity(gpu_str)) return Resources(int(parse_quantity(r[SPEC_RESOURCE_CPU_KEY])), int(parse_quantity(r[SPEC_RESOURCE_MEM_KEY])), int(parse_quantity(disk_str)), int(parse_quantity(r[SPEC_RESOURCE_NET_KEY])), gpu)
def convert_to_float(value): """ Convert an input value which could be a quantity to float. """ if value is None: return None if isinstance(value, float): return value return float(parse_quantity(value))
def main(): apiclient = config.new_client_from_config("/tmp/pre_kube_config") crd_api = client.CustomObjectsApi(apiclient) limit = 9999999 response = crd_api.list_cluster_custom_object( group='core.alibabacloud.com', version='v1', plural='quotas', limit=limit) for quota in response['items']: name = quota['metadata']['name'] # 正则匹配 pay-as-you-go-[a-z]+ if re.match(r'pay-as-you-go-[a-z]+', name, re.M | re.I): pprint(quota) print(name) print(parse_quantity(quota['spec']['admission']['cpu']))
def get_asi_pod_info_from_k8s(self, asi_apiclient): # asi_apiclient = config.new_client_from_config(asi_config) v1 = client.CoreV1Api(asi_apiclient) try: ## 该方式在pod数量较多的时候会比较慢,可以添加参数去除预处理。 _preload_content=False ## 然后在进行指定编码的解析,可以加快速度 ## response = json.loads(api_response.read().decode('utf-8')) api_response = v1.list_pod_for_all_namespaces(watch=False) except ApiException as e: print("Exception when calling CoreV1Api->list_node: %s\n" % e) pod_eni_info = {} for i in api_response.items: # pprint(i) if i.spec.node_name not in pod_eni_info: pod_eni_info[i.spec.node_name] = {} pod_eni_info[i.spec.node_name].update( {i.metadata.name: { 'memory': 0, 'cpu': 0 }}) for c in i.spec.containers: if c.resources.requests and ('memory' in c.resources.requests): pod_eni_info[i.spec.node_name][ i.metadata.name]['memory'] += int( self.Byte_to_GB( parse_quantity( c.resources.requests['memory']))) if c.resources.requests and ('cpu' in c.resources.requests): pod_eni_info[i.spec.node_name][ i.metadata.name]['cpu'] += cpu_core_to_float( c.resources.requests['cpu']) ##pprint(pod_eni_info[i.spec.node_name]) asi_pod_info = pod_eni_info return asi_pod_info
def validate(cls, v: Union[str, float]): quantity = parse_quantity(v) return cls(quantity) # noqa
def __new__( cls, quantity: Union[str, float, Decimal] = 0) -> "ResourceQuantity": quantity = parse_quantity(quantity) return super().__new__(cls, quantity) # noqa