forked from flannel-io/flannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
167 lines (142 loc) · 4.75 KB
/
registry.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
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package subnet
import (
"fmt"
"path"
"sync"
"time"
etcd "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/etcd/client"
"github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/etcd/pkg/transport"
log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
"github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
)
type Registry interface {
getConfig(ctx context.Context, network string) (*etcd.Response, error)
getSubnets(ctx context.Context, network string) (*etcd.Response, error)
createSubnet(ctx context.Context, network, sn, data string, ttl time.Duration) (*etcd.Response, error)
updateSubnet(ctx context.Context, network, sn, data string, ttl time.Duration) (*etcd.Response, error)
deleteSubnet(ctx context.Context, network, sn string) (*etcd.Response, error)
watchSubnets(ctx context.Context, network string, since uint64) (*etcd.Response, error)
}
type EtcdConfig struct {
Endpoints []string
Keyfile string
Certfile string
CAFile string
Prefix string
}
type etcdSubnetRegistry struct {
mux sync.Mutex
cli etcd.KeysAPI
etcdCfg *EtcdConfig
}
func newEtcdClient(c *EtcdConfig) (etcd.KeysAPI, error) {
tlsInfo := transport.TLSInfo{
CertFile: c.Certfile,
KeyFile: c.Keyfile,
CAFile: c.CAFile,
}
t, err := transport.NewTransport(tlsInfo)
if err != nil {
return nil, err
}
cli, err := etcd.New(etcd.Config{
Endpoints: c.Endpoints,
Transport: t,
})
if err != nil {
return nil, err
}
return etcd.NewKeysAPI(cli), nil
}
func newEtcdSubnetRegistry(config *EtcdConfig) (Registry, error) {
r := &etcdSubnetRegistry{
etcdCfg: config,
}
var err error
r.cli, err = newEtcdClient(config)
if err != nil {
return nil, err
}
return r, nil
}
func (esr *etcdSubnetRegistry) getConfig(ctx context.Context, network string) (*etcd.Response, error) {
key := path.Join(esr.etcdCfg.Prefix, network, "config")
resp, err := esr.client().Get(ctx, key, nil)
if err != nil {
return nil, err
}
return resp, nil
}
func (esr *etcdSubnetRegistry) getSubnets(ctx context.Context, network string) (*etcd.Response, error) {
key := path.Join(esr.etcdCfg.Prefix, network, "subnets")
return esr.client().Get(ctx, key, &etcd.GetOptions{Recursive: true})
}
func (esr *etcdSubnetRegistry) createSubnet(ctx context.Context, network, sn, data string, ttl time.Duration) (*etcd.Response, error) {
key := path.Join(esr.etcdCfg.Prefix, network, "subnets", sn)
opts := &etcd.SetOptions{
PrevExist: etcd.PrevNoExist,
TTL: ttl,
}
resp, err := esr.client().Set(ctx, key, data, opts)
if err != nil {
return nil, err
}
ensureExpiration(resp, ttl)
return resp, nil
}
func (esr *etcdSubnetRegistry) updateSubnet(ctx context.Context, network, sn, data string, ttl time.Duration) (*etcd.Response, error) {
key := path.Join(esr.etcdCfg.Prefix, network, "subnets", sn)
resp, err := esr.client().Set(ctx, key, data, &etcd.SetOptions{TTL: ttl})
if err != nil {
return nil, err
}
ensureExpiration(resp, ttl)
return resp, nil
}
func (esr *etcdSubnetRegistry) deleteSubnet(ctx context.Context, network, sn string) (*etcd.Response, error) {
key := path.Join(esr.etcdCfg.Prefix, network, "subnets", sn)
return esr.client().Delete(ctx, key, nil)
}
func (esr *etcdSubnetRegistry) watchSubnets(ctx context.Context, network string, since uint64) (*etcd.Response, error) {
key := path.Join(esr.etcdCfg.Prefix, network, "subnets")
opts := &etcd.WatcherOptions{
AfterIndex: since,
Recursive: true,
}
return esr.client().Watcher(key, opts).Next(ctx)
}
func (esr *etcdSubnetRegistry) client() etcd.KeysAPI {
esr.mux.Lock()
defer esr.mux.Unlock()
return esr.cli
}
func (esr *etcdSubnetRegistry) resetClient() {
esr.mux.Lock()
defer esr.mux.Unlock()
var err error
esr.cli, err = newEtcdClient(esr.etcdCfg)
if err != nil {
panic(fmt.Errorf("resetClient: error recreating etcd client: %v", err))
}
}
func ensureExpiration(resp *etcd.Response, ttl time.Duration) {
if resp.Node.Expiration == nil {
// should not be but calc it ourselves in this case
log.Info("Expiration field missing on etcd response, calculating locally")
exp := time.Now().Add(time.Duration(ttl) * time.Second)
resp.Node.Expiration = &exp
}
}