eventhandler.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package kube
  2. import (
  3. "sync"
  4. "github.com/tal-tech/go-zero/core/lang"
  5. "github.com/tal-tech/go-zero/core/logx"
  6. v1 "k8s.io/api/core/v1"
  7. )
  8. // EventHandler is ResourceEventHandler implementation.
  9. type EventHandler struct {
  10. update func([]string)
  11. endpoints map[string]lang.PlaceholderType
  12. lock sync.Mutex
  13. }
  14. // NewEventHandler returns an EventHandler.
  15. func NewEventHandler(update func([]string)) *EventHandler {
  16. return &EventHandler{
  17. update: update,
  18. endpoints: make(map[string]lang.PlaceholderType),
  19. }
  20. }
  21. // OnAdd handles the endpoints add events.
  22. func (h *EventHandler) OnAdd(obj interface{}) {
  23. endpoints, ok := obj.(*v1.Endpoints)
  24. if !ok {
  25. logx.Errorf("%v is not an object with type *v1.Endpoints", obj)
  26. return
  27. }
  28. h.lock.Lock()
  29. defer h.lock.Unlock()
  30. var changed bool
  31. for _, sub := range endpoints.Subsets {
  32. for _, point := range sub.Addresses {
  33. if _, ok := h.endpoints[point.IP]; !ok {
  34. h.endpoints[point.IP] = lang.Placeholder
  35. changed = true
  36. }
  37. }
  38. }
  39. if changed {
  40. h.notify()
  41. }
  42. }
  43. // OnDelete handles the endpoints delete events.
  44. func (h *EventHandler) OnDelete(obj interface{}) {
  45. endpoints, ok := obj.(*v1.Endpoints)
  46. if !ok {
  47. logx.Errorf("%v is not an object with type *v1.Endpoints", obj)
  48. return
  49. }
  50. h.lock.Lock()
  51. defer h.lock.Unlock()
  52. var changed bool
  53. for _, sub := range endpoints.Subsets {
  54. for _, point := range sub.Addresses {
  55. if _, ok := h.endpoints[point.IP]; ok {
  56. delete(h.endpoints, point.IP)
  57. changed = true
  58. }
  59. }
  60. }
  61. if changed {
  62. h.notify()
  63. }
  64. }
  65. // OnUpdate handles the endpoints update events.
  66. func (h *EventHandler) OnUpdate(oldObj, newObj interface{}) {
  67. oldEndpoints, ok := oldObj.(*v1.Endpoints)
  68. if !ok {
  69. logx.Errorf("%v is not an object with type *v1.Endpoints", oldObj)
  70. return
  71. }
  72. newEndpoints, ok := newObj.(*v1.Endpoints)
  73. if !ok {
  74. logx.Errorf("%v is not an object with type *v1.Endpoints", newObj)
  75. return
  76. }
  77. if oldEndpoints.ResourceVersion == newEndpoints.ResourceVersion {
  78. return
  79. }
  80. h.Update(newEndpoints)
  81. }
  82. // Update updates the endpoints.
  83. func (h *EventHandler) Update(endpoints *v1.Endpoints) {
  84. h.lock.Lock()
  85. defer h.lock.Unlock()
  86. old := h.endpoints
  87. h.endpoints = make(map[string]lang.PlaceholderType)
  88. for _, sub := range endpoints.Subsets {
  89. for _, point := range sub.Addresses {
  90. h.endpoints[point.IP] = lang.Placeholder
  91. }
  92. }
  93. if diff(old, h.endpoints) {
  94. h.notify()
  95. }
  96. }
  97. func (h *EventHandler) notify() {
  98. var targets []string
  99. for k := range h.endpoints {
  100. targets = append(targets, k)
  101. }
  102. h.update(targets)
  103. }
  104. func diff(o, n map[string]lang.PlaceholderType) bool {
  105. if len(o) != len(n) {
  106. return true
  107. }
  108. for k := range o {
  109. if _, ok := n[k]; !ok {
  110. return true
  111. }
  112. }
  113. return false
  114. }