core: parse peer pool ouput with --out-file flag

When mirror logs are enabled, `ceph osd pool get <poolname> all` command returns peer pool info along
with the debug logs. This causes error while parsing the logs for json
data. Using --out-file flag saves the json data in the file which can be
parsed easily while logging will still be written to stdout/stderr.

Signed-off-by: Santosh Pillai <sapillai@redhat.com>
This commit is contained in:
Santosh Pillai committed 2022-11-21 15:28:31 +05:30
1 parent fdc2ab55dd
commit 4f3cd1d030
2 files changed
+67 -2

No files matched your search

+23 -2
View File
@@ -46,6 +46,8 @@ const (
var logger = capnslog.NewPackageLogger("github.com/rook/rook", "peer-map")
var peerPoolTempFile = "peerPool"
type PeerIDMapping struct {
ClusterIDMapping map[string]string
RBDPoolIDMapping []map[string]string
@@ -351,12 +353,31 @@ func decodePeerToken(token string) (*cephclient.PeerToken, error) {
}
func getPeerPoolDetails(ctx *clusterd.Context, args ...string) (cephclient.CephStoragePoolDetails, error) {
peerPoolDetails, err := ctx.Executor.ExecuteCommandWithTimeout(exec.CephCommandsTimeout, "ceph", args...)
poolDetailsTempFile, err := os.CreateTemp("", peerPoolTempFile)
if err != nil {
return cephclient.CephStoragePoolDetails{}, errors.Wrap(err, "failed to generate temporary file")
}
defer func() {
err := os.Remove(poolDetailsTempFile.Name())
if err != nil {
logger.Errorf("failed to remove file %q. %v", poolDetailsTempFile.Name(), err)
}
}()
args = append(args, "--out-file", poolDetailsTempFile.Name())
_, err = ctx.Executor.ExecuteCommandWithTimeout(exec.CephCommandsTimeout, "ceph", args...)
if err != nil {
return cephclient.CephStoragePoolDetails{}, errors.Wrap(err, "failed to get pool details from peer cluster")
}
return cephclient.ParsePoolDetails([]byte(peerPoolDetails))
data, err := os.ReadFile(poolDetailsTempFile.Name())
if err != nil {
return cephclient.CephStoragePoolDetails{}, errors.Wrapf(err, "failed to read from the temporary json output file %q", poolDetailsTempFile.Name())
}
return cephclient.ParsePoolDetails(data)
}
func getMapKV(input map[string]string) (string, string) {
@@ -18,6 +18,9 @@ package peermap
import (
"context"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
@@ -235,6 +238,23 @@ var fakeMultiPeerCephBlockPool = cephv1.CephBlockPool{
},
}
func saveMockDataInTempFile(data, filePattern string) error {
matches, _ := filepath.Glob(fmt.Sprintf("/tmp/%s*", filePattern))
for _, m := range matches {
file, err := os.OpenFile(m, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open file %q. %v", m, err)
}
defer file.Close()
_, err = file.WriteString(data)
if err != nil {
return fmt.Errorf("failed to write data to the file %q. %v", m, err)
}
}
return nil
}
var mockExecutor = &exectest.MockExecutor{
MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) {
logger.Infof("Command: %s %v", command, args)
@@ -253,22 +273,46 @@ var mockExecutor = &exectest.MockExecutor{
logger.Infof("Command: %s %v", command, args)
if args[0] == "osd" && args[1] == "pool" && args[2] == "get" && strings.HasSuffix(args[5], "peer1") {
if args[3] == "mirrorPool1" {
err := saveMockDataInTempFile(`{"pool_id": 2}`, peerPoolTempFile)
if err != nil {
return "", err
}
return `{"pool_id": 2}`, nil
} else if args[3] == "mirrorPool2" {
err := saveMockDataInTempFile(`{"pool_id": 3}`, peerPoolTempFile)
if err != nil {
return "", err
}
return `{"pool_id": 3}`, nil
}
}
if args[0] == "osd" && args[1] == "pool" && args[2] == "get" && strings.HasSuffix(args[5], "peer2") {
if args[3] == "mirrorPool1" {
err := saveMockDataInTempFile(`{"pool_id": 3}`, peerPoolTempFile)
if err != nil {
return "", err
}
return `{"pool_id": 3}`, nil
} else if args[3] == "mirrorPool2" {
err := saveMockDataInTempFile(`{"pool_id": 4}`, peerPoolTempFile)
if err != nil {
return "", err
}
return `{"pool_id": 4}`, nil
}
}
if args[0] == "osd" && args[1] == "pool" && args[2] == "get" && strings.HasSuffix(args[5], "peer3") {
if args[3] == "mirrorPool1" {
err := saveMockDataInTempFile(`{"pool_id": 4}`, peerPoolTempFile)
if err != nil {
return "", err
}
return `{"pool_id": 4}`, nil
} else if args[3] == "mirrorPool2" {
err := saveMockDataInTempFile(`{"pool_id": 5}`, peerPoolTempFile)
if err != nil {
return "", err
}
return `{"pool_id": 5}`, nil
}
}