Skip to content

Commit

Permalink
fix kubelet status http calls with truncation
Browse files Browse the repository at this point in the history
PR#76518 introduced a change to limit the the read on various calls with
utilio.ReadAtMost.  By design, ReadAtMost will return an error if there
is a truncation, but the calling code needs to know to handle it.
  • Loading branch information
rphillips committed Sep 13, 2019
1 parent 4dd1e3f commit 1065add
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/probe/http/http.go
Expand Up @@ -113,7 +113,11 @@ func DoHTTPProbe(url *url.URL, headers http.Header, client GetHTTPInterface) (pr
defer res.Body.Close()
b, err := utilio.ReadAtMost(res.Body, maxRespBodyLength)
if err != nil {
return probe.Failure, "", err
if err == utilio.ErrLimitReached {
klog.V(4).Infof("Non fatal body truncation for %s, Response: %v", url.String(), *res)
} else {
return probe.Failure, "", err
}
}
body := string(b)
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {
Expand Down
68 changes: 68 additions & 0 deletions pkg/probe/http/http_test.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package http

import (
"bytes"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -368,3 +369,70 @@ func TestHTTPProbeChecker_HostHeaderPreservedAfterRedirect(t *testing.T) {
})
}
}

func TestHTTPProbeChecker_PayloadTruncated(t *testing.T) {
successHostHeader := "www.success.com"
oversizePayload := bytes.Repeat([]byte("a"), maxRespBodyLength+1)
truncatedPayload := bytes.Repeat([]byte("a"), maxRespBodyLength)

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/success":
if r.Host == successHostHeader {
w.WriteHeader(http.StatusOK)
w.Write(oversizePayload)
} else {
http.Error(w, "", http.StatusBadRequest)
}
default:
http.Error(w, "", http.StatusInternalServerError)
}
})
server := httptest.NewServer(handler)
defer server.Close()

headers := http.Header{}
headers.Add("Host", successHostHeader)
t.Run("truncated payload", func(t *testing.T) {
prober := New(false)
target, err := url.Parse(server.URL + "/success")
require.NoError(t, err)
result, body, err := prober.Probe(target, headers, wait.ForeverTestTimeout)
assert.NoError(t, err)
assert.Equal(t, result, probe.Success)
assert.Equal(t, body, string(truncatedPayload))
})
}

func TestHTTPProbeChecker_PayloadNormal(t *testing.T) {
successHostHeader := "www.success.com"
normalPayload := bytes.Repeat([]byte("a"), maxRespBodyLength-1)

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/success":
if r.Host == successHostHeader {
w.WriteHeader(http.StatusOK)
w.Write(normalPayload)
} else {
http.Error(w, "", http.StatusBadRequest)
}
default:
http.Error(w, "", http.StatusInternalServerError)
}
})
server := httptest.NewServer(handler)
defer server.Close()

headers := http.Header{}
headers.Add("Host", successHostHeader)
t.Run("normal payload", func(t *testing.T) {
prober := New(false)
target, err := url.Parse(server.URL + "/success")
require.NoError(t, err)
result, body, err := prober.Probe(target, headers, wait.ForeverTestTimeout)
assert.NoError(t, err)
assert.Equal(t, result, probe.Success)
assert.Equal(t, body, string(normalPayload))
})
}

0 comments on commit 1065add

Please sign in to comment.