github.com/cilium/cilium@v1.16.2/pkg/health/probe/probe.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package probe
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"net/url"
    10  	"time"
    11  )
    12  
    13  // http Client for probing
    14  // Use our custom client since DefaultClient specifies timeout of 0 (no timeout).
    15  // See https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779.
    16  // Use a timeout of 30s.
    17  var client = &http.Client{Timeout: 30 * time.Second}
    18  
    19  // GetHello performs a GET request on the /hello endpoint
    20  func GetHello(host string) error {
    21  	hostURL, err := url.Parse(host)
    22  	if err != nil {
    23  		return err
    24  	}
    25  
    26  	requestURL, err := hostURL.Parse("/hello")
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	resp, err := client.Get(requestURL.String())
    32  	if err != nil {
    33  		return err
    34  	}
    35  	defer resp.Body.Close()
    36  
    37  	if resp.StatusCode != http.StatusOK {
    38  		return fmt.Errorf("unexpected status code: %d %s", resp.StatusCode, resp.Status)
    39  	}
    40  
    41  	return nil
    42  }