error.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package predicates
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. )
  18. var (
  19. // The predicateName tries to be consistent as the predicate name used in DefaultAlgorithmProvider defined in
  20. // defaults.go (which tend to be stable for backward compatibility)
  21. // NOTE: If you add a new predicate failure error for a predicate that can never
  22. // be made to pass by removing pods, or you change an existing predicate so that
  23. // it can never be made to pass by removing pods, you need to add the predicate
  24. // failure error in nodesWherePreemptionMightHelp() in scheduler/core/generic_scheduler.go
  25. // ErrDiskConflict is used for NoDiskConflict predicate error.
  26. ErrDiskConflict = newPredicateFailureError("NoDiskConflict", "node(s) had no available disk")
  27. // ErrVolumeZoneConflict is used for NoVolumeZoneConflict predicate error.
  28. ErrVolumeZoneConflict = newPredicateFailureError("NoVolumeZoneConflict", "node(s) had no available volume zone")
  29. // ErrNodeSelectorNotMatch is used for MatchNodeSelector predicate error.
  30. ErrNodeSelectorNotMatch = newPredicateFailureError("MatchNodeSelector", "node(s) didn't match node selector")
  31. // ErrPodAffinityNotMatch is used for MatchInterPodAffinity predicate error.
  32. ErrPodAffinityNotMatch = newPredicateFailureError("MatchInterPodAffinity", "node(s) didn't match pod affinity/anti-affinity")
  33. // ErrPodAffinityRulesNotMatch is used for PodAffinityRulesNotMatch predicate error.
  34. ErrPodAffinityRulesNotMatch = newPredicateFailureError("PodAffinityRulesNotMatch", "node(s) didn't match pod affinity rules")
  35. // ErrPodAntiAffinityRulesNotMatch is used for PodAntiAffinityRulesNotMatch predicate error.
  36. ErrPodAntiAffinityRulesNotMatch = newPredicateFailureError("PodAntiAffinityRulesNotMatch", "node(s) didn't match pod anti-affinity rules")
  37. // ErrExistingPodsAntiAffinityRulesNotMatch is used for ExistingPodsAntiAffinityRulesNotMatch predicate error.
  38. ErrExistingPodsAntiAffinityRulesNotMatch = newPredicateFailureError("ExistingPodsAntiAffinityRulesNotMatch", "node(s) didn't satisfy existing pods anti-affinity rules")
  39. // ErrTaintsTolerationsNotMatch is used for PodToleratesNodeTaints predicate error.
  40. ErrTaintsTolerationsNotMatch = newPredicateFailureError("PodToleratesNodeTaints", "node(s) had taints that the pod didn't tolerate")
  41. // ErrPodNotMatchHostName is used for HostName predicate error.
  42. ErrPodNotMatchHostName = newPredicateFailureError("HostName", "node(s) didn't match the requested hostname")
  43. // ErrPodNotFitsHostPorts is used for PodFitsHostPorts predicate error.
  44. ErrPodNotFitsHostPorts = newPredicateFailureError("PodFitsHostPorts", "node(s) didn't have free ports for the requested pod ports")
  45. // ErrNodeLabelPresenceViolated is used for CheckNodeLabelPresence predicate error.
  46. ErrNodeLabelPresenceViolated = newPredicateFailureError("CheckNodeLabelPresence", "node(s) didn't have the requested labels")
  47. // ErrServiceAffinityViolated is used for CheckServiceAffinity predicate error.
  48. ErrServiceAffinityViolated = newPredicateFailureError("CheckServiceAffinity", "node(s) didn't match service affinity")
  49. // ErrMaxVolumeCountExceeded is used for MaxVolumeCount predicate error.
  50. ErrMaxVolumeCountExceeded = newPredicateFailureError("MaxVolumeCount", "node(s) exceed max volume count")
  51. // ErrNodeUnderMemoryPressure is used for NodeUnderMemoryPressure predicate error.
  52. ErrNodeUnderMemoryPressure = newPredicateFailureError("NodeUnderMemoryPressure", "node(s) had memory pressure")
  53. // ErrNodeUnderDiskPressure is used for NodeUnderDiskPressure predicate error.
  54. ErrNodeUnderDiskPressure = newPredicateFailureError("NodeUnderDiskPressure", "node(s) had disk pressure")
  55. // ErrNodeUnderPIDPressure is used for NodeUnderPIDPressure predicate error.
  56. ErrNodeUnderPIDPressure = newPredicateFailureError("NodeUnderPIDPressure", "node(s) had pid pressure")
  57. // ErrNodeNotReady is used for NodeNotReady predicate error.
  58. ErrNodeNotReady = newPredicateFailureError("NodeNotReady", "node(s) were not ready")
  59. // ErrNodeNetworkUnavailable is used for NodeNetworkUnavailable predicate error.
  60. ErrNodeNetworkUnavailable = newPredicateFailureError("NodeNetworkUnavailable", "node(s) had unavailable network")
  61. // ErrNodeUnschedulable is used for NodeUnschedulable predicate error.
  62. ErrNodeUnschedulable = newPredicateFailureError("NodeUnschedulable", "node(s) were unschedulable")
  63. // ErrNodeUnknownCondition is used for NodeUnknownCondition predicate error.
  64. ErrNodeUnknownCondition = newPredicateFailureError("NodeUnknownCondition", "node(s) had unknown conditions")
  65. // ErrVolumeNodeConflict is used for VolumeNodeAffinityConflict predicate error.
  66. ErrVolumeNodeConflict = newPredicateFailureError("VolumeNodeAffinityConflict", "node(s) had volume node affinity conflict")
  67. // ErrVolumeBindConflict is used for VolumeBindingNoMatch predicate error.
  68. ErrVolumeBindConflict = newPredicateFailureError("VolumeBindingNoMatch", "node(s) didn't find available persistent volumes to bind")
  69. // ErrFakePredicate is used for test only. The fake predicates returning false also returns error
  70. // as ErrFakePredicate.
  71. ErrFakePredicate = newPredicateFailureError("FakePredicateError", "Nodes failed the fake predicate")
  72. )
  73. // InsufficientResourceError is an error type that indicates what kind of resource limit is
  74. // hit and caused the unfitting failure.
  75. type InsufficientResourceError struct {
  76. // resourceName is the name of the resource that is insufficient
  77. ResourceName v1.ResourceName
  78. requested int64
  79. used int64
  80. capacity int64
  81. }
  82. // NewInsufficientResourceError returns an InsufficientResourceError.
  83. func NewInsufficientResourceError(resourceName v1.ResourceName, requested, used, capacity int64) *InsufficientResourceError {
  84. return &InsufficientResourceError{
  85. ResourceName: resourceName,
  86. requested: requested,
  87. used: used,
  88. capacity: capacity,
  89. }
  90. }
  91. func (e *InsufficientResourceError) Error() string {
  92. return fmt.Sprintf("Node didn't have enough resource: %s, requested: %d, used: %d, capacity: %d",
  93. e.ResourceName, e.requested, e.used, e.capacity)
  94. }
  95. // GetReason returns the reason of the InsufficientResourceError.
  96. func (e *InsufficientResourceError) GetReason() string {
  97. return fmt.Sprintf("Insufficient %v", e.ResourceName)
  98. }
  99. // GetInsufficientAmount returns the amount of the insufficient resource of the error.
  100. func (e *InsufficientResourceError) GetInsufficientAmount() int64 {
  101. return e.requested - (e.capacity - e.used)
  102. }
  103. // PredicateFailureError describes a failure error of predicate.
  104. type PredicateFailureError struct {
  105. PredicateName string
  106. PredicateDesc string
  107. }
  108. func newPredicateFailureError(predicateName, predicateDesc string) *PredicateFailureError {
  109. return &PredicateFailureError{PredicateName: predicateName, PredicateDesc: predicateDesc}
  110. }
  111. func (e *PredicateFailureError) Error() string {
  112. return fmt.Sprintf("Predicate %s failed", e.PredicateName)
  113. }
  114. // GetReason returns the reason of the PredicateFailureError.
  115. func (e *PredicateFailureError) GetReason() string {
  116. return e.PredicateDesc
  117. }
  118. // PredicateFailureReason interface represents the failure reason of a predicate.
  119. type PredicateFailureReason interface {
  120. GetReason() string
  121. }
  122. // FailureReason describes a failure reason.
  123. type FailureReason struct {
  124. reason string
  125. }
  126. // NewFailureReason creates a FailureReason with message.
  127. func NewFailureReason(msg string) *FailureReason {
  128. return &FailureReason{reason: msg}
  129. }
  130. // GetReason returns the reason of the FailureReason.
  131. func (e *FailureReason) GetReason() string {
  132. return e.reason
  133. }