registry.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2015 CoreOS, Inc.
  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. "fmt"
  17. golog "log"
  18. "os"
  19. "path"
  20. "sync"
  21. "time"
  22. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  23. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  24. )
  25. type subnetRegistry interface {
  26. getConfig() (*etcd.Response, error)
  27. getSubnets() (*etcd.Response, error)
  28. createSubnet(sn, data string, ttl uint64) (*etcd.Response, error)
  29. updateSubnet(sn, data string, ttl uint64) (*etcd.Response, error)
  30. watchSubnets(since uint64, stop chan bool) (*etcd.Response, error)
  31. }
  32. type EtcdConfig struct {
  33. Endpoints []string
  34. Keyfile string
  35. Certfile string
  36. CAFile string
  37. Prefix string
  38. }
  39. type etcdSubnetRegistry struct {
  40. mux sync.Mutex
  41. cli *etcd.Client
  42. etcdCfg *EtcdConfig
  43. }
  44. func init() {
  45. etcd.SetLogger(golog.New(os.Stderr, "go-etcd", golog.LstdFlags))
  46. }
  47. func newEtcdClient(c *EtcdConfig) (*etcd.Client, error) {
  48. if c.Keyfile != "" || c.Certfile != "" || c.CAFile != "" {
  49. return etcd.NewTLSClient(c.Endpoints, c.Certfile, c.Keyfile, c.CAFile)
  50. } else {
  51. return etcd.NewClient(c.Endpoints), nil
  52. }
  53. }
  54. func newEtcdSubnetRegistry(config *EtcdConfig) (subnetRegistry, error) {
  55. r := &etcdSubnetRegistry{
  56. etcdCfg: config,
  57. }
  58. var err error
  59. r.cli, err = newEtcdClient(config)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return r, nil
  64. }
  65. func (esr *etcdSubnetRegistry) getConfig() (*etcd.Response, error) {
  66. key := path.Join(esr.etcdCfg.Prefix, "config")
  67. resp, err := esr.client().Get(key, false, false)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return resp, nil
  72. }
  73. func (esr *etcdSubnetRegistry) getSubnets() (*etcd.Response, error) {
  74. key := path.Join(esr.etcdCfg.Prefix, "subnets")
  75. return esr.client().Get(key, false, true)
  76. }
  77. func (esr *etcdSubnetRegistry) createSubnet(sn, data string, ttl uint64) (*etcd.Response, error) {
  78. key := path.Join(esr.etcdCfg.Prefix, "subnets", sn)
  79. resp, err := esr.client().Create(key, data, ttl)
  80. if err != nil {
  81. return nil, err
  82. }
  83. ensureExpiration(resp, ttl)
  84. return resp, nil
  85. }
  86. func (esr *etcdSubnetRegistry) updateSubnet(sn, data string, ttl uint64) (*etcd.Response, error) {
  87. key := path.Join(esr.etcdCfg.Prefix, "subnets", sn)
  88. resp, err := esr.client().Set(key, data, ttl)
  89. if err != nil {
  90. return nil, err
  91. }
  92. ensureExpiration(resp, ttl)
  93. return resp, nil
  94. }
  95. func (esr *etcdSubnetRegistry) watchSubnets(since uint64, stop chan bool) (*etcd.Response, error) {
  96. for {
  97. key := path.Join(esr.etcdCfg.Prefix, "subnets")
  98. resp, err := esr.client().RawWatch(key, since, true, nil, stop)
  99. if err != nil {
  100. if err == etcd.ErrWatchStoppedByUser {
  101. return nil, nil
  102. } else {
  103. return nil, err
  104. }
  105. }
  106. if len(resp.Body) == 0 {
  107. // etcd timed out, go back but recreate the client as the underlying
  108. // http transport gets hosed (http://code.google.com/p/go/issues/detail?id=8648)
  109. esr.resetClient()
  110. continue
  111. }
  112. return resp.Unmarshal()
  113. }
  114. }
  115. func (esr *etcdSubnetRegistry) client() *etcd.Client {
  116. esr.mux.Lock()
  117. defer esr.mux.Unlock()
  118. return esr.cli
  119. }
  120. func (esr *etcdSubnetRegistry) resetClient() {
  121. esr.mux.Lock()
  122. defer esr.mux.Unlock()
  123. var err error
  124. esr.cli.Close()
  125. esr.cli, err = newEtcdClient(esr.etcdCfg)
  126. if err != nil {
  127. panic(fmt.Errorf("resetClient: error recreating etcd client: %v", err))
  128. }
  129. }
  130. func ensureExpiration(resp *etcd.Response, ttl uint64) {
  131. if resp.Node.Expiration == nil {
  132. // should not be but calc it ourselves in this case
  133. log.Info("Expiration field missing on etcd response, calculating locally")
  134. exp := time.Now().Add(time.Duration(ttl) * time.Second)
  135. resp.Node.Expiration = &exp
  136. }
  137. }