-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcd_reader.go
219 lines (191 loc) · 4.69 KB
/
etcd_reader.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
package protoconf
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb"
"github.com/yoozoo/protoconf_go/agentApplicationService"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
)
const (
defaultEnv = "default"
endpointsEnvKey = "etcd_endpoints"
userpassEnvKey = "etcd_user"
envEnvKey = "etcd_envkey"
dialTimeout = 2 * time.Second
)
// EtcdReader etcd reader
type EtcdReader struct {
client *clientv3.Client
endpoints []string
user string
pass string
env string
appToken string
}
//SetUser set etcd user/pass
func (p *EtcdReader) SetUser(user string, pass string) {
p.user = user
p.pass = pass
}
//SetEndpoints set etcd endpionts
func (p *EtcdReader) SetEndpoints(endpoints []string) {
p.endpoints = endpoints
}
//SetToken set proto agent token
func (p *EtcdReader) SetToken(token string) {
p.appToken = token
}
//NewEtcdReader create etcd reader
func NewEtcdReader(env string) *EtcdReader {
s := os.Getenv(endpointsEnvKey)
var result EtcdReader
if len(s) > 0 {
result.endpoints = strings.Split(s, ",")
}
userpass := strings.Split(os.Getenv(userpassEnvKey), ":")
if len(userpass) == 2 && len(userpass[0]) > 0 && len(userpass[1]) > 0 {
result.user = userpass[0]
result.pass = userpass[1]
}
result.env = env
if len(env) == 0 {
s = os.Getenv(envEnvKey)
if len(s) > 0 {
result.env = s
} else {
result.env = defaultEnv
}
}
return &result
}
func (p *EtcdReader) getClient(appName string) *clientv3.Client {
// todo add protoagent support
if p.client != nil {
return p.client
}
if len(p.endpoints) == 0 || len(p.user) == 0 || len(p.pass) == 0 {
p.getSettingFromAgent(appName)
}
if len(p.endpoints) == 0 {
panic(fmt.Errorf("empty etcd endpoints"))
}
if len(p.user) == 0 {
panic(fmt.Errorf("empty username"))
}
if len(p.pass) == 0 {
panic(fmt.Errorf("empty password"))
}
cli, err := clientv3.New(clientv3.Config{
DialTimeout: dialTimeout,
Endpoints: p.endpoints,
Username: p.user,
Password: p.pass,
})
if err != nil {
panic(err)
}
p.client = cli
return cli
}
//GetValues get values of the key list
func (p *EtcdReader) GetValues(appName string) map[string]string {
prefix := "/" + p.env + "/" + appName + "/"
result := make(map[string]string)
cli := p.getClient(appName)
if cli == nil {
fmt.Println("Failed to get etcd client connection")
return result
}
resp, err := cli.Get(context.TODO(), prefix, clientv3.WithPrefix())
if err != nil {
fmt.Println("error to retrieve config values: ", err)
return result
}
for _, kv := range resp.Kvs {
v := string(kv.Value)
key := strings.TrimPrefix(string(kv.Key), prefix)
result[key] = v
}
return result
}
//WatchApp watch keys of the app
func (p *EtcdReader) WatchApp(appName string, callback NotifyInterface) {
go p.watchingApp(appName, callback)
}
func (p *EtcdReader) watchingApp(appName string, callback NotifyInterface) {
prefix := "/" + p.env + "/" + appName + "/"
RESTART_POINT:
for {
ch := p.client.Watch(context.Background(), prefix, clientv3.WithPrefix())
for {
select {
case s, ok := <-ch:
if ok {
if len(s.Events) > 0 {
cache := make(map[string]*clientv3.Event)
for _, e := range s.Events {
cache[strings.TrimPrefix(string(e.Kv.Key), prefix)] = e
}
for k, e := range cache {
if e.Type == mvccpb.DELETE {
callback.DeleteKey(k)
} else if e.IsCreate() {
callback.AddKey(k, string(e.Kv.Value))
} else {
callback.UpdateKey(k, string(e.Kv.Value))
}
}
}
if s.Canceled {
err := s.Err()
if err != nil {
fmt.Println("error :", err)
}
break RESTART_POINT
}
} else {
fmt.Println("channel closed.")
break RESTART_POINT
}
}
}
}
}
func (p *EtcdReader) getSettingFromAgent(appName string) {
if len(p.appToken) == 0 {
return
}
agentaddr := "127.0.0.1:57581"
agentConn, err := grpc.Dial(agentaddr, grpc.WithInsecure())
if err != nil {
panic(err)
}
defer agentConn.Close()
c := agentApplicationService.NewAgentApplicationServiceClient(agentConn)
req := &agentApplicationService.LogonInfoRequest{
AppToken: p.appToken,
Env: p.env,
}
resp, err := c.GetLogonInfo(context.TODO(), req)
if err != nil {
st := status.Convert(err)
for _, t := range st.Details() {
switch t := t.(type) {
case *agentApplicationService.LogonError:
fmt.Println(t.Detail)
}
}
return
}
if resp.AppName != appName {
panic(fmt.Errorf("protoagent response app name is %s which is different from our app %s", resp.AppName, appName))
}
p.endpoints = strings.Split(resp.Endpoints, ",")
p.user = resp.User
p.pass = resp.Password
}