-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader_etcd.go
360 lines (311 loc) · 10.2 KB
/
loader_etcd.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xconf/blob/main/LICENSE.
package xconf
import (
"context"
"crypto/tls"
"io"
"os"
"strings"
"sync"
"time"
"github.com/actforgood/xerr"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
)
// Note: Etcd API ver was 3.5 at the time this code was written.
// API ref: https://etcd.io/docs/v3.5/learning/api/ .
const (
etcdDefaultEndpoint = "127.0.0.1:2379"
// etcdEndpointsEnvName defines an environment variable name which sets
// the Etcd endpoints, comma separated.
etcdEndpointsEnvName = "ETCD_ENDPOINTS"
)
// EtcdLoader loads configuration from etcd.
// Close it if watcher option is enabled, in order to properly release resources.
type EtcdLoader struct {
strategyInfo *etcdStrategyInfo // common strategy info.
strategy Loader // delegated strategy loader.
}
// NewEtcdLoader instantiates a new EtcdLoader object that loads
// configuration from etcd.
func NewEtcdLoader(key string, opts ...EtcdLoaderOption) EtcdLoader {
loader := EtcdLoader{
strategyInfo: &etcdStrategyInfo{
key: key,
valueFormat: RemoteValuePlain,
ctx: context.Background(),
clientCfg: clientv3.Config{DialTimeout: 10 * time.Second},
},
}
// apply options, if any.
for _, opt := range opts {
opt(&loader)
}
if loader.strategyInfo.clientCfg.Endpoints == nil {
loader.strategyInfo.clientCfg.Endpoints = getDefaultEtcdEndpoints()
}
if loader.strategy == nil {
loader.strategy = etcdSimpleLoadStrategy{info: loader.strategyInfo}
}
return loader
}
// Load returns a configuration key-value map from etcd, or an error
// if something bad happens along the process.
func (loader EtcdLoader) Load() (map[string]any, error) {
return loader.strategy.Load()
}
// Close needs to be called in case watch key changes were enabled.
// It releases associated resources.
func (loader EtcdLoader) Close() error {
if closeableStrategy, ok := loader.strategy.(io.Closer); ok {
return closeableStrategy.Close()
}
return nil
}
// getDefaultEtcdEndpoints tries to get etcd endpoints from ENV.
// It defaults on localhost address.
func getDefaultEtcdEndpoints() []string {
endpoints := []string{etcdDefaultEndpoint}
// try to get from env variables
if eps := os.Getenv(etcdEndpointsEnvName); eps != "" {
endpoints = strings.Split(eps, ",")
}
return endpoints
}
// EtcdLoaderOption defines optional function for configuring
// an Etcd Loader.
type EtcdLoaderOption func(*EtcdLoader)
// EtcdLoaderWithEndpoints sets the etcd host(s) for the client.
// By default, is set to "127.0.0.1:2379".
// Etcd hosts can also be set through ETCD_ENDPOINTS ENV
// (comma separated, if there is more than 1 ep).
func EtcdLoaderWithEndpoints(endpoints []string) EtcdLoaderOption {
return func(loader *EtcdLoader) {
loader.strategyInfo.clientCfg.Endpoints = endpoints
}
}
// EtcdLoaderWithPrefix sets the WithPrefix() option on etcd client.
// The loaded key will be treated as a prefix, and thus all the keys
// having that prefix will be returned.
func EtcdLoaderWithPrefix() EtcdLoaderOption {
return func(loader *EtcdLoader) {
loader.strategyInfo.clientOpOpts = []clientv3.OpOption{clientv3.WithPrefix()}
}
}
// EtcdLoaderWithContext sets request's context.
// By default, a context.Background() is used.
func EtcdLoaderWithContext(ctx context.Context) EtcdLoaderOption {
return func(loader *EtcdLoader) {
loader.strategyInfo.ctx = ctx
loader.strategyInfo.clientCfg.Context = ctx
}
}
// EtcdLoaderWithAuth sets the authentication username and password.
func EtcdLoaderWithAuth(username, pwd string) EtcdLoaderOption {
return func(loader *EtcdLoader) {
loader.strategyInfo.clientCfg.Username = username
loader.strategyInfo.clientCfg.Password = pwd
}
}
// EtcdLoaderWithTLS sets the TLS configuration for secure
// communication between client and server.
func EtcdLoaderWithTLS(tlsCfg *tls.Config) EtcdLoaderOption {
return func(loader *EtcdLoader) {
loader.strategyInfo.clientCfg.TLS = tlsCfg.Clone()
}
}
// EtcdLoaderWithValueFormat sets the value format for a key.
//
// If is set to [RemoteValueJSON], the key's value will be treated as JSON
// and configuration will be loaded from it.
//
// If is set to [RemoteValueYAML], the key's value will be treated as YAML
// and configuration will be loaded from it.
//
// If is set to [RemoteValuePlain], the key's value will be treated as plain content
// and configuration will contain the key and its plain value.
//
// By default, is set to [RemoteValuePlain].
func EtcdLoaderWithValueFormat(valueFormat string) EtcdLoaderOption {
return func(loader *EtcdLoader) {
if valueFormat == RemoteValueJSON ||
valueFormat == RemoteValueYAML ||
valueFormat == RemoteValuePlain {
loader.strategyInfo.valueFormat = valueFormat
}
}
}
// EtcdLoaderWithWatcher enables watch for keys changes.
// Use this if you intend to load configuration intensively, multiple times.
// If you plan to load configuration only once, or rarely, don't use this feature.
// If you use this feature, call Close() method on the loader to gracefully release resources
// (at your application shutdown).
func EtcdLoaderWithWatcher() EtcdLoaderOption {
return func(loader *EtcdLoader) {
loader.strategy = &etcdWatcherLoadStrategy{
info: loader.strategyInfo,
}
}
}
// etcdStrategyInfo holds common info needed for strategies.
type etcdStrategyInfo struct {
key string // the key to load
valueFormat string // value format, one of RemoteValue* constants
clientCfg clientv3.Config // client config
clientOpOpts []clientv3.OpOption // client operation options
ctx context.Context // request context
}
// etcdSimpleLoadStrategy loads configuration
// by making a grpc call.
type etcdSimpleLoadStrategy struct {
info *etcdStrategyInfo
}
// Load retrieves configuration by a simple client call.
func (loaderStrategy etcdSimpleLoadStrategy) Load() (map[string]any, error) {
cli, err := clientv3.New(loaderStrategy.info.clientCfg)
if err != nil {
return nil, err
}
defer cli.Close()
resp, err := cli.KV.Get(
loaderStrategy.info.ctx,
loaderStrategy.info.key,
loaderStrategy.info.clientOpOpts...,
)
if err != nil {
return nil, err
}
return etcdKVPairsLoad(resp.Kvs, loaderStrategy.info.valueFormat)
}
// etcdKVPairsLoad loads config from a Key's Value given the format provided.
func etcdKVPairsLoad(kvPairs []*mvccpb.KeyValue, format string) (map[string]any, error) {
var configMap map[string]any
for idx, kvPair := range kvPairs {
currentKeyConfigMap, err := getRemoteKVPairConfigMap(
string(kvPair.Key),
kvPair.Value,
format,
)
if err != nil {
return nil, err
}
if idx == 0 {
configMap = currentKeyConfigMap
} else {
// merge configs from different keys.
// Note: here, if a duplicate key exists, it will get overwritten.
for key, value := range currentKeyConfigMap {
configMap[key] = value
}
}
}
return configMap, nil
}
// etcdWatcherLoadStrategy loads initial configuration
// by making a grpc call, and after that listens for
// key changes asynchronously.
type etcdWatcherLoadStrategy struct {
info *etcdStrategyInfo
configMap map[string]any // "live" configuration map
client *clientv3.Client // underlying client
mErr *xerr.MultiError // error(s) occurred during watching, between 2 Loads.
mu sync.RWMutex // concurrency semaphore
wg sync.WaitGroup // wait group to wait for watching goroutine to finish
}
// Load returns a copy of the stored configuration map,
// or an error if something bad happens along the process.
func (loaderStrategy *etcdWatcherLoadStrategy) Load() (map[string]any, error) {
if err := loaderStrategy.init(); err != nil {
return nil, err
}
loaderStrategy.mu.RLock()
configMap := DeepCopyConfigMap(loaderStrategy.configMap)
err := loaderStrategy.mErr.ErrOrNil()
loaderStrategy.mErr.Reset()
loaderStrategy.mu.RUnlock()
return configMap, err
}
// init initializes the client, populates initial configuration map
// and starts watching for keys changes.
func (loaderStrategy *etcdWatcherLoadStrategy) init() error {
loaderStrategy.mu.Lock()
defer loaderStrategy.mu.Unlock()
if loaderStrategy.client == nil {
cli, err := clientv3.New(loaderStrategy.info.clientCfg)
if err != nil {
return err
}
loaderStrategy.client = cli
// populate config for the first time.
resp, err := cli.KV.Get(
loaderStrategy.info.ctx,
loaderStrategy.info.key,
loaderStrategy.info.clientOpOpts...,
)
if err != nil {
return err
}
configMap, err := etcdKVPairsLoad(resp.Kvs, loaderStrategy.info.valueFormat)
if err != nil {
return err
}
loaderStrategy.configMap = configMap
// listen for changes.
loaderStrategy.wg.Add(1)
go loaderStrategy.watchKeysAsync()
}
return nil
}
// watchKeysAsync listens for key(s) changes.
func (loaderStrategy *etcdWatcherLoadStrategy) watchKeysAsync() {
defer loaderStrategy.wg.Done()
watchChan := loaderStrategy.client.Watch(
loaderStrategy.info.ctx,
loaderStrategy.info.key,
loaderStrategy.info.clientOpOpts...,
)
for entry := range watchChan {
if entry.Canceled {
continue
}
for _, event := range entry.Events {
kvPair := event.Kv
if event.Type == mvccpb.DELETE { // key was deleted.
loaderStrategy.mu.Lock()
delete(loaderStrategy.configMap, string(kvPair.Key))
loaderStrategy.mu.Unlock()
continue
}
// key was created/modified.
currentKeyConfigMap, err := getRemoteKVPairConfigMap(
string(kvPair.Key),
kvPair.Value,
loaderStrategy.info.valueFormat,
)
loaderStrategy.mu.Lock()
if err != nil {
loaderStrategy.mErr = loaderStrategy.mErr.Add(err)
} else {
// merge configs from different keys.
for key, value := range currentKeyConfigMap {
loaderStrategy.configMap[key] = value
}
}
loaderStrategy.mu.Unlock()
}
}
}
// Close closes the underlying client connection.
func (loaderStrategy *etcdWatcherLoadStrategy) Close() error {
loaderStrategy.mu.RLock()
defer loaderStrategy.mu.RUnlock()
if loaderStrategy.client != nil {
err := loaderStrategy.client.Close()
loaderStrategy.wg.Wait()
return err
}
return nil
}