registry.go 4.7 KB

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