/
resource_store_mock.go
55 lines (41 loc) · 1.22 KB
/
resource_store_mock.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package store
import (
"golang.org/x/exp/maps"
"k8s.io/apimachinery/pkg/runtime"
"github.com/cilium/cilium/pkg/k8s/resource"
"github.com/cilium/cilium/pkg/lock"
)
var _ BGPCPResourceStore[*runtime.Unknown] = (*mockBGPCPResourceStore[*runtime.Unknown])(nil)
type mockBGPCPResourceStore[T runtime.Object] struct {
objMu lock.Mutex
objects map[resource.Key]T
}
func NewMockBGPCPResourceStore[T runtime.Object]() *mockBGPCPResourceStore[T] {
return &mockBGPCPResourceStore[T]{
objects: make(map[resource.Key]T),
}
}
func (mds *mockBGPCPResourceStore[T]) List() ([]T, error) {
mds.objMu.Lock()
defer mds.objMu.Unlock()
return maps.Values(mds.objects), nil
}
func (mds *mockBGPCPResourceStore[T]) GetByKey(key resource.Key) (item T, exists bool, err error) {
mds.objMu.Lock()
defer mds.objMu.Unlock()
item, exists = mds.objects[key]
return item, exists, nil
}
func (mds *mockBGPCPResourceStore[T]) Upsert(obj T) {
mds.objMu.Lock()
defer mds.objMu.Unlock()
key := resource.NewKey(obj)
mds.objects[key] = obj
}
func (mds *mockBGPCPResourceStore[T]) Delete(key resource.Key) {
mds.objMu.Lock()
defer mds.objMu.Unlock()
delete(mds.objects, key)
}