DoHTTPProbe checks if a GET request to the url succeeds. If the HTTP response code is successful (i.e. 400 > code >= 200), it returns Success. If the HTTP response code is unsuccessful or HTTP communication fails, it returns Failure. This is exported because some other packages may want to do direct
(req *http.Request, client GetHTTPInterface)
| 91 | // If the HTTP response code is unsuccessful or HTTP communication fails, it returns Failure. |
| 92 | // This is exported because some other packages may want to do direct HTTP probes. |
| 93 | func DoHTTPProbe(req *http.Request, client GetHTTPInterface) (probe.Result, string, error) { |
| 94 | url := req.URL |
| 95 | headers := req.Header |
| 96 | res, err := client.Do(req) |
| 97 | if err != nil { |
| 98 | // Convert errors into failures to catch timeouts. |
| 99 | return probe.Failure, err.Error(), nil |
| 100 | } |
| 101 | defer res.Body.Close() |
| 102 | b, err := utilio.ReadAtMost(res.Body, maxRespBodyLength) |
| 103 | if err != nil { |
| 104 | if err == utilio.ErrLimitReached { |
| 105 | klog.V(4).Infof("Non fatal body truncation for %s, Response: %v", url.String(), *res) |
| 106 | } else { |
| 107 | return probe.Failure, "", err |
| 108 | } |
| 109 | } |
| 110 | body := string(b) |
| 111 | if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest { |
| 112 | if res.StatusCode >= http.StatusMultipleChoices { // Redirect |
| 113 | klog.V(4).Infof("Probe terminated redirects for %s, Response: %v", url.String(), *res) |
| 114 | return probe.Warning, fmt.Sprintf("Probe terminated redirects, Response body: %v", body), nil |
| 115 | } |
| 116 | klog.V(4).Infof("Probe succeeded for %s, Response: %v", url.String(), *res) |
| 117 | return probe.Success, body, nil |
| 118 | } |
| 119 | klog.V(4).Infof("Probe failed for %s with request headers %v, response body: %v", url.String(), headers, body) |
| 120 | // Note: Until https://issue.k8s.io/99425 is addressed, this user-facing failure message must not contain the response body. |
| 121 | failureMsg := fmt.Sprintf("HTTP probe failed with statuscode: %d", res.StatusCode) |
| 122 | return probe.Failure, failureMsg, nil |
| 123 | } |
| 124 | |
| 125 | // RedirectChecker returns a function that can be used to check HTTP redirects. |
| 126 | func RedirectChecker(followNonLocalRedirects bool) func(*http.Request, []*http.Request) error { |