predicate.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 lifecycle
  14. import (
  15. "fmt"
  16. "k8s.io/klog"
  17. "k8s.io/api/core/v1"
  18. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  19. "k8s.io/kubernetes/pkg/kubelet/util/format"
  20. "k8s.io/kubernetes/pkg/scheduler/algorithm/predicates"
  21. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  22. )
  23. type getNodeAnyWayFuncType func() (*v1.Node, error)
  24. type pluginResourceUpdateFuncType func(*schedulernodeinfo.NodeInfo, *PodAdmitAttributes) error
  25. // AdmissionFailureHandler is an interface which defines how to deal with a failure to admit a pod.
  26. // This allows for the graceful handling of pod admission failure.
  27. type AdmissionFailureHandler interface {
  28. HandleAdmissionFailure(admitPod *v1.Pod, failureReasons []predicates.PredicateFailureReason) (bool, []predicates.PredicateFailureReason, error)
  29. }
  30. type predicateAdmitHandler struct {
  31. getNodeAnyWayFunc getNodeAnyWayFuncType
  32. pluginResourceUpdateFunc pluginResourceUpdateFuncType
  33. admissionFailureHandler AdmissionFailureHandler
  34. }
  35. var _ PodAdmitHandler = &predicateAdmitHandler{}
  36. func NewPredicateAdmitHandler(getNodeAnyWayFunc getNodeAnyWayFuncType, admissionFailureHandler AdmissionFailureHandler, pluginResourceUpdateFunc pluginResourceUpdateFuncType) *predicateAdmitHandler {
  37. return &predicateAdmitHandler{
  38. getNodeAnyWayFunc,
  39. pluginResourceUpdateFunc,
  40. admissionFailureHandler,
  41. }
  42. }
  43. func (w *predicateAdmitHandler) Admit(attrs *PodAdmitAttributes) PodAdmitResult {
  44. node, err := w.getNodeAnyWayFunc()
  45. if err != nil {
  46. klog.Errorf("Cannot get Node info: %v", err)
  47. return PodAdmitResult{
  48. Admit: false,
  49. Reason: "InvalidNodeInfo",
  50. Message: "Kubelet cannot get node info.",
  51. }
  52. }
  53. admitPod := attrs.Pod
  54. pods := attrs.OtherPods
  55. nodeInfo := schedulernodeinfo.NewNodeInfo(pods...)
  56. nodeInfo.SetNode(node)
  57. // ensure the node has enough plugin resources for that required in pods
  58. if err = w.pluginResourceUpdateFunc(nodeInfo, attrs); err != nil {
  59. message := fmt.Sprintf("Update plugin resources failed due to %v, which is unexpected.", err)
  60. klog.Warningf("Failed to admit pod %v - %s", format.Pod(admitPod), message)
  61. return PodAdmitResult{
  62. Admit: false,
  63. Reason: "UnexpectedAdmissionError",
  64. Message: message,
  65. }
  66. }
  67. // Remove the requests of the extended resources that are missing in the
  68. // node info. This is required to support cluster-level resources, which
  69. // are extended resources unknown to nodes.
  70. //
  71. // Caveat: If a pod was manually bound to a node (e.g., static pod) where a
  72. // node-level extended resource it requires is not found, then kubelet will
  73. // not fail admission while it should. This issue will be addressed with
  74. // the Resource Class API in the future.
  75. podWithoutMissingExtendedResources := removeMissingExtendedResources(admitPod, nodeInfo)
  76. fit, reasons, err := predicates.GeneralPredicates(podWithoutMissingExtendedResources, nil, nodeInfo)
  77. if err != nil {
  78. message := fmt.Sprintf("GeneralPredicates failed due to %v, which is unexpected.", err)
  79. klog.Warningf("Failed to admit pod %v - %s", format.Pod(admitPod), message)
  80. return PodAdmitResult{
  81. Admit: fit,
  82. Reason: "UnexpectedAdmissionError",
  83. Message: message,
  84. }
  85. }
  86. if !fit {
  87. fit, reasons, err = w.admissionFailureHandler.HandleAdmissionFailure(admitPod, reasons)
  88. if err != nil {
  89. message := fmt.Sprintf("Unexpected error while attempting to recover from admission failure: %v", err)
  90. klog.Warningf("Failed to admit pod %v - %s", format.Pod(admitPod), message)
  91. return PodAdmitResult{
  92. Admit: fit,
  93. Reason: "UnexpectedAdmissionError",
  94. Message: message,
  95. }
  96. }
  97. }
  98. if !fit {
  99. var reason string
  100. var message string
  101. if len(reasons) == 0 {
  102. message = fmt.Sprint("GeneralPredicates failed due to unknown reason, which is unexpected.")
  103. klog.Warningf("Failed to admit pod %v - %s", format.Pod(admitPod), message)
  104. return PodAdmitResult{
  105. Admit: fit,
  106. Reason: "UnknownReason",
  107. Message: message,
  108. }
  109. }
  110. // If there are failed predicates, we only return the first one as a reason.
  111. r := reasons[0]
  112. switch re := r.(type) {
  113. case *predicates.PredicateFailureError:
  114. reason = re.PredicateName
  115. message = re.Error()
  116. klog.V(2).Infof("Predicate failed on Pod: %v, for reason: %v", format.Pod(admitPod), message)
  117. case *predicates.InsufficientResourceError:
  118. reason = fmt.Sprintf("OutOf%s", re.ResourceName)
  119. message = re.Error()
  120. klog.V(2).Infof("Predicate failed on Pod: %v, for reason: %v", format.Pod(admitPod), message)
  121. case *predicates.FailureReason:
  122. reason = re.GetReason()
  123. message = fmt.Sprintf("Failure: %s", re.GetReason())
  124. klog.V(2).Infof("Predicate failed on Pod: %v, for reason: %v", format.Pod(admitPod), message)
  125. default:
  126. reason = "UnexpectedPredicateFailureType"
  127. message = fmt.Sprintf("GeneralPredicates failed due to %v, which is unexpected.", r)
  128. klog.Warningf("Failed to admit pod %v - %s", format.Pod(admitPod), message)
  129. }
  130. return PodAdmitResult{
  131. Admit: fit,
  132. Reason: reason,
  133. Message: message,
  134. }
  135. }
  136. return PodAdmitResult{
  137. Admit: true,
  138. }
  139. }
  140. func removeMissingExtendedResources(pod *v1.Pod, nodeInfo *schedulernodeinfo.NodeInfo) *v1.Pod {
  141. podCopy := pod.DeepCopy()
  142. for i, c := range pod.Spec.Containers {
  143. // We only handle requests in Requests but not Limits because the
  144. // PodFitsResources predicate, to which the result pod will be passed,
  145. // does not use Limits.
  146. podCopy.Spec.Containers[i].Resources.Requests = make(v1.ResourceList)
  147. for rName, rQuant := range c.Resources.Requests {
  148. if v1helper.IsExtendedResourceName(rName) {
  149. if _, found := nodeInfo.AllocatableResource().ScalarResources[rName]; !found {
  150. continue
  151. }
  152. }
  153. podCopy.Spec.Containers[i].Resources.Requests[rName] = rQuant
  154. }
  155. }
  156. return podCopy
  157. }