utils.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2017 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 util
  14. import (
  15. "time"
  16. v1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/klog"
  19. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  20. extenderv1 "k8s.io/kubernetes/pkg/scheduler/apis/extender/v1"
  21. )
  22. // GetPodFullName returns a name that uniquely identifies a pod.
  23. func GetPodFullName(pod *v1.Pod) string {
  24. // Use underscore as the delimiter because it is not allowed in pod name
  25. // (DNS subdomain format).
  26. return pod.Name + "_" + pod.Namespace
  27. }
  28. // GetPodStartTime returns start time of the given pod or current timestamp
  29. // if it hasn't started yet.
  30. func GetPodStartTime(pod *v1.Pod) *metav1.Time {
  31. if pod.Status.StartTime != nil {
  32. return pod.Status.StartTime
  33. }
  34. // Assumed pods and bound pods that haven't started don't have a StartTime yet.
  35. return &metav1.Time{Time: time.Now()}
  36. }
  37. // GetEarliestPodStartTime returns the earliest start time of all pods that
  38. // have the highest priority among all victims.
  39. func GetEarliestPodStartTime(victims *extenderv1.Victims) *metav1.Time {
  40. if len(victims.Pods) == 0 {
  41. // should not reach here.
  42. klog.Errorf("victims.Pods is empty. Should not reach here.")
  43. return nil
  44. }
  45. earliestPodStartTime := GetPodStartTime(victims.Pods[0])
  46. maxPriority := podutil.GetPodPriority(victims.Pods[0])
  47. for _, pod := range victims.Pods {
  48. if podutil.GetPodPriority(pod) == maxPriority {
  49. if GetPodStartTime(pod).Before(earliestPodStartTime) {
  50. earliestPodStartTime = GetPodStartTime(pod)
  51. }
  52. } else if podutil.GetPodPriority(pod) > maxPriority {
  53. maxPriority = podutil.GetPodPriority(pod)
  54. earliestPodStartTime = GetPodStartTime(pod)
  55. }
  56. }
  57. return earliestPodStartTime
  58. }
  59. // MoreImportantPod return true when priority of the first pod is higher than
  60. // the second one. If two pods' priorities are equal, compare their StartTime.
  61. // It takes arguments of the type "interface{}" to be used with SortableList,
  62. // but expects those arguments to be *v1.Pod.
  63. func MoreImportantPod(pod1, pod2 *v1.Pod) bool {
  64. p1 := podutil.GetPodPriority(pod1)
  65. p2 := podutil.GetPodPriority(pod2)
  66. if p1 != p2 {
  67. return p1 > p2
  68. }
  69. return GetPodStartTime(pod1).Before(GetPodStartTime(pod2))
  70. }
  71. // GetPodAffinityTerms gets pod affinity terms by a pod affinity object.
  72. func GetPodAffinityTerms(podAffinity *v1.PodAffinity) (terms []v1.PodAffinityTerm) {
  73. if podAffinity != nil {
  74. if len(podAffinity.RequiredDuringSchedulingIgnoredDuringExecution) != 0 {
  75. terms = podAffinity.RequiredDuringSchedulingIgnoredDuringExecution
  76. }
  77. // TODO: Uncomment this block when implement RequiredDuringSchedulingRequiredDuringExecution.
  78. //if len(podAffinity.RequiredDuringSchedulingRequiredDuringExecution) != 0 {
  79. // terms = append(terms, podAffinity.RequiredDuringSchedulingRequiredDuringExecution...)
  80. //}
  81. }
  82. return terms
  83. }
  84. // GetPodAntiAffinityTerms gets pod affinity terms by a pod anti-affinity.
  85. func GetPodAntiAffinityTerms(podAntiAffinity *v1.PodAntiAffinity) (terms []v1.PodAffinityTerm) {
  86. if podAntiAffinity != nil {
  87. if len(podAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution) != 0 {
  88. terms = podAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution
  89. }
  90. // TODO: Uncomment this block when implement RequiredDuringSchedulingRequiredDuringExecution.
  91. //if len(podAntiAffinity.RequiredDuringSchedulingRequiredDuringExecution) != 0 {
  92. // terms = append(terms, podAntiAffinity.RequiredDuringSchedulingRequiredDuringExecution...)
  93. //}
  94. }
  95. return terms
  96. }