Kubelet probes would result in the webhook writing the HTTP status twice (#1355)

* Kubelet probes would result in the webhook writing the HTTP status twice

Doesn't seem like it affected anything - just writes out some extra
log messages

* nits

* nits

* nits

* nits
This commit is contained in:
Dave Protasowski 2020-05-26 13:34:50 -04:00 committed by GitHub
parent 874e3e0c13
commit b0e3201ad2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 0 deletions

View File

@ -253,6 +253,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
default:
w.WriteHeader(http.StatusOK)
}
return
}
// Verify the content type is accurate.

View File

@ -20,6 +20,8 @@ import (
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
@ -96,3 +98,51 @@ func TestRegistrationStopChanFire(t *testing.T) {
t.Errorf("Unexpected success to dial to port %d", opts.Port)
}
}
func TestWebhookKubeletProbe(t *testing.T) {
opts := newDefaultOptions()
ctx, webhook, cancel := newNonRunningTestWebhook(t, opts)
defer cancel()
recorder := bombRecorder{ResponseRecorder: httptest.NewRecorder()}
probeReq := httptest.NewRequest("GET", "/", nil)
probeReq.Header.Set("User-Agent", "kube-probe/1.16")
webhook.ServeHTTP(&recorder, probeReq)
if got, want := recorder.Code, http.StatusOK; got != want {
t.Fatalf("Probe got HTTP status %d - expected %d", got, want)
}
if got, want := recorder.writeCount, 1; got != want {
t.Errorf("HTTP status was written %d times - expected only one write", got)
}
// Stop the webhook - which means probes should fail
//
// The steps below aren't obvious and requires you to
// know the implementation details
cancel()
webhook.Run(ctx.Done())
recorder = bombRecorder{ResponseRecorder: httptest.NewRecorder()}
webhook.ServeHTTP(&recorder, probeReq)
if got, want := recorder.Code, http.StatusInternalServerError; got != want {
t.Fatalf("Probe got HTTP status %d - expected %d", got, want)
}
if got, want := recorder.writeCount, 1; got != want {
t.Errorf("HTTP status was written %d times - expected only one write", got)
}
}
type bombRecorder struct {
*httptest.ResponseRecorder
writeCount int
}
func (rw *bombRecorder) WriteHeader(code int) {
rw.writeCount += 1
rw.ResponseRecorder.WriteHeader(code)
}