|
| 1 | +diff --git a/pkg/daemon/ceph/client/osd.go b/pkg/daemon/ceph/client/osd.go |
| 2 | +index 49d082e90881..95dbbbef3d66 100644 |
| 3 | +--- a/pkg/daemon/ceph/client/osd.go |
| 4 | ++++ b/pkg/daemon/ceph/client/osd.go |
| 5 | +@@ -16,9 +16,11 @@ limitations under the License. |
| 6 | + package client |
| 7 | + |
| 8 | + import ( |
| 9 | ++ "bytes" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "math" |
| 13 | ++ "os/exec" |
| 14 | + "strconv" |
| 15 | + "strings" |
| 16 | + |
| 17 | +@@ -303,8 +305,16 @@ func GetOSDDump(context *clusterd.Context, clusterInfo *ClusterInfo) (*OSDDump, |
| 18 | + return nil, errors.Wrap(err, "failed to get osd dump") |
| 19 | + } |
| 20 | + |
| 21 | ++ // Use jq to fix potentially invalid JSON from ceph osd dump |
| 22 | ++ jqCmd := exec.Command("jq", ".") |
| 23 | ++ jqCmd.Stdin = bytes.NewReader(buf) |
| 24 | ++ validJSON, err := jqCmd.Output() |
| 25 | ++ if err != nil { |
| 26 | ++ return nil, errors.Wrap(err, "failed to run jq on osd dump output") |
| 27 | ++ } |
| 28 | ++ |
| 29 | + var osdDump OSDDump |
| 30 | +- if err := json.Unmarshal(buf, &osdDump); err != nil { |
| 31 | ++ if err := json.Unmarshal(validJSON, &osdDump); err != nil { |
| 32 | + return nil, errors.Wrap(err, "failed to unmarshal osd dump response") |
| 33 | + } |
| 34 | + |
| 35 | +diff --git a/pkg/daemon/ceph/client/osd_test.go b/pkg/daemon/ceph/client/osd_test.go |
| 36 | +index 282cbf3f3703..2d4550607f6a 100644 |
| 37 | +--- a/pkg/daemon/ceph/client/osd_test.go |
| 38 | ++++ b/pkg/daemon/ceph/client/osd_test.go |
| 39 | +@@ -214,3 +214,79 @@ func TestOSDOkToStop(t *testing.T) { |
| 40 | + assert.Equal(t, "--max=0", seenArgs[3]) |
| 41 | + }) |
| 42 | + } |
| 43 | ++ |
| 44 | ++func TestGetOSDDump(t *testing.T) { |
| 45 | ++ // Valid JSON output from ceph osd dump |
| 46 | ++ validOSDDump := `{ |
| 47 | ++ "osds": [ |
| 48 | ++ {"osd": 0, "up": 1, "in": 1}, |
| 49 | ++ {"osd": 1, "up": 1, "in": 1} |
| 50 | ++ ], |
| 51 | ++ "flags": "nodown,sortbitwise", |
| 52 | ++ "crush_node_flags": {}, |
| 53 | ++ "full_ratio": 0.95, |
| 54 | ++ "backfillfull_ratio": 0.9, |
| 55 | ++ "nearfull_ratio": 0.85 |
| 56 | ++ }` |
| 57 | ++ |
| 58 | ++ // Invalid JSON with "inf" value that ceph osd dump can produce |
| 59 | ++ // This causes "invalid character 'i' looking for beginning of value" error |
| 60 | ++ invalidOSDDumpWithInf := `{ |
| 61 | ++ "osds": [ |
| 62 | ++ {"osd": 0, "up": 1, "in": 1} |
| 63 | ++ ], |
| 64 | ++ "flags": "nodown", |
| 65 | ++ "crush_node_flags": {}, |
| 66 | ++ "full_ratio": 0.95, |
| 67 | ++ "backfillfull_ratio": 0.9, |
| 68 | ++ "nearfull_ratio": inf |
| 69 | ++ }` |
| 70 | ++ |
| 71 | ++ t.Run("valid JSON is parsed correctly", func(t *testing.T) { |
| 72 | ++ executor := &exectest.MockExecutor{} |
| 73 | ++ executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) { |
| 74 | ++ logger.Infof("Command: %s %v", command, args) |
| 75 | ++ if args[0] == "osd" && args[1] == "dump" { |
| 76 | ++ return validOSDDump, nil |
| 77 | ++ } |
| 78 | ++ return "", errors.Errorf("unexpected ceph command %q", args) |
| 79 | ++ } |
| 80 | ++ |
| 81 | ++ context := &clusterd.Context{Executor: executor} |
| 82 | ++ clusterInfo := AdminTestClusterInfo("mycluster") |
| 83 | ++ |
| 84 | ++ dump, err := GetOSDDump(context, clusterInfo) |
| 85 | ++ assert.NoError(t, err) |
| 86 | ++ assert.NotNil(t, dump) |
| 87 | ++ assert.Equal(t, 2, len(dump.OSDs)) |
| 88 | ++ assert.Equal(t, "nodown,sortbitwise", dump.Flags) |
| 89 | ++ assert.Equal(t, 0.95, dump.FullRatio) |
| 90 | ++ assert.Equal(t, 0.9, dump.BackfillFullRatio) |
| 91 | ++ assert.Equal(t, 0.85, dump.NearFullRatio) |
| 92 | ++ }) |
| 93 | ++ |
| 94 | ++ t.Run("invalid JSON with inf is fixed by jq and parsed correctly", func(t *testing.T) { |
| 95 | ++ executor := &exectest.MockExecutor{} |
| 96 | ++ executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) { |
| 97 | ++ logger.Infof("Command: %s %v", command, args) |
| 98 | ++ if args[0] == "osd" && args[1] == "dump" { |
| 99 | ++ return invalidOSDDumpWithInf, nil |
| 100 | ++ } |
| 101 | ++ return "", errors.Errorf("unexpected ceph command %q", args) |
| 102 | ++ } |
| 103 | ++ |
| 104 | ++ context := &clusterd.Context{Executor: executor} |
| 105 | ++ clusterInfo := AdminTestClusterInfo("mycluster") |
| 106 | ++ |
| 107 | ++ dump, err := GetOSDDump(context, clusterInfo) |
| 108 | ++ assert.NoError(t, err) |
| 109 | ++ assert.NotNil(t, dump) |
| 110 | ++ assert.Equal(t, 1, len(dump.OSDs)) |
| 111 | ++ assert.Equal(t, "nodown", dump.Flags) |
| 112 | ++ assert.Equal(t, 0.95, dump.FullRatio) |
| 113 | ++ assert.Equal(t, 0.9, dump.BackfillFullRatio) |
| 114 | ++ // jq converts "inf" to 1.7976931348623157e+308 (max float64) |
| 115 | ++ // The important thing is that parsing succeeds without error |
| 116 | ++ assert.NotEqual(t, 0.0, dump.NearFullRatio) |
| 117 | ++ }) |
| 118 | ++} |
0 commit comments