Skip to content

Commit 44d1976

Browse files
committed
Remove HeadlessService label in endpoints controller before comparing
1 parent 3352c44 commit 44d1976

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pkg/controller/endpoint/endpoints_controller.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,18 @@ func (e *EndpointController) syncService(key string) error {
472472

473473
createEndpoints := len(currentEndpoints.ResourceVersion) == 0
474474

475+
// Compare the sorted subsets and labels
476+
// Remove the HeadlessService label from the endpoints if it exists,
477+
// as this won't be set on the service itself
478+
// and will cause a false negative in this diff check.
479+
// But first check if it has that label to avoid expensive copies.
480+
compareLabels := currentEndpoints.Labels
481+
if _, ok := currentEndpoints.Labels[v1.IsHeadlessService]; ok {
482+
compareLabels = utillabels.CloneAndRemoveLabel(currentEndpoints.Labels, v1.IsHeadlessService)
483+
}
475484
if !createEndpoints &&
476485
apiequality.Semantic.DeepEqual(currentEndpoints.Subsets, subsets) &&
477-
apiequality.Semantic.DeepEqual(currentEndpoints.Labels, service.Labels) {
486+
apiequality.Semantic.DeepEqual(compareLabels, service.Labels) {
478487
klog.V(5).Infof("endpoints are equal for %s/%s, skipping update", service.Namespace, service.Name)
479488
return nil
480489
}

pkg/controller/endpoint/endpoints_controller_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,33 @@ func TestSyncEndpointsProtocolTCP(t *testing.T) {
383383
endpointsHandler.ValidateRequest(t, "/api/v1/namespaces/"+ns+"/endpoints/foo", "PUT", &data)
384384
}
385385

386+
func TestSyncEndpointsHeadlessServiceLabel(t *testing.T) {
387+
ns := metav1.NamespaceDefault
388+
testServer, endpointsHandler := makeTestServer(t, ns)
389+
defer testServer.Close()
390+
endpoints := newController(testServer.URL, 0*time.Second)
391+
endpoints.endpointsStore.Add(&v1.Endpoints{
392+
ObjectMeta: metav1.ObjectMeta{
393+
Name: "foo",
394+
Namespace: ns,
395+
ResourceVersion: "1",
396+
Labels: map[string]string{
397+
v1.IsHeadlessService: "",
398+
},
399+
},
400+
Subsets: []v1.EndpointSubset{},
401+
})
402+
endpoints.serviceStore.Add(&v1.Service{
403+
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: ns},
404+
Spec: v1.ServiceSpec{
405+
Selector: map[string]string{"foo": "bar"},
406+
Ports: []v1.ServicePort{{Port: 80}},
407+
},
408+
})
409+
endpoints.syncService(ns + "/foo")
410+
endpointsHandler.ValidateRequestCount(t, 0)
411+
}
412+
386413
func TestSyncEndpointsProtocolUDP(t *testing.T) {
387414
ns := "other"
388415
testServer, endpointsHandler := makeTestServer(t, ns)

0 commit comments

Comments
 (0)