diff --git a/pkg/controller/endpointslice/BUILD b/pkg/controller/endpointslice/BUILD index 3ff4b012d1e..67833854a78 100644 --- a/pkg/controller/endpointslice/BUILD +++ b/pkg/controller/endpointslice/BUILD @@ -68,6 +68,7 @@ go_test( "//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/rand:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library", "//staging/src/k8s.io/client-go/informers:go_default_library", "//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library", "//staging/src/k8s.io/client-go/listers/core/v1:go_default_library", diff --git a/pkg/controller/endpointslice/endpointslice_controller.go b/pkg/controller/endpointslice/endpointslice_controller.go index fb17e28910e..7898a0e4f78 100644 --- a/pkg/controller/endpointslice/endpointslice_controller.go +++ b/pkg/controller/endpointslice/endpointslice_controller.go @@ -404,7 +404,7 @@ func (c *Controller) onEndpointSliceAdd(obj interface{}) { // endpointSliceTracker or the managed-by value of the EndpointSlice has changed // from or to this controller. func (c *Controller) onEndpointSliceUpdate(prevObj, obj interface{}) { - prevEndpointSlice := obj.(*discovery.EndpointSlice) + prevEndpointSlice := prevObj.(*discovery.EndpointSlice) endpointSlice := obj.(*discovery.EndpointSlice) if endpointSlice == nil || prevEndpointSlice == nil { utilruntime.HandleError(fmt.Errorf("Invalid EndpointSlice provided to onEndpointSliceUpdate()")) diff --git a/pkg/controller/endpointslice/endpointslice_controller_test.go b/pkg/controller/endpointslice/endpointslice_controller_test.go index 9fb023e1d40..189068c599b 100644 --- a/pkg/controller/endpointslice/endpointslice_controller_test.go +++ b/pkg/controller/endpointslice/endpointslice_controller_test.go @@ -30,6 +30,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/intstr" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes/fake" k8stesting "k8s.io/client-go/testing" @@ -274,6 +275,39 @@ func TestSyncServiceEndpointSliceLabelSelection(t *testing.T) { cmc.Check(t) } +func TestOnEndpointSliceUpdate(t *testing.T) { + _, esController := newController([]string{"node-1"}, time.Duration(0)) + ns := metav1.NamespaceDefault + serviceName := "testing-1" + epSlice1 := &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "matching-1", + Namespace: ns, + Labels: map[string]string{ + discovery.LabelServiceName: serviceName, + discovery.LabelManagedBy: controllerName, + }, + }, + AddressType: discovery.AddressTypeIPv4, + } + + epSlice2 := epSlice1.DeepCopy() + epSlice2.Labels[discovery.LabelManagedBy] = "something else" + + assert.Equal(t, 0, esController.queue.Len()) + esController.onEndpointSliceUpdate(epSlice1, epSlice2) + err := wait.PollImmediate(100*time.Millisecond, 3*time.Second, func() (bool, error) { + if esController.queue.Len() > 0 { + return true, nil + } + return false, nil + }) + if err != nil { + t.Fatalf("unexpected error waiting for add to queue") + } + assert.Equal(t, 1, esController.queue.Len()) +} + // Ensure SyncService handles a variety of protocols and IPs appropriately. func TestSyncServiceFull(t *testing.T) { client, esController := newController([]string{"node-1"}, time.Duration(0))