feat(zrpc): migrate kube resolver from Endpoints to EndpointSlice API (#4987)

Signed-off-by: soasurs <soasurs@gmail.com>
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
soasurs
2025-12-11 23:09:08 +08:00
committed by GitHub
parent 858f8ca82e
commit 3d291328d8
4 changed files with 180 additions and 196 deletions

View File

@@ -5,7 +5,7 @@ import (
"github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/logx"
"k8s.io/api/core/v1"
"k8s.io/api/discovery/v1"
"k8s.io/client-go/tools/cache"
)
@@ -28,9 +28,9 @@ func NewEventHandler(update func([]string)) *EventHandler {
// OnAdd handles the endpoints add events.
func (h *EventHandler) OnAdd(obj any, _ bool) {
endpoints, ok := obj.(*v1.Endpoints)
endpoints, ok := obj.(*v1.EndpointSlice)
if !ok {
logx.Errorf("%v is not an object with type *v1.Endpoints", obj)
logx.Errorf("%v is not an object with type *v1.EndpointSlice", obj)
return
}
@@ -38,10 +38,10 @@ func (h *EventHandler) OnAdd(obj any, _ bool) {
defer h.lock.Unlock()
var changed bool
for _, sub := range endpoints.Subsets {
for _, point := range sub.Addresses {
if _, ok := h.endpoints[point.IP]; !ok {
h.endpoints[point.IP] = lang.Placeholder
for _, point := range endpoints.Endpoints {
for _, address := range point.Addresses {
if _, ok := h.endpoints[address]; !ok {
h.endpoints[address] = lang.Placeholder
changed = true
}
}
@@ -54,9 +54,9 @@ func (h *EventHandler) OnAdd(obj any, _ bool) {
// OnDelete handles the endpoints delete events.
func (h *EventHandler) OnDelete(obj any) {
endpoints, ok := obj.(*v1.Endpoints)
endpoints, ok := obj.(*v1.EndpointSlice)
if !ok {
logx.Errorf("%v is not an object with type *v1.Endpoints", obj)
logx.Errorf("%v is not an object with type *v1.EndpointSlice", obj)
return
}
@@ -64,10 +64,10 @@ func (h *EventHandler) OnDelete(obj any) {
defer h.lock.Unlock()
var changed bool
for _, sub := range endpoints.Subsets {
for _, point := range sub.Addresses {
if _, ok := h.endpoints[point.IP]; ok {
delete(h.endpoints, point.IP)
for _, point := range endpoints.Endpoints {
for _, address := range point.Addresses {
if _, ok := h.endpoints[address]; ok {
delete(h.endpoints, address)
changed = true
}
}
@@ -80,35 +80,35 @@ func (h *EventHandler) OnDelete(obj any) {
// OnUpdate handles the endpoints update events.
func (h *EventHandler) OnUpdate(oldObj, newObj any) {
oldEndpoints, ok := oldObj.(*v1.Endpoints)
oldEndpointSlices, ok := oldObj.(*v1.EndpointSlice)
if !ok {
logx.Errorf("%v is not an object with type *v1.Endpoints", oldObj)
logx.Errorf("%v is not an object with type *v1.EndpointSlice", oldObj)
return
}
newEndpoints, ok := newObj.(*v1.Endpoints)
newEndpointSlices, ok := newObj.(*v1.EndpointSlice)
if !ok {
logx.Errorf("%v is not an object with type *v1.Endpoints", newObj)
logx.Errorf("%v is not an object with type *v1.EndpointSlice", newObj)
return
}
if oldEndpoints.ResourceVersion == newEndpoints.ResourceVersion {
if oldEndpointSlices.ResourceVersion == newEndpointSlices.ResourceVersion {
return
}
h.Update(newEndpoints)
h.Update(newEndpointSlices)
}
// Update updates the endpoints.
func (h *EventHandler) Update(endpoints *v1.Endpoints) {
func (h *EventHandler) Update(endpoints *v1.EndpointSlice) {
h.lock.Lock()
defer h.lock.Unlock()
old := h.endpoints
h.endpoints = make(map[string]lang.PlaceholderType)
for _, sub := range endpoints.Subsets {
for _, point := range sub.Addresses {
h.endpoints[point.IP] = lang.Placeholder
for _, point := range endpoints.Endpoints {
for _, address := range point.Addresses {
h.endpoints[address] = lang.Placeholder
}
}

View File

@@ -4,7 +4,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@@ -14,21 +14,19 @@ func TestAdd(t *testing.T) {
endpoints = change
})
h.OnAdd("bad", false)
h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
{
IP: "0.0.0.3",
},
h.OnAdd(&discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
{
Addresses: []string{"0.0.0.3"},
},
},
}}, false)
}, false)
assert.ElementsMatch(t, []string{"0.0.0.1", "0.0.0.2", "0.0.0.3"}, endpoints)
}
@@ -37,34 +35,30 @@ func TestDelete(t *testing.T) {
h := NewEventHandler(func(change []string) {
endpoints = change
})
h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
{
IP: "0.0.0.3",
},
h.OnAdd(&discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
{
Addresses: []string{"0.0.0.3"},
},
},
}}, false)
}, false)
h.OnDelete("bad")
h.OnDelete(&v1.Endpoints{Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
h.OnDelete(&discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
},
}})
})
assert.ElementsMatch(t, []string{"0.0.0.3"}, endpoints)
}
@@ -73,36 +67,28 @@ func TestUpdate(t *testing.T) {
h := NewEventHandler(func(change []string) {
endpoints = change
})
h.OnUpdate(&v1.Endpoints{
Subsets: []v1.EndpointSubset{
h.OnUpdate(&discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
},
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
},
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
}, &v1.Endpoints{
Subsets: []v1.EndpointSubset{
}, &discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
{
IP: "0.0.0.3",
},
},
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
{
Addresses: []string{"0.0.0.3"},
},
},
ObjectMeta: metav1.ObjectMeta{
@@ -116,33 +102,25 @@ func TestUpdateNoChange(t *testing.T) {
h := NewEventHandler(func(change []string) {
assert.Fail(t, "should not called")
})
h.OnUpdate(&v1.Endpoints{
Subsets: []v1.EndpointSubset{
h.OnUpdate(&discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
},
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
},
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
}, &v1.Endpoints{
Subsets: []v1.EndpointSubset{
}, &discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
},
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
},
ObjectMeta: metav1.ObjectMeta{
@@ -156,45 +134,35 @@ func TestUpdateChangeWithDifferentVersion(t *testing.T) {
h := NewEventHandler(func(change []string) {
endpoints = change
})
h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.3",
},
h.OnAdd(&discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.3"},
},
},
}}, false)
h.OnUpdate(&v1.Endpoints{
Subsets: []v1.EndpointSubset{
}, false)
h.OnUpdate(&discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.3",
},
},
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.3"},
},
},
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
}, &v1.Endpoints{
Subsets: []v1.EndpointSubset{
}, &discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
},
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
},
ObjectMeta: metav1.ObjectMeta{
@@ -209,63 +177,49 @@ func TestUpdateNoChangeWithDifferentVersion(t *testing.T) {
h := NewEventHandler(func(change []string) {
endpoints = change
})
h.OnAdd(&v1.Endpoints{Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
},
},
}}, false)
h.OnUpdate("bad", &v1.Endpoints{Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
},
},
}})
h.OnUpdate(&v1.Endpoints{Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
},
},
}}, "bad")
h.OnUpdate(&v1.Endpoints{
Subsets: []v1.EndpointSubset{
h.OnAdd(&discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
},
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
},
}, false)
h.OnUpdate("bad", &discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []string{"0.0.0.1"},
},
},
})
h.OnUpdate(&discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []string{"0.0.0.1"},
},
},
}, "bad")
h.OnUpdate(&discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
},
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
},
}, &v1.Endpoints{
Subsets: []v1.EndpointSubset{
}, &discoveryv1.EndpointSlice{
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []v1.EndpointAddress{
{
IP: "0.0.0.1",
},
{
IP: "0.0.0.2",
},
},
Addresses: []string{"0.0.0.1"},
},
{
Addresses: []string{"0.0.0.2"},
},
},
ObjectMeta: metav1.ObjectMeta{