http.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package http
  14. import (
  15. "crypto/tls"
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "net/url"
  20. "time"
  21. "k8s.io/kubernetes/pkg/probe"
  22. utilnet "k8s.io/kubernetes/pkg/util/net"
  23. "github.com/golang/glog"
  24. )
  25. func New() HTTPProber {
  26. tlsConfig := &tls.Config{InsecureSkipVerify: true}
  27. transport := utilnet.SetTransportDefaults(&http.Transport{TLSClientConfig: tlsConfig, DisableKeepAlives: true})
  28. return httpProber{transport}
  29. }
  30. type HTTPProber interface {
  31. Probe(url *url.URL, headers http.Header, timeout time.Duration) (probe.Result, string, error)
  32. }
  33. type httpProber struct {
  34. transport *http.Transport
  35. }
  36. // Probe returns a ProbeRunner capable of running an http check.
  37. func (pr httpProber) Probe(url *url.URL, headers http.Header, timeout time.Duration) (probe.Result, string, error) {
  38. return DoHTTPProbe(url, headers, &http.Client{Timeout: timeout, Transport: pr.transport})
  39. }
  40. type HTTPGetInterface interface {
  41. Do(req *http.Request) (*http.Response, error)
  42. }
  43. // DoHTTPProbe checks if a GET request to the url succeeds.
  44. // If the HTTP response code is successful (i.e. 400 > code >= 200), it returns Success.
  45. // If the HTTP response code is unsuccessful or HTTP communication fails, it returns Failure.
  46. // This is exported because some other packages may want to do direct HTTP probes.
  47. func DoHTTPProbe(url *url.URL, headers http.Header, client HTTPGetInterface) (probe.Result, string, error) {
  48. req, err := http.NewRequest("GET", url.String(), nil)
  49. if err != nil {
  50. // Convert errors into failures to catch timeouts.
  51. return probe.Failure, err.Error(), nil
  52. }
  53. req.Header = headers
  54. if headers.Get("Host") != "" {
  55. req.Host = headers.Get("Host")
  56. }
  57. res, err := client.Do(req)
  58. if err != nil {
  59. // Convert errors into failures to catch timeouts.
  60. return probe.Failure, err.Error(), nil
  61. }
  62. defer res.Body.Close()
  63. b, err := ioutil.ReadAll(res.Body)
  64. if err != nil {
  65. return probe.Failure, "", err
  66. }
  67. body := string(b)
  68. if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {
  69. glog.V(4).Infof("Probe succeeded for %s, Response: %v", url.String(), *res)
  70. return probe.Success, body, nil
  71. }
  72. glog.V(4).Infof("Probe failed for %s with request headers %v, response body: %v", url.String(), headers, body)
  73. return probe.Failure, fmt.Sprintf("HTTP probe failed with statuscode: %d", res.StatusCode), nil
  74. }