mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 13:02:14 +00:00
Move HTTP code out of health.go
Put it with the related HTTP code.
This commit is contained in:
parent
c67c1edfb4
commit
24c516ec1d
@ -16,11 +16,7 @@ limitations under the License.
|
|||||||
|
|
||||||
package health
|
package health
|
||||||
|
|
||||||
import (
|
import ()
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/golang/glog"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Status int
|
type Status int
|
||||||
|
|
||||||
@ -30,25 +26,3 @@ const (
|
|||||||
Unhealthy
|
Unhealthy
|
||||||
Unknown
|
Unknown
|
||||||
)
|
)
|
||||||
|
|
||||||
// HTTPGetInterface is an abstract interface for testability. It abstracts the interface of http.Client.Get.
|
|
||||||
type HTTPGetInterface interface {
|
|
||||||
Get(url string) (*http.Response, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DoHTTPCheck checks if a GET request to the url succeeds.
|
|
||||||
// If the HTTP response code is successful (i.e. 400 > code >= 200), it returns Healthy.
|
|
||||||
// If the HTTP response code is unsuccessful, it returns Unhealthy.
|
|
||||||
// It returns Unknown and err if the HTTP communication itself fails.
|
|
||||||
func DoHTTPCheck(url string, client HTTPGetInterface) (Status, error) {
|
|
||||||
res, err := client.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
return Unknown, err
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {
|
|
||||||
return Healthy, nil
|
|
||||||
}
|
|
||||||
glog.V(1).Infof("Health check failed for %s, Response: %v", url, *res)
|
|
||||||
return Unhealthy, nil
|
|
||||||
}
|
|
||||||
|
@ -61,6 +61,12 @@ func (m *MuxHealthChecker) HealthCheck(currentState api.PodState, container api.
|
|||||||
return checker.HealthCheck(currentState, container)
|
return checker.HealthCheck(currentState, container)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTPGetInterface is an abstract interface for testability. It abstracts the interface of http.Client.Get.
|
||||||
|
type HTTPGetInterface interface {
|
||||||
|
Get(url string) (*http.Response, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DoHTTPCheck checks if a GET request to the url succeeds.
|
||||||
// HTTPHealthChecker is an implementation of HealthChecker which checks container health by sending HTTP Get requests.
|
// HTTPHealthChecker is an implementation of HealthChecker which checks container health by sending HTTP Get requests.
|
||||||
type HTTPHealthChecker struct {
|
type HTTPHealthChecker struct {
|
||||||
client HTTPGetInterface
|
client HTTPGetInterface
|
||||||
@ -115,6 +121,22 @@ func formatURL(host string, port int, path string) string {
|
|||||||
return fmt.Sprintf("http://%s:%d%s", host, port, path)
|
return fmt.Sprintf("http://%s:%d%s", host, port, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the HTTP response code is successful (i.e. 400 > code >= 200), it returns Healthy.
|
||||||
|
// If the HTTP response code is unsuccessful, it returns Unhealthy.
|
||||||
|
// It returns Unknown and err if the HTTP communication itself fails.
|
||||||
|
func DoHTTPCheck(url string, client HTTPGetInterface) (Status, error) {
|
||||||
|
res, err := client.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return Unknown, err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {
|
||||||
|
return Healthy, nil
|
||||||
|
}
|
||||||
|
glog.V(1).Infof("Health check failed for %s, Response: %v", url, *res)
|
||||||
|
return Unhealthy, nil
|
||||||
|
}
|
||||||
|
|
||||||
// HealthCheck checks if the container is healthy by trying sending HTTP Get requests to the container.
|
// HealthCheck checks if the container is healthy by trying sending HTTP Get requests to the container.
|
||||||
func (h *HTTPHealthChecker) HealthCheck(currentState api.PodState, container api.Container) (Status, error) {
|
func (h *HTTPHealthChecker) HealthCheck(currentState api.PodState, container api.Container) (Status, error) {
|
||||||
host, port, path, err := getURLParts(currentState, container)
|
host, port, path, err := getURLParts(currentState, container)
|
||||||
|
Loading…
Reference in New Issue
Block a user