Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix]: Fix null pointer de-reference when parsing statistics from cAdvisor #83

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions pkg/wasp/pod-ranker/rank_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pod_ranker

import (
"github.com/openshift-virtualization/wasp-agent/pkg/log"
stats_collector "github.com/openshift-virtualization/wasp-agent/pkg/wasp/stats-collector"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -134,7 +135,23 @@ func exceedMemoryLimits(summary summaryFunc) cmpFunc {
func hasContainerExceedMemoryLimits(pod *v1.Pod, summary stats_collector.PodSummary) bool {
for _, container := range pod.Spec.Containers {
if rQuantity, ok := container.Resources.Limits[v1.ResourceMemory]; ok {
memoryAndSwapSumQuantity := memoryAndSwapUsage(*summary.Containers[container.Name].MemoryWorkingSetBytes, *summary.Containers[container.Name].MemorySwapCurrentBytes)
containerStats, exists := summary.Containers[container.Name]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Barakmor1 I could avoid code duplication here by appending the regular containers and init containers into a single list, but I prefer to do it in a follow-up, especially that we lack tests to protect us from re-factoring

if !exists {
continue
}

if containerStats.MemoryWorkingSetBytes == nil {
log.Log.Errorf("container %s - MemoryWorkingSetBytes is nil", container.Name)
continue
}
workingSet := *containerStats.MemoryWorkingSetBytes

if containerStats.MemorySwapCurrentBytes == nil {
log.Log.Errorf("container %s - MemorySwapCurrentBytes is nil", container.Name)
continue
}
swapUsage := *containerStats.MemorySwapCurrentBytes
memoryAndSwapSumQuantity := memoryAndSwapUsage(workingSet, swapUsage)
if memoryAndSwapSumQuantity.Cmp(rQuantity) == 1 {
return true
}
Expand All @@ -143,7 +160,22 @@ func hasContainerExceedMemoryLimits(pod *v1.Pod, summary stats_collector.PodSumm

for _, container := range pod.Spec.InitContainers {
if rQuantity, ok := container.Resources.Limits[v1.ResourceMemory]; ok {
memoryAndSwapSumQuantity := memoryAndSwapUsage(*summary.Containers[container.Name].MemoryWorkingSetBytes, *summary.Containers[container.Name].MemorySwapCurrentBytes)
containerStats, exists := summary.Containers[container.Name]
if !exists {
continue
}
if containerStats.MemoryWorkingSetBytes == nil {
log.Log.Errorf("init container %s - MemoryWorkingSetBytes is nil", container.Name)
continue
}
workingSet := *containerStats.MemoryWorkingSetBytes

if containerStats.MemorySwapCurrentBytes == nil {
log.Log.Errorf("init container %s - MemorySwapCurrentBytes is nil", container.Name)
continue
}
swapUsage := *containerStats.MemorySwapCurrentBytes
memoryAndSwapSumQuantity := memoryAndSwapUsage(workingSet, swapUsage)
if memoryAndSwapSumQuantity.Cmp(rQuantity) == 1 {
return true
}
Expand Down