Skip to content

Commit

Permalink
Add an api endpoint to get mon addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Sabaini <[email protected]>
  • Loading branch information
sabaini committed Nov 22, 2024
1 parent 7d54416 commit 9643029
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
16 changes: 16 additions & 0 deletions microceph/api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func cmdServicesGet(s state.State, r *http.Request) response.Response {
// Service endpoints.
var monServiceCmd = rest.Endpoint{
Path: "services/mon",
Get: rest.EndpointAction{Handler: cmdMonGet, ProxyTarget: true},
Put: rest.EndpointAction{Handler: cmdEnableServicePut, ProxyTarget: true},
Delete: rest.EndpointAction{Handler: cmdDeleteService, ProxyTarget: true},
}
Expand All @@ -60,6 +61,21 @@ var rbdMirroServiceCmd = rest.Endpoint{
Delete: rest.EndpointAction{Handler: cmdDeleteService, ProxyTarget: true},
}

// cmdMonGet returns the mon service status.
func cmdMonGet(s state.State, r *http.Request) response.Response {

// fetch monitor addresses
monitors, err := ceph.GetMonitorAddresses(r.Context(), interfaces.CephState{State: s})
if err != nil {
return response.InternalError(err)
}

monStatus := types.MonitorStatus{Addresses: monitors}

return response.SyncResponse(true, monStatus)

}

func cmdEnableServicePut(s state.State, r *http.Request) response.Response {
var payload types.EnableService

Expand Down
6 changes: 6 additions & 0 deletions microceph/api/types/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ type RGWService struct {
Port int `json:"port" yaml:"port"`
Enabled bool `json:"enabled" yaml:"enabled"`
}

// MonitorStatus holds the status of all monitors
// for now, this is just the addresses of the monitors
type MonitorStatus struct {
Addresses []string `json:"addresses" yaml:"addresses"`
}
27 changes: 24 additions & 3 deletions microceph/ceph/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func UpdateConfig(ctx context.Context, s interfaces.StateInterface) error {
// REF: https://docs.ceph.com/en/quincy/rados/configuration/network-config-ref/#ceph-daemons
// The mon host configuration option only needs to be sufficiently up to date such that a
// client can reach one monitor that is currently online.
monitorAddresses := getMonitorAddresses(config)
monitorAddresses := getMonitorsFromConfig(config)

// backward compat: if no mon hosts found, get them from the node addresses but don't
// insert into db, as the join logic will take care of that.
Expand Down Expand Up @@ -435,8 +435,29 @@ func GetConfigDb(ctx context.Context, s interfaces.StateInterface) (map[string]s
return config, nil
}

// getMonitorAddresses scans a provided config key/value map and returns a list of mon hosts found.
func getMonitorAddresses(configs map[string]string) []string {
// GetMonitorAddresses retrieves the monitor addresses from the database.
func GetMonitorAddresses(ctx context.Context, s interfaces.StateInterface) ([]string, error) {
config, err := GetConfigDb(ctx, s)
if err != nil {
return nil, fmt.Errorf("failed to get config db: %w", err)
}

monitorAddresses := getMonitorsFromConfig(config)

if len(monitorAddresses) == 0 {
monitorAddresses, err = backwardCompatMonitors(ctx, s)
if err != nil {
return nil, fmt.Errorf("failed to get monitor addresses: %w", err)
}
}

// Ensure that IPv6 addresses have square brackets around them (if IPv6 is used).
monitorAddresses = formatIPv6(monitorAddresses)
return monitorAddresses, nil
}

// getMonitorsFromConfig scans a provided config key/value map and returns a list of mon hosts found.
func getMonitorsFromConfig(configs map[string]string) []string {
monHosts := []string{}
for k, v := range configs {
if strings.Contains(k, "mon.host.") {
Expand Down
2 changes: 1 addition & 1 deletion microceph/ceph/services_placement_rgw.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (rgw *RgwServicePlacement) ServiceInit(ctx context.Context, s interfaces.St
return fmt.Errorf("failed to get config db: %w", err)
}

return EnableRGW(s, rgw.Port, rgw.SSLPort, rgw.SSLCertificate, rgw.SSLPrivateKey, getMonitorAddresses(config))
return EnableRGW(s, rgw.Port, rgw.SSLPort, rgw.SSLCertificate, rgw.SSLPrivateKey, getMonitorsFromConfig(config))
}

func (rgw *RgwServicePlacement) PostPlacementCheck(s interfaces.StateInterface) error {
Expand Down

0 comments on commit 9643029

Please sign in to comment.