watch.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, receiver chan []Event) {
  25. lw := &leaseWatcher{}
  26. var cursor interface{}
  27. for {
  28. res, err := sm.WatchLeases(ctx, network, cursor)
  29. if err != nil {
  30. if err == context.Canceled || err == context.DeadlineExceeded {
  31. return
  32. }
  33. log.Errorf("Watch subnets: %v", err)
  34. time.Sleep(time.Second)
  35. continue
  36. }
  37. cursor = res.Cursor
  38. batch := []Event{}
  39. if len(res.Snapshot) > 0 {
  40. batch = lw.reset(res.Snapshot)
  41. } else {
  42. batch = lw.update(res.Events)
  43. }
  44. if batch != nil {
  45. receiver <- batch
  46. }
  47. }
  48. }
  49. type leaseWatcher struct {
  50. leases []Lease
  51. }
  52. func (lw *leaseWatcher) reset(leases []Lease) []Event {
  53. batch := []Event{}
  54. for _, nl := range leases {
  55. found := false
  56. for i, ol := range lw.leases {
  57. if ol.Subnet.Equal(nl.Subnet) {
  58. lw.leases = deleteLease(lw.leases, i)
  59. found = true
  60. break
  61. }
  62. }
  63. if !found {
  64. // new lease
  65. batch = append(batch, Event{SubnetAdded, nl})
  66. }
  67. }
  68. // everything left in sm.leases has been deleted
  69. for _, l := range lw.leases {
  70. batch = append(batch, Event{SubnetRemoved, l})
  71. }
  72. lw.leases = leases
  73. return batch
  74. }
  75. func (lw *leaseWatcher) update(events []Event) []Event {
  76. batch := []Event{}
  77. for _, e := range events {
  78. switch e.Type {
  79. case SubnetAdded:
  80. batch = append(batch, lw.add(&e.Lease))
  81. case SubnetRemoved:
  82. batch = append(batch, lw.remove(&e.Lease))
  83. }
  84. }
  85. return batch
  86. }
  87. func (lw *leaseWatcher) add(lease *Lease) Event {
  88. for i, l := range lw.leases {
  89. if l.Subnet.Equal(lease.Subnet) {
  90. lw.leases[i] = *lease
  91. return Event{SubnetAdded, lw.leases[i]}
  92. }
  93. }
  94. lw.leases = append(lw.leases, *lease)
  95. return Event{SubnetAdded, lw.leases[len(lw.leases)-1]}
  96. }
  97. func (lw *leaseWatcher) remove(lease *Lease) Event {
  98. for i, l := range lw.leases {
  99. if l.Subnet.Equal(lease.Subnet) {
  100. lw.leases = deleteLease(lw.leases, i)
  101. return Event{SubnetRemoved, l}
  102. }
  103. }
  104. log.Errorf("Removed subnet (%s) was not found", lease.Subnet)
  105. return Event{SubnetRemoved, *lease}
  106. }
  107. func deleteLease(l []Lease, i int) []Lease {
  108. l[i], l = l[len(l)-1], l[:len(l)-1]
  109. return l
  110. }