mock_registry.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 := time.Time{}
  123. if ttl != 0 {
  124. exp = clock.Now().Add(ttl)
  125. }
  126. l := Lease{
  127. Subnet: sn,
  128. Attrs: *attrs,
  129. Expiration: exp,
  130. asof: msr.index,
  131. }
  132. n.subnets = append(n.subnets, l)
  133. evt := Event{
  134. Type: EventAdded,
  135. Lease: l,
  136. Network: network,
  137. }
  138. n.sendSubnetEvent(sn, event{evt, msr.index})
  139. return exp, nil
  140. }
  141. func (msr *MockSubnetRegistry) updateSubnet(ctx context.Context, network string, sn ip.IP4Net, attrs *LeaseAttrs, ttl time.Duration, asof uint64) (time.Time, error) {
  142. n, ok := msr.networks[network]
  143. if !ok {
  144. return time.Time{}, fmt.Errorf("Network %s not found", network)
  145. }
  146. msr.index += 1
  147. exp := time.Time{}
  148. if ttl != 0 {
  149. exp = clock.Now().Add(ttl)
  150. }
  151. sub, i, err := n.findSubnet(sn)
  152. if err != nil {
  153. return time.Time{}, err
  154. }
  155. sub.Attrs = *attrs
  156. sub.asof = msr.index
  157. sub.Expiration = exp
  158. n.subnets[i] = sub
  159. n.sendSubnetEvent(sn, event{
  160. Event{
  161. Type: EventAdded,
  162. Lease: sub,
  163. Network: network,
  164. }, msr.index,
  165. })
  166. return sub.Expiration, nil
  167. }
  168. func (msr *MockSubnetRegistry) deleteSubnet(ctx context.Context, network string, sn ip.IP4Net) error {
  169. n, ok := msr.networks[network]
  170. if !ok {
  171. return fmt.Errorf("Network %s not found", network)
  172. }
  173. msr.index += 1
  174. sub, i, err := n.findSubnet(sn)
  175. if err != nil {
  176. return err
  177. }
  178. n.subnets[i] = n.subnets[len(n.subnets)-1]
  179. n.subnets = n.subnets[:len(n.subnets)-1]
  180. sub.asof = msr.index
  181. n.sendSubnetEvent(sn, event{
  182. Event{
  183. Type: EventRemoved,
  184. Lease: sub,
  185. Network: network,
  186. }, msr.index,
  187. })
  188. return nil
  189. }
  190. func (msr *MockSubnetRegistry) watchSubnets(ctx context.Context, network string, since uint64) (Event, uint64, error) {
  191. n, ok := msr.networks[network]
  192. if !ok {
  193. return Event{}, msr.index, fmt.Errorf("Network %s not found", network)
  194. }
  195. for {
  196. if since < msr.index {
  197. return Event{}, msr.index, etcd.Error{
  198. Code: etcd.ErrorCodeEventIndexCleared,
  199. Cause: "out of date",
  200. Message: "cursor is out of date",
  201. Index: msr.index,
  202. }
  203. }
  204. select {
  205. case <-ctx.Done():
  206. return Event{}, msr.index, ctx.Err()
  207. case e := <-n.subnetsEvents:
  208. if e.index <= since {
  209. continue
  210. }
  211. return e.evt, msr.index, nil
  212. }
  213. }
  214. }
  215. func (msr *MockSubnetRegistry) watchSubnet(ctx context.Context, network string, since uint64, sn ip.IP4Net) (Event, uint64, error) {
  216. n, ok := msr.networks[network]
  217. if !ok {
  218. return Event{}, msr.index, fmt.Errorf("Network %s not found", network)
  219. }
  220. for {
  221. if since < msr.index {
  222. return Event{}, msr.index, etcd.Error{
  223. Code: etcd.ErrorCodeEventIndexCleared,
  224. Cause: "out of date",
  225. Message: "cursor is out of date",
  226. Index: msr.index,
  227. }
  228. }
  229. select {
  230. case <-ctx.Done():
  231. return Event{}, msr.index, ctx.Err()
  232. case e := <-n.subnetEventsChan(sn):
  233. if e.index <= since {
  234. continue
  235. }
  236. return e.evt, msr.index, nil
  237. }
  238. }
  239. }
  240. func (msr *MockSubnetRegistry) expireSubnet(network string, sn ip.IP4Net) {
  241. n, ok := msr.networks[network]
  242. if !ok {
  243. return
  244. }
  245. if sub, i, err := n.findSubnet(sn); err == nil {
  246. msr.index += 1
  247. n.subnets[i] = n.subnets[len(n.subnets)-1]
  248. n.subnets = n.subnets[:len(n.subnets)-1]
  249. sub.asof = msr.index
  250. n.sendSubnetEvent(sn, event{
  251. Event{
  252. Type: EventRemoved,
  253. Lease: sub,
  254. }, msr.index,
  255. })
  256. }
  257. }
  258. func configKeyToNetworkKey(configKey string) string {
  259. if !strings.HasSuffix(configKey, "/config") {
  260. return ""
  261. }
  262. return strings.TrimSuffix(configKey, "/config")
  263. }
  264. func (msr *MockSubnetRegistry) getNetworks(ctx context.Context) ([]string, uint64, error) {
  265. ns := []string{}
  266. for n, _ := range msr.networks {
  267. ns = append(ns, n)
  268. }
  269. return ns, msr.index, nil
  270. }
  271. func (msr *MockSubnetRegistry) watchNetworks(ctx context.Context, since uint64) (Event, uint64, error) {
  272. for {
  273. if since < msr.index {
  274. return Event{}, msr.index, etcd.Error{
  275. Code: etcd.ErrorCodeEventIndexCleared,
  276. Cause: "out of date",
  277. Message: "cursor is out of date",
  278. Index: msr.index,
  279. }
  280. }
  281. select {
  282. case <-ctx.Done():
  283. return Event{}, msr.index, ctx.Err()
  284. case e := <-msr.networkEvents:
  285. if e.index <= since {
  286. continue
  287. }
  288. return e.evt, msr.index, nil
  289. }
  290. }
  291. }
  292. func (msr *MockSubnetRegistry) getNetwork(ctx context.Context, network string) (*netwk, error) {
  293. n, ok := msr.networks[network]
  294. if !ok {
  295. return nil, fmt.Errorf("Network %s not found", network)
  296. }
  297. return n, nil
  298. }
  299. func (msr *MockSubnetRegistry) CreateNetwork(ctx context.Context, network, config string) error {
  300. _, ok := msr.networks[network]
  301. if ok {
  302. return fmt.Errorf("Network %s already exists", network)
  303. }
  304. msr.index += 1
  305. n := &netwk{
  306. config: network,
  307. subnetsEvents: make(chan event, 1000),
  308. subnetEvents: make(map[ip.IP4Net]chan event),
  309. }
  310. msr.networks[network] = n
  311. msr.networkEvents <- event{
  312. Event{
  313. Type: EventAdded,
  314. Network: network,
  315. }, msr.index,
  316. }
  317. return nil
  318. }
  319. func (msr *MockSubnetRegistry) DeleteNetwork(ctx context.Context, network string) error {
  320. _, ok := msr.networks[network]
  321. if !ok {
  322. return fmt.Errorf("Network %s not found", network)
  323. }
  324. delete(msr.networks, network)
  325. msr.index += 1
  326. msr.networkEvents <- event{
  327. Event{
  328. Type: EventRemoved,
  329. Network: network,
  330. }, msr.index,
  331. }
  332. return nil
  333. }
  334. func (n *netwk) findSubnet(sn ip.IP4Net) (Lease, int, error) {
  335. for i, sub := range n.subnets {
  336. if sub.Subnet.Equal(sn) {
  337. return sub, i, nil
  338. }
  339. }
  340. return Lease{}, 0, fmt.Errorf("subnet not found")
  341. }