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

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