diff --git a/pkg/daemon/ceph/client/fake/osd.go b/pkg/daemon/ceph/client/fake/osd.go index c1d4a0b52..111d8df29 100644 --- a/pkg/daemon/ceph/client/fake/osd.go +++ b/pkg/daemon/ceph/client/fake/osd.go @@ -128,3 +128,13 @@ func OsdOkToStopOutput(queriedID int, returnOsdIds []int, useCephPacificPlusOutp } return fmt.Sprintf(okTemplate, strings.Join(osdIdsStr, ",")) } + +// OSDDeviceClassOutput returns JSON output from 'ceph osd crush get-device-class' that can be used for unit tests. +// osdId is a osd ID to get from crush map. If ID is empty raise a fake error. +func OSDDeviceClassOutput(osdId string) string { + if osdId == "" { + return "ERR: fake error from ceph cli" + } + okTemplate := `[{"osd":%s,"device_class":"hdd"}]` + return fmt.Sprintf(okTemplate, osdId) +} diff --git a/pkg/daemon/ceph/client/osd.go b/pkg/daemon/ceph/client/osd.go index 1cdfd60c0..c571ec800 100644 --- a/pkg/daemon/ceph/client/osd.go +++ b/pkg/daemon/ceph/client/osd.go @@ -307,6 +307,31 @@ func OsdListNum(context *clusterd.Context, clusterInfo *ClusterInfo) (OsdList, e return output, nil } +// OSDDeviceClass report device class for osd +type OSDDeviceClass struct { + ID int `json:"osd"` + DeviceClass string `json:"device_class"` +} + +// OSDDeviceClasses returns the device classes for particular OsdIDs +func OSDDeviceClasses(context *clusterd.Context, clusterInfo *ClusterInfo, osdIds []string) ([]OSDDeviceClass, error) { + var deviceClasses []OSDDeviceClass + + args := []string{"osd", "crush", "get-device-class"} + args = append(args, osdIds...) + buf, err := NewCephCommand(context, clusterInfo, args).Run() + if err != nil { + return deviceClasses, errors.Wrap(err, "failed to get device-class info") + } + + err = json.Unmarshal(buf, &deviceClasses) + if err != nil { + return deviceClasses, errors.Wrap(err, "failed to unmarshal 'osd crush get-device-class' response") + } + + return deviceClasses, nil +} + // OSDOkToStopStats report detailed information about which OSDs are okay to stop type OSDOkToStopStats struct { OkToStop bool `json:"ok_to_stop"` diff --git a/pkg/daemon/ceph/client/osd_test.go b/pkg/daemon/ceph/client/osd_test.go index 16995df19..823c3e583 100644 --- a/pkg/daemon/ceph/client/osd_test.go +++ b/pkg/daemon/ceph/client/osd_test.go @@ -114,6 +114,33 @@ func TestOsdListNum(t *testing.T) { assert.Equal(t, 0, len(list)) } +func TestOSDDeviceClasses(t *testing.T) { + executor := &exectest.MockExecutor{} + executor.MockExecuteCommandWithOutputFile = func(command, outputFile string, args ...string) (string, error) { + logger.Infof("Command: %s %v", command, args) + switch { + case args[0] == "osd" && args[1] == "crush" && args[2] == "get-device-class" && len(args) > 3: + return fake.OSDDeviceClassOutput(args[3]), nil + default: + return fake.OSDDeviceClassOutput(""), nil + } + } + + context := &clusterd.Context{Executor: executor} + clusterInfo := AdminClusterInfo("mycluster") + + t.Run("device classes returned", func(t *testing.T) { + deviceClasses, err := OSDDeviceClasses(context, clusterInfo, []string{"0"}) + assert.NoError(t, err) + assert.Equal(t, deviceClasses[0].DeviceClass, "hdd") + }) + + t.Run("error happened when no id provided", func(t *testing.T) { + _, err := OSDDeviceClasses(context, clusterInfo, []string{}) + assert.Error(t, err) + }) +} + func TestOSDOkToStop(t *testing.T) { returnString := "" returnOkResult := true diff --git a/pkg/operator/ceph/cluster/osd/integration_test.go b/pkg/operator/ceph/cluster/osd/integration_test.go index 29812a818..eb558fc0b 100644 --- a/pkg/operator/ceph/cluster/osd/integration_test.go +++ b/pkg/operator/ceph/cluster/osd/integration_test.go @@ -557,6 +557,11 @@ func osdIntegrationTestExecutor(t *testing.T, clientset *fake.Clientset, namespa if args[1] == "tree" { return cephclientfake.OsdTreeOutput(3, 3), nil // fake output for cluster with 3 nodes having 3 OSDs } + if args[1] == "crush" { + if args[2] == "get-device-class" { + return cephclientfake.OSDDeviceClassOutput(args[3]), nil + } + } } if args[0] == "versions" { // the update deploy code only cares about the mons from the ceph version command results diff --git a/pkg/operator/ceph/cluster/osd/osd_test.go b/pkg/operator/ceph/cluster/osd/osd_test.go index 17fc77b70..0bf23a149 100644 --- a/pkg/operator/ceph/cluster/osd/osd_test.go +++ b/pkg/operator/ceph/cluster/osd/osd_test.go @@ -238,6 +238,9 @@ func TestAddRemoveNode(t *testing.T) { if args[2] == "rm" { return "", nil } + if args[2] == "get-device-class" { + return cephclientfake.OSDDeviceClassOutput(args[3]), nil + } } if args[1] == "out" { return "", nil diff --git a/pkg/operator/ceph/cluster/osd/update.go b/pkg/operator/ceph/cluster/osd/update.go index b514bf943..e5dcfdbdf 100644 --- a/pkg/operator/ceph/cluster/osd/update.go +++ b/pkg/operator/ceph/cluster/osd/update.go @@ -128,6 +128,16 @@ func (c *updateConfig) updateExistingOSDs(errs *provisionErrors) { continue } + // backward compatibility for old deployments + if osdInfo.DeviceClass == "" { + deviceClassInfo, err := cephclient.OSDDeviceClasses(c.cluster.context, c.cluster.clusterInfo, []string{strconv.Itoa(osdID)}) + if err != nil { + logger.Errorf("failed to get device class for existing deployment %q. %v", depName, err) + } else { + osdInfo.DeviceClass = deviceClassInfo[0].DeviceClass + } + } + nodeOrPVCName, err := getNodeOrPVCName(dep) if err != nil { errs.addError("%v", errors.Wrapf(err, "failed to update OSD %d", osdID)) diff --git a/pkg/operator/ceph/cluster/osd/update_test.go b/pkg/operator/ceph/cluster/osd/update_test.go index a17ce2515..8b9624e2c 100644 --- a/pkg/operator/ceph/cluster/osd/update_test.go +++ b/pkg/operator/ceph/cluster/osd/update_test.go @@ -144,19 +144,24 @@ func Test_updateExistingOSDs(t *testing.T) { executor = &exectest.MockExecutor{ MockExecuteCommandWithOutputFile: func(command string, outFileArg string, args ...string) (string, error) { t.Logf("command: %s %v", command, args) - if fmt.Sprintf("%s %s %s", command, args[0], args[1]) != "ceph osd ok-to-stop" { - panic(fmt.Sprintf("unexpected command %q with args %v", command, args)) + if args[0] == "osd" { + if args[1] == "ok-to-stop" { + queriedID := args[2] + if strconv.Itoa(osdToBeQueried) != queriedID { + err := errors.Errorf("OSD %d should have been queried, but %s was queried instead", osdToBeQueried, queriedID) + t.Error(err) + return "", err + } + if len(returnOkToStopIDs) > 0 { + return cephclientfake.OsdOkToStopOutput(osdToBeQueried, returnOkToStopIDs, true), nil + } + return cephclientfake.OsdOkToStopOutput(osdToBeQueried, []int{}, true), errors.Errorf("induced error") + } + if args[1] == "crush" && args[2] == "get-device-class" { + return cephclientfake.OSDDeviceClassOutput(args[3]), nil + } } - queriedID := args[2] - if strconv.Itoa(osdToBeQueried) != queriedID { - err := errors.Errorf("OSD %d should have been queried, but %s was queried instead", osdToBeQueried, queriedID) - t.Error(err) - return "", err - } - if len(returnOkToStopIDs) > 0 { - return cephclientfake.OsdOkToStopOutput(osdToBeQueried, returnOkToStopIDs, true), nil - } - return cephclientfake.OsdOkToStopOutput(osdToBeQueried, []int{}, true), errors.Errorf("induced error") + panic(fmt.Sprintf("unexpected command %q with args %v", command, args)) }, }