From afa0100e271be3b049e2cfe3bfe68bbb764a6ef2 Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Mon, 6 Apr 2015 13:14:54 -0700 Subject: [PATCH] Copy labels from the service to endpoints that we create. --- pkg/service/endpoints_controller.go | 6 +- pkg/service/endpoints_controller_test.go | 108 +++++++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/pkg/service/endpoints_controller.go b/pkg/service/endpoints_controller.go index cea75dd469..12f1255798 100644 --- a/pkg/service/endpoints_controller.go +++ b/pkg/service/endpoints_controller.go @@ -125,7 +125,8 @@ func (e *EndpointController) SyncServiceEndpoints() error { if errors.IsNotFound(err) { currentEndpoints = &api.Endpoints{ ObjectMeta: api.ObjectMeta{ - Name: service.Name, + Name: service.Name, + Labels: service.Labels, }, } } else { @@ -133,12 +134,13 @@ func (e *EndpointController) SyncServiceEndpoints() error { continue } } - if reflect.DeepEqual(currentEndpoints.Subsets, subsets) { + if reflect.DeepEqual(currentEndpoints.Subsets, subsets) && reflect.DeepEqual(currentEndpoints.Labels, service.Labels) { glog.V(5).Infof("endpoints are equal for %s/%s, skipping update", service.Namespace, service.Name) continue } newEndpoints := currentEndpoints newEndpoints.Subsets = subsets + newEndpoints.Labels = service.Labels if len(currentEndpoints.ResourceVersion) == 0 { // No previous endpoints, create them diff --git a/pkg/service/endpoints_controller_test.go b/pkg/service/endpoints_controller_test.go index 59de0b591b..0794b1b56e 100644 --- a/pkg/service/endpoints_controller_test.go +++ b/pkg/service/endpoints_controller_test.go @@ -560,3 +560,111 @@ func TestSyncEndpointsPodError(t *testing.T) { t.Error("Unexpected non-error") } } + +func TestSyncEndpointsItemsWithLabels(t *testing.T) { + serviceList := api.ServiceList{ + Items: []api.Service{ + { + ObjectMeta: api.ObjectMeta{ + Name: "foo", + Namespace: "other", + Labels: map[string]string{ + "foo": "bar", + }, + }, + Spec: api.ServiceSpec{ + Selector: map[string]string{"foo": "bar"}, + Ports: []api.ServicePort{ + {Name: "port0", Port: 80, Protocol: "TCP", TargetPort: util.NewIntOrStringFromInt(8080)}, + {Name: "port1", Port: 88, Protocol: "TCP", TargetPort: util.NewIntOrStringFromInt(8088)}, + }, + }, + }, + }, + } + testServer, endpointsHandler := makeTestServer(t, "other", + serverResponse{http.StatusOK, newPodList(3, 2)}, + serverResponse{http.StatusOK, &serviceList}, + serverResponse{http.StatusOK, &api.Endpoints{}}) + defer testServer.Close() + client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()}) + endpoints := NewEndpointController(client) + if err := endpoints.SyncServiceEndpoints(); err != nil { + t.Errorf("unexpected error: %v", err) + } + expectedSubsets := []api.EndpointSubset{{ + Addresses: []api.EndpointAddress{ + {IP: "1.2.3.4", TargetRef: &api.ObjectReference{Kind: "Pod", Name: "pod0"}}, + {IP: "1.2.3.5", TargetRef: &api.ObjectReference{Kind: "Pod", Name: "pod1"}}, + {IP: "1.2.3.6", TargetRef: &api.ObjectReference{Kind: "Pod", Name: "pod2"}}, + }, + Ports: []api.EndpointPort{ + {Name: "port0", Port: 8080, Protocol: "TCP"}, + {Name: "port1", Port: 8088, Protocol: "TCP"}, + }, + }} + data := runtime.EncodeOrDie(testapi.Codec(), &api.Endpoints{ + ObjectMeta: api.ObjectMeta{ + ResourceVersion: "", + Labels: serviceList.Items[0].Labels, + }, + Subsets: endptspkg.SortSubsets(expectedSubsets), + }) + // endpointsHandler should get 2 requests - one for "GET" and the next for "POST". + endpointsHandler.ValidateRequestCount(t, 2) + endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithQueryParams("endpoints", "other", ""), "POST", &data) +} + +func TestSyncEndpointsItemsPreexistingLabelsChange(t *testing.T) { + serviceList := api.ServiceList{ + Items: []api.Service{ + { + ObjectMeta: api.ObjectMeta{ + Name: "foo", + Namespace: "bar", + Labels: map[string]string{ + "baz": "blah", + }, + }, + Spec: api.ServiceSpec{ + Selector: map[string]string{"foo": "bar"}, + Ports: []api.ServicePort{{Port: 80, Protocol: "TCP", TargetPort: util.NewIntOrStringFromInt(8080)}}, + }, + }, + }, + } + testServer, endpointsHandler := makeTestServer(t, "bar", + serverResponse{http.StatusOK, newPodList(1, 1)}, + serverResponse{http.StatusOK, &serviceList}, + serverResponse{http.StatusOK, &api.Endpoints{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + ResourceVersion: "1", + Labels: map[string]string{ + "foo": "bar", + }, + }, + Subsets: []api.EndpointSubset{{ + Addresses: []api.EndpointAddress{{IP: "6.7.8.9"}}, + Ports: []api.EndpointPort{{Port: 1000}}, + }}, + }}) + defer testServer.Close() + client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()}) + endpoints := NewEndpointController(client) + if err := endpoints.SyncServiceEndpoints(); err != nil { + t.Errorf("unexpected error: %v", err) + } + data := runtime.EncodeOrDie(testapi.Codec(), &api.Endpoints{ + ObjectMeta: api.ObjectMeta{ + Name: "foo", + ResourceVersion: "1", + Labels: serviceList.Items[0].Labels, + }, + Subsets: []api.EndpointSubset{{ + Addresses: []api.EndpointAddress{{IP: "1.2.3.4", TargetRef: &api.ObjectReference{Kind: "Pod", Name: "pod0"}}}, + Ports: []api.EndpointPort{{Port: 8080, Protocol: "TCP"}}, + }}, + }) + endpointsHandler.ValidateRequest(t, testapi.ResourcePathWithQueryParams("endpoints", "bar", "foo"), "PUT", &data) +}