diff --git a/pkg/proxy/endpoints.go b/pkg/proxy/endpoints.go index bdd165c00ab..0ef4a73e1b5 100644 --- a/pkg/proxy/endpoints.go +++ b/pkg/proxy/endpoints.go @@ -369,7 +369,7 @@ func (em EndpointsMap) Update(changes *EndpointChangeTracker) (result UpdateEndp // TODO: If this will appear to be computationally expensive, consider // computing this incrementally similarly to endpointsMap. result.HCEndpointsLocalIPSize = make(map[types.NamespacedName]int) - localIPs := em.getLocalEndpointIPs() + localIPs := em.getLocalReadyEndpointIPs() for nsn, ips := range localIPs { result.HCEndpointsLocalIPSize[nsn] = len(ips) } @@ -481,10 +481,16 @@ func (em EndpointsMap) unmerge(other EndpointsMap) { } // GetLocalEndpointIPs returns endpoints IPs if given endpoint is local - local means the endpoint is running in same host as kube-proxy. -func (em EndpointsMap) getLocalEndpointIPs() map[types.NamespacedName]sets.String { +func (em EndpointsMap) getLocalReadyEndpointIPs() map[types.NamespacedName]sets.String { localIPs := make(map[types.NamespacedName]sets.String) for svcPortName, epList := range em { for _, ep := range epList { + // Only add ready endpoints for health checking. Terminating endpoints may still serve traffic + // but the health check signal should fail if there are only terminating endpoints on a node. + if !ep.IsReady() { + continue + } + if ep.GetIsLocal() { nsn := svcPortName.NamespacedName if localIPs[nsn] == nil { diff --git a/pkg/proxy/endpoints_test.go b/pkg/proxy/endpoints_test.go index b109f79229f..b6cd025399d 100644 --- a/pkg/proxy/endpoints_test.go +++ b/pkg/proxy/endpoints_test.go @@ -109,11 +109,36 @@ func TestGetLocalEndpointIPs(t *testing.T) { {Namespace: "ns2", Name: "ep2"}: sets.NewString("2.2.2.2", "2.2.2.22", "2.2.2.3"), {Namespace: "ns4", Name: "ep4"}: sets.NewString("4.4.4.4", "4.4.4.6"), }, + }, { + // Case[5]: named local and non-local ports for different IPs, some not ready. + endpointsMap: EndpointsMap{ + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolTCP): []Endpoint{ + &BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + }, + makeServicePortName("ns2", "ep2", "p22", v1.ProtocolTCP): []Endpoint{ + &BaseEndpointInfo{Endpoint: "2.2.2.2:22", IsLocal: true, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "2.2.2.22:22", IsLocal: true, Ready: true, Serving: true, Terminating: false}, + }, + makeServicePortName("ns2", "ep2", "p23", v1.ProtocolTCP): []Endpoint{ + &BaseEndpointInfo{Endpoint: "2.2.2.3:23", IsLocal: true, Ready: false, Serving: true, Terminating: true}, + }, + makeServicePortName("ns4", "ep4", "p44", v1.ProtocolTCP): []Endpoint{ + &BaseEndpointInfo{Endpoint: "4.4.4.4:44", IsLocal: true, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "4.4.4.5:44", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + }, + makeServicePortName("ns4", "ep4", "p45", v1.ProtocolTCP): []Endpoint{ + &BaseEndpointInfo{Endpoint: "4.4.4.6:45", IsLocal: true, Ready: true, Serving: true, Terminating: false}, + }, + }, + expected: map[types.NamespacedName]sets.String{ + {Namespace: "ns2", Name: "ep2"}: sets.NewString("2.2.2.2", "2.2.2.22"), + {Namespace: "ns4", Name: "ep4"}: sets.NewString("4.4.4.4", "4.4.4.6"), + }, }} for tci, tc := range testCases { // outputs - localIPs := tc.endpointsMap.getLocalEndpointIPs() + localIPs := tc.endpointsMap.getLocalReadyEndpointIPs() if !reflect.DeepEqual(localIPs, tc.expected) { t.Errorf("[%d] expected %#v, got %#v", tci, tc.expected, localIPs)