Skip to content

Commit 1065add

Browse files
committed
fix kubelet status http calls with truncation
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.
1 parent 4dd1e3f commit 1065add

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

pkg/probe/http/http.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ func DoHTTPProbe(url *url.URL, headers http.Header, client GetHTTPInterface) (pr
113113
defer res.Body.Close()
114114
b, err := utilio.ReadAtMost(res.Body, maxRespBodyLength)
115115
if err != nil {
116-
return probe.Failure, "", err
116+
if err == utilio.ErrLimitReached {
117+
klog.V(4).Infof("Non fatal body truncation for %s, Response: %v", url.String(), *res)
118+
} else {
119+
return probe.Failure, "", err
120+
}
117121
}
118122
body := string(b)
119123
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {

pkg/probe/http/http_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package http
1818

1919
import (
20+
"bytes"
2021
"fmt"
2122
"net"
2223
"net/http"
@@ -368,3 +369,70 @@ func TestHTTPProbeChecker_HostHeaderPreservedAfterRedirect(t *testing.T) {
368369
})
369370
}
370371
}
372+
373+
func TestHTTPProbeChecker_PayloadTruncated(t *testing.T) {
374+
successHostHeader := "www.success.com"
375+
oversizePayload := bytes.Repeat([]byte("a"), maxRespBodyLength+1)
376+
truncatedPayload := bytes.Repeat([]byte("a"), maxRespBodyLength)
377+
378+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
379+
switch r.URL.Path {
380+
case "/success":
381+
if r.Host == successHostHeader {
382+
w.WriteHeader(http.StatusOK)
383+
w.Write(oversizePayload)
384+
} else {
385+
http.Error(w, "", http.StatusBadRequest)
386+
}
387+
default:
388+
http.Error(w, "", http.StatusInternalServerError)
389+
}
390+
})
391+
server := httptest.NewServer(handler)
392+
defer server.Close()
393+
394+
headers := http.Header{}
395+
headers.Add("Host", successHostHeader)
396+
t.Run("truncated payload", func(t *testing.T) {
397+
prober := New(false)
398+
target, err := url.Parse(server.URL + "/success")
399+
require.NoError(t, err)
400+
result, body, err := prober.Probe(target, headers, wait.ForeverTestTimeout)
401+
assert.NoError(t, err)
402+
assert.Equal(t, result, probe.Success)
403+
assert.Equal(t, body, string(truncatedPayload))
404+
})
405+
}
406+
407+
func TestHTTPProbeChecker_PayloadNormal(t *testing.T) {
408+
successHostHeader := "www.success.com"
409+
normalPayload := bytes.Repeat([]byte("a"), maxRespBodyLength-1)
410+
411+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
412+
switch r.URL.Path {
413+
case "/success":
414+
if r.Host == successHostHeader {
415+
w.WriteHeader(http.StatusOK)
416+
w.Write(normalPayload)
417+
} else {
418+
http.Error(w, "", http.StatusBadRequest)
419+
}
420+
default:
421+
http.Error(w, "", http.StatusInternalServerError)
422+
}
423+
})
424+
server := httptest.NewServer(handler)
425+
defer server.Close()
426+
427+
headers := http.Header{}
428+
headers.Add("Host", successHostHeader)
429+
t.Run("normal payload", func(t *testing.T) {
430+
prober := New(false)
431+
target, err := url.Parse(server.URL + "/success")
432+
require.NoError(t, err)
433+
result, body, err := prober.Probe(target, headers, wait.ForeverTestTimeout)
434+
assert.NoError(t, err)
435+
assert.Equal(t, result, probe.Success)
436+
assert.Equal(t, body, string(normalPayload))
437+
})
438+
}

0 commit comments

Comments
 (0)