From ca9aa1a11c3cc0aa7480fcbd4469aef628a2bd14 Mon Sep 17 00:00:00 2001 From: Satoru Takeuchi Date: Thu, 27 May 2021 04:11:37 +0000 Subject: [PATCH] ceph: parse json correctly 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 --- pkg/operator/ceph/object/admin.go | 2 +- pkg/operator/ceph/object/admin_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/operator/ceph/object/admin.go b/pkg/operator/ceph/object/admin.go index c9f3ceb4c..b1d262fdd 100644 --- a/pkg/operator/ceph/object/admin.go +++ b/pkg/operator/ceph/object/admin.go @@ -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) diff --git a/pkg/operator/ceph/object/admin_test.go b/pkg/operator/ceph/object/admin_test.go index bab789ac1..2c43e81be 100644 --- a/pkg/operator/ceph/object/admin_test.go +++ b/pkg/operator/ceph/object/admin_test.go @@ -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))) }