util: make ExecComand return stdout and stderr as string

Most consumers of util.ExecCommand() need to convert the returned []byte
format of stdout and/or stderr to string. By having util.ExecCommand()
return strings instead, the code gets a little simpler.

A few commands return JSON that needs to be parsed. These commands will
be replaced by go-ceph implementations later on. For now, convert the
strings back to []byte when needed.

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos
2020-07-22 14:53:22 +02:00
committed by mergify[bot]
parent ddac66d76b
commit 36469b87e2
6 changed files with 31 additions and 28 deletions

View File

@ -90,9 +90,9 @@ func rbdGetDeviceList(ctx context.Context, accessType string) ([]rbdDeviceInfo,
}
if accessType == accessTypeKRbd {
err = json.Unmarshal(stdout, &rbdDeviceList)
err = json.Unmarshal([]byte(stdout), &rbdDeviceList)
} else {
err = json.Unmarshal(stdout, &nbdDeviceList)
err = json.Unmarshal([]byte(stdout), &nbdDeviceList)
}
if err != nil {
return nil, fmt.Errorf("error to parse JSON output of device list for devices of type (%s): (%v)", accessType, err)
@ -231,7 +231,7 @@ func createPath(ctx context.Context, volOpt *rbdVolume, cr *util.Credentials) (s
// Execute map
stdout, stderr, err := util.ExecCommand(ctx, rbd, mapOptions...)
if err != nil {
klog.Warningf(util.Log(ctx, "rbd: map error %v, rbd output: %s"), err, string(stderr))
klog.Warningf(util.Log(ctx, "rbd: map error %v, rbd output: %s"), err, stderr)
// unmap rbd image if connection timeout
if strings.Contains(err.Error(), rbdMapConnectionTimeout) {
detErr := detachRBDImageOrDeviceSpec(ctx, imagePath, true, isNbd, volOpt.Encrypted, volOpt.VolID)
@ -239,9 +239,9 @@ func createPath(ctx context.Context, volOpt *rbdVolume, cr *util.Credentials) (s
klog.Warningf(util.Log(ctx, "rbd: %s unmap error %v"), imagePath, detErr)
}
}
return "", fmt.Errorf("rbd: map failed %v, rbd output: %s", err, string(stderr))
return "", fmt.Errorf("rbd: map failed with error %v, rbd error output: %s", err, stderr)
}
devicePath := strings.TrimSuffix(string(stdout), "\n")
devicePath := strings.TrimSuffix(stdout, "\n")
return devicePath, nil
}
@ -311,13 +311,13 @@ func detachRBDImageOrDeviceSpec(ctx context.Context, imageOrDeviceSpec string, i
// Messages for krbd and nbd differ, hence checking either of them for missing mapping
// This is not applicable when a device path is passed in
if isImageSpec &&
(strings.Contains(string(stderr), fmt.Sprintf(rbdUnmapCmdkRbdMissingMap, imageOrDeviceSpec)) ||
strings.Contains(string(stderr), fmt.Sprintf(rbdUnmapCmdNbdMissingMap, imageOrDeviceSpec))) {
(strings.Contains(stderr, fmt.Sprintf(rbdUnmapCmdkRbdMissingMap, imageOrDeviceSpec)) ||
strings.Contains(stderr, fmt.Sprintf(rbdUnmapCmdNbdMissingMap, imageOrDeviceSpec))) {
// Devices found not to be mapped are treated as a successful detach
util.TraceLog(ctx, "image or device spec (%s) not mapped", imageOrDeviceSpec)
return nil
}
return fmt.Errorf("rbd: unmap for spec (%s) failed (%v): (%s)", imageOrDeviceSpec, err, string(stderr))
return fmt.Errorf("rbd: unmap for spec (%s) failed (%v): (%s)", imageOrDeviceSpec, err, stderr)
}
return nil

View File

@ -307,11 +307,11 @@ func addRbdManagerTask(ctx context.Context, pOpts *rbdVolume, arg []string) (boo
if err != nil {
switch {
case strings.Contains(string(stderr), rbdTaskRemoveCmdInvalidString1) &&
strings.Contains(string(stderr), rbdTaskRemoveCmdInvalidString2):
case strings.Contains(stderr, rbdTaskRemoveCmdInvalidString1) &&
strings.Contains(stderr, rbdTaskRemoveCmdInvalidString2):
klog.Warningf(util.Log(ctx, "cluster with cluster ID (%s) does not support Ceph manager based rbd commands (minimum ceph version required is v14.2.3)"), pOpts.ClusterID)
supported = false
case strings.HasPrefix(string(stderr), rbdTaskRemoveCmdAccessDeniedMessage):
case strings.HasPrefix(stderr, rbdTaskRemoveCmdAccessDeniedMessage):
klog.Warningf(util.Log(ctx, "access denied to Ceph MGR-based rbd commands on cluster ID (%s)"), pOpts.ClusterID)
supported = false
default:
@ -883,17 +883,17 @@ func (rv *rbdVolume) updateVolWithImageInfo(cr *util.Credentials) error {
"info", rv.String())
if err != nil {
klog.Errorf("failed getting information for image (%s): (%s)", rv, err)
if strings.Contains(string(stderr), "rbd: error opening image "+rv.RbdImageName+
if strings.Contains(stderr, "rbd: error opening image "+rv.RbdImageName+
": (2) No such file or directory") {
return util.JoinErrors(ErrImageNotFound, err)
}
return err
}
err = json.Unmarshal(stdout, &imgInfo)
err = json.Unmarshal([]byte(stdout), &imgInfo)
if err != nil {
klog.Errorf("failed to parse JSON output of image info (%s): (%s)", rv, err)
return fmt.Errorf("unmarshal failed: %+v. raw buffer response: %s", err, string(stdout))
return fmt.Errorf("unmarshal failed: %+v. raw buffer response: %s", err, stdout)
}
rv.VolSize = imgInfo.Size
@ -1051,7 +1051,7 @@ func resizeRBDImage(rbdVol *rbdVolume, cr *util.Credentials) error {
_, stderr, err := util.ExecCommand(context.TODO(), "rbd", args...)
if err != nil {
return fmt.Errorf("failed to resize rbd image (%w), command output: %s", err, string(stderr))
return fmt.Errorf("failed to resize rbd image (%w), command output: %s", err, stderr)
}
return nil
@ -1129,17 +1129,17 @@ func (rv *rbdVolume) listSnapshots(ctx context.Context, cr *util.Credentials) ([
"--all", rv.String())
if err != nil {
klog.Errorf(util.Log(ctx, "failed getting information for image (%s): (%s)"), rv, err)
if strings.Contains(string(stderr), "rbd: error opening image "+rv.RbdImageName+
if strings.Contains(stderr, "rbd: error opening image "+rv.RbdImageName+
": (2) No such file or directory") {
return snapInfo, util.JoinErrors(ErrImageNotFound, err)
}
return snapInfo, err
}
err = json.Unmarshal(stdout, &snapInfo)
err = json.Unmarshal([]byte(stdout), &snapInfo)
if err != nil {
klog.Errorf(util.Log(ctx, "failed to parse JSON output of snapshot info (%s)"), err)
return snapInfo, fmt.Errorf("unmarshal failed: %w. raw buffer response: %s", err, string(stdout))
return snapInfo, fmt.Errorf("unmarshal failed: %w. raw buffer response: %s", err, stdout)
}
return snapInfo, nil
}