registry.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "fmt"
  17. "path"
  18. "sync"
  19. "time"
  20. etcd "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/etcd/client"
  21. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/etcd/pkg/transport"
  22. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  23. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  24. )
  25. type Registry interface {
  26. getNetworkConfig(ctx context.Context, network string) (*etcd.Response, error)
  27. getSubnets(ctx context.Context, network string) (*etcd.Response, error)
  28. createSubnet(ctx context.Context, network, sn, data string, ttl time.Duration) (*etcd.Response, error)
  29. updateSubnet(ctx context.Context, network, sn, data string, ttl time.Duration) (*etcd.Response, error)
  30. deleteSubnet(ctx context.Context, network, sn string) (*etcd.Response, error)
  31. watch(ctx context.Context, path string, since uint64) (*etcd.Response, error)
  32. getNetworks(ctx context.Context) (*etcd.Response, error)
  33. }
  34. type EtcdConfig struct {
  35. Endpoints []string
  36. Keyfile string
  37. Certfile string
  38. CAFile string
  39. Prefix string
  40. }
  41. type etcdSubnetRegistry struct {
  42. mux sync.Mutex
  43. cli etcd.KeysAPI
  44. etcdCfg *EtcdConfig
  45. }
  46. func newEtcdClient(c *EtcdConfig) (etcd.KeysAPI, error) {
  47. tlsInfo := transport.TLSInfo{
  48. CertFile: c.Certfile,
  49. KeyFile: c.Keyfile,
  50. CAFile: c.CAFile,
  51. }
  52. t, err := transport.NewTransport(tlsInfo)
  53. if err != nil {
  54. return nil, err
  55. }
  56. cli, err := etcd.New(etcd.Config{
  57. Endpoints: c.Endpoints,
  58. Transport: t,
  59. })
  60. if err != nil {
  61. return nil, err
  62. }
  63. return etcd.NewKeysAPI(cli), nil
  64. }
  65. func newEtcdSubnetRegistry(config *EtcdConfig) (Registry, error) {
  66. r := &etcdSubnetRegistry{
  67. etcdCfg: config,
  68. }
  69. var err error
  70. r.cli, err = newEtcdClient(config)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return r, nil
  75. }
  76. func (esr *etcdSubnetRegistry) getNetworkConfig(ctx context.Context, network string) (*etcd.Response, error) {
  77. key := path.Join(esr.etcdCfg.Prefix, network, "config")
  78. resp, err := esr.client().Get(ctx, key, nil)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return resp, nil
  83. }
  84. func (esr *etcdSubnetRegistry) getSubnets(ctx context.Context, network string) (*etcd.Response, error) {
  85. key := path.Join(esr.etcdCfg.Prefix, network, "subnets")
  86. return esr.client().Get(ctx, key, &etcd.GetOptions{Recursive: true})
  87. }
  88. func (esr *etcdSubnetRegistry) createSubnet(ctx context.Context, network, sn, data string, ttl time.Duration) (*etcd.Response, error) {
  89. key := path.Join(esr.etcdCfg.Prefix, network, "subnets", sn)
  90. opts := &etcd.SetOptions{
  91. PrevExist: etcd.PrevNoExist,
  92. TTL: ttl,
  93. }
  94. resp, err := esr.client().Set(ctx, key, data, opts)
  95. if err != nil {
  96. return nil, err
  97. }
  98. ensureExpiration(resp, ttl)
  99. return resp, nil
  100. }
  101. func (esr *etcdSubnetRegistry) updateSubnet(ctx context.Context, network, sn, data string, ttl time.Duration) (*etcd.Response, error) {
  102. key := path.Join(esr.etcdCfg.Prefix, network, "subnets", sn)
  103. resp, err := esr.client().Set(ctx, key, data, &etcd.SetOptions{TTL: ttl})
  104. if err != nil {
  105. return nil, err
  106. }
  107. ensureExpiration(resp, ttl)
  108. return resp, nil
  109. }
  110. func (esr *etcdSubnetRegistry) deleteSubnet(ctx context.Context, network, sn string) (*etcd.Response, error) {
  111. key := path.Join(esr.etcdCfg.Prefix, network, "subnets", sn)
  112. return esr.client().Delete(ctx, key, nil)
  113. }
  114. func (esr *etcdSubnetRegistry) watch(ctx context.Context, subpath string, since uint64) (*etcd.Response, error) {
  115. key := path.Join(esr.etcdCfg.Prefix, subpath)
  116. opts := &etcd.WatcherOptions{
  117. AfterIndex: since,
  118. Recursive: true,
  119. }
  120. e, err := esr.client().Watcher(key, opts).Next(ctx)
  121. return e, err
  122. }
  123. func (esr *etcdSubnetRegistry) getNetworks(ctx context.Context) (*etcd.Response, error) {
  124. return esr.client().Get(ctx, esr.etcdCfg.Prefix, &etcd.GetOptions{Recursive: true})
  125. }
  126. func (esr *etcdSubnetRegistry) client() etcd.KeysAPI {
  127. esr.mux.Lock()
  128. defer esr.mux.Unlock()
  129. return esr.cli
  130. }
  131. func (esr *etcdSubnetRegistry) resetClient() {
  132. esr.mux.Lock()
  133. defer esr.mux.Unlock()
  134. var err error
  135. esr.cli, err = newEtcdClient(esr.etcdCfg)
  136. if err != nil {
  137. panic(fmt.Errorf("resetClient: error recreating etcd client: %v", err))
  138. }
  139. }
  140. func ensureExpiration(resp *etcd.Response, ttl time.Duration) {
  141. if resp.Node.Expiration == nil {
  142. // should not be but calc it ourselves in this case
  143. log.Info("Expiration field missing on etcd response, calculating locally")
  144. exp := time.Now().Add(time.Duration(ttl) * time.Second)
  145. resp.Node.Expiration = &exp
  146. }
  147. }