Fixes golint errors in pkg/health

This commit is contained in:
Yuki Sonoda (Yugui) 2014-07-16 21:55:24 +09:00
parent a28e900d46
commit f368e4be68
3 changed files with 14 additions and 3 deletions

View File

@ -22,18 +22,25 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
) )
// Status is a enum type which describes a health status of a container.
type Status int type Status int
// These are the valid values of type Status.
const ( const (
Healthy Status = iota Healthy Status = iota
Unhealthy Unhealthy
Unknown Unknown
) )
// HTTPGetInterface is an abstract interface for testability. It abstracts the interface of http.Client.Get.
type HTTPGetInterface interface { type HTTPGetInterface interface {
Get(url string) (*http.Response, error) Get(url string) (*http.Response, error)
} }
// Check checks if GET request to the url succeeds.
// If the HTTP response code is successful, it returns Healthy.
// If the HTTP response code is unsuccessful, it returns Unhealthy.
// And it return Unknown and err if the HTTP communication itself fails.
func Check(url string, client HTTPGetInterface) (Status, error) { func Check(url string, client HTTPGetInterface) (Status, error) {
res, err := client.Get(url) res, err := client.Get(url)
if res.Body != nil { if res.Body != nil {
@ -44,8 +51,7 @@ func Check(url string, client HTTPGetInterface) (Status, error) {
} }
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest { if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {
return Healthy, nil return Healthy, nil
} else {
glog.V(1).Infof("Health check failed for %s, Response: %v", url, *res)
return Unhealthy, nil
} }
glog.V(1).Infof("Health check failed for %s, Response: %v", url, *res)
return Unhealthy, nil
} }

View File

@ -25,6 +25,7 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
) )
// HealthChecker hides implementation details of how to check if the container is healthy.
type HealthChecker interface { type HealthChecker interface {
HealthCheck(container api.Container) (Status, error) HealthCheck(container api.Container) (Status, error)
} }
@ -45,6 +46,8 @@ type MuxHealthChecker struct {
checkers map[string]HealthChecker checkers map[string]HealthChecker
} }
// HealthCheck delegates the health-checking of the container to one of the bundled implementations.
// It chooses an implementation according to container.LivenessProbe.Type.
func (m *MuxHealthChecker) HealthCheck(container api.Container) (Status, error) { func (m *MuxHealthChecker) HealthCheck(container api.Container) (Status, error) {
checker, ok := m.checkers[container.LivenessProbe.Type] checker, ok := m.checkers[container.LivenessProbe.Type]
if !ok || checker == nil { if !ok || checker == nil {
@ -69,6 +72,7 @@ func (h *HTTPHealthChecker) findPort(container api.Container, portName string) i
return -1 return -1
} }
// HealthCheck checks if the container is healthy by trying sending HTTP Get requests to the container.
func (h *HTTPHealthChecker) HealthCheck(container api.Container) (Status, error) { func (h *HTTPHealthChecker) HealthCheck(container api.Container) (Status, error) {
params := container.LivenessProbe.HTTPGet params := container.LivenessProbe.HTTPGet
if params == nil { if params == nil {

View File

@ -23,6 +23,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
) )
// fakeHTTPClient is a face implementation of HTTPGetInterface.
type fakeHTTPClient struct { type fakeHTTPClient struct {
req string req string
res http.Response res http.Response