utils.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "sort"
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apiserver/pkg/util/feature"
  19. "k8s.io/klog"
  20. "k8s.io/kubernetes/pkg/apis/scheduling"
  21. "k8s.io/kubernetes/pkg/features"
  22. "k8s.io/kubernetes/pkg/scheduler/api"
  23. "time"
  24. )
  25. // GetContainerPorts returns the used host ports of Pods: if 'port' was used, a 'port:true' pair
  26. // will be in the result; but it does not resolve port conflict.
  27. func GetContainerPorts(pods ...*v1.Pod) []*v1.ContainerPort {
  28. var ports []*v1.ContainerPort
  29. for _, pod := range pods {
  30. for j := range pod.Spec.Containers {
  31. container := &pod.Spec.Containers[j]
  32. for k := range container.Ports {
  33. ports = append(ports, &container.Ports[k])
  34. }
  35. }
  36. }
  37. return ports
  38. }
  39. // PodPriorityEnabled indicates whether pod priority feature is enabled.
  40. func PodPriorityEnabled() bool {
  41. return feature.DefaultFeatureGate.Enabled(features.PodPriority)
  42. }
  43. // GetPodFullName returns a name that uniquely identifies a pod.
  44. func GetPodFullName(pod *v1.Pod) string {
  45. // Use underscore as the delimiter because it is not allowed in pod name
  46. // (DNS subdomain format).
  47. return pod.Name + "_" + pod.Namespace
  48. }
  49. // GetPodPriority returns priority of the given pod.
  50. func GetPodPriority(pod *v1.Pod) int32 {
  51. if pod.Spec.Priority != nil {
  52. return *pod.Spec.Priority
  53. }
  54. // When priority of a running pod is nil, it means it was created at a time
  55. // that there was no global default priority class and the priority class
  56. // name of the pod was empty. So, we resolve to the static default priority.
  57. return scheduling.DefaultPriorityWhenNoDefaultClassExists
  58. }
  59. // GetPodStartTime returns start time of the given pod.
  60. func GetPodStartTime(pod *v1.Pod) *metav1.Time {
  61. if pod.Status.StartTime != nil {
  62. return pod.Status.StartTime
  63. }
  64. // Should not reach here as the start time of a running time should not be nil
  65. // Return current timestamp as the default value.
  66. // This will not affect the calculation of earliest timestamp of all the pods on one node,
  67. // because current timestamp is always after the StartTime of any pod in good state.
  68. klog.Errorf("pod.Status.StartTime is nil for pod %s. Should not reach here.", pod.Name)
  69. return &metav1.Time{Time: time.Now()}
  70. }
  71. // GetEarliestPodStartTime returns the earliest start time of all pods that
  72. // have the highest priority among all victims.
  73. func GetEarliestPodStartTime(victims *api.Victims) *metav1.Time {
  74. if len(victims.Pods) == 0 {
  75. // should not reach here.
  76. klog.Errorf("victims.Pods is empty. Should not reach here.")
  77. return nil
  78. }
  79. earliestPodStartTime := GetPodStartTime(victims.Pods[0])
  80. highestPriority := GetPodPriority(victims.Pods[0])
  81. for _, pod := range victims.Pods {
  82. if GetPodPriority(pod) == highestPriority {
  83. if GetPodStartTime(pod).Before(earliestPodStartTime) {
  84. earliestPodStartTime = GetPodStartTime(pod)
  85. }
  86. } else if GetPodPriority(pod) > highestPriority {
  87. highestPriority = GetPodPriority(pod)
  88. earliestPodStartTime = GetPodStartTime(pod)
  89. }
  90. }
  91. return earliestPodStartTime
  92. }
  93. // SortableList is a list that implements sort.Interface.
  94. type SortableList struct {
  95. Items []interface{}
  96. CompFunc LessFunc
  97. }
  98. // LessFunc is a function that receives two items and returns true if the first
  99. // item should be placed before the second one when the list is sorted.
  100. type LessFunc func(item1, item2 interface{}) bool
  101. var _ = sort.Interface(&SortableList{})
  102. func (l *SortableList) Len() int { return len(l.Items) }
  103. func (l *SortableList) Less(i, j int) bool {
  104. return l.CompFunc(l.Items[i], l.Items[j])
  105. }
  106. func (l *SortableList) Swap(i, j int) {
  107. l.Items[i], l.Items[j] = l.Items[j], l.Items[i]
  108. }
  109. // Sort sorts the items in the list using the given CompFunc. Item1 is placed
  110. // before Item2 when CompFunc(Item1, Item2) returns true.
  111. func (l *SortableList) Sort() {
  112. sort.Sort(l)
  113. }
  114. // MoreImportantPod return true when priority of the first pod is higher than
  115. // the second one. If two pods' priorities are equal, compare their StartTime.
  116. // It takes arguments of the type "interface{}" to be used with SortableList,
  117. // but expects those arguments to be *v1.Pod.
  118. func MoreImportantPod(pod1, pod2 interface{}) bool {
  119. p1 := GetPodPriority(pod1.(*v1.Pod))
  120. p2 := GetPodPriority(pod2.(*v1.Pod))
  121. if p1 != p2 {
  122. return p1 > p2
  123. }
  124. return GetPodStartTime(pod1.(*v1.Pod)).Before(GetPodStartTime(pod2.(*v1.Pod)))
  125. }