mock_registry.go 9.4 KB

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