1
0

eventhandler.go 2.8 KB

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