55
66 "github.com/zeromicro/go-zero/core/lang"
77 "github.com/zeromicro/go-zero/core/logx"
8- v1 "k8s.io/api/core /v1"
8+ discoveryv1 "k8s.io/api/discovery /v1"
99 "k8s.io/client-go/tools/cache"
1010)
1111
@@ -28,20 +28,23 @@ func NewEventHandler(update func([]string)) *EventHandler {
2828
2929// OnAdd handles the endpoints add events.
3030func (h * EventHandler ) OnAdd (obj any , _ bool ) {
31- endpoints , ok := obj .(* v1. Endpoints )
31+ endpoints , ok := obj .(* discoveryv1. EndpointSlice )
3232 if ! ok {
33- logx .Errorf ("%v is not an object with type *v1.Endpoints " , obj )
33+ logx .Errorf ("%v is not an object with type *discoveryv1.EndpointSlice " , obj )
3434 return
3535 }
3636
3737 h .lock .Lock ()
3838 defer h .lock .Unlock ()
3939
4040 var changed bool
41- for _ , sub := range endpoints .Subsets {
42- for _ , point := range sub .Addresses {
43- if _ , ok := h .endpoints [point .IP ]; ! ok {
44- h .endpoints [point .IP ] = lang .Placeholder
41+ for _ , point := range endpoints .Endpoints {
42+ if len (point .Addresses ) == 0 {
43+ continue
44+ }
45+ for _ , address := range point .Addresses {
46+ if _ , ok := h .endpoints [address ]; ! ok {
47+ h .endpoints [address ] = lang .Placeholder
4548 changed = true
4649 }
4750 }
@@ -54,20 +57,23 @@ func (h *EventHandler) OnAdd(obj any, _ bool) {
5457
5558// OnDelete handles the endpoints delete events.
5659func (h * EventHandler ) OnDelete (obj any ) {
57- endpoints , ok := obj .(* v1. Endpoints )
60+ endpoints , ok := obj .(* discoveryv1. EndpointSlice )
5861 if ! ok {
59- logx .Errorf ("%v is not an object with type *v1.Endpoints " , obj )
62+ logx .Errorf ("%v is not an object with type *discoveryv1.EndpointSlice " , obj )
6063 return
6164 }
6265
6366 h .lock .Lock ()
6467 defer h .lock .Unlock ()
6568
6669 var changed bool
67- for _ , sub := range endpoints .Subsets {
68- for _ , point := range sub .Addresses {
69- if _ , ok := h .endpoints [point .IP ]; ok {
70- delete (h .endpoints , point .IP )
70+ for _ , point := range endpoints .Endpoints {
71+ if len (point .Addresses ) == 0 {
72+ continue
73+ }
74+ for _ , address := range point .Addresses {
75+ if _ , ok := h .endpoints [address ]; ok {
76+ delete (h .endpoints , address )
7177 changed = true
7278 }
7379 }
@@ -80,15 +86,15 @@ func (h *EventHandler) OnDelete(obj any) {
8086
8187// OnUpdate handles the endpoints update events.
8288func (h * EventHandler ) OnUpdate (oldObj , newObj any ) {
83- oldEndpoints , ok := oldObj .(* v1. Endpoints )
89+ oldEndpoints , ok := oldObj .(* discoveryv1. EndpointSlice )
8490 if ! ok {
85- logx .Errorf ("%v is not an object with type *v1.Endpoints " , oldObj )
91+ logx .Errorf ("%v is not an object with type *discoveryv1.EndpointSlice " , oldObj )
8692 return
8793 }
8894
89- newEndpoints , ok := newObj .(* v1. Endpoints )
95+ newEndpoints , ok := newObj .(* discoveryv1. EndpointSlice )
9096 if ! ok {
91- logx .Errorf ("%v is not an object with type *v1.Endpoints " , newObj )
97+ logx .Errorf ("%v is not an object with type *discoveryv1.EndpointSlice " , newObj )
9298 return
9399 }
94100
@@ -100,15 +106,18 @@ func (h *EventHandler) OnUpdate(oldObj, newObj any) {
100106}
101107
102108// Update updates the endpoints.
103- func (h * EventHandler ) Update (endpoints * v1. Endpoints ) {
109+ func (h * EventHandler ) Update (endpoints * discoveryv1. EndpointSlice ) {
104110 h .lock .Lock ()
105111 defer h .lock .Unlock ()
106112
107113 old := h .endpoints
108114 h .endpoints = make (map [string ]lang.PlaceholderType )
109- for _ , sub := range endpoints .Subsets {
110- for _ , point := range sub .Addresses {
111- h .endpoints [point .IP ] = lang .Placeholder
115+ for _ , point := range endpoints .Endpoints {
116+ if len (point .Addresses ) == 0 {
117+ continue
118+ }
119+ for _ , address := range point .Addresses {
120+ h .endpoints [address ] = lang .Placeholder
112121 }
113122 }
114123
0 commit comments