mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Hold error in framework's Status
To allow obtaining and comparing the original error. Signed-off-by: Aldo Culquicondor <acondor@google.com> Change-Id: Ibcef89f7b876a273ecc24548f8d204966e0e6059
This commit is contained in:
parent
44ecd80cf1
commit
34102140e2
@ -21,6 +21,7 @@ package v1alpha1
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -92,12 +93,14 @@ const (
|
|||||||
MaxTotalScore int64 = math.MaxInt64
|
MaxTotalScore int64 = math.MaxInt64
|
||||||
)
|
)
|
||||||
|
|
||||||
// Status indicates the result of running a plugin. It consists of a code and a
|
// Status indicates the result of running a plugin. It consists of a code, a
|
||||||
// message. When the status code is not `Success`, the reasons should explain why.
|
// 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.
|
// NOTE: A nil Status is also considered as Success.
|
||||||
type Status struct {
|
type Status struct {
|
||||||
code Code
|
code Code
|
||||||
reasons []string
|
reasons []string
|
||||||
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Code returns code of the Status.
|
// Code returns code of the Status.
|
||||||
@ -143,15 +146,31 @@ func (s *Status) AsError() error {
|
|||||||
if s.IsSuccess() {
|
if s.IsSuccess() {
|
||||||
return nil
|
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.
|
// NewStatus makes a Status out of the given arguments and returns its pointer.
|
||||||
func NewStatus(code Code, reasons ...string) *Status {
|
func NewStatus(code Code, reasons ...string) *Status {
|
||||||
return &Status{
|
s := &Status{
|
||||||
code: code,
|
code: code,
|
||||||
reasons: reasons,
|
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
|
// 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)
|
finalStatus := NewStatus(Success)
|
||||||
var hasError, hasUnschedulableAndUnresolvable, hasUnschedulable bool
|
var hasUnschedulableAndUnresolvable, hasUnschedulable bool
|
||||||
for _, s := range p {
|
for _, s := range p {
|
||||||
if s.Code() == Error {
|
if s.Code() == Error {
|
||||||
hasError = true
|
finalStatus.err = s.AsError()
|
||||||
} else if s.Code() == UnschedulableAndUnresolvable {
|
} else if s.Code() == UnschedulableAndUnresolvable {
|
||||||
hasUnschedulableAndUnresolvable = true
|
hasUnschedulableAndUnresolvable = true
|
||||||
} else if s.Code() == Unschedulable {
|
} else if s.Code() == Unschedulable {
|
||||||
@ -181,7 +200,7 @@ func (p PluginToStatus) Merge() *Status {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if hasError {
|
if finalStatus.err != nil {
|
||||||
finalStatus.code = Error
|
finalStatus.code = Error
|
||||||
} else if hasUnschedulableAndUnresolvable {
|
} else if hasUnschedulableAndUnresolvable {
|
||||||
finalStatus.code = UnschedulableAndUnresolvable
|
finalStatus.code = UnschedulableAndUnresolvable
|
||||||
|
Loading…
Reference in New Issue
Block a user