-
Notifications
You must be signed in to change notification settings - Fork 23
/
prefix.go
54 lines (46 loc) · 1.69 KB
/
prefix.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
package notifier
import (
clientv3 "go.etcd.io/etcd/client/v3"
etcdclient "github.com/fluxninja/aperture/pkg/etcd/client"
etcdwriter "github.com/fluxninja/aperture/pkg/etcd/writer"
"github.com/fluxninja/aperture/pkg/notifiers"
)
// PrefixToEtcdNotifier holds the state of a notifier that writes raw/transformed contents of a watched prefix to etcd.
type PrefixToEtcdNotifier struct {
notifiers.PrefixBase
etcdClient *etcdclient.Client
etcdWriter *etcdwriter.Writer
etcdPath string
}
// Make sure PrefixToEtcdNotifier implements PrefixNotifier.
var _ notifiers.PrefixNotifier = (*PrefixToEtcdNotifier)(nil)
// NewPrefixToEtcdNotifier returns a new prefix notifier that writes raw/transformed contents to etcd at "etcdPath/key".
func NewPrefixToEtcdNotifier(
etcdPath string,
etcdClient *etcdclient.Client,
withLease bool,
) *PrefixToEtcdNotifier {
pen := &PrefixToEtcdNotifier{
// subscribe to all prefixes
PrefixBase: notifiers.NewPrefixBase(""),
etcdPath: etcdPath,
etcdClient: etcdClient,
etcdWriter: etcdwriter.NewWriter(etcdClient, withLease),
}
return pen
}
// Start starts the prefix notifier.
func (pen *PrefixToEtcdNotifier) Start() error {
// purge etcd path -- as OnStart hooks are executed in order, this would be the first operation on the writer
pen.etcdWriter.Delete(pen.etcdPath, clientv3.WithPrefix())
return nil
}
// Stop stops the prefix notifier.
func (pen *PrefixToEtcdNotifier) Stop() error {
pen.etcdWriter.Close()
return nil
}
// GetKeyNotifier gets the underlying key notifier from prefix notifier.
func (pen *PrefixToEtcdNotifier) GetKeyNotifier(key notifiers.Key) (notifiers.KeyNotifier, error) {
return newKeyToEtcdNotifier(key, pen.etcdPath, pen.etcdWriter)
}