Skip to content

Commit eb9bca5

Browse files
committed
Fixed a bug that mistake use newObj as oldObj in endpoint slice update
1 parent c6011f2 commit eb9bca5

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

pkg/controller/endpointslice/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ go_test(
6868
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
6969
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
7070
"//staging/src/k8s.io/apimachinery/pkg/util/rand:go_default_library",
71+
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
7172
"//staging/src/k8s.io/client-go/informers:go_default_library",
7273
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
7374
"//staging/src/k8s.io/client-go/listers/core/v1:go_default_library",

pkg/controller/endpointslice/endpointslice_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ func (c *Controller) onEndpointSliceAdd(obj interface{}) {
404404
// endpointSliceTracker or the managed-by value of the EndpointSlice has changed
405405
// from or to this controller.
406406
func (c *Controller) onEndpointSliceUpdate(prevObj, obj interface{}) {
407-
prevEndpointSlice := obj.(*discovery.EndpointSlice)
407+
prevEndpointSlice := prevObj.(*discovery.EndpointSlice)
408408
endpointSlice := obj.(*discovery.EndpointSlice)
409409
if endpointSlice == nil || prevEndpointSlice == nil {
410410
utilruntime.HandleError(fmt.Errorf("Invalid EndpointSlice provided to onEndpointSliceUpdate()"))

pkg/controller/endpointslice/endpointslice_controller_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/runtime"
3232
"k8s.io/apimachinery/pkg/util/intstr"
33+
"k8s.io/apimachinery/pkg/util/wait"
3334
"k8s.io/client-go/informers"
3435
"k8s.io/client-go/kubernetes/fake"
3536
k8stesting "k8s.io/client-go/testing"
@@ -274,6 +275,39 @@ func TestSyncServiceEndpointSliceLabelSelection(t *testing.T) {
274275
cmc.Check(t)
275276
}
276277

278+
func TestOnEndpointSliceUpdate(t *testing.T) {
279+
_, esController := newController([]string{"node-1"}, time.Duration(0))
280+
ns := metav1.NamespaceDefault
281+
serviceName := "testing-1"
282+
epSlice1 := &discovery.EndpointSlice{
283+
ObjectMeta: metav1.ObjectMeta{
284+
Name: "matching-1",
285+
Namespace: ns,
286+
Labels: map[string]string{
287+
discovery.LabelServiceName: serviceName,
288+
discovery.LabelManagedBy: controllerName,
289+
},
290+
},
291+
AddressType: discovery.AddressTypeIPv4,
292+
}
293+
294+
epSlice2 := epSlice1.DeepCopy()
295+
epSlice2.Labels[discovery.LabelManagedBy] = "something else"
296+
297+
assert.Equal(t, 0, esController.queue.Len())
298+
esController.onEndpointSliceUpdate(epSlice1, epSlice2)
299+
err := wait.PollImmediate(100*time.Millisecond, 3*time.Second, func() (bool, error) {
300+
if esController.queue.Len() > 0 {
301+
return true, nil
302+
}
303+
return false, nil
304+
})
305+
if err != nil {
306+
t.Fatalf("unexpected error waiting for add to queue")
307+
}
308+
assert.Equal(t, 1, esController.queue.Len())
309+
}
310+
277311
// Ensure SyncService handles a variety of protocols and IPs appropriately.
278312
func TestSyncServiceFull(t *testing.T) {
279313
client, esController := newController([]string{"node-1"}, time.Duration(0))

0 commit comments

Comments
 (0)