Skip to content

Commit 4e6aa5f

Browse files
committed
kube-proxy service health: add new return header with number of local endpoints
- add new header "X-Load-Balancing-Endpoint-Weight" returned from service health. Value of the header is number of local endpoints. Header can be used in weighted load balancing. Parsing header for number of endpoints is faster than unmarshalling json from the content body. - add missing unit test for new and old headers returned from service health
1 parent 9516a25 commit 4e6aa5f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pkg/proxy/healthcheck/healthcheck_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ import (
2121
"net"
2222
"net/http"
2323
"net/http/httptest"
24+
"strconv"
2425
"testing"
2526
"time"
2627

28+
"github.com/google/go-cmp/cmp"
2729
"k8s.io/api/core/v1"
2830
"k8s.io/apimachinery/pkg/types"
2931
"k8s.io/apimachinery/pkg/util/dump"
3032
"k8s.io/apimachinery/pkg/util/sets"
33+
3134
proxyutil "k8s.io/kubernetes/pkg/proxy/util"
3235
testingclock "k8s.io/utils/clock/testing"
3336
)
@@ -412,6 +415,15 @@ func tHandler(hcs *server, nsn types.NamespacedName, status int, endpoints int,
412415
if payload.ServiceProxyHealthy != kubeProxyHealthy {
413416
t.Errorf("expected %v kubeProxyHealthy, got %v", kubeProxyHealthy, payload.ServiceProxyHealthy)
414417
}
418+
if !cmp.Equal(resp.Header()["Content-Type"], []string{"application/json"}) {
419+
t.Errorf("expected 'Content-Type: application/json' respose header, got: %v", resp.Header()["Content-Type"])
420+
}
421+
if !cmp.Equal(resp.Header()["X-Content-Type-Options"], []string{"nosniff"}) {
422+
t.Errorf("expected 'X-Content-Type-Options: nosniff' respose header, got: %v", resp.Header()["X-Content-Type-Options"])
423+
}
424+
if !cmp.Equal(resp.Header()["X-Load-Balancing-Endpoint-Weight"], []string{strconv.Itoa(endpoints)}) {
425+
t.Errorf("expected 'X-Load-Balancing-Endpoint-Weight: %d' respose header, got: %v", endpoints, resp.Header()["X-Load-Balancing-Endpoint-Weight"])
426+
}
415427
}
416428
}
417429

pkg/proxy/healthcheck/service_health.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"net"
2222
"net/http"
23+
"strconv"
2324
"strings"
2425
"sync"
2526

@@ -233,6 +234,8 @@ func (h hcHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
233234

234235
resp.Header().Set("Content-Type", "application/json")
235236
resp.Header().Set("X-Content-Type-Options", "nosniff")
237+
resp.Header().Set("X-Load-Balancing-Endpoint-Weight", strconv.Itoa(count))
238+
236239
if count != 0 && kubeProxyHealthy {
237240
resp.WriteHeader(http.StatusOK)
238241
} else {

0 commit comments

Comments
 (0)