node_selection.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 pod
  14. import (
  15. v1 "k8s.io/api/core/v1"
  16. )
  17. // NodeSelection specifies where to run a pod, using a combination of fixed node name,
  18. // node selector and/or affinity.
  19. type NodeSelection struct {
  20. Name string
  21. Selector map[string]string
  22. Affinity *v1.Affinity
  23. }
  24. // setNodeAffinityRequirement sets affinity with specified operator to nodeName to nodeSelection
  25. func setNodeAffinityRequirement(nodeSelection *NodeSelection, operator v1.NodeSelectorOperator, nodeName string) {
  26. // Add node-anti-affinity.
  27. if nodeSelection.Affinity == nil {
  28. nodeSelection.Affinity = &v1.Affinity{}
  29. }
  30. if nodeSelection.Affinity.NodeAffinity == nil {
  31. nodeSelection.Affinity.NodeAffinity = &v1.NodeAffinity{}
  32. }
  33. if nodeSelection.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil {
  34. nodeSelection.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution = &v1.NodeSelector{}
  35. }
  36. nodeSelection.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = append(nodeSelection.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms,
  37. v1.NodeSelectorTerm{
  38. MatchFields: []v1.NodeSelectorRequirement{
  39. {Key: "metadata.name", Operator: operator, Values: []string{nodeName}},
  40. },
  41. })
  42. }
  43. // SetNodeAffinityTopologyRequirement sets node affinity to a specified topology
  44. func SetNodeAffinityTopologyRequirement(nodeSelection *NodeSelection, topology map[string]string) {
  45. if nodeSelection.Affinity == nil {
  46. nodeSelection.Affinity = &v1.Affinity{}
  47. }
  48. if nodeSelection.Affinity.NodeAffinity == nil {
  49. nodeSelection.Affinity.NodeAffinity = &v1.NodeAffinity{}
  50. }
  51. if nodeSelection.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil {
  52. nodeSelection.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution = &v1.NodeSelector{}
  53. }
  54. for k, v := range topology {
  55. nodeSelection.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = append(nodeSelection.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms,
  56. v1.NodeSelectorTerm{
  57. MatchExpressions: []v1.NodeSelectorRequirement{
  58. {Key: k, Operator: v1.NodeSelectorOpIn, Values: []string{v}},
  59. },
  60. })
  61. }
  62. }
  63. // SetAffinity sets affinity to nodeName to nodeSelection
  64. func SetAffinity(nodeSelection *NodeSelection, nodeName string) {
  65. setNodeAffinityRequirement(nodeSelection, v1.NodeSelectorOpIn, nodeName)
  66. }
  67. // SetAntiAffinity sets anti-affinity to nodeName to nodeSelection
  68. func SetAntiAffinity(nodeSelection *NodeSelection, nodeName string) {
  69. setNodeAffinityRequirement(nodeSelection, v1.NodeSelectorOpNotIn, nodeName)
  70. }
  71. // SetNodeAffinity modifies the given pod object with
  72. // NodeAffinity to the given node name.
  73. func SetNodeAffinity(pod *v1.Pod, nodeName string) {
  74. nodeSelection := &NodeSelection{}
  75. SetAffinity(nodeSelection, nodeName)
  76. pod.Spec.Affinity = nodeSelection.Affinity
  77. }
  78. // SetNodeSelection modifies the given pod object with
  79. // the specified NodeSelection
  80. func SetNodeSelection(pod *v1.Pod, nodeSelection NodeSelection) {
  81. pod.Spec.NodeSelector = nodeSelection.Selector
  82. pod.Spec.Affinity = nodeSelection.Affinity
  83. // pod.Spec.NodeName should not be set directly because
  84. // it will bypass the scheduler, potentially causing
  85. // kubelet to Fail the pod immediately if it's out of
  86. // resources. Instead, we want the pod to remain
  87. // pending in the scheduler until the node has resources
  88. // freed up.
  89. if nodeSelection.Name != "" {
  90. SetNodeAffinity(pod, nodeSelection.Name)
  91. }
  92. }