etcd.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // Copyright 2015 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package subnet
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "net"
  20. "path"
  21. "regexp"
  22. "strconv"
  23. "time"
  24. etcd "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/etcd/client"
  25. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  26. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  27. "github.com/coreos/flannel/pkg/ip"
  28. )
  29. const (
  30. registerRetries = 10
  31. subnetTTL = 24 * time.Hour
  32. )
  33. type EtcdManager struct {
  34. registry Registry
  35. networkRegex *regexp.Regexp
  36. }
  37. var (
  38. subnetRegex *regexp.Regexp = regexp.MustCompile(`(\d+\.\d+.\d+.\d+)-(\d+)`)
  39. )
  40. type watchCursor struct {
  41. index uint64
  42. }
  43. func (c watchCursor) String() string {
  44. return strconv.FormatUint(c.index, 10)
  45. }
  46. func NewEtcdManager(config *EtcdConfig) (Manager, error) {
  47. r, err := newEtcdSubnetRegistry(config)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return &EtcdManager{
  52. registry: r,
  53. networkRegex: regexp.MustCompile(config.Prefix + `/([^/]*)(/|/config)?$`),
  54. }, nil
  55. }
  56. func newEtcdManager(r Registry) Manager {
  57. return &EtcdManager{
  58. registry: r,
  59. networkRegex: regexp.MustCompile(`/coreos.com/network/([^/]*)(/|/config)?$`),
  60. }
  61. }
  62. func (m *EtcdManager) GetNetworkConfig(ctx context.Context, network string) (*Config, error) {
  63. cfgResp, err := m.registry.getNetworkConfig(ctx, network)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return ParseConfig(cfgResp.Node.Value)
  68. }
  69. func (m *EtcdManager) AcquireLease(ctx context.Context, network string, attrs *LeaseAttrs) (*Lease, error) {
  70. config, err := m.GetNetworkConfig(ctx, network)
  71. if err != nil {
  72. return nil, err
  73. }
  74. for {
  75. l, err := m.acquireLeaseOnce(ctx, network, config, attrs)
  76. switch {
  77. case err == nil:
  78. log.Info("Subnet lease acquired: ", l.Subnet)
  79. return l, nil
  80. case err == context.Canceled, err == context.DeadlineExceeded:
  81. return nil, err
  82. default:
  83. log.Error("Failed to acquire subnet: ", err)
  84. }
  85. select {
  86. case <-time.After(time.Second):
  87. case <-ctx.Done():
  88. return nil, ctx.Err()
  89. }
  90. }
  91. }
  92. func findLeaseByIP(leases []Lease, pubIP ip.IP4) *Lease {
  93. for _, l := range leases {
  94. if pubIP == l.Attrs.PublicIP {
  95. return &l
  96. }
  97. }
  98. return nil
  99. }
  100. func (m *EtcdManager) tryAcquireLease(ctx context.Context, network string, config *Config, extIaddr ip.IP4, attrs *LeaseAttrs) (*Lease, error) {
  101. var err error
  102. leases, _, err := m.getLeases(ctx, network)
  103. if err != nil {
  104. return nil, err
  105. }
  106. attrBytes, err := json.Marshal(attrs)
  107. if err != nil {
  108. return nil, err
  109. }
  110. // try to reuse a subnet if there's one that matches our IP
  111. if l := findLeaseByIP(leases, extIaddr); l != nil {
  112. // make sure the existing subnet is still within the configured network
  113. if isSubnetConfigCompat(config, l.Subnet) {
  114. log.Infof("Found lease (%v) for current IP (%v), reusing", l.Subnet, extIaddr)
  115. resp, err := m.registry.updateSubnet(ctx, network, l.Key(), string(attrBytes), subnetTTL)
  116. if err != nil {
  117. return nil, err
  118. }
  119. l.Attrs = attrs
  120. l.Expiration = *resp.Node.Expiration
  121. return l, nil
  122. } else {
  123. log.Infof("Found lease (%v) for current IP (%v) but not compatible with current config, deleting", l.Subnet, extIaddr)
  124. if _, err := m.registry.deleteSubnet(ctx, network, l.Key()); err != nil {
  125. return nil, err
  126. }
  127. }
  128. }
  129. // no existing match, grab a new one
  130. sn, err := m.allocateSubnet(config, leases)
  131. if err != nil {
  132. return nil, err
  133. }
  134. resp, err := m.registry.createSubnet(ctx, network, sn.StringSep(".", "-"), string(attrBytes), subnetTTL)
  135. if err == nil {
  136. return &Lease{
  137. Subnet: sn,
  138. Attrs: attrs,
  139. Expiration: *resp.Node.Expiration,
  140. }, nil
  141. }
  142. if etcdErr, ok := err.(etcd.Error); ok && etcdErr.Code == etcd.ErrorCodeNodeExist {
  143. // if etcd returned Key Already Exists, try again.
  144. return nil, nil
  145. }
  146. return nil, err
  147. }
  148. func (m *EtcdManager) acquireLeaseOnce(ctx context.Context, network string, config *Config, attrs *LeaseAttrs) (*Lease, error) {
  149. for i := 0; i < registerRetries; i++ {
  150. l, err := m.tryAcquireLease(ctx, network, config, attrs.PublicIP, attrs)
  151. switch {
  152. case err != nil:
  153. return nil, err
  154. case l != nil:
  155. return l, nil
  156. }
  157. // before moving on, check for cancel
  158. // TODO(eyakubovich): propogate ctx deeper into registry
  159. select {
  160. case <-ctx.Done():
  161. return nil, ctx.Err()
  162. default:
  163. }
  164. }
  165. return nil, errors.New("Max retries reached trying to acquire a subnet")
  166. }
  167. func parseSubnetKey(s string) *ip.IP4Net {
  168. if parts := subnetRegex.FindStringSubmatch(s); len(parts) == 3 {
  169. snIp := net.ParseIP(parts[1]).To4()
  170. prefixLen, err := strconv.ParseUint(parts[2], 10, 5)
  171. if snIp != nil && err == nil {
  172. return &ip.IP4Net{IP: ip.FromIP(snIp), PrefixLen: uint(prefixLen)}
  173. }
  174. }
  175. return nil
  176. }
  177. func (m *EtcdManager) allocateSubnet(config *Config, leases []Lease) (ip.IP4Net, error) {
  178. log.Infof("Picking subnet in range %s ... %s", config.SubnetMin, config.SubnetMax)
  179. var bag []ip.IP4
  180. sn := ip.IP4Net{IP: config.SubnetMin, PrefixLen: config.SubnetLen}
  181. OuterLoop:
  182. for ; sn.IP <= config.SubnetMax && len(bag) < 100; sn = sn.Next() {
  183. for _, l := range leases {
  184. if sn.Overlaps(l.Subnet) {
  185. continue OuterLoop
  186. }
  187. }
  188. bag = append(bag, sn.IP)
  189. }
  190. if len(bag) == 0 {
  191. return ip.IP4Net{}, errors.New("out of subnets")
  192. } else {
  193. i := randInt(0, len(bag))
  194. return ip.IP4Net{IP: bag[i], PrefixLen: config.SubnetLen}, nil
  195. }
  196. }
  197. // getLeases queries etcd to get a list of currently allocated leases for a given network.
  198. // It returns the leases along with the "as-of" etcd-index that can be used as the starting
  199. // point for etcd watch.
  200. func (m *EtcdManager) getLeases(ctx context.Context, network string) ([]Lease, uint64, error) {
  201. resp, err := m.registry.getSubnets(ctx, network)
  202. leases := []Lease{}
  203. if err == nil {
  204. for _, node := range resp.Node.Nodes {
  205. if sn := parseSubnetKey(node.Key); sn != nil {
  206. attrs := &LeaseAttrs{}
  207. if err = json.Unmarshal([]byte(node.Value), attrs); err == nil {
  208. exp := time.Time{}
  209. if node.Expiration != nil {
  210. exp = *node.Expiration
  211. }
  212. lease := Lease{
  213. Subnet: *sn,
  214. Attrs: attrs,
  215. Expiration: exp,
  216. }
  217. leases = append(leases, lease)
  218. }
  219. }
  220. }
  221. return leases, resp.Index, nil
  222. }
  223. if etcdErr, ok := err.(etcd.Error); ok && etcdErr.Code == etcd.ErrorCodeKeyNotFound {
  224. // key not found: treat it as empty set
  225. return leases, etcdErr.Index, nil
  226. }
  227. return nil, 0, err
  228. }
  229. func (m *EtcdManager) RenewLease(ctx context.Context, network string, lease *Lease) error {
  230. attrBytes, err := json.Marshal(lease.Attrs)
  231. if err != nil {
  232. return err
  233. }
  234. // TODO(eyakubovich): propogate ctx into registry
  235. resp, err := m.registry.updateSubnet(ctx, network, lease.Key(), string(attrBytes), subnetTTL)
  236. if err != nil {
  237. return err
  238. }
  239. lease.Expiration = *resp.Node.Expiration
  240. return nil
  241. }
  242. func getNextIndex(cursor interface{}) (uint64, error) {
  243. nextIndex := uint64(0)
  244. if wc, ok := cursor.(watchCursor); ok {
  245. nextIndex = wc.index
  246. } else if s, ok := cursor.(string); ok {
  247. var err error
  248. nextIndex, err = strconv.ParseUint(s, 10, 64)
  249. if err != nil {
  250. return 0, fmt.Errorf("failed to parse cursor: %v", err)
  251. }
  252. } else {
  253. return 0, fmt.Errorf("internal error: watch cursor is of unknown type")
  254. }
  255. return nextIndex, nil
  256. }
  257. func (m *EtcdManager) WatchLeases(ctx context.Context, network string, cursor interface{}) (LeaseWatchResult, error) {
  258. if cursor == nil {
  259. return m.leaseWatchReset(ctx, network)
  260. }
  261. nextIndex, err := getNextIndex(cursor)
  262. if err != nil {
  263. return LeaseWatchResult{}, err
  264. }
  265. resp, err := m.registry.watch(ctx, path.Join(network, "subnets"), nextIndex)
  266. switch {
  267. case err == nil:
  268. return parseSubnetWatchResponse(resp)
  269. case isIndexTooSmall(err):
  270. log.Warning("Watch of subnet leases failed because etcd index outside history window")
  271. return m.leaseWatchReset(ctx, network)
  272. default:
  273. return LeaseWatchResult{}, err
  274. }
  275. }
  276. func (m *EtcdManager) WatchNetworks(ctx context.Context, cursor interface{}) (NetworkWatchResult, error) {
  277. if cursor == nil {
  278. return m.networkWatchReset(ctx)
  279. }
  280. nextIndex, err := getNextIndex(cursor)
  281. if err != nil {
  282. return NetworkWatchResult{}, err
  283. }
  284. DoWatch:
  285. resp, err := m.registry.watch(ctx, "", nextIndex)
  286. switch {
  287. case err == nil:
  288. result, err, again := m.parseNetworkWatchResponse(resp)
  289. if again {
  290. nextIndex = resp.Node.ModifiedIndex
  291. goto DoWatch
  292. }
  293. return result, err
  294. case isIndexTooSmall(err):
  295. log.Warning("Watch of subnet leases failed because etcd index outside history window")
  296. return m.networkWatchReset(ctx)
  297. default:
  298. return NetworkWatchResult{}, err
  299. }
  300. }
  301. func isIndexTooSmall(err error) bool {
  302. etcdErr, ok := err.(etcd.Error)
  303. return ok && etcdErr.Code == etcd.ErrorCodeEventIndexCleared
  304. }
  305. func parseSubnetWatchResponse(resp *etcd.Response) (LeaseWatchResult, error) {
  306. sn := parseSubnetKey(resp.Node.Key)
  307. if sn == nil {
  308. return LeaseWatchResult{}, fmt.Errorf("%v %q: not a subnet, skipping", resp.Action, resp.Node.Key)
  309. }
  310. evt := Event{}
  311. switch resp.Action {
  312. case "delete", "expire":
  313. evt = Event{
  314. EventRemoved,
  315. Lease{Subnet: *sn},
  316. "",
  317. }
  318. default:
  319. attrs := &LeaseAttrs{}
  320. err := json.Unmarshal([]byte(resp.Node.Value), attrs)
  321. if err != nil {
  322. return LeaseWatchResult{}, err
  323. }
  324. exp := time.Time{}
  325. if resp.Node.Expiration != nil {
  326. exp = *resp.Node.Expiration
  327. }
  328. evt = Event{
  329. EventAdded,
  330. Lease{
  331. Subnet: *sn,
  332. Attrs: attrs,
  333. Expiration: exp,
  334. },
  335. "",
  336. }
  337. }
  338. return LeaseWatchResult{
  339. Cursor: watchCursor{resp.Node.ModifiedIndex},
  340. Events: []Event{evt},
  341. }, nil
  342. }
  343. // Returns network name from config key (eg, /coreos.com/network/foobar/config),
  344. // if the 'config' key isn't present we don't consider the network valid
  345. func (m *EtcdManager) parseNetworkKey(s string) (string, bool) {
  346. if parts := m.networkRegex.FindStringSubmatch(s); len(parts) == 3 {
  347. return parts[1], parts[2] != ""
  348. }
  349. return "", false
  350. }
  351. func (m *EtcdManager) parseNetworkWatchResponse(resp *etcd.Response) (NetworkWatchResult, error, bool) {
  352. netname, isConfig := m.parseNetworkKey(resp.Node.Key)
  353. if netname == "" {
  354. return NetworkWatchResult{}, nil, true
  355. }
  356. evt := Event{}
  357. switch resp.Action {
  358. case "delete":
  359. evt = Event{
  360. EventRemoved,
  361. Lease{},
  362. netname,
  363. }
  364. default:
  365. if !isConfig {
  366. // Ignore non .../<netname>/config keys; tell caller to try again
  367. return NetworkWatchResult{}, nil, true
  368. }
  369. _, err := ParseConfig(resp.Node.Value)
  370. if err != nil {
  371. return NetworkWatchResult{}, err, false
  372. }
  373. evt = Event{
  374. EventAdded,
  375. Lease{},
  376. netname,
  377. }
  378. }
  379. return NetworkWatchResult{
  380. Cursor: watchCursor{resp.Node.ModifiedIndex},
  381. Events: []Event{evt},
  382. }, nil, false
  383. }
  384. // getNetworks queries etcd to get a list of network names. It returns the
  385. // networks along with the 'as-of' etcd-index that can be used as the starting
  386. // point for etcd watch.
  387. func (m *EtcdManager) getNetworks(ctx context.Context) ([]string, uint64, error) {
  388. resp, err := m.registry.getNetworks(ctx)
  389. networks := []string{}
  390. if err == nil {
  391. for _, node := range resp.Node.Nodes {
  392. // Look for '/config' on the child nodes
  393. for _, child := range node.Nodes {
  394. netname, isConfig := m.parseNetworkKey(child.Key)
  395. if isConfig {
  396. networks = append(networks, netname)
  397. }
  398. }
  399. }
  400. return networks, resp.Index, nil
  401. }
  402. if etcdErr, ok := err.(etcd.Error); ok && etcdErr.Code == etcd.ErrorCodeKeyNotFound {
  403. // key not found: treat it as empty set
  404. return networks, etcdErr.Index, nil
  405. }
  406. return nil, 0, err
  407. }
  408. // leaseWatchReset is called when incremental lease watch failed and we need to grab a snapshot
  409. func (m *EtcdManager) leaseWatchReset(ctx context.Context, network string) (LeaseWatchResult, error) {
  410. wr := LeaseWatchResult{}
  411. leases, index, err := m.getLeases(ctx, network)
  412. if err != nil {
  413. return wr, fmt.Errorf("failed to retrieve subnet leases: %v", err)
  414. }
  415. wr.Cursor = watchCursor{index}
  416. wr.Snapshot = leases
  417. return wr, nil
  418. }
  419. // networkWatchReset is called when incremental network watch failed and we need to grab a snapshot
  420. func (m *EtcdManager) networkWatchReset(ctx context.Context) (NetworkWatchResult, error) {
  421. wr := NetworkWatchResult{}
  422. networks, index, err := m.getNetworks(ctx)
  423. if err != nil {
  424. return wr, fmt.Errorf("failed to retrieve networks: %v", err)
  425. }
  426. wr.Cursor = watchCursor{index}
  427. wr.Snapshot = networks
  428. return wr, nil
  429. }
  430. func isSubnetConfigCompat(config *Config, sn ip.IP4Net) bool {
  431. if sn.IP < config.SubnetMin || sn.IP > config.SubnetMax {
  432. return false
  433. }
  434. return sn.PrefixLen == config.SubnetLen
  435. }