node_affinity.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright 2015 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. v1 "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/labels"
  18. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  19. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  20. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  21. )
  22. // CalculateNodeAffinityPriorityMap prioritizes nodes according to node affinity scheduling preferences
  23. // indicated in PreferredDuringSchedulingIgnoredDuringExecution. Each time a node matches a preferredSchedulingTerm,
  24. // it will get an add of preferredSchedulingTerm.Weight. Thus, the more preferredSchedulingTerms
  25. // the node satisfies and the more the preferredSchedulingTerm that is satisfied weights, the higher
  26. // score the node gets.
  27. func CalculateNodeAffinityPriorityMap(pod *v1.Pod, meta interface{}, nodeInfo *schedulernodeinfo.NodeInfo) (schedulerapi.HostPriority, error) {
  28. node := nodeInfo.Node()
  29. if node == nil {
  30. return schedulerapi.HostPriority{}, fmt.Errorf("node not found")
  31. }
  32. // default is the podspec.
  33. affinity := pod.Spec.Affinity
  34. if priorityMeta, ok := meta.(*priorityMetadata); ok {
  35. // We were able to parse metadata, use affinity from there.
  36. affinity = priorityMeta.affinity
  37. }
  38. var count int32
  39. // A nil element of PreferredDuringSchedulingIgnoredDuringExecution matches no objects.
  40. // An element of PreferredDuringSchedulingIgnoredDuringExecution that refers to an
  41. // empty PreferredSchedulingTerm matches all objects.
  42. if affinity != nil && affinity.NodeAffinity != nil && affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution != nil {
  43. // Match PreferredDuringSchedulingIgnoredDuringExecution term by term.
  44. for i := range affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
  45. preferredSchedulingTerm := &affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution[i]
  46. if preferredSchedulingTerm.Weight == 0 {
  47. continue
  48. }
  49. // TODO: Avoid computing it for all nodes if this becomes a performance problem.
  50. nodeSelector, err := v1helper.NodeSelectorRequirementsAsSelector(preferredSchedulingTerm.Preference.MatchExpressions)
  51. if err != nil {
  52. return schedulerapi.HostPriority{}, err
  53. }
  54. if nodeSelector.Matches(labels.Set(node.Labels)) {
  55. count += preferredSchedulingTerm.Weight
  56. }
  57. }
  58. }
  59. return schedulerapi.HostPriority{
  60. Host: node.Name,
  61. Score: float64(count),
  62. }, nil
  63. }
  64. // CalculateNodeAffinityPriorityReduce is a reduce function for node affinity priority calculation.
  65. var CalculateNodeAffinityPriorityReduce = NormalizeReduce(schedulerapi.MaxPriority, false)