-
Notifications
You must be signed in to change notification settings - Fork 24
/
prefix.go
49 lines (42 loc) · 1.55 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
package notifier
import (
etcdclient "github.com/fluxninja/aperture/v2/pkg/etcd/client"
etcdwriter "github.com/fluxninja/aperture/v2/pkg/etcd/writer"
"github.com/fluxninja/aperture/v2/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
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,
kv *etcdclient.KVWrapper,
) *PrefixToEtcdNotifier {
pen := &PrefixToEtcdNotifier{
// subscribe to all prefixes
PrefixBase: notifiers.NewPrefixBase(""),
etcdPath: etcdPath,
etcdWriter: etcdwriter.NewWriter(kv),
}
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.DeletePrefix(pen.etcdPath)
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)
}