Skip to content

Commit 738c4c6

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 139146a commit 738c4c6

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,38 @@ func Test_criService_podSandboxStats(t *testing.T) {
200200
},
201201
expectError: false,
202202
},
203+
{
204+
desc: "pod sandbox with empty stats still works (hostprocess container scenario)",
205+
metrics: map[string]*wstats.Statistics{
206+
"c1": {
207+
Container: windowsStat(currentStatsTimestamp, 400, 20),
208+
},
209+
"s1": {},
210+
},
211+
sandbox: sandboxPod("s1", initialStatsTimestamp, 200),
212+
containers: []containerstore.Container{
213+
{
214+
Metadata: containerstore.Metadata{ID: "c1"},
215+
Stats: &stats.ContainerStats{
216+
Timestamp: initialStatsTimestamp,
217+
UsageCoreNanoSeconds: 200,
218+
},
219+
},
220+
},
221+
expectedPodStats: expectedStats{
222+
UsageCoreNanoSeconds: 400,
223+
UsageNanoCores: 200,
224+
WorkingSetBytes: 20,
225+
},
226+
expectedContainerStats: []expectedStats{
227+
{
228+
UsageCoreNanoSeconds: 400,
229+
UsageNanoCores: 200,
230+
WorkingSetBytes: 20,
231+
},
232+
},
233+
expectError: false,
234+
},
203235
} {
204236
test := test
205237
t.Run(test.desc, func(t *testing.T) {

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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,38 @@ func Test_criService_podSandboxStats(t *testing.T) {
200200
},
201201
expectError: false,
202202
},
203+
{
204+
desc: "pod sandbox with empty stats still works (hostprocess container scenario)",
205+
metrics: map[string]*wstats.Statistics{
206+
"c1": {
207+
Container: windowsStat(currentStatsTimestamp, 400, 20),
208+
},
209+
"s1": {},
210+
},
211+
sandbox: sandboxPod("s1", initialStatsTimestamp, 200),
212+
containers: []containerstore.Container{
213+
{
214+
Metadata: containerstore.Metadata{ID: "c1"},
215+
Stats: &stats.ContainerStats{
216+
Timestamp: initialStatsTimestamp,
217+
UsageCoreNanoSeconds: 200,
218+
},
219+
},
220+
},
221+
expectedPodStats: expectedStats{
222+
UsageCoreNanoSeconds: 400,
223+
UsageNanoCores: 200,
224+
WorkingSetBytes: 20,
225+
},
226+
expectedContainerStats: []expectedStats{
227+
{
228+
UsageCoreNanoSeconds: 400,
229+
UsageNanoCores: 200,
230+
WorkingSetBytes: 20,
231+
},
232+
},
233+
expectError: false,
234+
},
203235
} {
204236
test := test
205237
t.Run(test.desc, func(t *testing.T) {

0 commit comments

Comments
 (0)