mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 03:11:40 +00:00
Merge pull request #82669 from rphillips/fixes/76518
fix kubelet status http calls with truncation
This commit is contained in:
commit
40695b0f48
@ -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 {
|
||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
@ -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))
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user