http.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package healthcheck
  14. import (
  15. "fmt"
  16. "net/http"
  17. "github.com/golang/glog"
  18. )
  19. // A healthCheckHandler serves http requests on /healthz on the service health check node port,
  20. // and responds to every request with either:
  21. // 200 OK and the count of endpoints for the given service that are local to this node.
  22. // or
  23. // 503 Service Unavailable If the count is zero or the service does not exist
  24. type healthCheckHandler struct {
  25. svcNsName string
  26. }
  27. // HTTP Utility function to send the required statusCode and error text to a http.ResponseWriter object
  28. func sendHealthCheckResponse(rw http.ResponseWriter, statusCode int, error string) {
  29. rw.Header().Set("Content-Type", "text/plain")
  30. rw.WriteHeader(statusCode)
  31. fmt.Fprint(rw, error)
  32. }
  33. // ServeHTTP: Interface callback method for net.Listener Handlers
  34. func (h healthCheckHandler) ServeHTTP(response http.ResponseWriter, req *http.Request) {
  35. glog.V(4).Infof("Received HC Request Service %s from Cloud Load Balancer", h.svcNsName)
  36. healthchecker.handleHealthCheckRequest(response, h.svcNsName)
  37. }