func()

in pkg/server/server.go [170:217]


func (s *Server) Healthz() http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.Header().Set("Content-Type", "application/json")

		errs := []Error{}
		svcs := []Service{}

		svcs = append(svcs, Service{
			Name:    "default liveness check",
			Healthy: true,
		})

		s.tickStatusMux.RLock()
		lastTickStart := s.tickLastStart
		s.tickStatusMux.RUnlock()

		maxTickWait := time.Duration(1.5 * float64(s.Config.ReportInterval))
		tickWait := time.Since(lastTickStart)
		klog.V(2).Infof("Liveness check by kubelet, tickWait is %s\n", tickWait.String())
		if !lastTickStart.IsZero() && tickWait > maxTickWait {
			klog.Warningf("Failed default Liveness probe, [tickWait]:%d exceeds [maxTickWait]:%d", tickWait, maxTickWait)
			err := fmt.Sprint("Tick not finished on time")
			errs = append(errs, Error{
				Name:    "Default liveness probe",
				Message: err,
			})
		}

		response := Response{
			Service: svcs,
			Errors:  errs,
			Healthy: true,
		}

		if len(errs) > 0 {
			response.Healthy = false
			w.WriteHeader(http.StatusServiceUnavailable)
		} else {
			w.WriteHeader(http.StatusOK)
		}

		json, err := json.Marshal(response)
		if err != nil {
			klog.Warning(err.Error())
		}
		w.Write(json)
	})
}