forked from chartmuseum/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
253 lines (231 loc) · 5.43 KB
/
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
package storage
import (
"context"
"fmt"
pathutil "path"
"strconv"
"strings"
"sync"
"time"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/pkg/transport"
)
const DefaultPrefix = "/chart_backend_bucket"
var (
DefileDialTimeOut = "5s"
TimeStampKey = "timestamp"
ErrNotExistEndpoints = fmt.Errorf("endpoints cannot connect !")
ErrNotExist = fmt.Errorf("not exist!")
)
type etcdOpts struct {
endpoints string
cafile, certfile, keyfile string
dialtimeout time.Duration
}
type etcdStorage struct {
c *clientv3.Client
opts *etcdOpts
base string
bucket string
ctx context.Context
mu sync.RWMutex
}
//connection cut off
func isServerErr(err error) bool {
if err != nil {
if err == context.Canceled {
return false
} else if err == context.DeadlineExceeded {
return false
} else {
return true
}
}
return false
}
//prapare {basepath} dir
func (e *etcdStorage) probe() error {
var (
err error
)
ctx, cancel := context.WithCancel(e.ctx)
_, err = e.c.Put(ctx, e.base, "")
cancel()
if err != nil && isServerErr(err) {
return err
}
return nil
}
func (e *etcdStorage) timeStamp(path string) time.Time {
ctx, cancel := context.WithTimeout(e.ctx, e.opts.dialtimeout)
newpath := pathutil.Join(path, TimeStampKey)
resps, err := e.c.Get(ctx, newpath)
cancel()
if err != nil {
return time.Unix(0, 0)
}
if len(resps.Kvs) != 1 || resps.Kvs[0].Value == nil {
return time.Unix(0, 0)
}
times, err := strconv.ParseInt(string(resps.Kvs[0].Value), 10, 64)
if err != nil {
return time.Unix(0, 0)
}
return time.Unix(times, 0)
}
func (e *etcdStorage) setTimeStamp(path string, updatetime time.Time) error {
ctx, cancel := context.WithTimeout(e.ctx, e.opts.dialtimeout)
newpath := pathutil.Join(path, TimeStampKey)
_, err := e.c.Put(ctx, newpath, fmt.Sprintf("%d", updatetime.Unix()))
cancel()
return err
}
func (e *etcdStorage) delTimeStamp(path string) error {
ctx, cancel := context.WithTimeout(e.ctx, e.opts.dialtimeout)
newpath := pathutil.Join(path, TimeStampKey)
_, err := e.c.Delete(ctx, newpath)
cancel()
return err
}
//
func (e *etcdStorage) ListObjects(prefix string) ([]Object, error) {
var (
objs []Object
)
ctx, cancel := context.WithTimeout(e.ctx, e.opts.dialtimeout)
newpath := pathutil.Join(e.base, prefix)
resps, err := e.c.Get(ctx, newpath, clientv3.WithPrefix())
cancel()
if err != nil {
return nil, err
}
for _, kv := range resps.Kvs {
if kv.Value != nil {
path := removePrefixFromObjectPath(newpath, string(kv.Key))
if objectPathIsInvalid(path) {
continue
}
//TODO need optimizate
if strings.HasSuffix(path, TimeStampKey) {
continue
}
modtime := e.timeStamp(newpath)
if modtime.IsZero() {
modtime = time.Unix(kv.ModRevision, 0)
}
objs = append(objs, Object{
Path: path,
Content: kv.Value,
LastModified: modtime,
})
}
}
return objs, nil
}
func (e *etcdStorage) GetObject(path string) (Object, error) {
var (
modifytime time.Time
)
ctx, cancel := context.WithTimeout(e.ctx, e.opts.dialtimeout)
newpath := pathutil.Join(e.base, path)
resps, err := e.c.Get(ctx, newpath)
cancel()
if err != nil {
return Object{}, err
}
if len(resps.Kvs) != 1 || resps.Kvs[0].Value == nil {
return Object{}, ErrNotExist
}
modifytime = e.timeStamp(newpath)
if modifytime.IsZero() {
// if timestamp not set , keep old version
modifytime = time.Unix(resps.Kvs[0].ModRevision, 0)
}
return Object{
Path: path,
Content: resps.Kvs[0].Value,
LastModified: modifytime,
}, nil
}
func (e *etcdStorage) PutObject(path string, content []byte) error {
var (
updatetime = time.Now()
)
ctx, cancel := context.WithTimeout(e.ctx, e.opts.dialtimeout)
newpath := pathutil.Join(e.base, path)
_, err := e.c.Put(ctx, newpath, string(content))
cancel()
if err != nil {
return err
} else {
return e.setTimeStamp(newpath, updatetime)
}
}
func (e *etcdStorage) DeleteObject(path string) error {
ctx, cancel := context.WithTimeout(e.ctx, e.opts.dialtimeout)
newpath := pathutil.Join(e.base, path)
_, err := e.c.Delete(ctx, newpath)
cancel()
if err != nil {
return err
} else {
return nil
}
}
func parseConf(endpoints string, cafile, certfile, keyfile string, dialtime time.Duration) clientv3.Config {
var (
es []string
)
if endpoints == "" {
panic(ErrNotExistEndpoints)
}
es = strings.Split(endpoints, ",")
tlsInfo := transport.TLSInfo{
CertFile: certfile,
KeyFile: keyfile,
TrustedCAFile: cafile,
}
tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
panic(err)
}
return clientv3.Config{
Endpoints: es,
DialTimeout: dialtime,
TLS: tlsConfig,
}
}
func NewEtcdCSBackend(endpoints string, cafile, certfile, keyfile string, prefix string) Backend {
var (
basepath string
)
DialTimeOut, _ := time.ParseDuration(DefileDialTimeOut)
cli, err := clientv3.New(parseConf(endpoints, cafile, certfile, keyfile, DialTimeOut))
if err != nil {
panic(err)
}
if prefix == "" {
basepath = DefaultPrefix
} else {
basepath = strings.TrimSuffix(prefix, "/")
if basepath != "" && !strings.HasPrefix(basepath, "/") {
basepath = "/" + basepath
}
}
e := &etcdStorage{
c: cli,
base: basepath,
opts: &etcdOpts{
endpoints: endpoints,
cafile: cafile,
dialtimeout: DialTimeOut,
certfile: certfile,
keyfile: keyfile,
},
ctx: context.Background(),
}
if err := e.probe(); err != nil {
panic(err)
}
return e
}