Merge pull request #487 from yugui/fix/golint

Fixes Go lint errors and a style fix
This commit is contained in:
Tim Hockin 2014-07-17 22:44:31 -07:00
commit 2f593c92fa
5 changed files with 18 additions and 6 deletions

View File

@ -24,16 +24,22 @@ import (
type Status int type Status int
// Status takes only one of values of these constants.
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 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 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 +50,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,12 +25,13 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
) )
// HealthChecker defines an abstract interface for checking container health.
type HealthChecker interface { type HealthChecker interface {
HealthCheck(container api.Container) (Status, error) HealthCheck(container api.Container) (Status, error)
} }
// MakeHealthChecker creates a new HealthChecker. // NewHealthChecker creates a new HealthChecker which supports multiple types of liveness probes.
func MakeHealthChecker() HealthChecker { func NewHealthChecker() HealthChecker {
return &MuxHealthChecker{ return &MuxHealthChecker{
checkers: map[string]HealthChecker{ checkers: map[string]HealthChecker{
"http": &HTTPHealthChecker{ "http": &HTTPHealthChecker{
@ -45,6 +46,9 @@ 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.
// If there is no matching health checker it returns Unknown, nil.
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 +73,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 fake implementation of HTTPGetInterface.
type fakeHTTPClient struct { type fakeHTTPClient struct {
req string req string
res http.Response res http.Response

View File

@ -30,6 +30,7 @@ func handleHealthz(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok")) w.Write([]byte("ok"))
} }
// InstallHandler registers a handler for health checking on the path "/healthz" to mux.
func InstallHandler(mux *http.ServeMux) { func InstallHandler(mux *http.ServeMux) {
mux.HandleFunc("/healthz", handleHealthz) mux.HandleFunc("/healthz", handleHealthz)
} }

View File

@ -138,7 +138,7 @@ func (kl *Kubelet) RunKubelet(dockerEndpoint, configPath, manifestURL, etcdServe
} }
go util.Forever(func() { s.ListenAndServe() }, 0) go util.Forever(func() { s.ListenAndServe() }, 0)
} }
kl.HealthChecker = health.MakeHealthChecker() kl.HealthChecker = health.NewHealthChecker()
kl.syncLoop(updateChannel, kl) kl.syncLoop(updateChannel, kl)
} }