Use %w instead of %v to format errors

This commit is contained in:
tanjing2020 2021-02-24 09:57:28 +08:00
parent d2b24a664e
commit 61cd099bc1
12 changed files with 23 additions and 22 deletions

View File

@ -21,9 +21,9 @@ import (
"sync" "sync"
) )
const ( var (
// NotFound is the not found error message. // ErrNotFound is the not found error message.
NotFound = "not found" ErrNotFound = errors.New("not found")
) )
// StateData is a generic type for arbitrary data stored in CycleState. // StateData is a generic type for arbitrary data stored in CycleState.
@ -92,7 +92,7 @@ func (c *CycleState) Read(key StateKey) (StateData, error) {
if v, ok := c.storage[key]; ok { if v, ok := c.storage[key]; ok {
return v, nil return v, nil
} }
return nil, errors.New(NotFound) return nil, ErrNotFound
} }
// Write stores the given "val" in CycleState with the given "key". // Write stores the given "val" in CycleState with the given "key".

View File

@ -295,7 +295,7 @@ func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error
c, err := cycleState.Read(preFilterStateKey) c, err := cycleState.Read(preFilterStateKey)
if err != nil { if err != nil {
// preFilterState doesn't exist, likely PreFilter wasn't invoked. // preFilterState doesn't exist, likely PreFilter wasn't invoked.
return nil, fmt.Errorf("error reading %q from cycleState: %v", preFilterStateKey, err) return nil, fmt.Errorf("error reading %q from cycleState: %w", preFilterStateKey, err)
} }
s, ok := c.(*preFilterState) s, ok := c.(*preFilterState)

View File

@ -18,6 +18,7 @@ package interpodaffinity
import ( import (
"context" "context"
"fmt"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
@ -1766,7 +1767,7 @@ func TestPreFilterDisabled(t *testing.T) {
p := &InterPodAffinity{} p := &InterPodAffinity{}
cycleState := framework.NewCycleState() cycleState := framework.NewCycleState()
gotStatus := p.Filter(context.Background(), cycleState, pod, nodeInfo) gotStatus := p.Filter(context.Background(), cycleState, pod, nodeInfo)
wantStatus := framework.NewStatus(framework.Error, `error reading "PreFilterInterPodAffinity" from cycleState: not found`) wantStatus := framework.AsStatus(fmt.Errorf(`error reading "PreFilterInterPodAffinity" from cycleState: %w`, framework.ErrNotFound))
if !reflect.DeepEqual(gotStatus, wantStatus) { if !reflect.DeepEqual(gotStatus, wantStatus) {
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus) t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)
} }

View File

@ -208,7 +208,7 @@ func (pl *InterPodAffinity) PreScore(
func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) { func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) {
c, err := cycleState.Read(preScoreStateKey) c, err := cycleState.Read(preScoreStateKey)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read %q from cycleState: %v", preScoreStateKey, err) return nil, fmt.Errorf("failed to read %q from cycleState: %w", preScoreStateKey, err)
} }
s, ok := c.(*preScoreState) s, ok := c.(*preScoreState)

View File

@ -195,7 +195,7 @@ func getPodPreferredNodeAffinity(pod *v1.Pod) (*nodeaffinity.PreferredScheduling
func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) { func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) {
c, err := cycleState.Read(preScoreStateKey) c, err := cycleState.Read(preScoreStateKey)
if err != nil { if err != nil {
return nil, fmt.Errorf("reading %q from cycleState: %v", preScoreStateKey, err) return nil, fmt.Errorf("reading %q from cycleState: %w", preScoreStateKey, err)
} }
s, ok := c.(*preScoreState) s, ok := c.(*preScoreState)

View File

@ -179,7 +179,7 @@ func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error
c, err := cycleState.Read(preFilterStateKey) c, err := cycleState.Read(preFilterStateKey)
if err != nil { if err != nil {
// preFilterState doesn't exist, likely PreFilter wasn't invoked. // preFilterState doesn't exist, likely PreFilter wasn't invoked.
return nil, fmt.Errorf("error reading %q from cycleState: %v", preFilterStateKey, err) return nil, fmt.Errorf("error reading %q from cycleState: %w", preFilterStateKey, err)
} }
s, ok := c.(*preFilterState) s, ok := c.(*preFilterState)

View File

@ -437,7 +437,7 @@ func TestPreFilterDisabled(t *testing.T) {
} }
cycleState := framework.NewCycleState() cycleState := framework.NewCycleState()
gotStatus := p.(framework.FilterPlugin).Filter(context.Background(), cycleState, pod, nodeInfo) gotStatus := p.(framework.FilterPlugin).Filter(context.Background(), cycleState, pod, nodeInfo)
wantStatus := framework.NewStatus(framework.Error, `error reading "PreFilterNodeResourcesFit" from cycleState: not found`) wantStatus := framework.AsStatus(fmt.Errorf(`error reading "PreFilterNodeResourcesFit" from cycleState: %w`, framework.ErrNotFound))
if !reflect.DeepEqual(gotStatus, wantStatus) { if !reflect.DeepEqual(gotStatus, wantStatus) {
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus) t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)
} }

View File

@ -184,7 +184,7 @@ func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error
c, err := cycleState.Read(preFilterStateKey) c, err := cycleState.Read(preFilterStateKey)
if err != nil { if err != nil {
// preFilterState doesn't exist, likely PreFilter wasn't invoked. // preFilterState doesn't exist, likely PreFilter wasn't invoked.
return nil, fmt.Errorf("reading %q from cycleState: %v", preFilterStateKey, err) return nil, fmt.Errorf("reading %q from cycleState: %w", preFilterStateKey, err)
} }
s, ok := c.(*preFilterState) s, ok := c.(*preFilterState)
@ -198,7 +198,7 @@ func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error
func (pl *PodTopologySpread) calPreFilterState(pod *v1.Pod) (*preFilterState, error) { func (pl *PodTopologySpread) calPreFilterState(pod *v1.Pod) (*preFilterState, error) {
allNodes, err := pl.sharedLister.NodeInfos().List() allNodes, err := pl.sharedLister.NodeInfos().List()
if err != nil { if err != nil {
return nil, fmt.Errorf("listing NodeInfos: %v", err) return nil, fmt.Errorf("listing NodeInfos: %w", err)
} }
var constraints []topologySpreadConstraint var constraints []topologySpreadConstraint
if len(pod.Spec.TopologySpreadConstraints) > 0 { if len(pod.Spec.TopologySpreadConstraints) > 0 {
@ -206,12 +206,12 @@ func (pl *PodTopologySpread) calPreFilterState(pod *v1.Pod) (*preFilterState, er
// so don't need to re-check feature gate, just check length of Constraints. // so don't need to re-check feature gate, just check length of Constraints.
constraints, err = filterTopologySpreadConstraints(pod.Spec.TopologySpreadConstraints, v1.DoNotSchedule) constraints, err = filterTopologySpreadConstraints(pod.Spec.TopologySpreadConstraints, v1.DoNotSchedule)
if err != nil { if err != nil {
return nil, fmt.Errorf("obtaining pod's hard topology spread constraints: %v", err) return nil, fmt.Errorf("obtaining pod's hard topology spread constraints: %w", err)
} }
} else { } else {
constraints, err = pl.buildDefaultConstraints(pod, v1.DoNotSchedule) constraints, err = pl.buildDefaultConstraints(pod, v1.DoNotSchedule)
if err != nil { if err != nil {
return nil, fmt.Errorf("setting default hard topology spread constraints: %v", err) return nil, fmt.Errorf("setting default hard topology spread constraints: %w", err)
} }
} }
if len(constraints) == 0 { if len(constraints) == 0 {

View File

@ -1663,7 +1663,7 @@ func TestPreFilterDisabled(t *testing.T) {
p := &PodTopologySpread{} p := &PodTopologySpread{}
cycleState := framework.NewCycleState() cycleState := framework.NewCycleState()
gotStatus := p.Filter(context.Background(), cycleState, pod, nodeInfo) gotStatus := p.Filter(context.Background(), cycleState, pod, nodeInfo)
wantStatus := framework.AsStatus(fmt.Errorf(`reading "PreFilterPodTopologySpread" from cycleState: not found`)) wantStatus := framework.AsStatus(fmt.Errorf(`reading "PreFilterPodTopologySpread" from cycleState: %w`, framework.ErrNotFound))
if !reflect.DeepEqual(gotStatus, wantStatus) { if !reflect.DeepEqual(gotStatus, wantStatus) {
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus) t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)
} }

View File

@ -62,12 +62,12 @@ func (pl *PodTopologySpread) initPreScoreState(s *preScoreState, pod *v1.Pod, fi
if len(pod.Spec.TopologySpreadConstraints) > 0 { if len(pod.Spec.TopologySpreadConstraints) > 0 {
s.Constraints, err = filterTopologySpreadConstraints(pod.Spec.TopologySpreadConstraints, v1.ScheduleAnyway) s.Constraints, err = filterTopologySpreadConstraints(pod.Spec.TopologySpreadConstraints, v1.ScheduleAnyway)
if err != nil { if err != nil {
return fmt.Errorf("obtaining pod's soft topology spread constraints: %v", err) return fmt.Errorf("obtaining pod's soft topology spread constraints: %w", err)
} }
} else { } else {
s.Constraints, err = pl.buildDefaultConstraints(pod, v1.ScheduleAnyway) s.Constraints, err = pl.buildDefaultConstraints(pod, v1.ScheduleAnyway)
if err != nil { if err != nil {
return fmt.Errorf("setting default soft topology spread constraints: %v", err) return fmt.Errorf("setting default soft topology spread constraints: %w", err)
} }
} }
if len(s.Constraints) == 0 { if len(s.Constraints) == 0 {
@ -257,7 +257,7 @@ func (pl *PodTopologySpread) ScoreExtensions() framework.ScoreExtensions {
func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) { func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) {
c, err := cycleState.Read(preScoreStateKey) c, err := cycleState.Read(preScoreStateKey)
if err != nil { if err != nil {
return nil, fmt.Errorf("error reading %q from cycleState: %v", preScoreStateKey, err) return nil, fmt.Errorf("error reading %q from cycleState: %w", preScoreStateKey, err)
} }
s, ok := c.(*preScoreState) s, ok := c.(*preScoreState)

View File

@ -108,14 +108,14 @@ func (pl *ServiceAffinity) createPreFilterState(pod *v1.Pod) (*preFilterState, e
// Store services which match the pod. // Store services which match the pod.
matchingPodServices, err := helper.GetPodServices(pl.serviceLister, pod) matchingPodServices, err := helper.GetPodServices(pl.serviceLister, pod)
if err != nil { if err != nil {
return nil, fmt.Errorf("listing pod services: %v", err.Error()) return nil, fmt.Errorf("listing pod services: %w", err)
} }
selector := createSelectorFromLabels(pod.Labels) selector := createSelectorFromLabels(pod.Labels)
// consider only the pods that belong to the same namespace // consider only the pods that belong to the same namespace
nodeInfos, err := pl.sharedLister.NodeInfos().List() nodeInfos, err := pl.sharedLister.NodeInfos().List()
if err != nil { if err != nil {
return nil, fmt.Errorf("listing nodeInfos: %v", err.Error()) return nil, fmt.Errorf("listing nodeInfos: %w", err)
} }
matchingPodList := filterPods(nodeInfos, selector, pod.Namespace) matchingPodList := filterPods(nodeInfos, selector, pod.Namespace)
@ -194,7 +194,7 @@ func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error
c, err := cycleState.Read(preFilterStateKey) c, err := cycleState.Read(preFilterStateKey)
if err != nil { if err != nil {
// preFilterState doesn't exist, likely PreFilter wasn't invoked. // preFilterState doesn't exist, likely PreFilter wasn't invoked.
return nil, fmt.Errorf("error reading %q from cycleState: %v", preFilterStateKey, err) return nil, fmt.Errorf("error reading %q from cycleState: %w", preFilterStateKey, err)
} }
if c == nil { if c == nil {

View File

@ -618,7 +618,7 @@ func TestPreFilterDisabled(t *testing.T) {
} }
cycleState := framework.NewCycleState() cycleState := framework.NewCycleState()
gotStatus := p.Filter(context.Background(), cycleState, pod, nodeInfo) gotStatus := p.Filter(context.Background(), cycleState, pod, nodeInfo)
wantStatus := framework.AsStatus(fmt.Errorf(`error reading "PreFilterServiceAffinity" from cycleState: not found`)) wantStatus := framework.AsStatus(fmt.Errorf(`error reading "PreFilterServiceAffinity" from cycleState: %w`, framework.ErrNotFound))
if !reflect.DeepEqual(gotStatus, wantStatus) { if !reflect.DeepEqual(gotStatus, wantStatus) {
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus) t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)
} }