github.com/m-lab/locate@v0.17.6/cmd/heartbeat/health/checker.go (about)

     1  package health
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  )
     6  
     7  // Checker checks the health of a local experiment instance.
     8  type Checker struct {
     9  	pp  *PortProbe
    10  	k8s *KubernetesClient
    11  	ec  *EndpointClient
    12  }
    13  
    14  // NewChecker creates a new Checker.
    15  func NewChecker(pp *PortProbe, ec *EndpointClient) *Checker {
    16  	return &Checker{
    17  		pp: pp,
    18  		ec: ec,
    19  	}
    20  }
    21  
    22  // NewCheckerK8S creates a new Checker for Kubernetes deployments.
    23  func NewCheckerK8S(pp *PortProbe, k8s *KubernetesClient, ec *EndpointClient) *Checker {
    24  	return &Checker{
    25  		pp:  pp,
    26  		k8s: k8s,
    27  		ec:  ec,
    28  	}
    29  }
    30  
    31  // GetHealth combines a set of health checks into a single score.
    32  func (hc *Checker) GetHealth(ctx context.Context) float64 {
    33  	if !hc.pp.checkPorts() {
    34  		return 0
    35  	}
    36  
    37  	if hc.k8s != nil && !hc.k8s.isHealthy(ctx) {
    38  		return 0
    39  	}
    40  
    41  	// Some experiments might not support a /health endpoint, so
    42  	// the result is only taken into account if the request error
    43  	// is nil.
    44  	status, err := hc.ec.checkHealthEndpoint()
    45  	if err == nil && !status {
    46  		return 0
    47  	}
    48  	return 1
    49  }