fix(hostmetrics): add checks for incomplete pod and container stats (#24551)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2026-03-25 10:31:37 +08:00
committed by GitHub
co-authored by Qiu Jian
parent 304b2e334c
commit 8673c046a9
3 changed files with 17 additions and 1 deletions
+5 -1
View File
@@ -1320,7 +1320,11 @@ func (s *sPodGuestInstance) getContainerCRIId(ctrId string) (string, error) {
func (s *sPodGuestInstance) GetContainerByCRIId(criId string) (*hostapi.ContainerDesc, error) {
for _, ctr := range s.containers {
if ctr.CRIId == criId {
return s.GetContainerById(ctr.Id), nil
desc := s.GetContainerById(ctr.Id)
if desc == nil {
return nil, errors.Wrapf(errors.ErrNotFound, "Not found container desc by CRIId %s", criId)
}
return desc, nil
}
}
return nil, errors.Wrapf(errors.ErrNotFound, "Not found container by CRIId %s", criId)
+4
View File
@@ -306,6 +306,10 @@ func (m *SCpuFreqRealTimeSimulateManager) startSetCpuFreqSimulate() {
log.Errorf("failed get %s ctrid by criId %s", pod.GetName(), criIds[i])
continue
}
if ctr == nil {
log.Errorf("found nil container desc by criId %s in pod %s", criIds[i], pod.GetName())
continue
}
cpuDir := pod.getContainerSystemCpusDir(ctr.Id)
if !fileutils2.Exists(cpuDir) {
log.Debugf("%s %s has no cpuDir", pod.GetName(), criIds[i])
@@ -728,6 +728,10 @@ func isPodContainerStopped(prevUsage *GuestMetrics, stat *stats.PodStats) bool {
func (m *SGuestMonitor) PodMetrics(prevUsage *GuestMetrics) *PodMetrics {
stat := m.podStat
if stat == nil || stat.CPU == nil || stat.CPU.UsageCoreNanoSeconds == nil || stat.Memory == nil || stat.Memory.WorkingSetBytes == nil || stat.Memory.UsageBytes == nil {
log.Warningf("skip pod metrics for %s(%s): incomplete pod stats", m.Name, m.Id)
return nil
}
curTime := stat.CPU.Time.Time
podCpu := &PodCpuMetric{
PodMetricMeta: NewPodMetricMeta(curTime),
@@ -748,6 +752,10 @@ func (m *SGuestMonitor) PodMetrics(prevUsage *GuestMetrics) *PodMetrics {
containers := make([]*ContainerMetrics, 0)
for _, ctr := range stat.Containers {
if ctr.CPU == nil || ctr.CPU.UsageCoreNanoSeconds == nil || ctr.Memory == nil || ctr.Memory.WorkingSetBytes == nil || ctr.Memory.UsageBytes == nil {
log.Warningf("skip incomplete container stats in pod %s(%s), container=%s", m.Name, m.Id, ctr.Name)
continue
}
ctrMeta := NewContainerMetricMeta(m.Id, "", ctr.Name, ctr.CPU.Time.Time)
cm := &ContainerMetrics{
ContainerCpu: &ContainerCpuMetric{