-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
192 lines (156 loc) · 4.08 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
package etcd
import (
"context"
"fmt"
"net"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/clientv3/concurrency"
"github.com/facebookgo/errgroup"
log "github.com/sirupsen/logrus"
)
const timeOut = 60
const lastIPEtcdPrefix = "last_reserved_ip."
const etcdPrefix = "/conf/odin-cni/ipam/"
type EtcdStore struct {
client *clientv3.Client
Session *concurrency.Session
TimeOut time.Duration
Prefix string
mutex *concurrency.Mutex
}
/*
https://github.com/containernetworking/plugins/blob/master/plugins/ipam/host-local/backend/store.go
Lock() error
Unlock() error
Close() error
Reserve(id string, ip net.IP, rangeID string) (bool, error)
LastReservedIP(rangeID string) (net.IP, error)
Release(ip net.IP) error
ReleaseByID(id string) error
*/
func NewEtcdStore(endpoints []string, key string) (*EtcdStore, error) {
if endpoints == nil {
return nil, fmt.Errorf("etcd endpoints cannot be nil")
}
cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
if err != nil {
return nil, err
}
if key == "" {
key = etcdPrefix
}
return &EtcdStore{
client: cli,
TimeOut: timeOut * time.Second,
Prefix: key,
}, nil
}
func (e *EtcdStore) DoSession() {
session, err := concurrency.NewSession(e.client)
if err != nil {
log.Fatal(err)
}
e.Session = session
e.mutex = concurrency.NewMutex(e.Session, e.Prefix)
}
func (e *EtcdStore) Lock() error {
if e.Session == nil || e.mutex == nil {
return fmt.Errorf("session or mutex is nil")
}
return e.mutex.Lock(context.TODO())
}
func (e *EtcdStore) Unlock() error {
if e.Session == nil || e.mutex == nil {
return fmt.Errorf("session or mutex is nil")
}
return e.mutex.Unlock(context.TODO())
}
func (e *EtcdStore) Close() error {
return errgroup.NewMultiError(
e.Session.Close(),
e.client.Close(),
)
}
func (e *EtcdStore) Release(ip net.IP) error {
return e.delKV(ip)
}
func (e *EtcdStore) ReleaseByID(id string) error {
resp, err := e.getKV(e.Prefix)
if err != nil {
return fmt.Errorf("GET etcd key err:%s", err)
}
for _, value := range resp.Kvs {
if string(value.Value) == id {
return e.delKey(string(value.Key))
}
}
return nil
}
func (e *EtcdStore) Reserve(id string, ip net.IP, rangeID string) (bool, error) {
resp, err := e.getKV(e.Prefix + ip.String())
log.Printf("[odin-ipam] want reserve ip:%s", ip.String())
if err != nil {
return false, err
} else if len(resp.Kvs) > 0 {
return false, fmt.Errorf("%s already exist,err:%+v", ip.String(), resp)
}
if err := e.putKV(e.Prefix+ip.String(), id); err != nil {
return false, err
}
lastIPKey := e.Prefix + lastIPEtcdPrefix + rangeID
if err := e.putKV(lastIPKey, ip.String()); err != nil {
return false, err
}
return true, nil
}
func (e *EtcdStore) LastReservedIP(rangeID string) (net.IP, error) {
lastIPKey := e.Prefix + lastIPEtcdPrefix + rangeID
resp, err := e.getKV(lastIPKey)
if err != nil || len(resp.Kvs) < 1 {
return nil, fmt.Errorf("cannot find last ip in %s", rangeID)
}
return net.ParseIP(string(resp.Kvs[0].Value)), nil
}
func (e *EtcdStore) getKV(key string) (*clientv3.GetResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), e.TimeOut)
defer cancel()
return e.client.Get(ctx, key, clientv3.WithPrefix())
}
func (e *EtcdStore) putKV(key, val string) error {
ctx, cancel := context.WithTimeout(context.Background(), e.TimeOut)
_, err := e.client.Put(ctx, key, val)
if err != nil {
return err
}
cancel()
return nil
}
func (e *EtcdStore) delKV(ip net.IP) error {
ctx, cancel := context.WithTimeout(context.Background(), e.TimeOut)
_, err := e.client.Delete(ctx, e.Prefix+ip.String())
defer cancel()
if err != nil {
return err
}
return nil
}
func (e *EtcdStore) delKey(key string) error {
ctx, cancel := context.WithTimeout(context.Background(), e.TimeOut)
_, err := e.client.Delete(ctx, key)
defer cancel()
if err != nil {
return err
}
return nil
}
func (e *EtcdStore) empltyPrefix() error {
resp, err := e.getKV(e.Prefix)
if err != nil {
return fmt.Errorf("GET etcd key err:%s", err)
}
for _, value := range resp.Kvs {
e.delKey(string(value.Key))
}
return nil
}