-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.go
87 lines (77 loc) · 1.95 KB
/
watcher.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
78
79
80
81
82
83
84
85
86
87
package discovery
import (
"bytes"
"errors"
"fmt"
)
// IWatcher is an interface that returns updates
// about services within the registry.
type IWatcher interface {
// Pattern watching pattern
Pattern() string
// Next is a blocking call
Next() (*Event, error)
// Terminate stop watching
Terminate() <-chan struct{}
}
// Event is returned by a call to Next on the watcher. Type can be create, update, delete
// +k8s:deepcopy-gen=true
type Event struct {
Type EventType `json:"type"`
Service *Service `json:"service"`
}
// EventType defines registry event type
type EventType int
const (
// Create is emitted when a new service is registered
Create EventType = iota
// Delete is emitted when an existing service is deregsitered
Delete
// Update is emitted when an existing service is updated
Update
)
// MarshalText marshals the EventType to text.
func (t *EventType) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
// UnmarshalText unmarshals text to a EventType.
func (t *EventType) UnmarshalText(text []byte) error {
if t == nil {
return errors.New("registry: can't unmarshal a nil *EventType")
}
if !t.unmarshalText(text) && !t.unmarshalText(bytes.ToLower(text)) {
return fmt.Errorf("registry: unrecognized EventType: %q", text)
}
return nil
}
func (t *EventType) unmarshalText(text []byte) bool {
switch string(text) {
case "create", "CREATE":
*t = Create
case "delete", "DELETE":
*t = Delete
case "update", "UPDATE":
*t = Update
default:
return false
}
return true
}
// String returns human readable EventType.
func (t EventType) String() string {
switch t {
case Create:
return "create"
case Delete:
return "delete"
case Update:
return "update"
default:
return fmt.Sprintf("EventType(%d)", t)
}
}
// Set converts a EventType string into a EventType value.
// returns error if the input string does not match known values.
func (t *EventType) Set(str string) error {
return t.UnmarshalText([]byte(str))
}