mock_registry.go 8.7 KB

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