This commit is contained in:
Murat Kabilov 2017-07-26 17:16:03 +02:00
parent 4d4fc394df
commit 9f9e841bf3
2 changed files with 14 additions and 17 deletions

View File

@ -28,27 +28,11 @@ var (
teamURL = regexp.MustCompile("^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)/?$")
)
func HandlerFunc(i interface{}) http.HandlerFunc {
b, err := json.Marshal(i)
if err != nil {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Could not marshal %T: %v", i, err)
}
}
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
}
func New(controller ClusterInformer, port int, logger *logrus.Logger) *Server {
s := &Server{
logger: logger.WithField("pkg", "apiserver"),
controller: controller,
}
mux := http.NewServeMux()
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
@ -56,7 +40,18 @@ func New(controller ClusterInformer, port int, logger *logrus.Logger) *Server {
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
mux.Handle("/status", HandlerFunc(s.controller.Status()))
mux.HandleFunc("/status", func(w http.ResponseWriter, req *http.Request) {
status := s.controller.Status()
b, err := json.Marshal(status)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Could not marshal controller status: %v", err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
})
mux.HandleFunc("/clusters/", func(w http.ResponseWriter, req *http.Request) {
var resp interface{}
if matches := clusterStatusURL.FindAllStringSubmatch(req.URL.Path, -1); matches != nil {

View File

@ -10,6 +10,7 @@ type controllerStatus struct {
ControllerConfig Config
OperatorConfig config.Config
LastSyncTime int64
ClustersRunning int
}
type clusterStatus struct {
@ -48,5 +49,6 @@ func (c *Controller) Status() interface{} {
ControllerConfig: c.config,
OperatorConfig: *c.opConfig,
LastSyncTime: c.lastClusterSyncTime,
ClustersRunning: len(c.clusters),
}
}