|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. |
| 3 | + * Licensed under the Universal Permissive License v 1.0 as shown at |
| 4 | + * http://oss.oracle.com/licenses/upl. |
| 5 | + */ |
| 6 | + |
| 7 | +package statefulset |
| 8 | + |
| 9 | +import ( |
| 10 | + "crypto/tls" |
| 11 | + "fmt" |
| 12 | + "github.com/oracle/coherence-operator/pkg/operator" |
| 13 | + "io/ioutil" |
| 14 | + "k8s.io/apimachinery/pkg/util/net" |
| 15 | + "net/http" |
| 16 | + "net/url" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +// NewHTTPProbe creates Probe that will skip TLS verification while probing. |
| 21 | +func NewHTTPProbe() HTTPProbe { |
| 22 | + tlsConfig := &tls.Config{InsecureSkipVerify: true} |
| 23 | + return NewHTTPProbeWithTLSConfig(tlsConfig) |
| 24 | +} |
| 25 | + |
| 26 | +// NewHTTPProbeWithTLSConfig takes tls config as parameter. |
| 27 | +func NewHTTPProbeWithTLSConfig(config *tls.Config) HTTPProbe { |
| 28 | + transport := net.SetTransportDefaults(&http.Transport{TLSClientConfig: config, DisableKeepAlives: true}) |
| 29 | + return httpProbe{transport} |
| 30 | +} |
| 31 | + |
| 32 | +// HTTPProbe is an interface that defines the Probe function for doing HTTP readiness/liveness checks. |
| 33 | +type HTTPProbe interface { |
| 34 | + Probe(url *url.URL, headers http.Header, timeout time.Duration) (Result, string, error) |
| 35 | +} |
| 36 | + |
| 37 | +type httpProbe struct { |
| 38 | + transport *http.Transport |
| 39 | +} |
| 40 | + |
| 41 | +// Probe returns a ProbeRunner capable of running an HTTP check. |
| 42 | +func (pr httpProbe) Probe(url *url.URL, headers http.Header, timeout time.Duration) (Result, string, error) { |
| 43 | + return DoHTTPProbe(url, headers, &http.Client{Timeout: timeout, Transport: pr.transport}) |
| 44 | +} |
| 45 | + |
| 46 | +// GetHTTPInterface is an interface for making HTTP requests, that returns a response and error. |
| 47 | +type GetHTTPInterface interface { |
| 48 | + Do(req *http.Request) (*http.Response, error) |
| 49 | +} |
| 50 | + |
| 51 | +// DoHTTPProbe checks if a GET request to the url succeeds. |
| 52 | +// If the HTTP response code is successful (i.e. 400 > code >= 200), it returns Success. |
| 53 | +// If the HTTP response code is unsuccessful or HTTP communication fails, it returns Failure. |
| 54 | +// This is exported because some other packages may want to do direct HTTP probes. |
| 55 | +func DoHTTPProbe(url *url.URL, headers http.Header, client GetHTTPInterface) (Result, string, error) { |
| 56 | + req, err := http.NewRequest("GET", url.String(), nil) |
| 57 | + if err != nil { |
| 58 | + // Convert errors into failures to catch timeouts. |
| 59 | + return Failure, err.Error(), nil |
| 60 | + } |
| 61 | + if _, ok := headers["User-Agent"]; !ok { |
| 62 | + if headers == nil { |
| 63 | + headers = http.Header{} |
| 64 | + } |
| 65 | + // explicitly set User-Agent, so it's not set to default Go value |
| 66 | + headers.Set("User-Agent", fmt.Sprintf("coherence-operator/%s", operator.GetVersion())) |
| 67 | + } |
| 68 | + req.Header = headers |
| 69 | + if headers.Get("Host") != "" { |
| 70 | + req.Host = headers.Get("Host") |
| 71 | + } |
| 72 | + res, err := client.Do(req) |
| 73 | + if err != nil { |
| 74 | + // Convert errors into failures to catch timeouts. |
| 75 | + return Failure, err.Error(), nil |
| 76 | + } |
| 77 | + defer closeBody(res) |
| 78 | + b, err := ioutil.ReadAll(res.Body) |
| 79 | + if err != nil { |
| 80 | + return Failure, "", err |
| 81 | + } |
| 82 | + body := string(b) |
| 83 | + if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest { |
| 84 | + return Success, body, nil |
| 85 | + } |
| 86 | + return Failure, fmt.Sprintf("HTTP probe failed with statuscode: %d", res.StatusCode), nil |
| 87 | +} |
| 88 | + |
| 89 | +func closeBody(res *http.Response) { |
| 90 | + // close the response body, ignoring any errors |
| 91 | + _ = res.Body.Close() |
| 92 | +} |
0 commit comments