watch.go 6.6 KB

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