-
Notifications
You must be signed in to change notification settings - Fork 127
/
fake_runtime_service.go
796 lines (655 loc) · 23.5 KB
/
fake_runtime_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testing
import (
"context"
"fmt"
"reflect"
"sync"
"time"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
)
var (
// FakeVersion is a version of a fake runtime.
FakeVersion = "0.1.0"
// FakeRuntimeName is the name of the fake runtime.
FakeRuntimeName = "fakeRuntime"
// FakePodSandboxIPs is an IP address of the fake runtime.
FakePodSandboxIPs = []string{"192.168.192.168"}
)
// FakePodSandbox is the fake implementation of runtimeapi.PodSandboxStatus.
type FakePodSandbox struct {
// PodSandboxStatus contains the runtime information for a sandbox.
runtimeapi.PodSandboxStatus
// RuntimeHandler is the runtime handler that was issued with the RunPodSandbox request.
RuntimeHandler string
}
// FakeContainer is a fake container.
type FakeContainer struct {
// ContainerStatus contains the runtime information for a container.
runtimeapi.ContainerStatus
// LinuxResources contains the resources specific to linux containers.
LinuxResources *runtimeapi.LinuxContainerResources
// the sandbox id of this container
SandboxID string
}
// FakeRuntimeService is a fake runetime service.
type FakeRuntimeService struct {
sync.Mutex
Called []string
Errors map[string][]error
FakeStatus *runtimeapi.RuntimeStatus
Containers map[string]*FakeContainer
Sandboxes map[string]*FakePodSandbox
FakeContainerStats map[string]*runtimeapi.ContainerStats
FakePodSandboxStats map[string]*runtimeapi.PodSandboxStats
FakePodSandboxMetrics map[string]*runtimeapi.PodSandboxMetrics
FakeMetricDescriptors map[string]*runtimeapi.MetricDescriptor
FakeContainerMetrics map[string]*runtimeapi.ContainerMetrics
FakeLinuxConfiguration *runtimeapi.LinuxRuntimeConfiguration
ErrorOnSandboxCreate bool
}
// GetContainerID returns the unique container ID from the FakeRuntimeService.
func (r *FakeRuntimeService) GetContainerID(sandboxID, name string, attempt uint32) (string, error) {
r.Lock()
defer r.Unlock()
for id, c := range r.Containers {
if c.SandboxID == sandboxID && c.Metadata.Name == name && c.Metadata.Attempt == attempt {
return id, nil
}
}
return "", fmt.Errorf("container (name, attempt, sandboxID)=(%q, %d, %q) not found", name, attempt, sandboxID)
}
// SetFakeSandboxes sets the fake sandboxes for the FakeRuntimeService.
func (r *FakeRuntimeService) SetFakeSandboxes(sandboxes []*FakePodSandbox) {
r.Lock()
defer r.Unlock()
r.Sandboxes = make(map[string]*FakePodSandbox)
for _, sandbox := range sandboxes {
sandboxID := sandbox.Id
r.Sandboxes[sandboxID] = sandbox
}
}
// SetFakeContainers sets fake containers for the FakeRuntimeService.
func (r *FakeRuntimeService) SetFakeContainers(containers []*FakeContainer) {
r.Lock()
defer r.Unlock()
r.Containers = make(map[string]*FakeContainer)
for _, c := range containers {
containerID := c.Id
r.Containers[containerID] = c
}
}
// AssertCalls validates whether specified calls were made to the FakeRuntimeService.
func (r *FakeRuntimeService) AssertCalls(calls []string) error {
r.Lock()
defer r.Unlock()
if !reflect.DeepEqual(calls, r.Called) {
return fmt.Errorf("expected %#v, got %#v", calls, r.Called)
}
return nil
}
// GetCalls returns the list of calls made to the FakeRuntimeService.
func (r *FakeRuntimeService) GetCalls() []string {
r.Lock()
defer r.Unlock()
return append([]string{}, r.Called...)
}
// InjectError inject the error to the next call to the FakeRuntimeService.
func (r *FakeRuntimeService) InjectError(f string, err error) {
r.Lock()
defer r.Unlock()
r.Errors[f] = append(r.Errors[f], err)
}
// caller of popError must grab a lock.
func (r *FakeRuntimeService) popError(f string) error {
if r.Errors == nil {
return nil
}
errs := r.Errors[f]
if len(errs) == 0 {
return nil
}
err, errs := errs[0], errs[1:]
r.Errors[f] = errs
return err
}
// NewFakeRuntimeService creates a new FakeRuntimeService.
func NewFakeRuntimeService() *FakeRuntimeService {
return &FakeRuntimeService{
Called: make([]string, 0),
Errors: make(map[string][]error),
Containers: make(map[string]*FakeContainer),
Sandboxes: make(map[string]*FakePodSandbox),
FakeContainerStats: make(map[string]*runtimeapi.ContainerStats),
FakePodSandboxStats: make(map[string]*runtimeapi.PodSandboxStats),
FakePodSandboxMetrics: make(map[string]*runtimeapi.PodSandboxMetrics),
FakeContainerMetrics: make(map[string]*runtimeapi.ContainerMetrics),
}
}
// Version returns version information from the FakeRuntimeService.
func (r *FakeRuntimeService) Version(_ context.Context, apiVersion string) (*runtimeapi.VersionResponse, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "Version")
if err := r.popError("Version"); err != nil {
return nil, err
}
return &runtimeapi.VersionResponse{
Version: FakeVersion,
RuntimeName: FakeRuntimeName,
RuntimeVersion: FakeVersion,
RuntimeApiVersion: FakeVersion,
}, nil
}
// Status returns runtime status of the FakeRuntimeService.
func (r *FakeRuntimeService) Status(_ context.Context, verbose bool) (*runtimeapi.StatusResponse, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "Status")
if err := r.popError("Status"); err != nil {
return nil, err
}
return &runtimeapi.StatusResponse{Status: r.FakeStatus}, nil
}
// RunPodSandbox emulates the run of the pod sandbox in the FakeRuntimeService.
func (r *FakeRuntimeService) RunPodSandbox(_ context.Context, config *runtimeapi.PodSandboxConfig, runtimeHandler string) (string, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "RunPodSandbox")
if err := r.popError("RunPodSandbox"); err != nil {
return "", err
}
if r.ErrorOnSandboxCreate {
return "", fmt.Errorf("error on sandbox create")
}
// PodSandboxID should be randomized for real container runtime, but here just use
// fixed name from BuildSandboxName() for easily making fake sandboxes.
podSandboxID := BuildSandboxName(config.Metadata)
createdAt := time.Now().UnixNano()
r.Sandboxes[podSandboxID] = &FakePodSandbox{
PodSandboxStatus: runtimeapi.PodSandboxStatus{
Id: podSandboxID,
Metadata: config.Metadata,
State: runtimeapi.PodSandboxState_SANDBOX_READY,
CreatedAt: createdAt,
Network: &runtimeapi.PodSandboxNetworkStatus{
Ip: FakePodSandboxIPs[0],
},
// Without setting sandboxStatus's Linux.Namespaces.Options, kubeGenericRuntimeManager's podSandboxChanged will consider it as network
// namespace changed and always recreate sandbox which causes pod creation failed.
// Ref `sandboxStatus.GetLinux().GetNamespaces().GetOptions().GetNetwork() != networkNamespaceForPod(pod)` in podSandboxChanged function.
Linux: &runtimeapi.LinuxPodSandboxStatus{
Namespaces: &runtimeapi.Namespace{
Options: config.GetLinux().GetSecurityContext().GetNamespaceOptions(),
},
},
Labels: config.Labels,
Annotations: config.Annotations,
RuntimeHandler: runtimeHandler,
},
RuntimeHandler: runtimeHandler,
}
// assign additional IPs
additionalIPs := FakePodSandboxIPs[1:]
additionalPodIPs := make([]*runtimeapi.PodIP, 0, len(additionalIPs))
for _, ip := range additionalIPs {
additionalPodIPs = append(additionalPodIPs, &runtimeapi.PodIP{
Ip: ip,
})
}
r.Sandboxes[podSandboxID].PodSandboxStatus.Network.AdditionalIps = additionalPodIPs
return podSandboxID, nil
}
// StopPodSandbox emulates the stop of pod sandbox in the FakeRuntimeService.
func (r *FakeRuntimeService) StopPodSandbox(_ context.Context, podSandboxID string) error {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "StopPodSandbox")
if err := r.popError("StopPodSandbox"); err != nil {
return err
}
if s, ok := r.Sandboxes[podSandboxID]; ok {
s.State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
} else {
return fmt.Errorf("pod sandbox %s not found", podSandboxID)
}
return nil
}
// RemovePodSandbox emulates removal of the pod sadbox in the FakeRuntimeService.
func (r *FakeRuntimeService) RemovePodSandbox(_ context.Context, podSandboxID string) error {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "RemovePodSandbox")
if err := r.popError("RemovePodSandbox"); err != nil {
return err
}
// Remove the pod sandbox
delete(r.Sandboxes, podSandboxID)
return nil
}
// PodSandboxStatus returns pod sandbox status from the FakeRuntimeService.
func (r *FakeRuntimeService) PodSandboxStatus(_ context.Context, podSandboxID string, verbose bool) (*runtimeapi.PodSandboxStatusResponse, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "PodSandboxStatus")
if err := r.popError("PodSandboxStatus"); err != nil {
return nil, err
}
s, ok := r.Sandboxes[podSandboxID]
if !ok {
return nil, fmt.Errorf("pod sandbox %q not found", podSandboxID)
}
status := s.PodSandboxStatus
return &runtimeapi.PodSandboxStatusResponse{Status: &status}, nil
}
// ListPodSandbox returns the list of pod sandboxes in the FakeRuntimeService.
func (r *FakeRuntimeService) ListPodSandbox(_ context.Context, filter *runtimeapi.PodSandboxFilter) ([]*runtimeapi.PodSandbox, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "ListPodSandbox")
if err := r.popError("ListPodSandbox"); err != nil {
return nil, err
}
result := make([]*runtimeapi.PodSandbox, 0)
for id, s := range r.Sandboxes {
if filter != nil {
if filter.Id != "" && filter.Id != id {
continue
}
if filter.State != nil && filter.GetState().State != s.State {
continue
}
if filter.LabelSelector != nil && !filterInLabels(filter.LabelSelector, s.GetLabels()) {
continue
}
}
result = append(result, &runtimeapi.PodSandbox{
Id: s.Id,
Metadata: s.Metadata,
State: s.State,
CreatedAt: s.CreatedAt,
Labels: s.Labels,
Annotations: s.Annotations,
RuntimeHandler: s.RuntimeHandler,
})
}
return result, nil
}
// PortForward emulates the set up of port forward in the FakeRuntimeService.
func (r *FakeRuntimeService) PortForward(context.Context, *runtimeapi.PortForwardRequest) (*runtimeapi.PortForwardResponse, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "PortForward")
if err := r.popError("PortForward"); err != nil {
return nil, err
}
return &runtimeapi.PortForwardResponse{}, nil
}
// CreateContainer emulates container creation in the FakeRuntimeService.
func (r *FakeRuntimeService) CreateContainer(_ context.Context, podSandboxID string, config *runtimeapi.ContainerConfig, sandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "CreateContainer")
if err := r.popError("CreateContainer"); err != nil {
return "", err
}
// ContainerID should be randomized for real container runtime, but here just use
// fixed BuildContainerName() for easily making fake containers.
containerID := BuildContainerName(config.Metadata, podSandboxID)
createdAt := time.Now().UnixNano()
createdState := runtimeapi.ContainerState_CONTAINER_CREATED
imageRef := config.Image.Image
r.Containers[containerID] = &FakeContainer{
ContainerStatus: runtimeapi.ContainerStatus{
Id: containerID,
Metadata: config.Metadata,
Image: config.Image,
ImageRef: imageRef,
CreatedAt: createdAt,
State: createdState,
Labels: config.Labels,
Annotations: config.Annotations,
},
SandboxID: podSandboxID,
LinuxResources: config.GetLinux().GetResources(),
}
return containerID, nil
}
// StartContainer emulates start of a container in the FakeRuntimeService.
func (r *FakeRuntimeService) StartContainer(_ context.Context, containerID string) error {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "StartContainer")
if err := r.popError("StartContainer"); err != nil {
return err
}
c, ok := r.Containers[containerID]
if !ok {
return fmt.Errorf("container %s not found", containerID)
}
// Set container to running.
c.State = runtimeapi.ContainerState_CONTAINER_RUNNING
c.StartedAt = time.Now().UnixNano()
return nil
}
// StopContainer emulates stop of a container in the FakeRuntimeService.
func (r *FakeRuntimeService) StopContainer(_ context.Context, containerID string, timeout int64) error {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "StopContainer")
if err := r.popError("StopContainer"); err != nil {
return err
}
c, ok := r.Containers[containerID]
if !ok {
return fmt.Errorf("container %q not found", containerID)
}
// Set container to exited state.
finishedAt := time.Now().UnixNano()
exitedState := runtimeapi.ContainerState_CONTAINER_EXITED
c.State = exitedState
c.FinishedAt = finishedAt
return nil
}
// RemoveContainer emulates remove of a container in the FakeRuntimeService.
func (r *FakeRuntimeService) RemoveContainer(_ context.Context, containerID string) error {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "RemoveContainer")
if err := r.popError("RemoveContainer"); err != nil {
return err
}
// Remove the container
delete(r.Containers, containerID)
return nil
}
// ListContainers returns the list of containers in the FakeRuntimeService.
func (r *FakeRuntimeService) ListContainers(_ context.Context, filter *runtimeapi.ContainerFilter) ([]*runtimeapi.Container, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "ListContainers")
if err := r.popError("ListContainers"); err != nil {
return nil, err
}
result := make([]*runtimeapi.Container, 0)
for _, s := range r.Containers {
if filter != nil {
if filter.Id != "" && filter.Id != s.Id {
continue
}
if filter.PodSandboxId != "" && filter.PodSandboxId != s.SandboxID {
continue
}
if filter.State != nil && filter.GetState().State != s.State {
continue
}
if filter.LabelSelector != nil && !filterInLabels(filter.LabelSelector, s.GetLabels()) {
continue
}
}
result = append(result, &runtimeapi.Container{
Id: s.Id,
CreatedAt: s.CreatedAt,
PodSandboxId: s.SandboxID,
Metadata: s.Metadata,
State: s.State,
Image: s.Image,
ImageRef: s.ImageRef,
Labels: s.Labels,
Annotations: s.Annotations,
})
}
return result, nil
}
// ContainerStatus returns the container status given the container ID in FakeRuntimeService.
func (r *FakeRuntimeService) ContainerStatus(_ context.Context, containerID string, verbose bool) (*runtimeapi.ContainerStatusResponse, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "ContainerStatus")
if err := r.popError("ContainerStatus"); err != nil {
return nil, err
}
c, ok := r.Containers[containerID]
if !ok {
return nil, fmt.Errorf("container %q not found", containerID)
}
status := c.ContainerStatus
return &runtimeapi.ContainerStatusResponse{Status: &status}, nil
}
// UpdateContainerResources returns the container resource in the FakeRuntimeService.
func (r *FakeRuntimeService) UpdateContainerResources(context.Context, string, *runtimeapi.ContainerResources) error {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "UpdateContainerResources")
return r.popError("UpdateContainerResources")
}
// ExecSync emulates the sync execution of a command in a container in the FakeRuntimeService.
func (r *FakeRuntimeService) ExecSync(_ context.Context, containerID string, cmd []string, timeout time.Duration) (stdout []byte, stderr []byte, err error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "ExecSync")
err = r.popError("ExecSync")
return
}
// Exec emulates the execution of a command in a container in the FakeRuntimeService.
func (r *FakeRuntimeService) Exec(context.Context, *runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "Exec")
if err := r.popError("Exec"); err != nil {
return nil, err
}
return &runtimeapi.ExecResponse{}, nil
}
// Attach emulates the attach request in the FakeRuntimeService.
func (r *FakeRuntimeService) Attach(_ context.Context, req *runtimeapi.AttachRequest) (*runtimeapi.AttachResponse, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "Attach")
if err := r.popError("Attach"); err != nil {
return nil, err
}
return &runtimeapi.AttachResponse{}, nil
}
// UpdateRuntimeConfig emulates the update of a runtime config for the FakeRuntimeService.
func (r *FakeRuntimeService) UpdateRuntimeConfig(_ context.Context, runtimeCOnfig *runtimeapi.RuntimeConfig) error {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "UpdateRuntimeConfig")
return r.popError("UpdateRuntimeConfig")
}
// SetFakeContainerStats sets the fake container stats in the FakeRuntimeService.
func (r *FakeRuntimeService) SetFakeContainerStats(containerStats []*runtimeapi.ContainerStats) {
r.Lock()
defer r.Unlock()
r.FakeContainerStats = make(map[string]*runtimeapi.ContainerStats)
for _, s := range containerStats {
r.FakeContainerStats[s.Attributes.Id] = s
}
}
// ContainerStats returns the container stats in the FakeRuntimeService.
func (r *FakeRuntimeService) ContainerStats(_ context.Context, containerID string) (*runtimeapi.ContainerStats, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "ContainerStats")
if err := r.popError("ContainerStats"); err != nil {
return nil, err
}
s, found := r.FakeContainerStats[containerID]
if !found {
return nil, fmt.Errorf("no stats for container %q", containerID)
}
return s, nil
}
// ListContainerStats returns the list of all container stats given the filter in the FakeRuntimeService.
func (r *FakeRuntimeService) ListContainerStats(_ context.Context, filter *runtimeapi.ContainerStatsFilter) ([]*runtimeapi.ContainerStats, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "ListContainerStats")
if err := r.popError("ListContainerStats"); err != nil {
return nil, err
}
var result []*runtimeapi.ContainerStats
for _, c := range r.Containers {
if filter != nil {
if filter.Id != "" && filter.Id != c.Id {
continue
}
if filter.PodSandboxId != "" && filter.PodSandboxId != c.SandboxID {
continue
}
if filter.LabelSelector != nil && !filterInLabels(filter.LabelSelector, c.GetLabels()) {
continue
}
}
s, found := r.FakeContainerStats[c.Id]
if !found {
continue
}
result = append(result, s)
}
return result, nil
}
// SetFakePodSandboxStats sets the fake pod sandbox stats in the FakeRuntimeService.
func (r *FakeRuntimeService) SetFakePodSandboxStats(podStats []*runtimeapi.PodSandboxStats) {
r.Lock()
defer r.Unlock()
r.FakePodSandboxStats = make(map[string]*runtimeapi.PodSandboxStats)
for _, s := range podStats {
r.FakePodSandboxStats[s.Attributes.Id] = s
}
}
// PodSandboxStats returns the sandbox stats in the FakeRuntimeService.
func (r *FakeRuntimeService) PodSandboxStats(_ context.Context, podSandboxID string) (*runtimeapi.PodSandboxStats, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "PodSandboxStats")
if err := r.popError("PodSandboxStats"); err != nil {
return nil, err
}
s, found := r.FakePodSandboxStats[podSandboxID]
if !found {
return nil, fmt.Errorf("no stats for pod sandbox %q", podSandboxID)
}
return s, nil
}
// ListPodSandboxStats returns the list of all pod sandbox stats given the filter in the FakeRuntimeService.
func (r *FakeRuntimeService) ListPodSandboxStats(_ context.Context, filter *runtimeapi.PodSandboxStatsFilter) ([]*runtimeapi.PodSandboxStats, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "ListPodSandboxStats")
if err := r.popError("ListPodSandboxStats"); err != nil {
return nil, err
}
var result []*runtimeapi.PodSandboxStats
for _, sb := range r.Sandboxes {
if filter != nil {
if filter.Id != "" && filter.Id != sb.Id {
continue
}
if filter.LabelSelector != nil && !filterInLabels(filter.LabelSelector, sb.GetLabels()) {
continue
}
}
s, found := r.FakePodSandboxStats[sb.Id]
if !found {
continue
}
result = append(result, s)
}
return result, nil
}
// ReopenContainerLog emulates call to the reopen container log in the FakeRuntimeService.
func (r *FakeRuntimeService) ReopenContainerLog(_ context.Context, containerID string) error {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "ReopenContainerLog")
if err := r.popError("ReopenContainerLog"); err != nil {
return err
}
return nil
}
// CheckpointContainer emulates call to checkpoint a container in the FakeRuntimeService.
func (r *FakeRuntimeService) CheckpointContainer(_ context.Context, options *runtimeapi.CheckpointContainerRequest) error {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "CheckpointContainer")
if err := r.popError("CheckpointContainer"); err != nil {
return err
}
return nil
}
func (f *FakeRuntimeService) GetContainerEvents(ctx context.Context, containerEventsCh chan *runtimeapi.ContainerEventResponse, connectionEstablishedCallback func(runtimeapi.RuntimeService_GetContainerEventsClient)) error {
return nil
}
// SetFakeMetricDescriptors sets the fake metrics descriptors in the FakeRuntimeService.
func (r *FakeRuntimeService) SetFakeMetricDescriptors(descs []*runtimeapi.MetricDescriptor) {
r.Lock()
defer r.Unlock()
r.FakeMetricDescriptors = make(map[string]*runtimeapi.MetricDescriptor)
for _, d := range descs {
r.FakeMetricDescriptors[d.Name] = d
}
}
// ListMetricDescriptors gets the descriptors for the metrics that will be returned in ListPodSandboxMetrics.
func (r *FakeRuntimeService) ListMetricDescriptors(_ context.Context) ([]*runtimeapi.MetricDescriptor, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "ListMetricDescriptors")
if err := r.popError("ListMetricDescriptors"); err != nil {
return nil, err
}
descs := make([]*runtimeapi.MetricDescriptor, 0, len(r.FakeMetricDescriptors))
for _, d := range r.FakeMetricDescriptors {
descs = append(descs, d)
}
return descs, nil
}
// SetFakePodSandboxMetrics sets the fake pod sandbox metrics in the FakeRuntimeService.
func (r *FakeRuntimeService) SetFakePodSandboxMetrics(podStats []*runtimeapi.PodSandboxMetrics) {
r.Lock()
defer r.Unlock()
r.FakePodSandboxMetrics = make(map[string]*runtimeapi.PodSandboxMetrics)
for _, s := range podStats {
r.FakePodSandboxMetrics[s.PodSandboxId] = s
}
}
// ListPodSandboxMetrics returns the list of all pod sandbox metrics in the FakeRuntimeService.
func (r *FakeRuntimeService) ListPodSandboxMetrics(_ context.Context) ([]*runtimeapi.PodSandboxMetrics, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "ListPodSandboxMetrics")
if err := r.popError("ListPodSandboxMetrics"); err != nil {
return nil, err
}
var result []*runtimeapi.PodSandboxMetrics
for _, sb := range r.Sandboxes {
s, found := r.FakePodSandboxMetrics[sb.Id]
if !found {
continue
}
result = append(result, s)
}
return result, nil
}
// RuntimeConfig returns runtime configuration of the FakeRuntimeService.
func (r *FakeRuntimeService) RuntimeConfig(_ context.Context) (*runtimeapi.RuntimeConfigResponse, error) {
r.Lock()
defer r.Unlock()
r.Called = append(r.Called, "RuntimeConfig")
if err := r.popError("RuntimeConfig"); err != nil {
return nil, err
}
return &runtimeapi.RuntimeConfigResponse{Linux: r.FakeLinuxConfiguration}, nil
}