2016-11-05 17:09:05 -07:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 The Rook Authors. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
2016-09-15 13:52:55 -07:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2017-03-10 19:00:27 -08:00
|
|
|
"fmt"
|
2016-09-20 15:20:17 -07:00
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
2016-09-15 13:52:55 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
2017-03-10 19:00:27 -08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-10-26 00:38:39 +02:00
|
|
|
"github.com/rook/rook/pkg/clusterd"
|
2017-12-04 08:33:19 -08:00
|
|
|
ceph "github.com/rook/rook/pkg/daemon/ceph/client"
|
|
|
|
|
"github.com/rook/rook/pkg/daemon/ceph/mon"
|
2016-09-15 13:52:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Handler struct {
|
2017-03-10 19:00:27 -08:00
|
|
|
context *clusterd.Context
|
|
|
|
|
config *Config
|
|
|
|
|
cephExporter *CephExporter
|
2016-09-15 13:52:55 -07:00
|
|
|
}
|
|
|
|
|
|
2017-02-01 15:41:46 -08:00
|
|
|
func newHandler(context *clusterd.Context, config *Config) *Handler {
|
2016-09-15 13:52:55 -07:00
|
|
|
return &Handler{
|
2017-02-01 15:41:46 -08:00
|
|
|
context: context,
|
|
|
|
|
config: config,
|
2016-09-15 13:52:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-10 19:00:27 -08:00
|
|
|
// RegisterMetrics registers all collected metrics by this API server. Note this should be called in a
|
|
|
|
|
// goroutine because it will retry upon failure and block until successful.
|
|
|
|
|
func (h *Handler) RegisterMetrics(retryMs int) error {
|
2017-06-02 13:44:54 -07:00
|
|
|
h.cephExporter = NewCephExporter(h)
|
2017-03-10 19:00:27 -08:00
|
|
|
if err := prometheus.Register(h.cephExporter); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to register metrics: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) Shutdown() {
|
|
|
|
|
if h.cephExporter != nil {
|
|
|
|
|
prometheus.Unregister(h.cephExporter)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 13:52:55 -07:00
|
|
|
// Format a json response
|
|
|
|
|
func FormatJsonResponse(w http.ResponseWriter, object interface{}) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
|
|
|
|
|
|
output, err := json.Marshal(object)
|
|
|
|
|
if err != nil {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Errorf("failed to marshal object '%+v': %+v", object, err)
|
2016-09-15 13:52:55 -07:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(output)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-23 09:21:35 -07:00
|
|
|
type overallMonStatus struct {
|
2016-11-10 10:59:26 -08:00
|
|
|
Status ceph.MonStatusResponse `json:"status"`
|
|
|
|
|
Desired []*mon.CephMonitorConfig `json:"desired"`
|
2016-09-23 09:21:35 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 12:56:56 -07:00
|
|
|
func handleReadBody(w http.ResponseWriter, r *http.Request, opName string) ([]byte, bool) {
|
|
|
|
|
if r.Body == nil {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Errorf("nil request body for %s", opName)
|
2016-09-30 12:56:56 -07:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1024))
|
|
|
|
|
if err == nil {
|
|
|
|
|
r.Body.Close()
|
|
|
|
|
} else {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Errorf("failed to read %s request body: %+v", opName, err)
|
2016-09-30 12:56:56 -07:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return body, true
|
|
|
|
|
}
|