taint_toleration.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 priorities
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  18. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  19. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  20. )
  21. // CountIntolerableTaintsPreferNoSchedule gives the count of intolerable taints of a pod with effect PreferNoSchedule
  22. func countIntolerableTaintsPreferNoSchedule(taints []v1.Taint, tolerations []v1.Toleration) (intolerableTaints int) {
  23. for _, taint := range taints {
  24. // check only on taints that have effect PreferNoSchedule
  25. if taint.Effect != v1.TaintEffectPreferNoSchedule {
  26. continue
  27. }
  28. if !v1helper.TolerationsTolerateTaint(tolerations, &taint) {
  29. intolerableTaints++
  30. }
  31. }
  32. return
  33. }
  34. // getAllTolerationEffectPreferNoSchedule gets the list of all Tolerations with Effect PreferNoSchedule or with no effect.
  35. func getAllTolerationPreferNoSchedule(tolerations []v1.Toleration) (tolerationList []v1.Toleration) {
  36. for _, toleration := range tolerations {
  37. // Empty effect means all effects which includes PreferNoSchedule, so we need to collect it as well.
  38. if len(toleration.Effect) == 0 || toleration.Effect == v1.TaintEffectPreferNoSchedule {
  39. tolerationList = append(tolerationList, toleration)
  40. }
  41. }
  42. return
  43. }
  44. // ComputeTaintTolerationPriorityMap prepares the priority list for all the nodes based on the number of intolerable taints on the node
  45. func ComputeTaintTolerationPriorityMap(pod *v1.Pod, meta interface{}, nodeInfo *schedulernodeinfo.NodeInfo) (schedulerapi.HostPriority, error) {
  46. node := nodeInfo.Node()
  47. if node == nil {
  48. return schedulerapi.HostPriority{}, fmt.Errorf("node not found")
  49. }
  50. // To hold all the tolerations with Effect PreferNoSchedule
  51. var tolerationsPreferNoSchedule []v1.Toleration
  52. if priorityMeta, ok := meta.(*priorityMetadata); ok {
  53. tolerationsPreferNoSchedule = priorityMeta.podTolerations
  54. } else {
  55. tolerationsPreferNoSchedule = getAllTolerationPreferNoSchedule(pod.Spec.Tolerations)
  56. }
  57. return schedulerapi.HostPriority{
  58. Host: node.Name,
  59. Score: countIntolerableTaintsPreferNoSchedule(node.Spec.Taints, tolerationsPreferNoSchedule),
  60. }, nil
  61. }
  62. // ComputeTaintTolerationPriorityReduce calculates the source of each node based on the number of intolerable taints on the node
  63. var ComputeTaintTolerationPriorityReduce = NormalizeReduce(schedulerapi.MaxPriority, true)