Skip to content

Commit 89415fe

Browse files
committed
Fix issue for HPC pod metrics
The initial PR had a check for nil metrics but after some refactoring in the PR the test case that was suppose cover HPC was missing a scenario where the metric was not nil but didn't contain any metrics. This fixes that case and adds a testcase to cover it. Signed-off-by: James Sturtevant <[email protected]>
1 parent 0aa82d1 commit 89415fe

4 files changed

Lines changed: 68 additions & 2 deletions

File tree

pkg/cri/sbserver/sandbox_stats_windows.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ func (c *criService) listWindowsMetricsForSandbox(ctx context.Context, sandbox s
267267

268268
func (c *criService) convertToCRIStats(stats *wstats.Statistics) (*runtime.WindowsContainerStats, error) {
269269
var cs runtime.WindowsContainerStats
270-
if stats != nil {
270+
// the metric should exist but stats or stats.container will be nil for HostProcess sandbox containers
271+
// this can also be the case when the container has not started yet
272+
if stats != nil && stats.Container != nil {
271273
wstats := stats.GetWindows()
272274
if wstats == nil {
273275
return nil, fmt.Errorf("windows stats is empty")

pkg/cri/sbserver/sandbox_stats_windows_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,37 @@ func Test_criService_podSandboxStats(t *testing.T) {
192192
},
193193
expectError: false,
194194
},
195+
"pod sandbox with empty stats still works (hostprocess container scenario)": {
196+
metrics: map[string]*wstats.Statistics{
197+
"c1": {
198+
Container: windowsStat(currentStatsTimestamp, 400, 20),
199+
},
200+
"s1": {},
201+
},
202+
sandbox: sandboxPod("s1", initialStatsTimestamp, 200),
203+
containers: []containerstore.Container{
204+
{
205+
Metadata: containerstore.Metadata{ID: "c1"},
206+
Stats: &stats.ContainerStats{
207+
Timestamp: initialStatsTimestamp,
208+
UsageCoreNanoSeconds: 200,
209+
},
210+
},
211+
},
212+
expectedPodStats: expectedStats{
213+
UsageCoreNanoSeconds: 400,
214+
UsageNanoCores: 200,
215+
WorkingSetBytes: 20,
216+
},
217+
expectedContainerStats: []expectedStats{
218+
{
219+
UsageCoreNanoSeconds: 400,
220+
UsageNanoCores: 200,
221+
WorkingSetBytes: 20,
222+
},
223+
},
224+
expectError: false,
225+
},
195226
} {
196227
t.Run(desc, func(t *testing.T) {
197228
actualPodStats, actualContainerStats, err := c.toPodSandboxStats(test.sandbox, test.metrics, test.containers, currentStatsTimestamp)

pkg/cri/server/sandbox_stats_windows.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ func (c *criService) listWindowsMetricsForSandbox(ctx context.Context, sandbox s
266266

267267
func (c *criService) convertToCRIStats(stats *wstats.Statistics) (*runtime.WindowsContainerStats, error) {
268268
var cs runtime.WindowsContainerStats
269-
if stats != nil {
269+
// the metric should exist but stats or stats.container will be nil for HostProcess sandbox containers
270+
// this can also be the case when the container has not started yet
271+
if stats != nil && stats.Container != nil {
270272
wstats := stats.GetWindows()
271273
if wstats == nil {
272274
return nil, fmt.Errorf("windows stats is empty")

pkg/cri/server/sandbox_stats_windows_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,37 @@ func Test_criService_podSandboxStats(t *testing.T) {
192192
},
193193
expectError: false,
194194
},
195+
"pod sandbox with empty stats still works (hostprocess container scenario)": {
196+
metrics: map[string]*wstats.Statistics{
197+
"c1": {
198+
Container: windowsStat(currentStatsTimestamp, 400, 20),
199+
},
200+
"s1": {},
201+
},
202+
sandbox: sandboxPod("s1", initialStatsTimestamp, 200),
203+
containers: []containerstore.Container{
204+
{
205+
Metadata: containerstore.Metadata{ID: "c1"},
206+
Stats: &stats.ContainerStats{
207+
Timestamp: initialStatsTimestamp,
208+
UsageCoreNanoSeconds: 200,
209+
},
210+
},
211+
},
212+
expectedPodStats: expectedStats{
213+
UsageCoreNanoSeconds: 400,
214+
UsageNanoCores: 200,
215+
WorkingSetBytes: 20,
216+
},
217+
expectedContainerStats: []expectedStats{
218+
{
219+
UsageCoreNanoSeconds: 400,
220+
UsageNanoCores: 200,
221+
WorkingSetBytes: 20,
222+
},
223+
},
224+
expectError: false,
225+
},
195226
} {
196227
t.Run(desc, func(t *testing.T) {
197228
actualPodStats, actualContainerStats, err := c.toPodSandboxStats(test.sandbox, test.metrics, test.containers, currentStatsTimestamp)

0 commit comments

Comments
 (0)