Skip to content

Commit

Permalink
ceph: parse json correctly
Browse files Browse the repository at this point in the history
radosgw-admin may output logs before and after JSON when this command succeeds. We should
get rid of these logs before unmarshaling output. Although the logs before JSON were
treated in  PR7354, the logs after JSON weren't.

Signed-off-by: Satoru Takeuchi <satoru.takeuchi@gmail.com>
  • Loading branch information
satoru-takeuchi committed May 27, 2021
1 parent 3eb758a commit ca9aa1a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/operator/ceph/object/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewMultisiteContext(context *clusterd.Context, clusterInfo *client.ClusterI
func extractJSON(output string) (string, error) {
// `radosgw-admin` sometimes leaves logs to stderr even if it succeeds.
// So we should skip them if parsing output as json.
pattern := regexp.MustCompile("(?ms)^{.*")
pattern := regexp.MustCompile(`(?ms)^{.*}$`)
match := pattern.Find([]byte(output))
if match == nil {
return "", errors.Errorf("didn't contain json. %s", output)
Expand Down
6 changes: 6 additions & 0 deletions pkg/operator/ceph/object/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ func TestExtractJson(t *testing.T) {
match, err = extractJSON(s)
assert.NoError(t, err)
assert.True(t, json.Valid([]byte(match)))

s = `{"test": "test"}
this line can't be parsed as json`
match, err = extractJSON(s)
assert.NoError(t, err)
assert.True(t, json.Valid([]byte(match)))
}

0 comments on commit ca9aa1a

Please sign in to comment.