-
Notifications
You must be signed in to change notification settings - Fork 53
/
lifecycler.go
67 lines (55 loc) · 1.5 KB
/
lifecycler.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
package config
import (
"errors"
"github.com/rancher/opni/pkg/config/meta"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Lifecycler interface {
GetObjectList() (meta.ObjectList, error)
UpdateObjectList(objects meta.ObjectList) error
ReloadC() (chan struct{}, error)
}
type lifecycler struct {
objects meta.ObjectList
reloadC chan struct{}
}
func NewLifecycler(objects meta.ObjectList) Lifecycler {
return &lifecycler{
objects: objects,
reloadC: make(chan struct{}),
}
}
func (l *lifecycler) ReloadC() (chan struct{}, error) {
return l.reloadC, nil
}
func (l *lifecycler) GetObjectList() (meta.ObjectList, error) {
return l.objects, nil
}
func (l *lifecycler) UpdateObjectList(objects meta.ObjectList) error {
l.objects = objects
select {
case l.reloadC <- struct{}{}:
default:
return errors.New("no reload handler available")
}
return nil
}
// default no-op lifecycler with limited functionality
type unavailableLifecycler struct {
objects meta.ObjectList
}
func NewUnavailableLifecycler(objects meta.ObjectList) Lifecycler {
return &unavailableLifecycler{
objects: objects,
}
}
func (l *unavailableLifecycler) ReloadC() (chan struct{}, error) {
return nil, status.Error(codes.Unavailable, "lifecycler not available")
}
func (l *unavailableLifecycler) GetObjectList() (meta.ObjectList, error) {
return l.objects, nil
}
func (l *unavailableLifecycler) UpdateObjectList(_ meta.ObjectList) error {
return status.Error(codes.Unavailable, "lifecycler not available")
}