@@ -13,13 +13,14 @@ import (
13
13
v1 "k8s.io/api/core/v1"
14
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
15
"k8s.io/apimachinery/pkg/util/uuid"
16
- kubeletevents "k8s.io/kubernetes/pkg/kubelet/events"
17
16
"k8s.io/kubernetes/test/e2e/feature"
18
17
"k8s.io/kubernetes/test/e2e/framework"
19
18
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
20
19
admissionapi "k8s.io/pod-security-admission/api"
21
20
)
22
21
22
+ const containerName = "restarts"
23
+
23
24
var _ = SIGDescribe ("Container Restart" , feature .CriProxy , framework .WithSerial (), func () {
24
25
f := framework .NewDefaultFramework ("container-restart" )
25
26
f .NamespacePodSecurityLevel = admissionapi .LevelPrivileged
@@ -38,8 +39,8 @@ var _ = SIGDescribe("Container Restart", feature.CriProxy, framework.WithSerial(
38
39
})
39
40
40
41
ginkgo .It ("Container restart backs off." , func (ctx context.Context ) {
41
- // 3 would take 10s best case, 6 would take 150s best case
42
- doTest (ctx , f , 5 , time . Duration ( 80 * time . Second ), time . Duration ( 10 * time . Second ) )
42
+ // 0s, 0s, 10s, 30s, 70s, 150s, 310s
43
+ doTest (ctx , f , 5 , containerName , 7 )
43
44
})
44
45
})
45
46
@@ -62,41 +63,42 @@ var _ = SIGDescribe("Container Restart", feature.CriProxy, framework.WithSerial(
62
63
})
63
64
64
65
ginkgo .It ("Alternate restart backs off." , func (ctx context.Context ) {
65
- doTest (ctx , f , 7 , time .Duration (120 * time .Second ), time .Duration (10 * time .Second ))
66
+ // 0s, 0s, 10s, 30s, 60s, 90s, 120s, 150, 180, 210)
67
+ doTest (ctx , f , 7 , containerName , 10 )
66
68
})
67
69
})
68
70
})
69
71
70
- func doTest (ctx context.Context , f * framework.Framework , maxRestarts int , target time. Duration , threshold time. Duration ) {
72
+ func doTest (ctx context.Context , f * framework.Framework , targetRestarts int , containerName string , maxRestarts int ) {
71
73
72
74
pod := e2epod .NewPodClient (f ).Create (ctx , newFailAlwaysPod ())
73
75
podErr := e2epod .WaitForPodContainerToFail (ctx , f .ClientSet , f .Namespace .Name , pod .Name , 0 , "CrashLoopBackOff" , 1 * time .Minute )
74
76
gomega .Expect (podErr ).To (gomega .HaveOccurred ())
75
77
76
- // Wait for 120s worth of backoffs to occur so we can confirm the backoff growth.
77
- podErr = e2epod .WaitForContainerRestartedNTimes (ctx , f .ClientSet , f .Namespace .Name , pod .Name , "restart" , 150 * time .Second , maxRestarts )
78
+ // Wait for 150s worth of backoffs to occur so we can confirm the backoff growth.
79
+ podErr = e2epod .WaitForContainerRestartedNTimes (ctx , f .ClientSet , f .Namespace .Name , pod .Name , "restart" , 150 * time .Second , targetRestarts )
78
80
gomega .Expect (podErr ).ShouldNot (gomega .HaveOccurred (), "Expected container to repeatedly back off container failures" )
79
81
80
- d , err := getContainerRetryDuration (ctx , f , pod .Name )
82
+ r , err := extractObservedBackoff (ctx , f , pod .Name , containerName )
81
83
framework .ExpectNoError (err )
82
84
83
- gomega .Expect (d ).Should (gomega .BeNumerically ("~ " , target , threshold ))
85
+ gomega .Expect (r ).Should (gomega .BeNumerically ("<= " , maxRestarts ))
84
86
}
85
87
86
- func getContainerRetryDuration (ctx context.Context , f * framework.Framework , podName string ) (time.Duration , error ) {
87
-
88
- var d time.Duration
89
- e , err := f .ClientSet .CoreV1 ().Events (f .Namespace .Name ).List (ctx , metav1.ListOptions {})
88
+ func extractObservedBackoff (ctx context.Context , f * framework.Framework , podName string , containerName string ) (int32 , error ) {
89
+ var r int32
90
+ pod , err := f .ClientSet .CoreV1 ().Pods (f .Namespace .Name ).Get (ctx , podName , metav1.GetOptions {})
90
91
if err != nil {
91
- return d , err
92
+ return r , err
92
93
}
93
-
94
- for _ , event := range e .Items {
95
- if event .InvolvedObject .Name == podName && event .Reason == kubeletevents .StartedContainer {
96
- return event .LastTimestamp .Time .Sub (event .FirstTimestamp .Time ), nil
94
+ for _ , statuses := range [][]v1.ContainerStatus {pod .Status .ContainerStatuses , pod .Status .InitContainerStatuses , pod .Status .EphemeralContainerStatuses } {
95
+ for _ , cs := range statuses {
96
+ if cs .Name == containerName {
97
+ return r , nil
98
+ }
97
99
}
98
100
}
99
- return d , nil
101
+ return r , nil
100
102
}
101
103
102
104
func newFailAlwaysPod () * v1.Pod {
@@ -108,10 +110,9 @@ func newFailAlwaysPod() *v1.Pod {
108
110
Spec : v1.PodSpec {
109
111
Containers : []v1.Container {
110
112
{
111
- Name : "restart" ,
113
+ Name : containerName ,
112
114
Image : imageutils .GetBusyBoxImageName (),
113
- ImagePullPolicy : v1 .PullAlways ,
114
- Command : []string {"exit 1" },
115
+ ImagePullPolicy : v1 .PullIfNotPresent ,
115
116
},
116
117
},
117
118
},
0 commit comments