watch.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2015 CoreOS, Inc.
  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. "time"
  17. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  18. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  19. )
  20. // WatchLeases performs a long term watch of the given network's subnet leases
  21. // and communicates addition/deletion events on receiver channel. It takes care
  22. // of handling "fall-behind" logic where the history window has advanced too far
  23. // and it needs to diff the latest snapshot with its saved state and generate events
  24. func WatchLeases(ctx context.Context, sm Manager, network string, ownLease *Lease, receiver chan []Event) {
  25. lw := &leaseWatcher{
  26. ownLease: ownLease,
  27. }
  28. var cursor interface{}
  29. for {
  30. res, err := sm.WatchLeases(ctx, network, cursor)
  31. if err != nil {
  32. if err == context.Canceled || err == context.DeadlineExceeded {
  33. return
  34. }
  35. log.Errorf("Watch subnets: %v", err)
  36. time.Sleep(time.Second)
  37. continue
  38. }
  39. cursor = res.Cursor
  40. batch := []Event{}
  41. if len(res.Events) > 0 {
  42. batch = lw.update(res.Events)
  43. } else {
  44. batch = lw.reset(res.Snapshot)
  45. }
  46. if batch != nil {
  47. receiver <- batch
  48. }
  49. }
  50. }
  51. type leaseWatcher struct {
  52. ownLease *Lease
  53. leases []Lease
  54. }
  55. func (lw *leaseWatcher) reset(leases []Lease) []Event {
  56. batch := []Event{}
  57. for _, nl := range leases {
  58. if lw.ownLease != nil && nl.Subnet.Equal(lw.ownLease.Subnet) {
  59. continue
  60. }
  61. found := false
  62. for i, ol := range lw.leases {
  63. if ol.Subnet.Equal(nl.Subnet) {
  64. lw.leases = deleteLease(lw.leases, i)
  65. found = true
  66. break
  67. }
  68. }
  69. if !found {
  70. // new lease
  71. batch = append(batch, Event{EventAdded, nl, ""})
  72. }
  73. }
  74. // everything left in sm.leases has been deleted
  75. for _, l := range lw.leases {
  76. batch = append(batch, Event{EventRemoved, l, ""})
  77. }
  78. lw.leases = leases
  79. return batch
  80. }
  81. func (lw *leaseWatcher) update(events []Event) []Event {
  82. batch := []Event{}
  83. for _, e := range events {
  84. if lw.ownLease != nil && e.Lease.Subnet.Equal(lw.ownLease.Subnet) {
  85. continue
  86. }
  87. switch e.Type {
  88. case EventAdded:
  89. batch = append(batch, lw.add(&e.Lease))
  90. case EventRemoved:
  91. batch = append(batch, lw.remove(&e.Lease))
  92. }
  93. }
  94. return batch
  95. }
  96. func (lw *leaseWatcher) add(lease *Lease) Event {
  97. for i, l := range lw.leases {
  98. if l.Subnet.Equal(lease.Subnet) {
  99. lw.leases[i] = *lease
  100. return Event{EventAdded, lw.leases[i], ""}
  101. }
  102. }
  103. lw.leases = append(lw.leases, *lease)
  104. return Event{EventAdded, lw.leases[len(lw.leases)-1], ""}
  105. }
  106. func (lw *leaseWatcher) remove(lease *Lease) Event {
  107. for i, l := range lw.leases {
  108. if l.Subnet.Equal(lease.Subnet) {
  109. lw.leases = deleteLease(lw.leases, i)
  110. return Event{EventRemoved, l, ""}
  111. }
  112. }
  113. log.Errorf("Removed subnet (%s) was not found", lease.Subnet)
  114. return Event{EventRemoved, *lease, ""}
  115. }
  116. func deleteLease(l []Lease, i int) []Lease {
  117. l[i], l = l[len(l)-1], l[:len(l)-1]
  118. return l
  119. }
  120. // WatchNetworks performs a long term watch of flannel networks and communicates
  121. // addition/deletion events on receiver channel. It takes care of handling
  122. // "fall-behind" logic where the history window has advanced too far and it
  123. // needs to diff the latest snapshot with its saved state and generate events
  124. func WatchNetworks(ctx context.Context, sm Manager, receiver chan []Event) {
  125. nw := newNetWatcher()
  126. var cursor interface{}
  127. for {
  128. res, err := sm.WatchNetworks(ctx, cursor)
  129. if err != nil {
  130. if err == context.Canceled || err == context.DeadlineExceeded {
  131. return
  132. }
  133. log.Errorf("Watch networks: %v", err)
  134. time.Sleep(time.Second)
  135. continue
  136. }
  137. cursor = res.Cursor
  138. batch := []Event{}
  139. if len(res.Events) > 0 {
  140. batch = nw.update(res.Events)
  141. } else {
  142. batch = nw.reset(res.Snapshot)
  143. }
  144. if batch != nil {
  145. receiver <- batch
  146. }
  147. }
  148. }
  149. type netWatcher struct {
  150. networks map[string]bool
  151. }
  152. func newNetWatcher() *netWatcher {
  153. return &netWatcher{networks: make(map[string]bool)}
  154. }
  155. func (nw *netWatcher) reset(networks []string) []Event {
  156. batch := []Event{}
  157. newNetworks := make(map[string]bool)
  158. for _, netname := range networks {
  159. if nw.networks[netname] {
  160. delete(nw.networks, netname)
  161. } else {
  162. // new network
  163. batch = append(batch, Event{EventAdded, Lease{}, netname})
  164. }
  165. newNetworks[netname] = true
  166. }
  167. // everything left in sm.networks has been deleted
  168. for netname, _ := range nw.networks {
  169. batch = append(batch, Event{EventRemoved, Lease{}, netname})
  170. }
  171. nw.networks = newNetworks
  172. return batch
  173. }
  174. func (nw *netWatcher) update(events []Event) []Event {
  175. batch := []Event{}
  176. for _, e := range events {
  177. switch e.Type {
  178. case EventAdded:
  179. batch = append(batch, nw.add(e.Network))
  180. case EventRemoved:
  181. batch = append(batch, nw.remove(e.Network))
  182. }
  183. }
  184. return batch
  185. }
  186. func (nw *netWatcher) add(network string) Event {
  187. if _, ok := nw.networks[network]; !ok {
  188. nw.networks[network] = true
  189. }
  190. return Event{EventAdded, Lease{}, network}
  191. }
  192. func (nw *netWatcher) remove(network string) Event {
  193. if _, ok := nw.networks[network]; ok {
  194. delete(nw.networks, network)
  195. } else {
  196. log.Errorf("Removed network (%s) was not found", network)
  197. }
  198. return Event{EventRemoved, Lease{}, network}
  199. }