Fix pod and container level swap metrics for CRI

This commit is contained in:
Yuan Wang
2025-06-03 02:15:06 +00:00
parent fd53f7292c
commit c5f061e0df
2 changed files with 17 additions and 1 deletions

View File

@@ -343,6 +343,7 @@ func (p *criStatsProvider) ListPodCPUAndMemoryStats(ctx context.Context) ([]stat
// Fill available CPU and memory stats for full set of required pod stats
cs := p.makeContainerCPUAndMemoryStats(stats, container)
p.addPodCPUMemoryStats(ps, types.UID(podSandbox.Metadata.Uid), allInfos, cs)
p.addSwapStats(ps, types.UID(podSandbox.Metadata.Uid), allInfos, cs)
// If cadvisor stats is available for the container, use it to populate
// container stats
@@ -743,6 +744,11 @@ func (p *criStatsProvider) makeContainerCPUAndMemoryStats(
StartTime: metav1.NewTime(time.Unix(0, container.CreatedAt)),
CPU: &statsapi.CPUStats{},
Memory: &statsapi.MemoryStats{},
Swap: &statsapi.SwapStats{
Time: metav1.NewTime(time.Unix(0, time.Now().UnixNano())),
SwapUsageBytes: uint64Ptr(0),
SwapAvailableBytes: uint64Ptr(0),
},
// UserDefinedMetrics is not supported by CRI.
}
if stats.Cpu != nil {
@@ -771,6 +777,15 @@ func (p *criStatsProvider) makeContainerCPUAndMemoryStats(
result.Memory.Time = metav1.NewTime(time.Unix(0, time.Now().UnixNano()))
result.Memory.WorkingSetBytes = uint64Ptr(0)
}
if stats.Swap != nil {
result.Swap.Time = metav1.NewTime(time.Unix(0, stats.Swap.Timestamp))
if stats.Swap.SwapUsageBytes != nil {
result.Swap.SwapUsageBytes = &stats.Swap.SwapUsageBytes.Value
}
if stats.Swap.SwapAvailableBytes != nil {
result.Swap.SwapAvailableBytes = &stats.Swap.SwapAvailableBytes.Value
}
}
return result
}

View File

@@ -61,7 +61,8 @@ var _ = SIGDescribe("ResourceMetricsAPI", feature.ResourceMetrics, func() {
keys := []string{
"resource_scrape_error", "node_cpu_usage_seconds_total", "node_memory_working_set_bytes",
"pod_cpu_usage_seconds_total", "pod_memory_working_set_bytes",
"pod_cpu_usage_seconds_total", "pod_memory_working_set_bytes", "node_swap_usage_bytes",
"container_swap_usage_bytes", "pod_swap_usage_bytes",
}
// NOTE: This check should be removed when ListMetricDescriptors is implemented