...
1
16
17 package volumescheduling
18
19 import (
20 "context"
21 "time"
22
23 v1 "k8s.io/api/core/v1"
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/util/wait"
26 clientset "k8s.io/client-go/kubernetes"
27 podutil "k8s.io/kubernetes/pkg/api/v1/pod"
28 )
29
30
31
32 func waitForPodToScheduleWithTimeout(cs clientset.Interface, pod *v1.Pod, timeout time.Duration) error {
33 return wait.Poll(100*time.Millisecond, timeout, podScheduled(cs, pod.Namespace, pod.Name))
34 }
35
36
37
38 func waitForPodToSchedule(cs clientset.Interface, pod *v1.Pod) error {
39 return waitForPodToScheduleWithTimeout(cs, pod, 30*time.Second)
40 }
41
42
43
44 func waitForPodUnschedulableWithTimeout(cs clientset.Interface, pod *v1.Pod, timeout time.Duration) error {
45 return wait.Poll(100*time.Millisecond, timeout, podUnschedulable(cs, pod.Namespace, pod.Name))
46 }
47
48
49
50 func waitForPodUnschedulable(cs clientset.Interface, pod *v1.Pod) error {
51 return waitForPodUnschedulableWithTimeout(cs, pod, 30*time.Second)
52 }
53
54
55 func podScheduled(c clientset.Interface, podNamespace, podName string) wait.ConditionFunc {
56 return func() (bool, error) {
57 pod, err := c.CoreV1().Pods(podNamespace).Get(context.TODO(), podName, metav1.GetOptions{})
58 if err != nil {
59
60 return false, nil
61 }
62 if pod.Spec.NodeName == "" {
63 return false, nil
64 }
65 return true, nil
66 }
67 }
68
69
70
71 func podUnschedulable(c clientset.Interface, podNamespace, podName string) wait.ConditionFunc {
72 return func() (bool, error) {
73 pod, err := c.CoreV1().Pods(podNamespace).Get(context.TODO(), podName, metav1.GetOptions{})
74 if err != nil {
75
76 return false, nil
77 }
78 _, cond := podutil.GetPodCondition(&pod.Status, v1.PodScheduled)
79 return cond != nil && cond.Status == v1.ConditionFalse &&
80 cond.Reason == v1.PodReasonUnschedulable, nil
81 }
82 }
83
View as plain text