registry.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package subnet
  2. import (
  3. "sync"
  4. "time"
  5. "path"
  6. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  7. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  8. )
  9. type subnetRegistry interface {
  10. getConfig() (*etcd.Response, error)
  11. getSubnets() (*etcd.Response, error)
  12. createSubnet(sn, data string, ttl uint64) (*etcd.Response, error)
  13. updateSubnet(sn, data string, ttl uint64) (*etcd.Response, error)
  14. watchSubnets(since uint64, stop chan bool) (*etcd.Response, error)
  15. }
  16. type etcdSubnetRegistry struct {
  17. mux sync.Mutex
  18. cli *etcd.Client
  19. endpoint string
  20. prefix string
  21. }
  22. func newEtcdSubnetRegistry(endpoint, prefix string) subnetRegistry {
  23. return &etcdSubnetRegistry{
  24. cli: etcd.NewClient([]string{endpoint}),
  25. endpoint: endpoint,
  26. prefix: prefix,
  27. }
  28. }
  29. func (esr *etcdSubnetRegistry) getConfig() (*etcd.Response, error) {
  30. key := path.Join(esr.prefix, "config")
  31. resp, err := esr.client().Get(key, false, false)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return resp, nil
  36. }
  37. func (esr *etcdSubnetRegistry) getSubnets() (*etcd.Response, error) {
  38. key := path.Join(esr.prefix, "subnets")
  39. return esr.client().Get(key, false, true)
  40. }
  41. func (esr *etcdSubnetRegistry) createSubnet(sn, data string, ttl uint64) (*etcd.Response, error) {
  42. key := path.Join(esr.prefix, "subnets", sn)
  43. resp, err := esr.client().Create(key, data, ttl)
  44. if err != nil {
  45. return nil, err
  46. }
  47. ensureExpiration(resp, ttl)
  48. return resp, nil
  49. }
  50. func (esr *etcdSubnetRegistry) updateSubnet(sn, data string, ttl uint64) (*etcd.Response, error) {
  51. key := path.Join(esr.prefix, "subnets", sn)
  52. resp, err := esr.client().Set(key, data, ttl)
  53. if err != nil {
  54. return nil, err
  55. }
  56. ensureExpiration(resp, ttl)
  57. return resp, nil
  58. }
  59. func (esr *etcdSubnetRegistry) watchSubnets(since uint64, stop chan bool) (*etcd.Response, error) {
  60. for {
  61. key := path.Join(esr.prefix, "subnets")
  62. resp, err := esr.client().RawWatch(key, since, true, nil, stop)
  63. if err != nil {
  64. if err == etcd.ErrWatchStoppedByUser {
  65. return nil, nil
  66. } else {
  67. return nil, err
  68. }
  69. }
  70. if len(resp.Body) == 0 {
  71. // etcd timed out, go back but recreate the client as the underlying
  72. // http transport gets hosed (http://code.google.com/p/go/issues/detail?id=8648)
  73. esr.resetClient()
  74. continue
  75. }
  76. return resp.Unmarshal()
  77. }
  78. }
  79. func (esr *etcdSubnetRegistry) client() *etcd.Client {
  80. esr.mux.Lock()
  81. defer esr.mux.Unlock()
  82. return esr.cli
  83. }
  84. func (esr *etcdSubnetRegistry) resetClient() {
  85. esr.mux.Lock()
  86. defer esr.mux.Unlock()
  87. esr.cli = etcd.NewClient([]string{esr.endpoint})
  88. }
  89. func ensureExpiration(resp *etcd.Response, ttl uint64) {
  90. if resp.Node.Expiration == nil {
  91. // should not be but calc it ourselves in this case
  92. log.Info("Expiration field missing on etcd response, calculating locally")
  93. exp := time.Now().Add(time.Duration(ttl) * time.Second)
  94. resp.Node.Expiration = &exp
  95. }
  96. }