-
Notifications
You must be signed in to change notification settings - Fork 324
/
fake.go
70 lines (55 loc) · 2.17 KB
/
fake.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package watcher
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"
)
// FakeK8sWatcher is used as an "empty" K8sResourceWatcher when --enable-k8s-api flag is not set.
// It is also used for testing, allowing users to specify a static list of pods.
type FakeK8sWatcher struct {
pods []interface{}
services []interface{}
}
// NewK8sWatcher returns a pointer to an initialized FakeK8sWatcher struct.
func NewFakeK8sWatcher(pods []interface{}) *FakeK8sWatcher {
return NewFakeK8sWatcherWithPodsAndServices(pods, nil)
}
// NewFakeK8sWatcherWithPodsAndServices returns a pointer to an initialized FakeK8sWatcher struct
func NewFakeK8sWatcherWithPodsAndServices(pods []interface{}, services []interface{}) *FakeK8sWatcher {
return &FakeK8sWatcher{pods, services}
}
// FindContainer implements K8sResourceWatcher.FindContainer
func (watcher *FakeK8sWatcher) FindContainer(containerID string) (*corev1.Pod, *corev1.ContainerStatus, bool) {
return findContainer(containerID, watcher.pods)
}
func (watcher *FakeK8sWatcher) FindPod(podID string) (*corev1.Pod, error) {
ret, ok := findPod(watcher.pods)
if ok {
return ret, nil
}
return nil, fmt.Errorf("podID %s not found (in %d pods)", podID, len(watcher.pods))
}
// AddPod adds a pod to the fake k8s watcher. This is intended for testing.
func (watcher *FakeK8sWatcher) AddPod(pod *corev1.Pod) {
watcher.pods = append(watcher.pods, pod)
}
// ClearPods() removes all pods from the fake watcher. This is intended for testing.
func (watcher *FakeK8sWatcher) ClearAllPods() {
watcher.pods = nil
}
// AddService adds a service to the fake k8s watcher.
func (watcher *FakeK8sWatcher) AddService(service *corev1.Service) {
watcher.services = append(watcher.services, service)
}
// ClearAllServices removes all services from the fake watcher.
func (watcher *FakeK8sWatcher) ClearAllServices() {
watcher.services = nil
}
func (watcher *FakeK8sWatcher) AddInformers(_ InternalSharedInformerFactory, _ ...*InternalInformer) {
}
func (watcher *FakeK8sWatcher) GetInformer(_ string) cache.SharedIndexInformer {
return nil
}
func (watcher *FakeK8sWatcher) Start() {}