@@ -23,7 +23,10 @@ import (
23
23
"github.com/google/go-cmp/cmp"
24
24
v1 "k8s.io/api/core/v1"
25
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
+ utilfeature "k8s.io/apiserver/pkg/util/feature"
27
+ featuregatetesting "k8s.io/component-base/featuregate/testing"
26
28
"k8s.io/klog/v2/ktesting"
29
+ "k8s.io/kubernetes/pkg/features"
27
30
clocktesting "k8s.io/utils/clock/testing"
28
31
"k8s.io/utils/ptr"
29
32
)
@@ -200,8 +203,10 @@ func TestNewBackoffRecord(t *testing.T) {
200
203
}
201
204
202
205
func TestGetFinishedTime (t * testing.T ) {
206
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .SidecarContainers , true )
203
207
defaultTestTime := time .Date (2009 , time .November , 10 , 23 , 0 , 0 , 0 , time .UTC )
204
208
defaultTestTimeMinus30s := defaultTestTime .Add (- 30 * time .Second )
209
+ containerRestartPolicyAlways := v1 .ContainerRestartPolicyAlways
205
210
testCases := map [string ]struct {
206
211
pod v1.Pod
207
212
wantFinishTime time.Time
@@ -355,6 +360,39 @@ func TestGetFinishedTime(t *testing.T) {
355
360
},
356
361
wantFinishTime : defaultTestTime ,
357
362
},
363
+ // In this case, init container is stopped after the regular containers.
364
+ // This is because with the sidecar (restartable init) containers,
365
+ // sidecar containers will always finish later than regular containers.
366
+ "Pod with sidecar container and all containers terminated" : {
367
+ pod : v1.Pod {
368
+ Spec : v1.PodSpec {
369
+ InitContainers : []v1.Container {
370
+ {
371
+ Name : "sidecar" ,
372
+ RestartPolicy : & containerRestartPolicyAlways ,
373
+ },
374
+ },
375
+ },
376
+ Status : v1.PodStatus {
377
+ ContainerStatuses : []v1.ContainerStatus {
378
+ {
379
+ State : v1.ContainerState {
380
+ Terminated : & v1.ContainerStateTerminated {FinishedAt : metav1 .NewTime (defaultTestTime .Add (- 1 * time .Second ))},
381
+ },
382
+ },
383
+ },
384
+ InitContainerStatuses : []v1.ContainerStatus {
385
+ {
386
+ Name : "sidecar" ,
387
+ State : v1.ContainerState {
388
+ Terminated : & v1.ContainerStateTerminated {FinishedAt : metav1 .NewTime (defaultTestTime )},
389
+ },
390
+ },
391
+ },
392
+ },
393
+ },
394
+ wantFinishTime : defaultTestTime ,
395
+ },
358
396
}
359
397
360
398
for name , tc := range testCases {
0 commit comments