You need to enable JavaScript to run this app. 从含K8s资源推荐YAML的深层嵌套字典中提取统计值方案咨询
优惠活动
大模型
产品
解决方案
定价
更多

从含K8s资源推荐YAML的深层嵌套字典中提取统计值方案咨询

单位标准化工具函数

首先实现两个工具函数,将带单位的CPU、内存值统一转换为可计算的数值:

def parse_cpu(cpu_str):
    """将CPU字符串转换为核数数值,例:812m -> 0.812,2 -> 2"""
    cpu_str = cpu_str.strip()
    if cpu_str.endswith('m'):
        return float(cpu_str[:-1]) / 1000
    return float(cpu_str)

def parse_memory(mem_str):
    """将内存字符串转换为MB数值,例:4506M -> 4506,2G -> 2048"""
    mem_str = mem_str.strip()
    unit = mem_str[-1].upper()
    val = float(mem_str[:-1])
    unit_map = {
        'K': 1/1024,
        'M': 1,
        'G': 1024,
        'T': 1024*1024
    }
    return val * unit_map.get(unit, 1)

转换嵌套字典为结构化DataFrame

把你之前生成的soa_recs嵌套字典展平,生成每行对应一个SOA实例的结构化表:

import pandas as pd
from nested_dict import nested_dict

rows = []
# 遍历嵌套字典的所有层级
for cluster, ns_dict in soa_recs.items():
    for namespace, soa_dict in ns_dict.items():
        for soa_name, res_config in soa_dict.items():
            rows.append({
                "cluster": cluster,
                "namespace": namespace,
                "soa": soa_name,
                "req_cpu_core": parse_cpu(res_config["requests"]["cpu"]),
                "req_mem_mb": parse_memory(res_config["requests"]["memory"]),
                "lim_cpu_core": parse_cpu(res_config["limits"]["cpu"]),
                "lim_mem_mb": parse_memory(res_config["limits"]["memory"])
            })

df = pd.DataFrame(rows)

统计逻辑实现

1. 单集群指定SOA统计

# 替换为你要查询的集群名和SOA名
target_cluster = "cluster-prod-01"
target_soa = "order-service"

single_cluster_stats = df[
    (df["cluster"] == target_cluster) & (df["soa"] == target_soa)
].agg({
    "req_cpu_core": ["min", "max", "median", "mean"],
    "req_mem_mb": ["min", "max", "median", "mean"],
    "lim_cpu_core": ["min", "max", "median", "mean"],
    "lim_mem_mb": ["min", "max", "median", "mean"]
}).round(2)

2. 跨集群指定SOA统计

cross_cluster_stats = df[
    df["soa"] == target_soa
].agg({
    "req_cpu_core": ["min", "max", "median", "mean"],
    "req_mem_mb": ["min", "max", "median", "mean"],
    "lim_cpu_core": ["min", "max", "median", "mean"],
    "lim_mem_mb": ["min", "max", "median", "mean"]
}).round(2)

3. 批量统计所有SOA

# 跨集群所有SOA统计
all_soa_cross_stats = df.groupby("soa").agg({
    "req_cpu_core": ["min", "max", "median", "mean"],
    "req_mem_mb": ["min", "max", "median", "mean"],
    "lim_cpu_core": ["min", "max", "median", "mean"],
    "lim_mem_mb": ["min", "max", "median", "mean"]
}).round(2)

# 单集群所有SOA统计
all_soa_single_cluster_stats = df[
    df["cluster"] == target_cluster
].groupby("soa").agg({
    "req_cpu_core": ["min", "max", "median", "mean"],
    "req_mem_mb": ["min", "max", "median", "mean"],
    "lim_cpu_core": ["min", "max", "median", "mean"],
    "lim_mem_mb": ["min", "max", "median", "mean"]
}).round(2)

补充说明

  • 所有CPU结果单位为核,内存结果单位为MB,需要格式化回原单位的话可以自行做单位转换
  • 聚合函数会自动忽略空值,天然适配部分SOA未部署在部分命名空间的场景
  • 多集群数据可以全部存入同一个DataFrame,不需要拆分存储,统计时按cluster字段过滤即可

内容的提问来源于stack exchange,提问作者Stephan Garland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 18:30:00