From ca44f22c7ed235bd0aff548445cfebef3a9d8baa Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Fri, 14 Feb 2020 16:24:43 +0100 Subject: [PATCH] Revert "rbd: use go-ceph to implement getImageInfo()" This reverts commit 9f135b0dbabc2a75ec01d7fdbb31d457c34e2a08. --- pkg/rbd/rbd_util.go | 60 ++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/pkg/rbd/rbd_util.go b/pkg/rbd/rbd_util.go index 3459aba943ca..fcbcde11b95a 100644 --- a/pkg/rbd/rbd_util.go +++ b/pkg/rbd/rbd_util.go @@ -360,16 +360,14 @@ func updateSnapWithImageInfo(ctx context.Context, rbdSnap *rbdSnapshot, cr *util // updateVolWithImageInfo updates provided rbdVolume with information from on-disk data // regarding the same func updateVolWithImageInfo(ctx context.Context, rbdVol *rbdVolume, cr *util.Credentials) error { - err := rbdVol.Connect(cr) - if err != nil { - return errors.Wrapf(err, "failed to connect to Ceph cluster") - } - - err = rbdVol.getImageInfo() + imageInfo, err := getImageInfo(ctx, rbdVol.Monitors, cr, rbdVol.Pool, rbdVol.RbdImageName) if err != nil { return err } + rbdVol.VolSize = imageInfo.Size + rbdVol.ImageFeatures = strings.Join(imageInfo.Features, ",") + return nil } @@ -769,35 +767,47 @@ func getSnapshotMetadata(ctx context.Context, pSnapOpts *rbdSnapshot, cr *util.C return nil } +// imageInfo strongly typed JSON spec for image info +type imageInfo struct { + ObjectUUID string `json:"name"` + Size int64 `json:"size"` + Features []string `json:"features"` + CreatedAt string `json:"create_timestamp"` +} + // getImageInfo queries rbd about the given image and returns its metadata, and returns // ErrImageNotFound if provided image is not found -func (rv *rbdVolume) getImageInfo() error { +func getImageInfo(ctx context.Context, monitors string, cr *util.Credentials, poolName, imageName string) (imageInfo, error) { // rbd --format=json info [image-spec | snap-spec] - image, err := rv.open() - if err != nil { - return errors.Wrapf(err, "failed to open image") - } - defer image.Close() - imageInfo, err := image.Stat() - if err != nil { - return errors.Wrapf(err, "failed to get stat of image") - } - // TODO: can rv.VolSize not be a uint64? Or initialize it to -1? - rv.VolSize = int64(imageInfo.Size) + var imgInfo imageInfo - features, err := image.GetFeaturesNamed() + stdout, stderr, err := util.ExecCommand( + "rbd", + "-m", monitors, + "--id", cr.ID, + "--keyfile="+cr.KeyFile, + "-c", util.CephConfigPath, + "--format="+"json", + "info", poolName+"/"+imageName) if err != nil { - return errors.Wrapf(err, "failed to get features of image") + klog.Errorf(util.Log(ctx, "failed getting information for image (%s): (%s)"), poolName+"/"+imageName, err) + if strings.Contains(string(stderr), "rbd: error opening image "+imageName+ + ": (2) No such file or directory") { + return imgInfo, ErrImageNotFound{imageName, err} + } + return imgInfo, err } - featueNames := make([]string, 0) - for _, f := range(features) { - featueNames = append(featueNames, f.GetName()) + err = json.Unmarshal(stdout, &imgInfo) + if err != nil { + klog.Errorf(util.Log(ctx, "failed to parse JSON output of image info (%s): (%s)"), + poolName+"/"+imageName, err) + return imgInfo, fmt.Errorf("unmarshal failed: %+v. raw buffer response: %s", + err, string(stdout)) } - rv.ImageFeatures = strings.Join(featueNames, ",") - return nil + return imgInfo, nil } // snapInfo strongly typed JSON spec for snap ls rbd output