github.com/cilium/cilium@v1.16.2/clustermesh-apiserver/kvstoremesh/health.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package kvstoremesh
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/cilium/hive/cell"
    10  	"github.com/sirupsen/logrus"
    11  
    12  	"github.com/cilium/cilium/clustermesh-apiserver/health"
    13  	"github.com/cilium/cilium/clustermesh-apiserver/syncstate"
    14  )
    15  
    16  var HealthAPIEndpointsCell = cell.Module(
    17  	"health-api-endpoints",
    18  	"ClusterMesh Health API Endpoints",
    19  
    20  	syncstate.Cell,
    21  	cell.Provide(healthEndpoints),
    22  )
    23  
    24  type healthParameters struct {
    25  	cell.In
    26  
    27  	SyncState syncstate.SyncState
    28  	Logger    logrus.FieldLogger
    29  }
    30  
    31  func healthEndpoints(params healthParameters) []health.EndpointFunc {
    32  	return []health.EndpointFunc{
    33  		{
    34  			Path: "/readyz",
    35  			HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
    36  				statusCode := http.StatusInternalServerError
    37  				reply := "NotReady"
    38  
    39  				if params.SyncState.Complete() {
    40  					statusCode = http.StatusOK
    41  					reply = "Ready"
    42  				}
    43  				w.WriteHeader(statusCode)
    44  				if _, err := w.Write([]byte(reply)); err != nil {
    45  					params.Logger.WithError(err).Error("Failed to respond to /readyz request")
    46  				}
    47  			},
    48  		},
    49  	}
    50  }