taint_toleration.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Copyright 2019 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 tainttoleration
  14. import (
  15. "context"
  16. "fmt"
  17. v1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  20. pluginhelper "k8s.io/kubernetes/pkg/scheduler/framework/plugins/helper"
  21. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  22. "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  23. )
  24. // TaintToleration is a plugin that checks if a pod tolerates a node's taints.
  25. type TaintToleration struct {
  26. handle framework.FrameworkHandle
  27. }
  28. var _ framework.FilterPlugin = &TaintToleration{}
  29. var _ framework.PreScorePlugin = &TaintToleration{}
  30. var _ framework.ScorePlugin = &TaintToleration{}
  31. const (
  32. // Name is the name of the plugin used in the plugin registry and configurations.
  33. Name = "TaintToleration"
  34. // preScoreStateKey is the key in CycleState to TaintToleration pre-computed data for Scoring.
  35. preScoreStateKey = "PreScore" + Name
  36. // ErrReasonNotMatch is the Filter reason status when not matching.
  37. ErrReasonNotMatch = "node(s) had taints that the pod didn't tolerate"
  38. )
  39. // Name returns name of the plugin. It is used in logs, etc.
  40. func (pl *TaintToleration) Name() string {
  41. return Name
  42. }
  43. // Filter invoked at the filter extension point.
  44. func (pl *TaintToleration) Filter(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
  45. if nodeInfo == nil || nodeInfo.Node() == nil {
  46. return framework.NewStatus(framework.Error, "invalid nodeInfo")
  47. }
  48. taints, err := nodeInfo.Taints()
  49. if err != nil {
  50. return framework.NewStatus(framework.Error, err.Error())
  51. }
  52. filterPredicate := func(t *v1.Taint) bool {
  53. // PodToleratesNodeTaints is only interested in NoSchedule and NoExecute taints.
  54. return t.Effect == v1.TaintEffectNoSchedule || t.Effect == v1.TaintEffectNoExecute
  55. }
  56. taint, isUntolerated := v1helper.FindMatchingUntoleratedTaint(taints, pod.Spec.Tolerations, filterPredicate)
  57. if !isUntolerated {
  58. return nil
  59. }
  60. errReason := fmt.Sprintf("node(s) had taint {%s: %s}, that the pod didn't tolerate",
  61. taint.Key, taint.Value)
  62. return framework.NewStatus(framework.UnschedulableAndUnresolvable, errReason)
  63. }
  64. // preScoreState computed at PreScore and used at Score.
  65. type preScoreState struct {
  66. tolerationsPreferNoSchedule []v1.Toleration
  67. }
  68. // Clone implements the mandatory Clone interface. We don't really copy the data since
  69. // there is no need for that.
  70. func (s *preScoreState) Clone() framework.StateData {
  71. return s
  72. }
  73. // getAllTolerationEffectPreferNoSchedule gets the list of all Tolerations with Effect PreferNoSchedule or with no effect.
  74. func getAllTolerationPreferNoSchedule(tolerations []v1.Toleration) (tolerationList []v1.Toleration) {
  75. for _, toleration := range tolerations {
  76. // Empty effect means all effects which includes PreferNoSchedule, so we need to collect it as well.
  77. if len(toleration.Effect) == 0 || toleration.Effect == v1.TaintEffectPreferNoSchedule {
  78. tolerationList = append(tolerationList, toleration)
  79. }
  80. }
  81. return
  82. }
  83. // PreScore builds and writes cycle state used by Score and NormalizeScore.
  84. func (pl *TaintToleration) PreScore(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod, nodes []*v1.Node) *framework.Status {
  85. if len(nodes) == 0 {
  86. return nil
  87. }
  88. tolerationsPreferNoSchedule := getAllTolerationPreferNoSchedule(pod.Spec.Tolerations)
  89. state := &preScoreState{
  90. tolerationsPreferNoSchedule: tolerationsPreferNoSchedule,
  91. }
  92. cycleState.Write(preScoreStateKey, state)
  93. return nil
  94. }
  95. func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error) {
  96. c, err := cycleState.Read(preScoreStateKey)
  97. if err != nil {
  98. return nil, fmt.Errorf("Error reading %q from cycleState: %v", preScoreStateKey, err)
  99. }
  100. s, ok := c.(*preScoreState)
  101. if !ok {
  102. return nil, fmt.Errorf("%+v convert to tainttoleration.preScoreState error", c)
  103. }
  104. return s, nil
  105. }
  106. // CountIntolerableTaintsPreferNoSchedule gives the count of intolerable taints of a pod with effect PreferNoSchedule
  107. func countIntolerableTaintsPreferNoSchedule(taints []v1.Taint, tolerations []v1.Toleration) (intolerableTaints int) {
  108. for _, taint := range taints {
  109. // check only on taints that have effect PreferNoSchedule
  110. if taint.Effect != v1.TaintEffectPreferNoSchedule {
  111. continue
  112. }
  113. if !v1helper.TolerationsTolerateTaint(tolerations, &taint) {
  114. intolerableTaints++
  115. }
  116. }
  117. return
  118. }
  119. // Score invoked at the Score extension point.
  120. func (pl *TaintToleration) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
  121. nodeInfo, err := pl.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
  122. if err != nil || nodeInfo.Node() == nil {
  123. return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v", nodeName, err))
  124. }
  125. node := nodeInfo.Node()
  126. s, err := getPreScoreState(state)
  127. if err != nil {
  128. return 0, framework.NewStatus(framework.Error, err.Error())
  129. }
  130. score := int64(countIntolerableTaintsPreferNoSchedule(node.Spec.Taints, s.tolerationsPreferNoSchedule))
  131. return score, nil
  132. }
  133. // NormalizeScore invoked after scoring all nodes.
  134. func (pl *TaintToleration) NormalizeScore(ctx context.Context, _ *framework.CycleState, pod *v1.Pod, scores framework.NodeScoreList) *framework.Status {
  135. return pluginhelper.DefaultNormalizeScore(framework.MaxNodeScore, true, scores)
  136. }
  137. // ScoreExtensions of the Score plugin.
  138. func (pl *TaintToleration) ScoreExtensions() framework.ScoreExtensions {
  139. return pl
  140. }
  141. // New initializes a new plugin and returns it.
  142. func New(_ *runtime.Unknown, h framework.FrameworkHandle) (framework.Plugin, error) {
  143. return &TaintToleration{handle: h}, nil
  144. }