generated from kedacore/github-template
-
Notifications
You must be signed in to change notification settings - Fork 89
/
endpoints_cache_fake.go
123 lines (108 loc) · 3.01 KB
/
endpoints_cache_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
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
package k8s
import (
"encoding/json"
"fmt"
"sync"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
)
// FakeEndpointsCache is a fake implementation of
// EndpointsCache, suitable for testing interceptor-level
// logic, without requiring any real Kubernetes client
// or API interaction
type FakeEndpointsCache struct {
json.Marshaler
mut *sync.RWMutex
current map[string]v1.Endpoints
watchers map[string]*watch.RaceFreeFakeWatcher
}
var _ EndpointsCache = &FakeEndpointsCache{}
func NewFakeEndpointsCache() *FakeEndpointsCache {
return &FakeEndpointsCache{
mut: &sync.RWMutex{},
current: make(map[string]v1.Endpoints),
watchers: make(map[string]*watch.RaceFreeFakeWatcher),
}
}
// GetWatcher gets the watcher for the given namespace and name, or
// nil if there wasn't one registered.
//
// Watchers are registered by the .Watch() method
func (f *FakeEndpointsCache) GetWatcher(ns, name string) *watch.RaceFreeFakeWatcher {
f.mut.RLock()
defer f.mut.RUnlock()
return f.watchers[key(ns, name)]
}
func (f *FakeEndpointsCache) MarshalJSON() ([]byte, error) {
f.mut.RLock()
defer f.mut.RUnlock()
ret := map[string]int{}
for name, endpoints := range f.current {
total := 0
for _, subset := range endpoints.Subsets {
total += len(subset.Addresses)
}
ret[name] = total
}
return json.Marshal(ret)
}
func (f *FakeEndpointsCache) Get(ns string, name string) (v1.Endpoints, error) {
f.mut.RLock()
defer f.mut.RUnlock()
ret, ok := f.current[key(ns, name)]
if ok {
return ret, nil
}
return v1.Endpoints{}, fmt.Errorf("no endpoints %s found", name)
}
// Set adds a endpoints to the current in-memory cache without
// sending an event to any of the watchers
func (f *FakeEndpointsCache) Set(endp v1.Endpoints) {
f.mut.Lock()
defer f.mut.Unlock()
f.current[key(endp.Namespace, endp.Name)] = endp
}
func (f *FakeEndpointsCache) Watch(ns, name string) (watch.Interface, error) {
f.mut.RLock()
defer f.mut.RUnlock()
watcher, ok := f.watchers[key(ns, name)]
if !ok {
watcher = watch.NewRaceFreeFake()
f.watchers[key(ns, name)] = watcher
}
return watcher, nil
}
// SetWatcher creates a new race-free fake watcher and sets it into
// the internal watchers map. After this call, a call to Watch() with
// the same namespace and name values will return a valid
// watcher
func (f *FakeEndpointsCache) SetWatcher(ns, name string) *watch.RaceFreeFakeWatcher {
f.mut.Lock()
defer f.mut.Unlock()
watcher := watch.NewRaceFreeFake()
f.watchers[key(ns, name)] = watcher
return watcher
}
func (f *FakeEndpointsCache) SetSubsets(ns, name string, num int) error {
endpoints, err := f.Get(ns, name)
if err != nil {
return fmt.Errorf("no endpoints %s found", name)
}
subsets := []v1.EndpointSubset{}
for i := 0; i < num; i++ {
subset := v1.EndpointSubset{
Addresses: []v1.EndpointAddress{
{
IP: "1.2.3.4",
},
},
}
subsets = append(subsets, subset)
}
endpoints.Subsets = subsets
f.Set(endpoints)
return nil
}
func key(ns, name string) string {
return fmt.Sprintf("%s/%s", ns, name)
}