Skip to content

Commit

Permalink
Split health_check.go into smaller parts.
Browse files Browse the repository at this point in the history
Distinct files for core, http, and tcp.
  • Loading branch information
thockin committed Aug 11, 2014
1 parent 24c516e commit 7beac7a
Show file tree
Hide file tree
Showing 7 changed files with 534 additions and 446 deletions.
55 changes: 53 additions & 2 deletions pkg/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,64 @@ limitations under the License.

package health

import ()
import (
"net/http"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog"
)

// Status represents the result of a single health-check operation.
type Status int

// Status takes only one of values of these constants.
// Status values must be one of these constants.
const (
Healthy Status = iota
Unhealthy
Unknown
)

// HealthChecker defines an abstract interface for checking container health.
type HealthChecker interface {
HealthCheck(currentState api.PodState, container api.Container) (Status, error)
}

// NewHealthChecker creates a new HealthChecker which supports multiple types of liveness probes.
func NewHealthChecker() HealthChecker {
return &MuxHealthChecker{
checkers: map[string]HealthChecker{
"http": &HTTPHealthChecker{
client: &http.Client{},
},
"tcp": &TCPHealthChecker{},
},
}
}

// MuxHealthChecker bundles multiple implementations of HealthChecker of different types.
type MuxHealthChecker struct {
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(currentState api.PodState, container api.Container) (Status, error) {
checker, ok := m.checkers[container.LivenessProbe.Type]
if !ok || checker == nil {
glog.Warningf("Failed to find health checker for %s %s", container.Name, container.LivenessProbe.Type)
return Unknown, nil
}
return checker.HealthCheck(currentState, container)
}

// A helper function to look up a port in a container by name.
// Returns the HostPort if found, -1 if not found.
func findPortByName(container api.Container, portName string) int {
for _, port := range container.Ports {
if port.Name == portName {
return port.HostPort
}
}
return -1
}
Loading

0 comments on commit 7beac7a

Please sign in to comment.