mock_registry.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. "strings"
  18. "time"
  19. etcd "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/etcd/client"
  20. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  21. "github.com/coreos/flannel/pkg/ip"
  22. )
  23. type netwk struct {
  24. config string
  25. subnets []Lease
  26. subnetEvents chan event
  27. }
  28. type event struct {
  29. evt Event
  30. index uint64
  31. }
  32. type MockSubnetRegistry struct {
  33. networks map[string]*netwk
  34. networkEvents chan event
  35. index uint64
  36. }
  37. func NewMockRegistry(network, config string, initialSubnets []Lease) *MockSubnetRegistry {
  38. msr := &MockSubnetRegistry{
  39. networkEvents: make(chan event, 1000),
  40. index: 1000,
  41. networks: make(map[string]*netwk),
  42. }
  43. msr.networks[network] = &netwk{
  44. config: config,
  45. subnets: initialSubnets,
  46. subnetEvents: make(chan event, 1000),
  47. }
  48. return msr
  49. }
  50. func (msr *MockSubnetRegistry) getNetworkConfig(ctx context.Context, network string) (string, error) {
  51. n, ok := msr.networks[network]
  52. if !ok {
  53. return "", fmt.Errorf("Network %s not found", network)
  54. }
  55. return n.config, nil
  56. }
  57. func (msr *MockSubnetRegistry) setConfig(network, config string) error {
  58. n, ok := msr.networks[network]
  59. if !ok {
  60. return fmt.Errorf("Network %s not found", network)
  61. }
  62. n.config = config
  63. return nil
  64. }
  65. func (msr *MockSubnetRegistry) getSubnets(ctx context.Context, network string) ([]Lease, uint64, error) {
  66. n, ok := msr.networks[network]
  67. if !ok {
  68. return nil, 0, fmt.Errorf("Network %s not found", network)
  69. }
  70. return n.subnets, msr.index, nil
  71. }
  72. func (msr *MockSubnetRegistry) createSubnet(ctx context.Context, network string, sn ip.IP4Net, attrs *LeaseAttrs, ttl time.Duration) (time.Time, error) {
  73. n, ok := msr.networks[network]
  74. if !ok {
  75. return time.Time{}, fmt.Errorf("Network %s not found", network)
  76. }
  77. // check for existing
  78. if _, _, err := n.findSubnet(sn); err == nil {
  79. return time.Time{}, etcd.Error{
  80. Code: etcd.ErrorCodeNodeExist,
  81. Index: msr.index,
  82. }
  83. }
  84. msr.index += 1
  85. exp := clock.Now().Add(ttl)
  86. l := Lease{
  87. Subnet: sn,
  88. Attrs: *attrs,
  89. Expiration: exp,
  90. asof: msr.index,
  91. }
  92. n.subnets = append(n.subnets, l)
  93. evt := Event{
  94. Type: EventAdded,
  95. Lease: l,
  96. Network: network,
  97. }
  98. n.subnetEvents <- event{evt, msr.index}
  99. return exp, nil
  100. }
  101. func (msr *MockSubnetRegistry) updateSubnet(ctx context.Context, network string, sn ip.IP4Net, attrs *LeaseAttrs, ttl time.Duration, asof uint64) (time.Time, error) {
  102. n, ok := msr.networks[network]
  103. if !ok {
  104. return time.Time{}, fmt.Errorf("Network %s not found", network)
  105. }
  106. msr.index += 1
  107. exp := clock.Now().Add(ttl)
  108. sub, _, err := n.findSubnet(sn)
  109. if err != nil {
  110. return time.Time{}, err
  111. }
  112. sub.Attrs = *attrs
  113. sub.Expiration = exp
  114. sub.asof = msr.index
  115. n.subnetEvents <- event{
  116. Event{
  117. Type: EventAdded,
  118. Lease: sub,
  119. Network: network,
  120. }, msr.index}
  121. return sub.Expiration, nil
  122. }
  123. func (msr *MockSubnetRegistry) deleteSubnet(ctx context.Context, network string, sn ip.IP4Net) error {
  124. n, ok := msr.networks[network]
  125. if !ok {
  126. return fmt.Errorf("Network %s not found", network)
  127. }
  128. msr.index += 1
  129. sub, i, err := n.findSubnet(sn)
  130. if err != nil {
  131. return err
  132. }
  133. n.subnets[i] = n.subnets[len(n.subnets)-1]
  134. n.subnets = n.subnets[:len(n.subnets)-1]
  135. sub.asof = msr.index
  136. n.subnetEvents <- event{
  137. Event{
  138. Type: EventRemoved,
  139. Lease: sub,
  140. Network: network,
  141. }, msr.index}
  142. return nil
  143. }
  144. func (msr *MockSubnetRegistry) watchSubnets(ctx context.Context, network string, since uint64) (Event, uint64, error) {
  145. n, ok := msr.networks[network]
  146. if !ok {
  147. return Event{}, msr.index, fmt.Errorf("Network %s not found", network)
  148. }
  149. for {
  150. if since < msr.index {
  151. return Event{}, msr.index, etcd.Error{
  152. Code: etcd.ErrorCodeEventIndexCleared,
  153. Cause: "out of date",
  154. Message: "cursor is out of date",
  155. Index: msr.index,
  156. }
  157. }
  158. select {
  159. case <-ctx.Done():
  160. return Event{}, msr.index, ctx.Err()
  161. case e := <-n.subnetEvents:
  162. if e.index <= since {
  163. continue
  164. }
  165. return e.evt, msr.index, nil
  166. }
  167. }
  168. }
  169. func (msr *MockSubnetRegistry) expireSubnet(network string, sn ip.IP4Net) {
  170. n, ok := msr.networks[network]
  171. if !ok {
  172. return
  173. }
  174. if sub, i, err := n.findSubnet(sn); err == nil {
  175. msr.index += 1
  176. n.subnets[i] = n.subnets[len(n.subnets)-1]
  177. n.subnets = n.subnets[:len(n.subnets)-1]
  178. sub.asof = msr.index
  179. n.subnetEvents <- event{
  180. Event{
  181. Type: EventRemoved,
  182. Lease: sub,
  183. }, msr.index,
  184. }
  185. }
  186. }
  187. func configKeyToNetworkKey(configKey string) string {
  188. if !strings.HasSuffix(configKey, "/config") {
  189. return ""
  190. }
  191. return strings.TrimSuffix(configKey, "/config")
  192. }
  193. func (msr *MockSubnetRegistry) getNetworks(ctx context.Context) ([]string, uint64, error) {
  194. ns := []string{}
  195. for n, _ := range msr.networks {
  196. ns = append(ns, n)
  197. }
  198. return ns, msr.index, nil
  199. }
  200. func (msr *MockSubnetRegistry) watchNetworks(ctx context.Context, since uint64) (Event, uint64, error) {
  201. for {
  202. if since < msr.index {
  203. return Event{}, msr.index, etcd.Error{
  204. Code: etcd.ErrorCodeEventIndexCleared,
  205. Cause: "out of date",
  206. Message: "cursor is out of date",
  207. Index: msr.index,
  208. }
  209. }
  210. select {
  211. case <-ctx.Done():
  212. return Event{}, msr.index, ctx.Err()
  213. case e := <-msr.networkEvents:
  214. if e.index <= since {
  215. continue
  216. }
  217. return e.evt, msr.index, nil
  218. }
  219. }
  220. }
  221. func (msr *MockSubnetRegistry) getNetwork(ctx context.Context, network string) (*netwk, error) {
  222. n, ok := msr.networks[network]
  223. if !ok {
  224. return nil, fmt.Errorf("Network %s not found", network)
  225. }
  226. return n, nil
  227. }
  228. func (msr *MockSubnetRegistry) CreateNetwork(ctx context.Context, network, config string) error {
  229. _, ok := msr.networks[network]
  230. if ok {
  231. return fmt.Errorf("Network %s already exists", network)
  232. }
  233. msr.index += 1
  234. n := &netwk{
  235. config: network,
  236. subnetEvents: make(chan event, 1000),
  237. }
  238. msr.networks[network] = n
  239. msr.networkEvents <- event{
  240. Event{
  241. Type: EventAdded,
  242. Network: network,
  243. }, msr.index,
  244. }
  245. return nil
  246. }
  247. func (msr *MockSubnetRegistry) DeleteNetwork(ctx context.Context, network string) error {
  248. _, ok := msr.networks[network]
  249. if !ok {
  250. return fmt.Errorf("Network %s not found", network)
  251. }
  252. delete(msr.networks, network)
  253. msr.index += 1
  254. msr.networkEvents <- event{
  255. Event{
  256. Type: EventRemoved,
  257. Network: network,
  258. }, msr.index,
  259. }
  260. return nil
  261. }
  262. func (n *netwk) findSubnet(sn ip.IP4Net) (Lease, int, error) {
  263. for i, sub := range n.subnets {
  264. if sub.Subnet.Equal(sn) {
  265. return sub, i, nil
  266. }
  267. }
  268. return Lease{}, 0, fmt.Errorf("subnet not found")
  269. }