eventhandler.go 2.4 KB

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