@@ -31,6 +31,7 @@ import (
31
31
"k8s.io/apimachinery/pkg/api/resource"
32
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
33
"k8s.io/apimachinery/pkg/types"
34
+ "k8s.io/apimachinery/pkg/util/uuid"
34
35
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
35
36
critest "k8s.io/cri-api/pkg/apis/testing"
36
37
statsapi "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
@@ -77,6 +78,8 @@ const (
77
78
cName2 = "container2-name"
78
79
cName3 = "container3-name"
79
80
cName5 = "container5-name"
81
+ cName6 = "container6-name"
82
+ cName7 = "container7-name"
80
83
)
81
84
82
85
func TestCRIListPodStats (t * testing.T ) {
@@ -86,7 +89,7 @@ func TestCRIListPodStats(t *testing.T) {
86
89
imageFsInfo = getTestFsInfo (2000 )
87
90
rootFsInfo = getTestFsInfo (1000 )
88
91
89
- sandbox0 = makeFakePodSandbox ("sandbox0-name" , "sandbox0-uid" , "sandbox0-ns" )
92
+ sandbox0 = makeFakePodSandbox ("sandbox0-name" , "sandbox0-uid" , "sandbox0-ns" , false )
90
93
sandbox0Cgroup = "/" + cm .GetPodCgroupNameSuffix (types .UID (sandbox0 .PodSandboxStatus .Metadata .Uid ))
91
94
container0 = makeFakeContainer (sandbox0 , cName0 , 0 , false )
92
95
containerStats0 = makeFakeContainerStats (container0 , imageFsMountpoint )
@@ -95,25 +98,35 @@ func TestCRIListPodStats(t *testing.T) {
95
98
containerStats1 = makeFakeContainerStats (container1 , unknownMountpoint )
96
99
containerLogStats1 = makeFakeLogStats (2000 )
97
100
98
- sandbox1 = makeFakePodSandbox ("sandbox1-name" , "sandbox1-uid" , "sandbox1-ns" )
101
+ sandbox1 = makeFakePodSandbox ("sandbox1-name" , "sandbox1-uid" , "sandbox1-ns" , false )
99
102
sandbox1Cgroup = "/" + cm .GetPodCgroupNameSuffix (types .UID (sandbox1 .PodSandboxStatus .Metadata .Uid ))
100
103
container2 = makeFakeContainer (sandbox1 , cName2 , 0 , false )
101
104
containerStats2 = makeFakeContainerStats (container2 , imageFsMountpoint )
102
105
containerLogStats2 = makeFakeLogStats (3000 )
103
106
104
- sandbox2 = makeFakePodSandbox ("sandbox2-name" , "sandbox2-uid" , "sandbox2-ns" )
107
+ sandbox2 = makeFakePodSandbox ("sandbox2-name" , "sandbox2-uid" , "sandbox2-ns" , false )
105
108
sandbox2Cgroup = "/" + cm .GetPodCgroupNameSuffix (types .UID (sandbox2 .PodSandboxStatus .Metadata .Uid ))
106
109
container3 = makeFakeContainer (sandbox2 , cName3 , 0 , true )
107
110
containerStats3 = makeFakeContainerStats (container3 , imageFsMountpoint )
108
111
container4 = makeFakeContainer (sandbox2 , cName3 , 1 , false )
109
112
containerStats4 = makeFakeContainerStats (container4 , imageFsMountpoint )
110
113
containerLogStats4 = makeFakeLogStats (4000 )
111
114
112
- sandbox3 = makeFakePodSandbox ("sandbox3-name" , "sandbox3-uid" , "sandbox3-ns" )
115
+ sandbox3 = makeFakePodSandbox ("sandbox3-name" , "sandbox3-uid" , "sandbox3-ns" , false )
113
116
container5 = makeFakeContainer (sandbox3 , cName5 , 0 , true )
114
117
containerStats5 = makeFakeContainerStats (container5 , imageFsMountpoint )
115
118
containerLogStats5 = makeFakeLogStats (5000 )
116
119
120
+ // Terminated pod sandbox
121
+ sandbox4 = makeFakePodSandbox ("sandbox1-name" , "sandbox1-uid" , "sandbox1-ns" , true )
122
+ container6 = makeFakeContainer (sandbox4 , cName6 , 0 , true )
123
+ containerStats6 = makeFakeContainerStats (container6 , imageFsMountpoint )
124
+
125
+ // Terminated pod
126
+ sandbox5 = makeFakePodSandbox ("sandbox1-name" , "sandbox5-uid" , "sandbox1-ns" , true )
127
+ container7 = makeFakeContainer (sandbox5 , cName7 , 0 , true )
128
+ containerStats7 = makeFakeContainerStats (container7 , imageFsMountpoint )
129
+
117
130
podLogName0 = "pod-log-0"
118
131
podLogName1 = "pod-log-1"
119
132
podLogStats0 = makeFakeLogStats (5000 )
@@ -157,13 +170,13 @@ func TestCRIListPodStats(t *testing.T) {
157
170
On ("GetDirFsInfo" , imageFsMountpoint ).Return (imageFsInfo , nil ).
158
171
On ("GetDirFsInfo" , unknownMountpoint ).Return (cadvisorapiv2.FsInfo {}, cadvisorfs .ErrNoSuchDevice )
159
172
fakeRuntimeService .SetFakeSandboxes ([]* critest.FakePodSandbox {
160
- sandbox0 , sandbox1 , sandbox2 , sandbox3 ,
173
+ sandbox0 , sandbox1 , sandbox2 , sandbox3 , sandbox4 , sandbox5 ,
161
174
})
162
175
fakeRuntimeService .SetFakeContainers ([]* critest.FakeContainer {
163
- container0 , container1 , container2 , container3 , container4 , container5 ,
176
+ container0 , container1 , container2 , container3 , container4 , container5 , container6 , container7 ,
164
177
})
165
178
fakeRuntimeService .SetFakeContainerStats ([]* runtimeapi.ContainerStats {
166
- containerStats0 , containerStats1 , containerStats2 , containerStats3 , containerStats4 , containerStats5 ,
179
+ containerStats0 , containerStats1 , containerStats2 , containerStats3 , containerStats4 , containerStats5 , containerStats6 , containerStats7 ,
167
180
})
168
181
169
182
ephemeralVolumes := makeFakeVolumeStats ([]string {"ephVolume1, ephVolumes2" })
@@ -304,28 +317,38 @@ func TestCRIListPodCPUAndMemoryStats(t *testing.T) {
304
317
imageFsMountpoint = "/test/mount/point"
305
318
unknownMountpoint = "/unknown/mount/point"
306
319
307
- sandbox0 = makeFakePodSandbox ("sandbox0-name" , "sandbox0-uid" , "sandbox0-ns" )
320
+ sandbox0 = makeFakePodSandbox ("sandbox0-name" , "sandbox0-uid" , "sandbox0-ns" , false )
308
321
sandbox0Cgroup = "/" + cm .GetPodCgroupNameSuffix (types .UID (sandbox0 .PodSandboxStatus .Metadata .Uid ))
309
322
container0 = makeFakeContainer (sandbox0 , cName0 , 0 , false )
310
323
containerStats0 = makeFakeContainerStats (container0 , imageFsMountpoint )
311
324
container1 = makeFakeContainer (sandbox0 , cName1 , 0 , false )
312
325
containerStats1 = makeFakeContainerStats (container1 , unknownMountpoint )
313
326
314
- sandbox1 = makeFakePodSandbox ("sandbox1-name" , "sandbox1-uid" , "sandbox1-ns" )
327
+ sandbox1 = makeFakePodSandbox ("sandbox1-name" , "sandbox1-uid" , "sandbox1-ns" , false )
315
328
sandbox1Cgroup = "/" + cm .GetPodCgroupNameSuffix (types .UID (sandbox1 .PodSandboxStatus .Metadata .Uid ))
316
329
container2 = makeFakeContainer (sandbox1 , cName2 , 0 , false )
317
330
containerStats2 = makeFakeContainerStats (container2 , imageFsMountpoint )
318
331
319
- sandbox2 = makeFakePodSandbox ("sandbox2-name" , "sandbox2-uid" , "sandbox2-ns" )
332
+ sandbox2 = makeFakePodSandbox ("sandbox2-name" , "sandbox2-uid" , "sandbox2-ns" , false )
320
333
sandbox2Cgroup = "/" + cm .GetPodCgroupNameSuffix (types .UID (sandbox2 .PodSandboxStatus .Metadata .Uid ))
321
334
container3 = makeFakeContainer (sandbox2 , cName3 , 0 , true )
322
335
containerStats3 = makeFakeContainerStats (container3 , imageFsMountpoint )
323
336
container4 = makeFakeContainer (sandbox2 , cName3 , 1 , false )
324
337
containerStats4 = makeFakeContainerStats (container4 , imageFsMountpoint )
325
338
326
- sandbox3 = makeFakePodSandbox ("sandbox3-name" , "sandbox3-uid" , "sandbox3-ns" )
339
+ sandbox3 = makeFakePodSandbox ("sandbox3-name" , "sandbox3-uid" , "sandbox3-ns" , false )
327
340
container5 = makeFakeContainer (sandbox3 , cName5 , 0 , true )
328
341
containerStats5 = makeFakeContainerStats (container5 , imageFsMountpoint )
342
+
343
+ // Terminated pod sandbox
344
+ sandbox4 = makeFakePodSandbox ("sandbox1-name" , "sandbox1-uid" , "sandbox1-ns" , true )
345
+ container6 = makeFakeContainer (sandbox4 , cName6 , 0 , true )
346
+ containerStats6 = makeFakeContainerStats (container6 , imageFsMountpoint )
347
+
348
+ // Terminated pod
349
+ sandbox5 = makeFakePodSandbox ("sandbox1-name" , "sandbox5-uid" , "sandbox1-ns" , true )
350
+ container7 = makeFakeContainer (sandbox5 , cName7 , 0 , true )
351
+ containerStats7 = makeFakeContainerStats (container7 , imageFsMountpoint )
329
352
)
330
353
331
354
var (
@@ -361,13 +384,13 @@ func TestCRIListPodCPUAndMemoryStats(t *testing.T) {
361
384
mockCadvisor .
362
385
On ("ContainerInfoV2" , "/" , options ).Return (infos , nil )
363
386
fakeRuntimeService .SetFakeSandboxes ([]* critest.FakePodSandbox {
364
- sandbox0 , sandbox1 , sandbox2 , sandbox3 ,
387
+ sandbox0 , sandbox1 , sandbox2 , sandbox3 , sandbox4 , sandbox5 ,
365
388
})
366
389
fakeRuntimeService .SetFakeContainers ([]* critest.FakeContainer {
367
- container0 , container1 , container2 , container3 , container4 , container5 ,
390
+ container0 , container1 , container2 , container3 , container4 , container5 , container6 , container7 ,
368
391
})
369
392
fakeRuntimeService .SetFakeContainerStats ([]* runtimeapi.ContainerStats {
370
- containerStats0 , containerStats1 , containerStats2 , containerStats3 , containerStats4 , containerStats5 ,
393
+ containerStats0 , containerStats1 , containerStats2 , containerStats3 , containerStats4 , containerStats5 , containerStats6 , containerStats7 ,
371
394
})
372
395
373
396
ephemeralVolumes := makeFakeVolumeStats ([]string {"ephVolume1, ephVolumes2" })
@@ -523,7 +546,7 @@ func TestCRIImagesFsStats(t *testing.T) {
523
546
mockCadvisor .AssertExpectations (t )
524
547
}
525
548
526
- func makeFakePodSandbox (name , uid , namespace string ) * critest.FakePodSandbox {
549
+ func makeFakePodSandbox (name , uid , namespace string , terminated bool ) * critest.FakePodSandbox {
527
550
p := & critest.FakePodSandbox {
528
551
PodSandboxStatus : runtimeapi.PodSandboxStatus {
529
552
Metadata : & runtimeapi.PodSandboxMetadata {
@@ -535,7 +558,10 @@ func makeFakePodSandbox(name, uid, namespace string) *critest.FakePodSandbox {
535
558
CreatedAt : time .Now ().UnixNano (),
536
559
},
537
560
}
538
- p .PodSandboxStatus .Id = critest .BuildSandboxName (p .PodSandboxStatus .Metadata )
561
+ if terminated {
562
+ p .PodSandboxStatus .State = runtimeapi .PodSandboxState_SANDBOX_NOTREADY
563
+ }
564
+ p .PodSandboxStatus .Id = string (uuid .NewUUID ())
539
565
return p
540
566
}
541
567
@@ -561,7 +587,7 @@ func makeFakeContainer(sandbox *critest.FakePodSandbox, name string, attempt uin
561
587
} else {
562
588
c .ContainerStatus .State = runtimeapi .ContainerState_CONTAINER_RUNNING
563
589
}
564
- c .ContainerStatus .Id = critest . BuildContainerName ( c . ContainerStatus . Metadata , sandboxID )
590
+ c .ContainerStatus .Id = string ( uuid . NewUUID () )
565
591
return c
566
592
}
567
593
0 commit comments