api: return endpoints target object references

Signed-off-by: Federico Simoncelli <fsimonce@redhat.com>
This commit is contained in:
Federico Simoncelli
2015-02-17 08:24:05 -05:00
parent aa4dbc0df8
commit daed0af3b5
9 changed files with 130 additions and 18 deletions

View File

@@ -93,7 +93,17 @@ func (e *EndpointController) SyncServiceEndpoints() error {
continue
}
endpoints = append(endpoints, api.Endpoint{IP: pod.Status.PodIP, Port: port})
endpoints = append(endpoints, api.Endpoint{
IP: pod.Status.PodIP,
Port: port,
TargetRef: &api.ObjectReference{
Kind: "Pod",
Namespace: pod.ObjectMeta.Namespace,
Name: pod.ObjectMeta.Name,
UID: pod.ObjectMeta.UID,
ResourceVersion: pod.ObjectMeta.ResourceVersion,
},
})
}
currentEndpoints, err := e.client.Endpoints(service.Namespace).Get(service.Name)
if err != nil {
@@ -118,7 +128,7 @@ func (e *EndpointController) SyncServiceEndpoints() error {
_, err = e.client.Endpoints(service.Namespace).Create(newEndpoints)
} else {
// Pre-existing
if currentEndpoints.Protocol == service.Spec.Protocol && endpointsEqual(currentEndpoints, endpoints) {
if currentEndpoints.Protocol == service.Spec.Protocol && endpointsListEqual(currentEndpoints, endpoints) {
glog.V(5).Infof("protocol and endpoints are equal for %s/%s, skipping update", service.Namespace, service.Name)
continue
}
@@ -132,19 +142,31 @@ func (e *EndpointController) SyncServiceEndpoints() error {
return resultErr
}
func endpointEqual(this, that *api.Endpoint) bool {
if this.IP != that.IP || this.Port != that.Port {
return false
}
if this.TargetRef == nil || that.TargetRef == nil {
return this.TargetRef == that.TargetRef
}
return *this.TargetRef == *that.TargetRef
}
func containsEndpoint(haystack *api.Endpoints, needle *api.Endpoint) bool {
if haystack == nil || needle == nil {
return false
}
for ix := range haystack.Endpoints {
if haystack.Endpoints[ix] == *needle {
if endpointEqual(&haystack.Endpoints[ix], needle) {
return true
}
}
return false
}
func endpointsEqual(eps *api.Endpoints, endpoints []api.Endpoint) bool {
func endpointsListEqual(eps *api.Endpoints, endpoints []api.Endpoint) bool {
if len(eps.Endpoints) != len(endpoints) {
return false
}

View File

@@ -408,8 +408,15 @@ func TestSyncEndpointsItemsEmptySelectorSelectsAll(t *testing.T) {
Name: "foo",
ResourceVersion: "1",
},
Protocol: api.ProtocolTCP,
Endpoints: []api.Endpoint{{IP: "1.2.3.4", Port: 8080}},
Protocol: api.ProtocolTCP,
Endpoints: []api.Endpoint{{
IP: "1.2.3.4",
Port: 8080,
TargetRef: &api.ObjectReference{
Kind: "Pod",
Name: "pod0",
},
}},
})
endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints/foo?namespace=other", "PUT", &data)
}
@@ -449,8 +456,15 @@ func TestSyncEndpointsItemsPreexisting(t *testing.T) {
Name: "foo",
ResourceVersion: "1",
},
Protocol: api.ProtocolTCP,
Endpoints: []api.Endpoint{{IP: "1.2.3.4", Port: 8080}},
Protocol: api.ProtocolTCP,
Endpoints: []api.Endpoint{{
IP: "1.2.3.4",
Port: 8080,
TargetRef: &api.ObjectReference{
Kind: "Pod",
Name: "pod0",
},
}},
})
endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints/foo?namespace=bar", "PUT", &data)
}
@@ -475,8 +489,15 @@ func TestSyncEndpointsItemsPreexistingIdentical(t *testing.T) {
ObjectMeta: api.ObjectMeta{
ResourceVersion: "1",
},
Protocol: api.ProtocolTCP,
Endpoints: []api.Endpoint{{IP: "1.2.3.4", Port: 8080}},
Protocol: api.ProtocolTCP,
Endpoints: []api.Endpoint{{
IP: "1.2.3.4",
Port: 8080,
TargetRef: &api.ObjectReference{
Kind: "Pod",
Name: "pod0",
},
}},
}})
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
@@ -514,8 +535,15 @@ func TestSyncEndpointsItems(t *testing.T) {
ObjectMeta: api.ObjectMeta{
ResourceVersion: "",
},
Protocol: api.ProtocolTCP,
Endpoints: []api.Endpoint{{IP: "1.2.3.4", Port: 8080}},
Protocol: api.ProtocolTCP,
Endpoints: []api.Endpoint{{
IP: "1.2.3.4",
Port: 8080,
TargetRef: &api.ObjectReference{
Kind: "Pod",
Name: "pod0",
},
}},
})
endpointsHandler.ValidateRequest(t, "/api/"+testapi.Version()+"/endpoints?namespace=other", "POST", &data)
}