-
Notifications
You must be signed in to change notification settings - Fork 97
/
util.go
95 lines (82 loc) · 3.02 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Copyright 2023 The KubeAdmiral 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 automigration
import (
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// Returns the number of unschedulable pods that remain
// unschedulable for more than unschedulableThreshold,
// and a time.Duration representing the time from now
// when the new unschedulable pod will cross the threshold, if any.
func countScheduledAndUnschedulablePods(
podList []*corev1.Pod,
currentTime time.Time,
unschedulableThreshold time.Duration,
) (scheduledCount, unschedulableCount int, nextCrossIn *time.Duration) {
for _, pod := range podList {
if pod.GetDeletionTimestamp() != nil {
continue
}
scheduledCondition, isUnschedulable := getPodScheduledCondition(pod)
if !isUnschedulable {
if scheduledCondition != nil && scheduledCondition.Status == corev1.ConditionTrue {
scheduledCount++
}
continue
}
timeBecameUnschedulable := scheduledCondition.LastTransitionTime
timeCrossingThreshold := timeBecameUnschedulable.Add(unschedulableThreshold)
crossingThresholdIn := timeCrossingThreshold.Sub(currentTime)
if crossingThresholdIn <= 0 {
unschedulableCount++
} else if nextCrossIn == nil || *nextCrossIn > crossingThresholdIn {
nextCrossIn = &crossingThresholdIn
}
}
return scheduledCount, unschedulableCount, nextCrossIn
}
func getPodScheduledCondition(pod *corev1.Pod) (scheduledCondition *corev1.PodCondition, isUnschedulable bool) {
for i := range pod.Status.Conditions {
condition := &pod.Status.Conditions[i]
if condition.Type == corev1.PodScheduled {
scheduledCondition = condition
break
}
}
if scheduledCondition == nil ||
scheduledCondition.Status != corev1.ConditionFalse ||
scheduledCondition.Reason != corev1.PodReasonUnschedulable {
return scheduledCondition, false
}
return scheduledCondition, true
}
func podScheduledConditionChanged(oldPod, newPod *corev1.Pod) bool {
condition, _ := getPodScheduledCondition(newPod)
oldCondition, _ := getPodScheduledCondition(oldPod)
if condition == nil || oldCondition == nil {
return condition != oldCondition
}
isEqual := condition.Status == oldCondition.Status &&
condition.Reason == oldCondition.Reason &&
condition.Message == oldCondition.Message &&
condition.LastProbeTime.Equal(&oldCondition.LastProbeTime) &&
condition.LastTransitionTime.Equal(&oldCondition.LastTransitionTime)
return !isEqual
}
// An object with an origin information.
type FederatedObject struct {
Object *unstructured.Unstructured
ClusterName string
}