Skip to content

Commit e1704f5

Browse files
committed
Revert "Made the router skip health checks when there is one endpoint"
This reverts commit 4833eb5. This logic is not correct. If a router is backed by two services, it will misbehave.
1 parent d3924ff commit e1704f5

File tree

4 files changed

+188
-158
lines changed

4 files changed

+188
-158
lines changed

pkg/router/template/plugin.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,9 @@ func createRouterEndpoints(endpoints *kapi.Endpoints, excludeUDP bool, lookupSvc
265265
wasIdled = true
266266
}
267267

268-
// Take a reasonable guess at the number of endpoints to avoid reallocating the out array when we append
269268
out := make([]Endpoint, 0, len(endpoints.Subsets)*4)
270269

271-
// Now build the actual endpoints we pass to the template
270+
// TODO: review me for sanity
272271
for _, s := range subsets {
273272
for _, p := range s.Ports {
274273
if excludeUDP && p.Protocol == kapi.ProtocolUDP {
@@ -307,11 +306,5 @@ func createRouterEndpoints(endpoints *kapi.Endpoints, excludeUDP bool, lookupSvc
307306
}
308307
}
309308

310-
// We want to disable endpoint checks if there is only one endpoint
311-
// We skip the case where it is idled, since we set NoHealthCheck above
312-
if wasIdled == false && len(out) == 1 {
313-
out[0].NoHealthCheck = true
314-
}
315-
316309
return out
317310
}

pkg/router/template/plugin_test.go

Lines changed: 9 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,15 @@ func TestHandleEndpoints(t *testing.T) {
275275
Name: "foo/test", //service name from kapi.endpoints object
276276
EndpointTable: []Endpoint{
277277
{
278-
ID: "ept:test:1.1.1.1:345",
279-
IP: "1.1.1.1",
280-
Port: "345",
281-
NoHealthCheck: true,
278+
ID: "ept:test:1.1.1.1:345",
279+
IP: "1.1.1.1",
280+
Port: "345",
282281
},
283282
},
284283
},
285284
},
286285
{
287-
name: "Endpoint mod (one ep, one address)",
286+
name: "Endpoint mod",
288287
eventType: watch.Modified,
289288
endpoints: &kapi.Endpoints{
290289
ObjectMeta: metav1.ObjectMeta{
@@ -300,143 +299,9 @@ func TestHandleEndpoints(t *testing.T) {
300299
Name: "foo/test",
301300
EndpointTable: []Endpoint{
302301
{
303-
ID: "pod:pod-1:test:2.2.2.2:8080",
304-
IP: "2.2.2.2",
305-
Port: "8080",
306-
NoHealthCheck: true,
307-
},
308-
},
309-
},
310-
},
311-
{
312-
name: "Endpoint mod (second ep, one address each)",
313-
eventType: watch.Modified,
314-
endpoints: &kapi.Endpoints{
315-
ObjectMeta: metav1.ObjectMeta{
316-
Namespace: "foo",
317-
Name: "test",
318-
},
319-
Subsets: []kapi.EndpointSubset{
320-
{
321-
Addresses: []kapi.EndpointAddress{{IP: "2.2.2.2", TargetRef: &kapi.ObjectReference{Kind: "Pod", Name: "pod-1"}}},
322-
Ports: []kapi.EndpointPort{{Port: 8080}},
323-
},
324-
{
325-
Addresses: []kapi.EndpointAddress{{IP: "3.3.3.3", TargetRef: &kapi.ObjectReference{Kind: "Pod", Name: "pod-2"}}},
326-
Ports: []kapi.EndpointPort{{Port: 8081}},
327-
},
328-
},
329-
},
330-
expectedServiceUnit: &ServiceUnit{
331-
Name: "foo/test",
332-
EndpointTable: []Endpoint{
333-
{
334-
ID: "pod:pod-1:test:2.2.2.2:8080",
335-
IP: "2.2.2.2",
336-
Port: "8080",
337-
NoHealthCheck: false,
338-
},
339-
{
340-
ID: "pod:pod-2:test:3.3.3.3:8081",
341-
IP: "3.3.3.3",
342-
Port: "8081",
343-
NoHealthCheck: false,
344-
},
345-
},
346-
},
347-
},
348-
{
349-
name: "Endpoint mod (one ep, two addresses)",
350-
eventType: watch.Modified,
351-
endpoints: &kapi.Endpoints{
352-
ObjectMeta: metav1.ObjectMeta{
353-
Namespace: "foo",
354-
Name: "test",
355-
},
356-
Subsets: []kapi.EndpointSubset{
357-
{
358-
Addresses: []kapi.EndpointAddress{
359-
{IP: "3.3.3.3", TargetRef: &kapi.ObjectReference{Kind: "Pod", Name: "pod-2"}},
360-
{IP: "4.4.4.4", TargetRef: &kapi.ObjectReference{Kind: "Pod", Name: "pod-3"}},
361-
},
362-
Ports: []kapi.EndpointPort{{Port: 8080}},
363-
},
364-
},
365-
},
366-
expectedServiceUnit: &ServiceUnit{
367-
Name: "foo/test",
368-
EndpointTable: []Endpoint{
369-
{
370-
ID: "pod:pod-2:test:3.3.3.3:8080",
371-
IP: "3.3.3.3",
372-
Port: "8080",
373-
NoHealthCheck: false,
374-
},
375-
{
376-
ID: "pod:pod-3:test:4.4.4.4:8080",
377-
IP: "4.4.4.4",
378-
Port: "8080",
379-
NoHealthCheck: false,
380-
},
381-
},
382-
},
383-
},
384-
{
385-
name: "Endpoint mod (one ep, two ports)",
386-
eventType: watch.Modified,
387-
endpoints: &kapi.Endpoints{
388-
ObjectMeta: metav1.ObjectMeta{
389-
Namespace: "foo",
390-
Name: "test",
391-
},
392-
Subsets: []kapi.EndpointSubset{
393-
{
394-
Addresses: []kapi.EndpointAddress{
395-
{IP: "3.3.3.3", TargetRef: &kapi.ObjectReference{Kind: "Pod", Name: "pod-2"}},
396-
},
397-
Ports: []kapi.EndpointPort{{Port: 8080}, {Port: 8081}},
398-
},
399-
},
400-
},
401-
expectedServiceUnit: &ServiceUnit{
402-
Name: "foo/test",
403-
EndpointTable: []Endpoint{
404-
{
405-
ID: "pod:pod-2:test:3.3.3.3:8080",
406-
IP: "3.3.3.3",
407-
Port: "8080",
408-
NoHealthCheck: false,
409-
},
410-
{
411-
ID: "pod:pod-2:test:3.3.3.3:8081",
412-
IP: "3.3.3.3",
413-
Port: "8081",
414-
NoHealthCheck: false,
415-
},
416-
},
417-
},
418-
},
419-
{
420-
name: "Endpoint mod (back to one ep)",
421-
eventType: watch.Modified,
422-
endpoints: &kapi.Endpoints{
423-
ObjectMeta: metav1.ObjectMeta{
424-
Namespace: "foo",
425-
Name: "test",
426-
},
427-
Subsets: []kapi.EndpointSubset{{
428-
Addresses: []kapi.EndpointAddress{{IP: "3.3.3.3", TargetRef: &kapi.ObjectReference{Kind: "Pod", Name: "pod-1"}}},
429-
Ports: []kapi.EndpointPort{{Port: 8080}},
430-
}},
431-
},
432-
expectedServiceUnit: &ServiceUnit{
433-
Name: "foo/test",
434-
EndpointTable: []Endpoint{
435-
{
436-
ID: "pod:pod-1:test:3.3.3.3:8080",
437-
IP: "3.3.3.3",
438-
Port: "8080",
439-
NoHealthCheck: true,
302+
ID: "pod:pod-1:test:2.2.2.2:8080",
303+
IP: "2.2.2.2",
304+
Port: "8080",
440305
},
441306
},
442307
},
@@ -450,8 +315,8 @@ func TestHandleEndpoints(t *testing.T) {
450315
Name: "test",
451316
},
452317
Subsets: []kapi.EndpointSubset{{
453-
Addresses: []kapi.EndpointAddress{{IP: "3.3.3.3", TargetRef: &kapi.ObjectReference{Kind: "Pod", Name: "pod-1"}}},
454-
Ports: []kapi.EndpointPort{{Port: 8080}},
318+
Addresses: []kapi.EndpointAddress{{IP: "3.3.3.3"}},
319+
Ports: []kapi.EndpointPort{{Port: 0}},
455320
}},
456321
},
457322
expectedServiceUnit: &ServiceUnit{

pkg/router/template/template_helper.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,22 @@ func processEndpointsForAlias(alias ServiceAliasConfig, svc ServiceUnit, action
157157
}
158158

159159
func endpointsForAlias(alias ServiceAliasConfig, svc ServiceUnit) []Endpoint {
160-
if len(alias.PreferPort) == 0 {
161-
return svc.EndpointTable
162-
}
163160
endpoints := make([]Endpoint, 0, len(svc.EndpointTable))
164-
for i := range svc.EndpointTable {
165-
endpoint := svc.EndpointTable[i]
166-
if endpoint.PortName == alias.PreferPort || endpoint.Port == alias.PreferPort {
161+
for _, endpoint := range svc.EndpointTable {
162+
// Filter the list:
163+
// - If PreferPort length is 0, match everything
164+
// - Otherwise, if the PortName or Port matches PreferPort, then we have a match
165+
if len(alias.PreferPort) == 0 || endpoint.PortName == alias.PreferPort || endpoint.Port == alias.PreferPort {
167166
endpoints = append(endpoints, endpoint)
168167
}
169168
}
169+
170+
// We want to disable endpoint checks if there is only one endpoint since there's no point in
171+
// testing and removing it from the set ahead of time since there are no other eps to replace it.
172+
if len(endpoints) == 1 {
173+
endpoints[0].NoHealthCheck = true
174+
}
175+
170176
return endpoints
171177
}
172178

0 commit comments

Comments
 (0)