@@ -30,16 +30,22 @@ import (
30
30
"k8s.io/apiserver/pkg/admission"
31
31
"k8s.io/client-go/informers"
32
32
"k8s.io/client-go/kubernetes"
33
+ clientset "k8s.io/client-go/kubernetes"
33
34
restclient "k8s.io/client-go/rest"
35
+ podutil "k8s.io/kubernetes/pkg/api/v1/pod"
34
36
"k8s.io/kubernetes/pkg/controller/nodelifecycle"
35
37
"k8s.io/kubernetes/plugin/pkg/admission/defaulttolerationseconds"
36
38
"k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction"
37
39
pluginapi "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction"
38
- "k8s.io/kubernetes/test/e2e/framework/pod"
39
40
testutils "k8s.io/kubernetes/test/integration/util"
40
41
imageutils "k8s.io/kubernetes/test/utils/image"
41
42
)
42
43
44
+ // poll is how often to poll pods, nodes and claims.
45
+ const poll = 2 * time .Second
46
+
47
+ type podCondition func (pod * v1.Pod ) (bool , error )
48
+
43
49
// TestTaintBasedEvictions tests related cases for the TaintBasedEvictions feature
44
50
func TestTaintBasedEvictions (t * testing.T ) {
45
51
// we need at least 2 nodes to prevent lifecycle manager from entering "fully-disrupted" mode
@@ -275,7 +281,7 @@ func TestTaintBasedEvictions(t *testing.T) {
275
281
}
276
282
277
283
if test .pod != nil {
278
- err = pod . WaitForPodCondition (cs , testCtx .NS .Name , test .pod .Name , test .waitForPodCondition , time .Second * 15 , func (pod * v1.Pod ) (bool , error ) {
284
+ err = waitForPodCondition (cs , testCtx .NS .Name , test .pod .Name , test .waitForPodCondition , time .Second * 15 , func (pod * v1.Pod ) (bool , error ) {
279
285
// as node is unreachable, pod0 is expected to be in Terminating status
280
286
// rather than getting deleted
281
287
if tolerationSeconds [i ] == 0 {
@@ -285,7 +291,7 @@ func TestTaintBasedEvictions(t *testing.T) {
285
291
return seconds == tolerationSeconds [i ], nil
286
292
}
287
293
return false , nil
288
- })
294
+ }, t )
289
295
if err != nil {
290
296
pod , _ := cs .CoreV1 ().Pods (testCtx .NS .Name ).Get (context .TODO (), test .pod .Name , metav1.GetOptions {})
291
297
t .Fatalf ("Error: %v, Expected test pod to be %s but it's %v" , err , test .waitForPodCondition , pod )
@@ -297,3 +303,29 @@ func TestTaintBasedEvictions(t *testing.T) {
297
303
})
298
304
}
299
305
}
306
+
307
+ // waitForPodCondition waits a pods to be matched to the given condition.
308
+ func waitForPodCondition (c clientset.Interface , ns , podName , desc string , timeout time.Duration , condition podCondition , t * testing.T ) error {
309
+ t .Logf ("Waiting up to %v for pod %q in namespace %q to be %q" , timeout , podName , ns , desc )
310
+ for start := time .Now (); time .Since (start ) < timeout ; time .Sleep (poll ) {
311
+ pod , err := c .CoreV1 ().Pods (ns ).Get (context .TODO (), podName , metav1.GetOptions {})
312
+ if err != nil {
313
+ if apierrors .IsNotFound (err ) {
314
+ t .Logf ("Pod %q in namespace %q not found. Error: %v" , podName , ns , err )
315
+ return err
316
+ }
317
+ t .Logf ("Get pod %q in namespace %q failed, ignoring for %v. Error: %v" , podName , ns , poll , err )
318
+ continue
319
+ }
320
+ // log now so that current pod info is reported before calling `condition()`
321
+ t .Logf ("Pod %q: Phase=%q, Reason=%q, readiness=%t. Elapsed: %v" ,
322
+ podName , pod .Status .Phase , pod .Status .Reason , podutil .IsPodReady (pod ), time .Since (start ))
323
+ if done , err := condition (pod ); done {
324
+ if err == nil {
325
+ t .Logf ("Pod %q satisfied condition %q" , podName , desc )
326
+ }
327
+ return err
328
+ }
329
+ }
330
+ return fmt .Errorf ("gave up after waiting %v for pod %q to be %q" , timeout , podName , desc )
331
+ }
0 commit comments