diff --git a/pkg/api/handlers.go b/pkg/api/handlers.go index 4c6d0bbad..f77e471ff 100644 --- a/pkg/api/handlers.go +++ b/pkg/api/handlers.go @@ -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 diff --git a/pkg/api/handlers_test.go b/pkg/api/handlers_test.go index cbf70a28a..db1379ba3 100644 --- a/pkg/api/handlers_test.go +++ b/pkg/api/handlers_test.go @@ -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 { diff --git a/pkg/api/routes.go b/pkg/api/routes.go index f6c83ef06..406b552bf 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -20,5 +20,11 @@ func (h *Handler) GetRoutes() []Route { "/pool", h.CreatePool, }, + { + "GetMonitors", + "GET", + "/mon", + h.GetMonitors, + }, } } diff --git a/pkg/castled/ceph.go b/pkg/castled/ceph.go index f821af134..edcf9d6dc 100644 --- a/pkg/castled/ceph.go +++ b/pkg/castled/ceph.go @@ -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 } diff --git a/pkg/castled/config.go b/pkg/castled/config.go index 98b67ed6d..d40aad52a 100644 --- a/pkg/castled/config.go +++ b/pkg/castled/config.go @@ -13,8 +13,8 @@ import ( ) type CephMonitorConfig struct { - Name string - Endpoint string + Name string `json:"name"` + Endpoint string `json:"endpoint"` } type cephConfig struct { diff --git a/pkg/castled/mon.go b/pkg/castled/mon.go index bd35d2677..ec0bcc567 100644 --- a/pkg/castled/mon.go +++ b/pkg/castled/mon.go @@ -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 { diff --git a/pkg/castled/monleader.go b/pkg/castled/monleader.go index bd292b762..79e0761ca 100644 --- a/pkg/castled/monleader.go +++ b/pkg/castled/monleader.go @@ -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