-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathprojectcache.go
77 lines (67 loc) · 2.17 KB
/
projectcache.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
package server
import (
"fmt"
"time"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
// projectObjectListStore represents a cache of objects indexed by a project name.
// Used to store a list of items per namespace.
type projectObjectListStore interface {
add(namespace string, obj runtime.Object) error
get(namespace string) (obj runtime.Object, exists bool, err error)
}
// projectObjectListCache implements projectObjectListStore.
type projectObjectListCache struct {
store cache.Store
}
var _ projectObjectListStore = &projectObjectListCache{}
// newProjectObjectListCache creates a cache to hold object list objects that will expire with the given ttl.
func newProjectObjectListCache(ttl time.Duration) projectObjectListStore {
return &projectObjectListCache{
store: cache.NewTTLStore(metaProjectObjectListKeyFunc, ttl),
}
}
// add stores given list object under the given namespace. Any prior object under this
// key will be replaced.
func (c *projectObjectListCache) add(namespace string, obj runtime.Object) error {
if namespace == "" {
return fmt.Errorf("namespace cannot be empty")
}
no := &namespacedObject{
namespace: namespace,
object: obj,
}
return c.store.Add(no)
}
// get retrieves a cached list object if present and not expired.
func (c *projectObjectListCache) get(namespace string) (runtime.Object, bool, error) {
entry, exists, err := c.store.GetByKey(namespace)
if err != nil {
return nil, exists, err
}
if !exists {
return nil, false, err
}
no, ok := entry.(*namespacedObject)
if !ok {
return nil, false, fmt.Errorf("%T is not a namespaced object", entry)
}
return no.object, true, nil
}
// namespacedObject is a container associating an object with a namespace.
type namespacedObject struct {
namespace string
object runtime.Object
}
// metaProjectObjectListKeyFunc returns a key for given namespaced object. The key is object's namespace.
func metaProjectObjectListKeyFunc(obj interface{}) (string, error) {
if key, ok := obj.(cache.ExplicitKey); ok {
return string(key), nil
}
no, ok := obj.(*namespacedObject)
if !ok {
return "", fmt.Errorf("object %T is not a namespaced object", obj)
}
return no.namespace, nil
}