mock_registry.go 9.5 KB

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