From 34102140e23b1286ecae3b95a3bd3bf9a60d99c6 Mon Sep 17 00:00:00 2001 From: Aldo Culquicondor Date: Wed, 9 Sep 2020 13:45:53 -0400 Subject: [PATCH] Hold error in framework's Status To allow obtaining and comparing the original error. Signed-off-by: Aldo Culquicondor Change-Id: Ibcef89f7b876a273ecc24548f8d204966e0e6059 --- pkg/scheduler/framework/v1alpha1/interface.go | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/pkg/scheduler/framework/v1alpha1/interface.go b/pkg/scheduler/framework/v1alpha1/interface.go index 8c49bf0d60f..4b8b38ee16b 100644 --- a/pkg/scheduler/framework/v1alpha1/interface.go +++ b/pkg/scheduler/framework/v1alpha1/interface.go @@ -21,6 +21,7 @@ package v1alpha1 import ( "context" "errors" + "fmt" "math" "strings" "time" @@ -92,12 +93,14 @@ const ( MaxTotalScore int64 = math.MaxInt64 ) -// Status indicates the result of running a plugin. It consists of a code and a -// message. When the status code is not `Success`, the reasons should explain why. +// Status indicates the result of running a plugin. It consists of a code, a +// message and (optionally) an error. When the status code is not `Success`, +// the reasons should explain why. // NOTE: A nil Status is also considered as Success. type Status struct { code Code reasons []string + err error } // Code returns code of the Status. @@ -143,15 +146,31 @@ func (s *Status) AsError() error { if s.IsSuccess() { return nil } - return errors.New(s.Message()) + if s.err != nil { + return s.err + } + return fmt.Errorf("%s: %s", s.code.String(), s.Message()) } // NewStatus makes a Status out of the given arguments and returns its pointer. func NewStatus(code Code, reasons ...string) *Status { - return &Status{ + s := &Status{ code: code, reasons: reasons, } + if code == Error { + s.err = errors.New(s.Message()) + } + return s +} + +// AsStatus wraps an error in a Status. +func AsStatus(err error) *Status { + return &Status{ + code: Error, + reasons: []string{err.Error()}, + err: err, + } } // PluginToStatus maps plugin name to status. Currently used to identify which Filter plugin @@ -166,10 +185,10 @@ func (p PluginToStatus) Merge() *Status { } finalStatus := NewStatus(Success) - var hasError, hasUnschedulableAndUnresolvable, hasUnschedulable bool + var hasUnschedulableAndUnresolvable, hasUnschedulable bool for _, s := range p { if s.Code() == Error { - hasError = true + finalStatus.err = s.AsError() } else if s.Code() == UnschedulableAndUnresolvable { hasUnschedulableAndUnresolvable = true } else if s.Code() == Unschedulable { @@ -181,7 +200,7 @@ func (p PluginToStatus) Merge() *Status { } } - if hasError { + if finalStatus.err != nil { finalStatus.code = Error } else if hasUnschedulableAndUnresolvable { finalStatus.code = UnschedulableAndUnresolvable