Skip to content

Commit 3410214

Browse files
Hold error in framework's Status
To allow obtaining and comparing the original error. Signed-off-by: Aldo Culquicondor <[email protected]> Change-Id: Ibcef89f7b876a273ecc24548f8d204966e0e6059
1 parent 44ecd80 commit 3410214

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

pkg/scheduler/framework/v1alpha1/interface.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package v1alpha1
2121
import (
2222
"context"
2323
"errors"
24+
"fmt"
2425
"math"
2526
"strings"
2627
"time"
@@ -92,12 +93,14 @@ const (
9293
MaxTotalScore int64 = math.MaxInt64
9394
)
9495

95-
// Status indicates the result of running a plugin. It consists of a code and a
96-
// message. When the status code is not `Success`, the reasons should explain why.
96+
// Status indicates the result of running a plugin. It consists of a code, a
97+
// message and (optionally) an error. When the status code is not `Success`,
98+
// the reasons should explain why.
9799
// NOTE: A nil Status is also considered as Success.
98100
type Status struct {
99101
code Code
100102
reasons []string
103+
err error
101104
}
102105

103106
// Code returns code of the Status.
@@ -143,15 +146,31 @@ func (s *Status) AsError() error {
143146
if s.IsSuccess() {
144147
return nil
145148
}
146-
return errors.New(s.Message())
149+
if s.err != nil {
150+
return s.err
151+
}
152+
return fmt.Errorf("%s: %s", s.code.String(), s.Message())
147153
}
148154

149155
// NewStatus makes a Status out of the given arguments and returns its pointer.
150156
func NewStatus(code Code, reasons ...string) *Status {
151-
return &Status{
157+
s := &Status{
152158
code: code,
153159
reasons: reasons,
154160
}
161+
if code == Error {
162+
s.err = errors.New(s.Message())
163+
}
164+
return s
165+
}
166+
167+
// AsStatus wraps an error in a Status.
168+
func AsStatus(err error) *Status {
169+
return &Status{
170+
code: Error,
171+
reasons: []string{err.Error()},
172+
err: err,
173+
}
155174
}
156175

157176
// PluginToStatus maps plugin name to status. Currently used to identify which Filter plugin
@@ -166,10 +185,10 @@ func (p PluginToStatus) Merge() *Status {
166185
}
167186

168187
finalStatus := NewStatus(Success)
169-
var hasError, hasUnschedulableAndUnresolvable, hasUnschedulable bool
188+
var hasUnschedulableAndUnresolvable, hasUnschedulable bool
170189
for _, s := range p {
171190
if s.Code() == Error {
172-
hasError = true
191+
finalStatus.err = s.AsError()
173192
} else if s.Code() == UnschedulableAndUnresolvable {
174193
hasUnschedulableAndUnresolvable = true
175194
} else if s.Code() == Unschedulable {
@@ -181,7 +200,7 @@ func (p PluginToStatus) Merge() *Status {
181200
}
182201
}
183202

184-
if hasError {
203+
if finalStatus.err != nil {
185204
finalStatus.code = Error
186205
} else if hasUnschedulableAndUnresolvable {
187206
finalStatus.code = UnschedulableAndUnresolvable

0 commit comments

Comments
 (0)