watch.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.Snapshot) > 0 {
  42. batch = lw.reset(res.Snapshot)
  43. } else {
  44. batch = lw.update(res.Events)
  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{SubnetAdded, nl})
  72. }
  73. }
  74. // everything left in sm.leases has been deleted
  75. for _, l := range lw.leases {
  76. batch = append(batch, Event{SubnetRemoved, 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 SubnetAdded:
  89. batch = append(batch, lw.add(&e.Lease))
  90. case SubnetRemoved:
  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{SubnetAdded, lw.leases[i]}
  101. }
  102. }
  103. lw.leases = append(lw.leases, *lease)
  104. return Event{SubnetAdded, 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{SubnetRemoved, l}
  111. }
  112. }
  113. log.Errorf("Removed subnet (%s) was not found", lease.Subnet)
  114. return Event{SubnetRemoved, *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. }