Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use %w instead of %v to format errors #99389

Merged
merged 1 commit into from Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/scheduler/framework/cycle_state.go
Expand Up @@ -21,9 +21,9 @@ import (
"sync"
)

const (
// NotFound is the not found error message.
NotFound = "not found"
var (
// ErrNotFound is the not found error message.
ErrNotFound = errors.New("not found")
)

// StateData is a generic type for arbitrary data stored in CycleState.
Expand Down Expand Up @@ -92,7 +92,7 @@ func (c *CycleState) Read(key StateKey) (StateData, error) {
if v, ok := c.storage[key]; ok {
return v, nil
}
return nil, errors.New(NotFound)
return nil, ErrNotFound
}

// Write stores the given "val" in CycleState with the given "key".
Expand Down
Expand Up @@ -295,7 +295,7 @@ func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error
c, err := cycleState.Read(preFilterStateKey)
if err != nil {
// 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)
Expand Down
Expand Up @@ -18,6 +18,7 @@ package interpodaffinity

import (
"context"
"fmt"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -1766,7 +1767,7 @@ func TestPreFilterDisabled(t *testing.T) {
p := &InterPodAffinity{}
cycleState := framework.NewCycleState()
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) {
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)
}
Expand Down
Expand Up @@ -208,7 +208,7 @@ func (pl *InterPodAffinity) PreScore(
func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) {
c, err := cycleState.Read(preScoreStateKey)
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)
Expand Down
Expand Up @@ -195,7 +195,7 @@ func getPodPreferredNodeAffinity(pod *v1.Pod) (*nodeaffinity.PreferredScheduling
func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) {
c, err := cycleState.Read(preScoreStateKey)
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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/scheduler/framework/plugins/noderesources/fit.go
Expand Up @@ -179,7 +179,7 @@ func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error
c, err := cycleState.Read(preFilterStateKey)
tanjing2020 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
// 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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/scheduler/framework/plugins/noderesources/fit_test.go
Expand Up @@ -437,7 +437,7 @@ func TestPreFilterDisabled(t *testing.T) {
}
cycleState := framework.NewCycleState()
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) {
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)
}
Expand Down
Expand Up @@ -184,7 +184,7 @@ func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error
c, err := cycleState.Read(preFilterStateKey)
if err != nil {
// 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)
Expand All @@ -198,20 +198,20 @@ func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error
func (pl *PodTopologySpread) calPreFilterState(pod *v1.Pod) (*preFilterState, error) {
allNodes, err := pl.sharedLister.NodeInfos().List()
if err != nil {
return nil, fmt.Errorf("listing NodeInfos: %v", err)
return nil, fmt.Errorf("listing NodeInfos: %w", err)
}
var constraints []topologySpreadConstraint
if len(pod.Spec.TopologySpreadConstraints) > 0 {
// We have feature gating in APIServer to strip the spec
// so don't need to re-check feature gate, just check length of Constraints.
constraints, err = filterTopologySpreadConstraints(pod.Spec.TopologySpreadConstraints, v1.DoNotSchedule)
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 {
constraints, err = pl.buildDefaultConstraints(pod, v1.DoNotSchedule)
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 {
Expand Down
Expand Up @@ -1663,7 +1663,7 @@ func TestPreFilterDisabled(t *testing.T) {
p := &PodTopologySpread{}
cycleState := framework.NewCycleState()
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) {
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/scheduler/framework/plugins/podtopologyspread/scoring.go
Expand Up @@ -62,12 +62,12 @@ func (pl *PodTopologySpread) initPreScoreState(s *preScoreState, pod *v1.Pod, fi
if len(pod.Spec.TopologySpreadConstraints) > 0 {
s.Constraints, err = filterTopologySpreadConstraints(pod.Spec.TopologySpreadConstraints, v1.ScheduleAnyway)
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 {
s.Constraints, err = pl.buildDefaultConstraints(pod, v1.ScheduleAnyway)
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 {
Expand Down Expand Up @@ -257,7 +257,7 @@ func (pl *PodTopologySpread) ScoreExtensions() framework.ScoreExtensions {
func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) {
c, err := cycleState.Read(preScoreStateKey)
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)
Expand Down
Expand Up @@ -108,14 +108,14 @@ func (pl *ServiceAffinity) createPreFilterState(pod *v1.Pod) (*preFilterState, e
// Store services which match the pod.
matchingPodServices, err := helper.GetPodServices(pl.serviceLister, pod)
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)

// consider only the pods that belong to the same namespace
nodeInfos, err := pl.sharedLister.NodeInfos().List()
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)

Expand Down Expand Up @@ -194,7 +194,7 @@ func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error
c, err := cycleState.Read(preFilterStateKey)
if err != nil {
// 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 {
Expand Down
Expand Up @@ -618,7 +618,7 @@ func TestPreFilterDisabled(t *testing.T) {
}
cycleState := framework.NewCycleState()
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) {
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)
}
Expand Down