node_affinity.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 helper
  14. import (
  15. "k8s.io/api/core/v1"
  16. "k8s.io/apimachinery/pkg/fields"
  17. "k8s.io/apimachinery/pkg/labels"
  18. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  19. )
  20. // PodMatchesNodeSelectorAndAffinityTerms checks whether the pod is schedulable onto nodes according to
  21. // the requirements in both NodeAffinity and nodeSelector.
  22. func PodMatchesNodeSelectorAndAffinityTerms(pod *v1.Pod, node *v1.Node) bool {
  23. // Check if node.Labels match pod.Spec.NodeSelector.
  24. if len(pod.Spec.NodeSelector) > 0 {
  25. selector := labels.SelectorFromSet(pod.Spec.NodeSelector)
  26. if !selector.Matches(labels.Set(node.Labels)) {
  27. return false
  28. }
  29. }
  30. // 1. nil NodeSelector matches all nodes (i.e. does not filter out any nodes)
  31. // 2. nil []NodeSelectorTerm (equivalent to non-nil empty NodeSelector) matches no nodes
  32. // 3. zero-length non-nil []NodeSelectorTerm matches no nodes also, just for simplicity
  33. // 4. nil []NodeSelectorRequirement (equivalent to non-nil empty NodeSelectorTerm) matches no nodes
  34. // 5. zero-length non-nil []NodeSelectorRequirement matches no nodes also, just for simplicity
  35. // 6. non-nil empty NodeSelectorRequirement is not allowed
  36. nodeAffinityMatches := true
  37. affinity := pod.Spec.Affinity
  38. if affinity != nil && affinity.NodeAffinity != nil {
  39. nodeAffinity := affinity.NodeAffinity
  40. // if no required NodeAffinity requirements, will do no-op, means select all nodes.
  41. // TODO: Replace next line with subsequent commented-out line when implement RequiredDuringSchedulingRequiredDuringExecution.
  42. if nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil {
  43. // if nodeAffinity.RequiredDuringSchedulingRequiredDuringExecution == nil && nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil {
  44. return true
  45. }
  46. // Match node selector for requiredDuringSchedulingRequiredDuringExecution.
  47. // TODO: Uncomment this block when implement RequiredDuringSchedulingRequiredDuringExecution.
  48. // if nodeAffinity.RequiredDuringSchedulingRequiredDuringExecution != nil {
  49. // nodeSelectorTerms := nodeAffinity.RequiredDuringSchedulingRequiredDuringExecution.NodeSelectorTerms
  50. // klog.V(10).Infof("Match for RequiredDuringSchedulingRequiredDuringExecution node selector terms %+v", nodeSelectorTerms)
  51. // nodeAffinityMatches = nodeMatchesNodeSelectorTerms(node, nodeSelectorTerms)
  52. // }
  53. // Match node selector for requiredDuringSchedulingIgnoredDuringExecution.
  54. if nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution != nil {
  55. nodeSelectorTerms := nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms
  56. nodeAffinityMatches = nodeAffinityMatches && nodeMatchesNodeSelectorTerms(node, nodeSelectorTerms)
  57. }
  58. }
  59. return nodeAffinityMatches
  60. }
  61. // nodeMatchesNodeSelectorTerms checks if a node's labels satisfy a list of node selector terms,
  62. // terms are ORed, and an empty list of terms will match nothing.
  63. func nodeMatchesNodeSelectorTerms(node *v1.Node, nodeSelectorTerms []v1.NodeSelectorTerm) bool {
  64. return v1helper.MatchNodeSelectorTerms(nodeSelectorTerms, node.Labels, fields.Set{
  65. "metadata.name": node.Name,
  66. })
  67. }