Skip to content

Commit 4be4e51

Browse files
ymichaelsonk8s-infra-cherrypick-robot
authored andcommitted
Fix nil pointer dereference in container spec memory metrics
Add nil checks for Memory.Limit, Memory.Reservation, and Memory.Swap before dereferencing them in extractContainerSpecMetrics. These fields are optional pointers in OCI runtime spec and can be nil when not set. - Memory.Limit: Set from Pod resources.limits.memory - Memory.Reservation: Only set via NRI plugins (rarely used) - Memory.Swap: Set from memory_swap_limit_in_bytes (optional) This prevents potential panic when collecting metrics from containers that don't have all memory limits configured, and makes the code consistent with CPU resource handling and helpers.go. Signed-off-by: You Binhao <[email protected]>
1 parent 683ccda commit 4be4e51

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

internal/cri/server/list_pod_sandbox_metrics_linux.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -991,48 +991,54 @@ func (c *criService) extractContainerSpecMetrics(ctx context.Context, containerI
991991
}
992992

993993
if cpuResource := resource.CPU; cpuResource != nil {
994-
metrics = append(metrics, []*runtime.Metric{
995-
{
994+
if cpuResource.Period != nil {
995+
metrics = append(metrics, &runtime.Metric{
996996
Name: "container_spec_cpu_period",
997997
Timestamp: timestamp,
998998
MetricType: runtime.MetricType_GAUGE,
999999
LabelValues: labels,
10001000
Value: &runtime.UInt64Value{Value: *cpuResource.Period},
1001-
},
1002-
{
1001+
})
1002+
}
1003+
if cpuResource.Shares != nil {
1004+
metrics = append(metrics, &runtime.Metric{
10031005
Name: "container_spec_cpu_shares",
10041006
Timestamp: timestamp,
10051007
MetricType: runtime.MetricType_GAUGE,
10061008
LabelValues: labels,
10071009
Value: &runtime.UInt64Value{Value: *cpuResource.Shares},
1008-
},
1009-
}...)
1010+
})
1011+
}
10101012
}
10111013

10121014
if memoryResource := resource.Memory; memoryResource != nil {
1013-
metrics = append(metrics, []*runtime.Metric{
1014-
{
1015+
if memoryResource.Limit != nil {
1016+
metrics = append(metrics, &runtime.Metric{
10151017
Name: "container_spec_memory_limit_bytes",
10161018
Timestamp: timestamp,
10171019
MetricType: runtime.MetricType_GAUGE,
10181020
LabelValues: labels,
10191021
Value: &runtime.UInt64Value{Value: uint64(*memoryResource.Limit)},
1020-
},
1021-
{
1022+
})
1023+
}
1024+
if memoryResource.Reservation != nil {
1025+
metrics = append(metrics, &runtime.Metric{
10221026
Name: "container_spec_memory_reservation_limit_bytes",
10231027
Timestamp: timestamp,
10241028
MetricType: runtime.MetricType_GAUGE,
10251029
LabelValues: labels,
10261030
Value: &runtime.UInt64Value{Value: uint64(*memoryResource.Reservation)},
1027-
},
1028-
{
1031+
})
1032+
}
1033+
if memoryResource.Swap != nil {
1034+
metrics = append(metrics, &runtime.Metric{
10291035
Name: "container_spec_memory_swap_limit_bytes",
10301036
Timestamp: timestamp,
10311037
MetricType: runtime.MetricType_GAUGE,
10321038
LabelValues: labels,
10331039
Value: &runtime.UInt64Value{Value: uint64(*memoryResource.Swap)},
1034-
},
1035-
}...)
1040+
})
1041+
}
10361042
}
10371043
return metrics, nil
10381044
}

0 commit comments

Comments
 (0)