2019-05-31 09:45:11 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-01-14 10:38:55 +00:00
|
|
|
"time"
|
2019-05-31 09:45:11 +00:00
|
|
|
|
2020-01-14 10:38:55 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2019-06-24 09:08:09 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/klog"
|
2020-01-14 10:38:55 +00:00
|
|
|
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
|
|
|
|
extenderv1 "k8s.io/kubernetes/pkg/scheduler/apis/extender/v1"
|
2019-05-31 09:45:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetContainerPorts returns the used host ports of Pods: if 'port' was used, a 'port:true' pair
|
|
|
|
// will be in the result; but it does not resolve port conflict.
|
|
|
|
func GetContainerPorts(pods ...*v1.Pod) []*v1.ContainerPort {
|
|
|
|
var ports []*v1.ContainerPort
|
|
|
|
for _, pod := range pods {
|
|
|
|
for j := range pod.Spec.Containers {
|
|
|
|
container := &pod.Spec.Containers[j]
|
|
|
|
for k := range container.Ports {
|
|
|
|
ports = append(ports, &container.Ports[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ports
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPodFullName returns a name that uniquely identifies a pod.
|
|
|
|
func GetPodFullName(pod *v1.Pod) string {
|
|
|
|
// Use underscore as the delimiter because it is not allowed in pod name
|
|
|
|
// (DNS subdomain format).
|
|
|
|
return pod.Name + "_" + pod.Namespace
|
|
|
|
}
|
|
|
|
|
2019-06-24 09:08:09 +00:00
|
|
|
// GetPodStartTime returns start time of the given pod.
|
|
|
|
func GetPodStartTime(pod *v1.Pod) *metav1.Time {
|
|
|
|
if pod.Status.StartTime != nil {
|
|
|
|
return pod.Status.StartTime
|
|
|
|
}
|
|
|
|
// Should not reach here as the start time of a running time should not be nil
|
|
|
|
// Return current timestamp as the default value.
|
|
|
|
// This will not affect the calculation of earliest timestamp of all the pods on one node,
|
|
|
|
// because current timestamp is always after the StartTime of any pod in good state.
|
|
|
|
klog.Errorf("pod.Status.StartTime is nil for pod %s. Should not reach here.", pod.Name)
|
|
|
|
return &metav1.Time{Time: time.Now()}
|
|
|
|
}
|
|
|
|
|
2020-01-14 10:38:55 +00:00
|
|
|
// lessFunc is a function that receives two items and returns true if the first
|
|
|
|
// item should be placed before the second one when the list is sorted.
|
|
|
|
type lessFunc = func(item1, item2 interface{}) bool
|
|
|
|
|
2019-06-24 09:08:09 +00:00
|
|
|
// GetEarliestPodStartTime returns the earliest start time of all pods that
|
|
|
|
// have the highest priority among all victims.
|
2020-01-14 10:38:55 +00:00
|
|
|
func GetEarliestPodStartTime(victims *extenderv1.Victims) *metav1.Time {
|
2019-06-24 09:08:09 +00:00
|
|
|
if len(victims.Pods) == 0 {
|
|
|
|
// should not reach here.
|
|
|
|
klog.Errorf("victims.Pods is empty. Should not reach here.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
earliestPodStartTime := GetPodStartTime(victims.Pods[0])
|
2020-01-14 10:38:55 +00:00
|
|
|
maxPriority := podutil.GetPodPriority(victims.Pods[0])
|
2019-06-24 09:08:09 +00:00
|
|
|
|
|
|
|
for _, pod := range victims.Pods {
|
2020-01-14 10:38:55 +00:00
|
|
|
if podutil.GetPodPriority(pod) == maxPriority {
|
2019-06-24 09:08:09 +00:00
|
|
|
if GetPodStartTime(pod).Before(earliestPodStartTime) {
|
|
|
|
earliestPodStartTime = GetPodStartTime(pod)
|
|
|
|
}
|
2020-01-14 10:38:55 +00:00
|
|
|
} else if podutil.GetPodPriority(pod) > maxPriority {
|
|
|
|
maxPriority = podutil.GetPodPriority(pod)
|
2019-06-24 09:08:09 +00:00
|
|
|
earliestPodStartTime = GetPodStartTime(pod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return earliestPodStartTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoreImportantPod return true when priority of the first pod is higher than
|
|
|
|
// the second one. If two pods' priorities are equal, compare their StartTime.
|
|
|
|
// It takes arguments of the type "interface{}" to be used with SortableList,
|
|
|
|
// but expects those arguments to be *v1.Pod.
|
2020-01-14 10:38:55 +00:00
|
|
|
func MoreImportantPod(pod1, pod2 *v1.Pod) bool {
|
|
|
|
p1 := podutil.GetPodPriority(pod1)
|
|
|
|
p2 := podutil.GetPodPriority(pod2)
|
2019-06-24 09:08:09 +00:00
|
|
|
if p1 != p2 {
|
|
|
|
return p1 > p2
|
|
|
|
}
|
2020-01-14 10:38:55 +00:00
|
|
|
return GetPodStartTime(pod1).Before(GetPodStartTime(pod2))
|
2019-05-31 09:45:11 +00:00
|
|
|
}
|