diff --git a/pkg/scheduler/framework/cycle_state.go b/pkg/scheduler/framework/cycle_state.go index 85e5a6c0169e..fd7f5543d8cc 100644 --- a/pkg/scheduler/framework/cycle_state.go +++ b/pkg/scheduler/framework/cycle_state.go @@ -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. @@ -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". diff --git a/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go b/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go index 43f5834683d3..a85e5cf6eaa6 100644 --- a/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go +++ b/pkg/scheduler/framework/plugins/interpodaffinity/filtering.go @@ -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) diff --git a/pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go b/pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go index cc9e58732156..51c989756c5e 100644 --- a/pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go +++ b/pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go @@ -18,6 +18,7 @@ package interpodaffinity import ( "context" + "fmt" "reflect" "strings" "testing" @@ -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) } diff --git a/pkg/scheduler/framework/plugins/interpodaffinity/scoring.go b/pkg/scheduler/framework/plugins/interpodaffinity/scoring.go index dd05918158f4..f01537fd48db 100644 --- a/pkg/scheduler/framework/plugins/interpodaffinity/scoring.go +++ b/pkg/scheduler/framework/plugins/interpodaffinity/scoring.go @@ -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) diff --git a/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go b/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go index f0b6677f93a6..2d89d1968e79 100644 --- a/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go +++ b/pkg/scheduler/framework/plugins/nodeaffinity/node_affinity.go @@ -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) diff --git a/pkg/scheduler/framework/plugins/noderesources/fit.go b/pkg/scheduler/framework/plugins/noderesources/fit.go index f8d54a2ebb78..4c118f5a1894 100644 --- a/pkg/scheduler/framework/plugins/noderesources/fit.go +++ b/pkg/scheduler/framework/plugins/noderesources/fit.go @@ -179,7 +179,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) diff --git a/pkg/scheduler/framework/plugins/noderesources/fit_test.go b/pkg/scheduler/framework/plugins/noderesources/fit_test.go index 1c6d4de3511f..f9529006e714 100644 --- a/pkg/scheduler/framework/plugins/noderesources/fit_test.go +++ b/pkg/scheduler/framework/plugins/noderesources/fit_test.go @@ -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) } diff --git a/pkg/scheduler/framework/plugins/podtopologyspread/filtering.go b/pkg/scheduler/framework/plugins/podtopologyspread/filtering.go index 7635d3d941fd..c4d9253c1d31 100644 --- a/pkg/scheduler/framework/plugins/podtopologyspread/filtering.go +++ b/pkg/scheduler/framework/plugins/podtopologyspread/filtering.go @@ -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) @@ -198,7 +198,7 @@ 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 { @@ -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. 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 { diff --git a/pkg/scheduler/framework/plugins/podtopologyspread/filtering_test.go b/pkg/scheduler/framework/plugins/podtopologyspread/filtering_test.go index 25304502906b..1628604832f4 100644 --- a/pkg/scheduler/framework/plugins/podtopologyspread/filtering_test.go +++ b/pkg/scheduler/framework/plugins/podtopologyspread/filtering_test.go @@ -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) } diff --git a/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go b/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go index b137577a9be9..bba5abe9750b 100644 --- a/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go +++ b/pkg/scheduler/framework/plugins/podtopologyspread/scoring.go @@ -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 { @@ -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) diff --git a/pkg/scheduler/framework/plugins/serviceaffinity/service_affinity.go b/pkg/scheduler/framework/plugins/serviceaffinity/service_affinity.go index 8760c91bbaf3..4648d7d1e7d9 100644 --- a/pkg/scheduler/framework/plugins/serviceaffinity/service_affinity.go +++ b/pkg/scheduler/framework/plugins/serviceaffinity/service_affinity.go @@ -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) @@ -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 { diff --git a/pkg/scheduler/framework/plugins/serviceaffinity/service_affinity_test.go b/pkg/scheduler/framework/plugins/serviceaffinity/service_affinity_test.go index cfddd24ad5e6..b2e6da83218b 100644 --- a/pkg/scheduler/framework/plugins/serviceaffinity/service_affinity_test.go +++ b/pkg/scheduler/framework/plugins/serviceaffinity/service_affinity_test.go @@ -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) }