diff --git a/pkg/hostman/guestman/pod.go b/pkg/hostman/guestman/pod.go index 05f3256480..018132ac4c 100644 --- a/pkg/hostman/guestman/pod.go +++ b/pkg/hostman/guestman/pod.go @@ -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) diff --git a/pkg/hostman/guestman/pod_helper.go b/pkg/hostman/guestman/pod_helper.go index 40abd88c02..0d6612b9d9 100644 --- a/pkg/hostman/guestman/pod_helper.go +++ b/pkg/hostman/guestman/pod_helper.go @@ -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]) diff --git a/pkg/hostman/hostmetrics/container_metrics.go b/pkg/hostman/hostmetrics/container_metrics.go index 45e5eaa9f8..8af182678b 100644 --- a/pkg/hostman/hostmetrics/container_metrics.go +++ b/pkg/hostman/hostmetrics/container_metrics.go @@ -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{