wrap errors in service affinity plugin

This commit is contained in:
Arghya Sadhu 2020-09-26 21:32:38 +05:30
parent ff3c751afc
commit ad415b9f54
2 changed files with 11 additions and 10 deletions

View File

@ -133,7 +133,7 @@ func (pl *ServiceAffinity) PreFilter(ctx context.Context, cycleState *framework.
s, err := pl.createPreFilterState(pod)
if err != nil {
return framework.NewStatus(framework.Error, fmt.Sprintf("could not create preFilterState: %v", err))
return framework.AsStatus(fmt.Errorf("could not create preFilterState: %w", err))
}
cycleState.Write(preFilterStateKey, s)
return nil
@ -151,7 +151,7 @@ func (pl *ServiceAffinity) PreFilterExtensions() framework.PreFilterExtensions {
func (pl *ServiceAffinity) AddPod(ctx context.Context, cycleState *framework.CycleState, podToSchedule *v1.Pod, podToAdd *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
s, err := getPreFilterState(cycleState)
if err != nil {
return framework.NewStatus(framework.Error, err.Error())
return framework.AsStatus(err)
}
// If addedPod is in the same namespace as the pod, update the list
@ -172,7 +172,7 @@ func (pl *ServiceAffinity) AddPod(ctx context.Context, cycleState *framework.Cyc
func (pl *ServiceAffinity) RemovePod(ctx context.Context, cycleState *framework.CycleState, podToSchedule *v1.Pod, podToRemove *v1.Pod, nodeInfo *framework.NodeInfo) *framework.Status {
s, err := getPreFilterState(cycleState)
if err != nil {
return framework.NewStatus(framework.Error, err.Error())
return framework.AsStatus(err)
}
if len(s.matchingPodList) == 0 ||
@ -239,12 +239,12 @@ func (pl *ServiceAffinity) Filter(ctx context.Context, cycleState *framework.Cyc
node := nodeInfo.Node()
if node == nil {
return framework.NewStatus(framework.Error, "node not found")
return framework.AsStatus(fmt.Errorf("node not found"))
}
s, err := getPreFilterState(cycleState)
if err != nil {
return framework.NewStatus(framework.Error, err.Error())
return framework.AsStatus(err)
}
pods, services := s.matchingPodList, s.matchingPodServices
@ -257,7 +257,7 @@ func (pl *ServiceAffinity) Filter(ctx context.Context, cycleState *framework.Cyc
if len(filteredPods) > 0 {
nodeWithAffinityLabels, err := pl.sharedLister.NodeInfos().Get(filteredPods[0].Spec.NodeName)
if err != nil {
return framework.NewStatus(framework.Error, "node not found")
return framework.AsStatus(fmt.Errorf("node not found"))
}
addUnsetLabelsToMap(affinityLabels, pl.args.AffinityLabels, labels.Set(nodeWithAffinityLabels.Node().Labels))
}
@ -275,12 +275,12 @@ func (pl *ServiceAffinity) Filter(ctx context.Context, cycleState *framework.Cyc
func (pl *ServiceAffinity) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
nodeInfo, err := pl.sharedLister.NodeInfos().Get(nodeName)
if err != nil {
return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v", nodeName, err))
return 0, framework.AsStatus(fmt.Errorf("getting node %q from Snapshot: %w", nodeName, err))
}
node := nodeInfo.Node()
if node == nil {
return 0, framework.NewStatus(framework.Error, fmt.Sprintf("node not found"))
return 0, framework.AsStatus(fmt.Errorf("node not found"))
}
// Pods matched namespace,selector on current node.
@ -313,7 +313,7 @@ func (pl *ServiceAffinity) NormalizeScore(ctx context.Context, _ *framework.Cycl
reduceResult := make([]float64, len(scores))
for _, label := range pl.args.AntiAffinityLabelsPreference {
if err := pl.updateNodeScoresForLabel(pl.sharedLister, scores, reduceResult, label); err != nil {
return framework.NewStatus(framework.Error, err.Error())
return framework.AsStatus(err)
}
}

View File

@ -18,6 +18,7 @@ package serviceaffinity
import (
"context"
"fmt"
"reflect"
"sort"
"testing"
@ -615,7 +616,7 @@ func TestPreFilterDisabled(t *testing.T) {
}
cycleState := framework.NewCycleState()
gotStatus := p.Filter(context.Background(), cycleState, pod, nodeInfo)
wantStatus := framework.NewStatus(framework.Error, `error reading "PreFilterServiceAffinity" from cycleState: not found`)
wantStatus := framework.AsStatus(fmt.Errorf(`error reading "PreFilterServiceAffinity" from cycleState: not found`))
if !reflect.DeepEqual(gotStatus, wantStatus) {
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)
}