node_affinity.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 nodeaffinity
  14. import (
  15. "context"
  16. "fmt"
  17. v1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/labels"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  21. pluginhelper "k8s.io/kubernetes/pkg/scheduler/framework/plugins/helper"
  22. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  23. "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  24. )
  25. // NodeAffinity is a plugin that checks if a pod node selector matches the node label.
  26. type NodeAffinity struct {
  27. handle framework.FrameworkHandle
  28. }
  29. var _ framework.FilterPlugin = &NodeAffinity{}
  30. var _ framework.ScorePlugin = &NodeAffinity{}
  31. const (
  32. // Name is the name of the plugin used in the plugin registry and configurations.
  33. Name = "NodeAffinity"
  34. // ErrReason for node affinity/selector not matching.
  35. ErrReason = "node(s) didn't match node selector"
  36. )
  37. // Name returns name of the plugin. It is used in logs, etc.
  38. func (pl *NodeAffinity) Name() string {
  39. return Name
  40. }
  41. // Filter invoked at the filter extension point.
  42. func (pl *NodeAffinity) Filter(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
  43. node := nodeInfo.Node()
  44. if node == nil {
  45. return framework.NewStatus(framework.Error, "node not found")
  46. }
  47. if !pluginhelper.PodMatchesNodeSelectorAndAffinityTerms(pod, node) {
  48. return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason)
  49. }
  50. return nil
  51. }
  52. // Score invoked at the Score extension point.
  53. func (pl *NodeAffinity) Score(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
  54. nodeInfo, err := pl.handle.SnapshotSharedLister().NodeInfos().Get(nodeName)
  55. if err != nil {
  56. return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v", nodeName, err))
  57. }
  58. node := nodeInfo.Node()
  59. if node == nil {
  60. return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v", nodeName, err))
  61. }
  62. affinity := pod.Spec.Affinity
  63. var count int64
  64. // A nil element of PreferredDuringSchedulingIgnoredDuringExecution matches no objects.
  65. // An element of PreferredDuringSchedulingIgnoredDuringExecution that refers to an
  66. // empty PreferredSchedulingTerm matches all objects.
  67. if affinity != nil && affinity.NodeAffinity != nil && affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution != nil {
  68. // Match PreferredDuringSchedulingIgnoredDuringExecution term by term.
  69. for i := range affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
  70. preferredSchedulingTerm := &affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution[i]
  71. if preferredSchedulingTerm.Weight == 0 {
  72. continue
  73. }
  74. // TODO: Avoid computing it for all nodes if this becomes a performance problem.
  75. nodeSelector, err := v1helper.NodeSelectorRequirementsAsSelector(preferredSchedulingTerm.Preference.MatchExpressions)
  76. if err != nil {
  77. return 0, framework.NewStatus(framework.Error, err.Error())
  78. }
  79. if nodeSelector.Matches(labels.Set(node.Labels)) {
  80. count += int64(preferredSchedulingTerm.Weight)
  81. }
  82. }
  83. }
  84. return count, nil
  85. }
  86. // NormalizeScore invoked after scoring all nodes.
  87. func (pl *NodeAffinity) NormalizeScore(ctx context.Context, state *framework.CycleState, pod *v1.Pod, scores framework.NodeScoreList) *framework.Status {
  88. return pluginhelper.DefaultNormalizeScore(framework.MaxNodeScore, false, scores)
  89. }
  90. // ScoreExtensions of the Score plugin.
  91. func (pl *NodeAffinity) ScoreExtensions() framework.ScoreExtensions {
  92. return pl
  93. }
  94. // New initializes a new plugin and returns it.
  95. func New(_ *runtime.Unknown, h framework.FrameworkHandle) (framework.Plugin, error) {
  96. return &NodeAffinity{handle: h}, nil
  97. }