5
5
6
6
"github.com/zeromicro/go-zero/core/lang"
7
7
"github.com/zeromicro/go-zero/core/logx"
8
- v1 "k8s.io/api/core /v1"
8
+ discoveryv1 "k8s.io/api/discovery /v1"
9
9
"k8s.io/client-go/tools/cache"
10
10
)
11
11
@@ -28,20 +28,23 @@ func NewEventHandler(update func([]string)) *EventHandler {
28
28
29
29
// OnAdd handles the endpoints add events.
30
30
func (h * EventHandler ) OnAdd (obj any , _ bool ) {
31
- endpoints , ok := obj .(* v1. Endpoints )
31
+ endpoints , ok := obj .(* discoveryv1. EndpointSlice )
32
32
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 )
34
34
return
35
35
}
36
36
37
37
h .lock .Lock ()
38
38
defer h .lock .Unlock ()
39
39
40
40
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
45
48
changed = true
46
49
}
47
50
}
@@ -54,20 +57,23 @@ func (h *EventHandler) OnAdd(obj any, _ bool) {
54
57
55
58
// OnDelete handles the endpoints delete events.
56
59
func (h * EventHandler ) OnDelete (obj any ) {
57
- endpoints , ok := obj .(* v1. Endpoints )
60
+ endpoints , ok := obj .(* discoveryv1. EndpointSlice )
58
61
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 )
60
63
return
61
64
}
62
65
63
66
h .lock .Lock ()
64
67
defer h .lock .Unlock ()
65
68
66
69
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 )
71
77
changed = true
72
78
}
73
79
}
@@ -80,15 +86,15 @@ func (h *EventHandler) OnDelete(obj any) {
80
86
81
87
// OnUpdate handles the endpoints update events.
82
88
func (h * EventHandler ) OnUpdate (oldObj , newObj any ) {
83
- oldEndpoints , ok := oldObj .(* v1. Endpoints )
89
+ oldEndpoints , ok := oldObj .(* discoveryv1. EndpointSlice )
84
90
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 )
86
92
return
87
93
}
88
94
89
- newEndpoints , ok := newObj .(* v1. Endpoints )
95
+ newEndpoints , ok := newObj .(* discoveryv1. EndpointSlice )
90
96
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 )
92
98
return
93
99
}
94
100
@@ -100,15 +106,18 @@ func (h *EventHandler) OnUpdate(oldObj, newObj any) {
100
106
}
101
107
102
108
// Update updates the endpoints.
103
- func (h * EventHandler ) Update (endpoints * v1. Endpoints ) {
109
+ func (h * EventHandler ) Update (endpoints * discoveryv1. EndpointSlice ) {
104
110
h .lock .Lock ()
105
111
defer h .lock .Unlock ()
106
112
107
113
old := h .endpoints
108
114
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
112
121
}
113
122
}
114
123
0 commit comments