rest api for getting ceph mon desired and actual status

This commit is contained in:
Travis Nielsen committed 2016-09-26 14:30:41 -07:00
1 parent 2bafccb814
commit 4cc85d7978
7 files changed
+92 -9

No files matched your search

+47
View File
@@ -86,6 +86,53 @@ func (h *Handler) GetNodes(w http.ResponseWriter, r *http.Request) {
FormatJsonResponse(w, nodes)
}
type overallMonStatus struct {
Status castled.MonStatusResponse `json:"status"`
Desired []*castled.CephMonitorConfig `json:"desired"`
}
// Gets the monitors that have been created in this cluster.
// GET
// /mon
func (h *Handler) GetMonitors(w http.ResponseWriter, r *http.Request) {
desiredMons, err := castled.GetDesiredMonitors(h.EtcdClient)
if err != nil {
log.Printf("failed to load monitors: %+v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
mons := []*castled.CephMonitorConfig{}
if len(desiredMons) == 0 {
// no monitors to connect to
FormatJsonResponse(w, mons)
return
}
// connect to ceph
adminConn, ok := h.connectToCeph(w)
if !ok {
return
}
defer adminConn.Shutdown()
// get the monitor status
monStatusResp, err := castled.GetMonStatus(adminConn)
if err != nil {
log.Printf("failed to get mon_status, err: %+v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
status := &overallMonStatus{Status: monStatusResp}
for _, mon := range desiredMons {
status.Desired = append(status.Desired, mon)
}
FormatJsonResponse(w, status)
}
// Gets the storage pools that have been created in this cluster.
// GET
// /pool
+30
View File
@@ -77,6 +77,36 @@ func TestGetNodesHandlerFailure(t *testing.T) {
assert.Equal(t, ``, w.Body.String())
}
func TestGetMonsHandler(t *testing.T) {
etcdClient := util.NewMockEtcdClient()
cephFactory := &testceph.MockConnectionFactory{Fsid: "myfsid", SecretKey: "mykey"}
req, err := http.NewRequest("GET", "http://10.0.0.100/mon", nil)
assert.Nil(t, err)
// first return no mons
w := httptest.NewRecorder()
// no mons will be returned, should be empty output
h := NewHandler(etcdClient, &test.MockConnectionFactory{}, cephFactory)
h.GetMonitors(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, `[]`, w.Body.String())
// now return some monitors from etcd
key := "/castle/services/ceph/monitor/desired/a"
etcdClient.SetValue(path.Join(key, "id"), "mon0")
etcdClient.SetValue(path.Join(key, "ipaddress"), "1.2.3.4")
etcdClient.SetValue(path.Join(key, "port"), "8765")
// monitors should be returned now, verify the output
w = httptest.NewRecorder()
h = NewHandler(etcdClient, &test.MockConnectionFactory{}, cephFactory)
h.GetMonitors(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "{\"status\":{\"quorum\":[0],\"monmap\":{\"mons\":[{\"name\":\"mon0\",\"rank\":0,\"addr\":\"10.37.129.87:6790\"}]}},\"desired\":[{\"name\":\"mon0\",\"endpoint\":\"1.2.3.4:8765\"}]}", w.Body.String())
}
func TestGetPoolsHandler(t *testing.T) {
req, err := http.NewRequest("GET", "http://10.0.0.100/pool", nil)
if err != nil {
+6
View File
@@ -20,5 +20,11 @@ func (h *Handler) GetRoutes() []Route {
"/pool",
h.CreatePool,
},
{
"GetMonitors",
"GET",
"/mon",
h.GetMonitors,
},
}
}
+1 -1
View File
@@ -79,7 +79,7 @@ func LoadClusterInfo(etcdClient etcd.KeysAPI) (*ClusterInfo, error) {
}
// Get the monitors that have been applied in a previous orchestration
cluster.Monitors, err = getChosenMonitors(etcdClient)
cluster.Monitors, err = GetDesiredMonitors(etcdClient)
return cluster, nil
}
+2 -2
View File
@@ -13,8 +13,8 @@ import (
)
type CephMonitorConfig struct {
Name string
Endpoint string
Name string `json:"name"`
Endpoint string `json:"endpoint"`
}
type cephConfig struct {
+1 -1
View File
@@ -77,7 +77,7 @@ type MonMapEntry struct {
}
// calls mon_status mon_command
func getMonStatus(adminConn cephclient.Connection) (MonStatusResponse, error) {
func GetMonStatus(adminConn cephclient.Connection) (MonStatusResponse, error) {
monCommand := "mon_status"
command, err := json.Marshal(map[string]string{"prefix": monCommand, "format": "json"})
if err != nil {
+5 -5
View File
@@ -137,7 +137,8 @@ func isMonitor(cluster *ClusterInfo, nodeID string) bool {
return false
}
func getChosenMonitors(etcdClient etcd.KeysAPI) (map[string]*CephMonitorConfig, error) {
func GetDesiredMonitors(etcdClient etcd.KeysAPI) (map[string]*CephMonitorConfig, error) {
// query the desired monitors from etcd
monitors := make(map[string]*CephMonitorConfig)
monKey := path.Join(cephKey, monitorKey, desiredKey)
previousMonitors, err := etcdClient.Get(ctx.Background(), monKey, &etcd.GetOptions{Recursive: true})
@@ -151,8 +152,7 @@ func getChosenMonitors(etcdClient etcd.KeysAPI) (map[string]*CephMonitorConfig,
return monitors, nil
}
// Load the previously selected monitors
log.Printf("Loading previously selected monitors")
// parse the monitor info from etcd
for _, node := range previousMonitors.Node.Nodes {
nodeID := util.GetLeafKeyPath(node.Key)
mon := &CephMonitorConfig{}
@@ -187,7 +187,7 @@ func chooseMonitorNodes(context *clusterd.Context) (map[string]*CephMonitorConfi
desiredMonitors := calculateMonitorCount(nodeCount)
// get the monitors that have already been chosen
monitors, err := getChosenMonitors(context.EtcdClient)
monitors, err := GetDesiredMonitors(context.EtcdClient)
if err != nil {
return nil, nil, err
}
@@ -361,7 +361,7 @@ func waitForQuorum(factory cephclient.ConnectionFactory, context *clusterd.Conte
// get the mon_status response that contains info about all monitors in the mon map and
// their quorum status
monStatusResp, err := getMonStatus(adminConn)
monStatusResp, err := GetMonStatus(adminConn)
if err != nil {
log.Printf("failed to get mon_status, err: %+v", err)
continue