-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
214 lines (181 loc) · 4.49 KB
/
agent.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package agent
import (
"context"
"time"
"github.com/thetechnick/nginx-ingress/pkg/storage"
"github.com/thetechnick/nginx-ingress/pkg/storage/pb"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb"
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
)
const (
serverKeyPrefix = "lbc/server/"
mainConfigKey = "lbc/main-config"
)
// Agent watches etcd to trigger config updates
type Agent struct {
serverConfigStorage storage.ServerConfigStorage
mainConfigStorage storage.MainConfigStorage
client *clientv3.Client
log *log.Entry
readyCh chan interface{}
}
// NewAgent creates a new Agent instance
func NewAgent(
client *clientv3.Client,
scs storage.ServerConfigStorage,
mcs storage.MainConfigStorage,
readyCh chan interface{},
) *Agent {
return &Agent{scs, mcs, client, log.WithField("module", "agent"), readyCh}
}
func (a *Agent) runServerWatcher() {
a.log.Info("Starting Server watcher")
wch := a.client.Watch(context.Background(), serverKeyPrefix, clientv3.WithPrefix(), clientv3.WithPrevKV())
for wresp := range wch {
for _, e := range wresp.Events {
a.handleServerEvent(e)
}
}
}
func (a *Agent) runMainConfigWatcher() {
a.log.Info("Starting MainConfig watcher")
wch := a.client.Watch(context.Background(), mainConfigKey, clientv3.WithPrevKV())
for wresp := range wch {
for _, e := range wresp.Events {
a.handleMainConfigEvent(e)
}
}
}
func (a *Agent) serverFromKey(kv *mvccpb.KeyValue) *pb.ServerConfig {
s := &pb.ServerConfig{}
if err := proto.Unmarshal(kv.Value, s); err != nil {
a.log.
WithField("key", string(kv.Key)).
WithError(err).
Error("Unmarshal error, skipping")
return nil
}
return s
}
func (a *Agent) mainConfigFromKey(kv *mvccpb.KeyValue) *pb.MainConfig {
c := &pb.MainConfig{}
if err := proto.Unmarshal(kv.Value, c); err != nil {
a.log.
WithField("key", string(kv.Key)).
WithError(err).
Error("Unmarshal error, skipping")
return nil
}
return c
}
func (a *Agent) updateMainConfigFromKey(kv *mvccpb.KeyValue) {
c := a.mainConfigFromKey(kv)
if c == nil {
return
}
if err := a.mainConfigStorage.Put(c); err != nil {
a.log.
WithField("key", string(kv.Key)).
WithError(err).
Error("Error updating MainConfig")
return
}
}
func (a *Agent) deleteServerFromKey(kv *mvccpb.KeyValue) {
s := a.serverFromKey(kv)
if s == nil {
return
}
if err := a.serverConfigStorage.Delete(s); err != nil {
a.log.
WithField("key", string(kv.Key)).
WithError(err).
Error("Error deleting Server")
}
}
func (a *Agent) updateServerFromKey(kv *mvccpb.KeyValue) {
s := a.serverFromKey(kv)
if s == nil {
return
}
if err := a.serverConfigStorage.Put(s); err != nil {
a.log.
WithField("key", string(kv.Key)).
WithError(err).
Error("Error updating Server")
}
}
func (a *Agent) handleMainConfigEvent(event *clientv3.Event) {
a.log.
WithField("key", string(event.Kv.Key)).
Debug("MainConfig key changed")
if event.Type == clientv3.EventTypeDelete {
a.log.
WithField("key", string(event.Kv.Key)).
Error("MainConfig key deleted, still using old config")
return
}
a.updateMainConfigFromKey(event.Kv)
}
func (a *Agent) handleServerEvent(event *clientv3.Event) {
a.log.
WithField("key", string(event.Kv.Key)).
Debug("Server key changed")
if event.Type == clientv3.EventTypeDelete {
a.deleteServerFromKey(event.PrevKv)
return
}
a.updateServerFromKey(event.Kv)
}
func (a *Agent) syncExistingServers() {
a.log.
Info("Syncing existing Servers")
resp, err := a.client.Get(context.Background(), serverKeyPrefix, clientv3.WithPrefix())
if err != nil {
a.log.
WithError(err).
Debug("Error syncing servers")
return
}
for _, kv := range resp.Kvs {
a.updateServerFromKey(kv)
}
}
func (a *Agent) syncMainConfig() {
a.log.
Info("Syncing existing MainConfig")
resp, err := a.client.Get(context.Background(), mainConfigKey)
if err != nil {
a.log.
WithError(err).
Debug("Error syncing servers")
return
}
for _, kv := range resp.Kvs {
a.updateMainConfigFromKey(kv)
}
}
// Run starts the agent
func (a *Agent) Run(ctx context.Context) {
// start watchers
go a.runMainConfigWatcher()
go a.runServerWatcher()
// sync existing config
a.syncMainConfig()
a.syncExistingServers()
a.readyCh <- nil
close(a.readyCh)
t := time.NewTicker(30 * time.Second)
go func(t *time.Ticker) {
for range t.C {
a.log.Info("Resync after 30s")
a.syncMainConfig()
a.syncExistingServers()
}
}(t)
// block
<-ctx.Done()
t.Stop()
}