From 29cd4e3af108ec421aaad02774c1ca1c21811bea Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Thu, 20 Jul 2023 11:51:40 +0800 Subject: [PATCH] fix(monitor): return error if metric is nil (#17575) --- pkg/monitor/controller/balancer/balancer.go | 13 ++++++++++++- pkg/monitor/controller/balancer/cpu.go | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/monitor/controller/balancer/balancer.go b/pkg/monitor/controller/balancer/balancer.go index 4e7d664948..c42fb324b9 100644 --- a/pkg/monitor/controller/balancer/balancer.go +++ b/pkg/monitor/controller/balancer/balancer.go @@ -266,6 +266,8 @@ func findGuestsOfHost(drv IMetricDriver, host IHost, ds *tsdb.DataSource, ms *mo } ret := make([]ICandidate, 0) + found := false + errs := []error{} for _, obj := range objs { gHostId, err := obj.GetString("host_id") if err != nil { @@ -291,15 +293,24 @@ func findGuestsOfHost(drv IMetricDriver, host IHost, ds *tsdb.DataSource, ms *mo } c, err := drv.GetCandidate(obj, host, ds) if err != nil { - return nil, errors.Wrapf(err, "drv.GetCandidate of guest %s", obj) + errs = append(errs, errors.Wrapf(err, "drv.GetCandidate of guest %s", obj)) + continue + } if c.GetScore() == 0 { log.Debugf("ignore guest %s cause %s score is 0", c.GetName(), drv.GetType()) continue } ret = append(ret, c) + found = true } } + if !found { + return nil, errors.NewAggregate(errs) + } + if len(errs) != 0 { + log.Warningf("not all guests found: %s", errors.NewAggregate(errs)) + } return ret, nil } diff --git a/pkg/monitor/controller/balancer/cpu.go b/pkg/monitor/controller/balancer/cpu.go index b732bbc889..c1cfaf1d86 100644 --- a/pkg/monitor/controller/balancer/cpu.go +++ b/pkg/monitor/controller/balancer/cpu.go @@ -127,6 +127,9 @@ func newCPUCandidate(gst jsonutils.JSONObject, host *HostResource, ds *tsdb.Data } metric := metrics.Get(res.GetId()) + if metric == nil { + return nil, errors.Errorf("not found resource %q metric from %#v", res.GetId(), metrics.indexes) + } usage := metric.Values["usage_active"] return &cpuCandidate{ guestResource: res,