Repository navigation
cri: route sandbox stats through Controller.Metrics - #13312
Conversation
There was a problem hiding this comment.
Pull request overview
This PR moves pod sandbox cgroup metrics collection out of the CRI server layer and into the pod sandbox Controller.Metrics implementation, leaving the CRI layer responsible for decoding and shaping the CRI PodSandboxStats response.
Changes:
- Update CRI sandbox stats path to call
SandboxController(...).Metrics(...)and decode returnedtypes.Metricdata via typeurl. - Implement Linux
podsandbox.Controller.Metricsto read parent cgroup CPU/memory stats and return them as a*types.Metric. - Simplify
cpuContainerStats/memoryContainerStatshelpers to take onlycgroupMetrics+ timestamp, updating call sites and tests.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/cri/server/sandbox_stats_linux.go | Switch sandbox stats to fetch metrics via controller and decode them into cgroupMetrics. |
| internal/cri/server/podsandbox/sandbox_stats_linux.go | New Linux implementation of Controller.Metrics that reads cgroup v1/v2 stats and marshals them into types.Metric. |
| internal/cri/server/podsandbox/sandbox_stats_other.go | Non-Linux stub for Controller.Metrics guarded by //go:build !linux. |
| internal/cri/server/container_stats_list.go | Update stats helpers’ signatures and adjust container metrics path accordingly. |
| internal/cri/server/container_stats_list_test.go | Update tests to match the updated memoryContainerStats signature. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Maksym Pavlenko <[email protected]>
Signed-off-by: Maksym Pavlenko <[email protected]>
Signed-off-by: Maksym Pavlenko <[email protected]>
| // Sample at the metric's own timestamp, captured by the controller at | ||
| // cgroup read time. This keeps the delta-time in the UsageNanoCores | ||
| // rate accurate and free of per-sandbox collection latency. | ||
| c.addSample(sb.ID, metric.GetTimestamp().AsTime(), usageCoreNanoSeconds) |
There was a problem hiding this comment.
metric.GetTimestamp().AsTime() is correct for podsandbox (it sets time.Now()), but a controller that leaves Timestamp nil yields the Unix epoch here rather than being skipped, feeding a garbage delta into the UsageNanoCores rate. The same latent issue exists on the container path, but the sandbox path now inherits it via arbitrary controllers. Worth guarding with a zero-timestamp check (skip, or fall back to time.Now()).
Review generated from Claude Opus 4.8.
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get controller for sandbox %s: %w", sandbox.ID, err) | ||
| } | ||
| metric, err := ctrl.Metrics(ctx, sandbox.ID) |
There was a problem hiding this comment.
Routing through ctrl.Metrics here changes the failure surface of ListPodSandboxStats. Previously this path read the parent cgroup directly and could only fail with cgroup load/read errors; now any controller that doesn't implement metrics returns errdefs.ErrNotImplemented (gRPC Unimplemented, not Unavailable).
In sandbox_stats_list.go only Unavailable/NotFound/ttrpc.ErrClosed are tolerated — everything else goes into errs[i] and errors.Join fails the whole RPC. So on a node mixing the default podsandbox sandboxer with a shim/remote sandboxer whose controller lacks SandboxMetrics, a single such pod breaks stats for every pod on the node.
The background collector (collectSandboxStats) already tolerates this — logs at debug and continues. The synchronous path should be similarly forgiving, e.g. also skip on errdefs.IsNotImplemented(err).
Review generated from Claude Opus 4.8.
There was a problem hiding this comment.
This is probably valid, but also quite exotic edge case today.
Will follow up as I plan more work around sandbox APIs.
Metricsendpoint inpodsandbox/is noop, everything is happening in CRI layer. This PR movecgroupsmetrics queries behind Controller, leaving CRI decoding logic.