From 9c5e5102f6f784207b2adb870493dc9bf61f893c Mon Sep 17 00:00:00 2001
From: Stephen Taylor <stephen.taylor.1@att.com>
Date: Tue, 9 May 2023 14:01:42 -0600
Subject: [PATCH] [ceph-client] Strip any errors preceding pool properties JSON

Sometimes errors appear in the 'ceph osd pool get' output before
the JSON string. The returned string is saved and is assumed to
contain only the JSON string with the pool properties. When errors
appear in the string, pool properties are not read properly, which
can cause pools to be misconfigured. This change filters that
output so only the expected JSON string is returned. It can then be
parsed correctly.

Change-Id: I83347cc32da7e7af160b5cacc2a99de74eebebc7
---
 ceph-client/Chart.yaml                      | 2 +-
 ceph-client/templates/bin/pool/_init.sh.tpl | 4 +++-
 releasenotes/notes/ceph-client.yaml         | 1 +
 3 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/ceph-client/Chart.yaml b/ceph-client/Chart.yaml
index f7835edd98..4f48d2bc71 100644
--- a/ceph-client/Chart.yaml
+++ b/ceph-client/Chart.yaml
@@ -15,6 +15,6 @@ apiVersion: v1
 appVersion: v1.0.0
 description: OpenStack-Helm Ceph Client
 name: ceph-client
-version: 0.1.45
+version: 0.1.46
 home: https://github.com/ceph/ceph-client
 ...
diff --git a/ceph-client/templates/bin/pool/_init.sh.tpl b/ceph-client/templates/bin/pool/_init.sh.tpl
index 3babe9297e..07ac4726e2 100644
--- a/ceph-client/templates/bin/pool/_init.sh.tpl
+++ b/ceph-client/templates/bin/pool/_init.sh.tpl
@@ -249,7 +249,9 @@ function create_pool () {
     ceph --cluster "${CLUSTER}" osd pool application enable "${POOL_NAME}" "${POOL_APPLICATION}"
   fi
 
-  pool_values=$(ceph --cluster "${CLUSTER}" osd pool get "${POOL_NAME}" all -f json)
+  # 'tr' and 'awk' are needed here to strip off text that is echoed before the JSON string.
+  # In some cases, errors/warnings are written to stdout and the JSON doesn't parse correctly.
+  pool_values=$(ceph --cluster "${CLUSTER}" osd pool get "${POOL_NAME}" all -f json | tr -d '\n' | awk -F{ '{print "{" $2}')
 
   if [[ $(ceph mgr versions | awk '/version/{print $3}' | cut -d. -f1) -ge 14 ]]; then
     if [[ "${ENABLE_AUTOSCALER}" == "true" ]]; then
diff --git a/releasenotes/notes/ceph-client.yaml b/releasenotes/notes/ceph-client.yaml
index 4a2c885fe5..26da7f164d 100644
--- a/releasenotes/notes/ceph-client.yaml
+++ b/releasenotes/notes/ceph-client.yaml
@@ -46,4 +46,5 @@ ceph-client:
   - 0 1.43 Document the use of mon_allow_pool_size_one
   - 0.1.44 Allow pg_num_min to be overridden per pool
   - 0.1.45 Update Ceph to 17.2.6
+  - 0.1.46 Strip any errors preceding pool properties JSON
 ...