Skip to content

Commit

Permalink
Merge pull request #97794 from chendave/cleanup_status
Browse files Browse the repository at this point in the history
Refactor: rewrite `Merge` method to address readability and efficiency
  • Loading branch information
k8s-ci-robot committed Jan 14, 2021
2 parents c98ccce + 3c197c5 commit faaae71
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions pkg/scheduler/framework/interface.go
Expand Up @@ -77,6 +77,15 @@ const (
// This list should be exactly the same as the codes iota defined above in the same order.
var codes = []string{"Success", "Error", "Unschedulable", "UnschedulableAndUnresolvable", "Wait", "Skip"}

// statusPrecedence defines a map from status to its precedence, larger value means higher precedent.
var statusPrecedence = map[Code]int{
Error: 3,
UnschedulableAndUnresolvable: 2,
Unschedulable: 1,
// Any other statuses we know today, `Skip` or `Wait`, will take precedence over `Success`.
Success: -1,
}

func (c Code) String() string {
return codes[c]
}
Expand Down Expand Up @@ -184,28 +193,19 @@ func (p PluginToStatus) Merge() *Status {
}

finalStatus := NewStatus(Success)
var hasUnschedulableAndUnresolvable, hasUnschedulable bool
for _, s := range p {
if s.Code() == Error {
finalStatus.err = s.AsError()
} else if s.Code() == UnschedulableAndUnresolvable {
hasUnschedulableAndUnresolvable = true
} else if s.Code() == Unschedulable {
hasUnschedulable = true
}
finalStatus.code = s.Code()
if statusPrecedence[s.Code()] > statusPrecedence[finalStatus.code] {
finalStatus.code = s.Code()
}

for _, r := range s.reasons {
finalStatus.AppendReason(r)
}
}

if finalStatus.err != nil {
finalStatus.code = Error
} else if hasUnschedulableAndUnresolvable {
finalStatus.code = UnschedulableAndUnresolvable
} else if hasUnschedulable {
finalStatus.code = Unschedulable
}
return finalStatus
}

Expand Down

0 comments on commit faaae71

Please sign in to comment.