Skip to content

Commit

Permalink
rbd: add listsnapshots function
Browse files Browse the repository at this point in the history
added listsnapshots function for an
rbd image to list all the snapshots
created from an rbd images, This will
list the snapshots which are in trash also.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
  • Loading branch information
Madhu-1 committed Jul 2, 2020
1 parent f711d15 commit ed63d01
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/rbd/rbd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1149,3 +1149,45 @@ func (rv *rbdVolume) ensureEncryptionMetadataSet(status string) error {

return nil
}

type snapshotInfo struct {
ID int `json:"id"`
Name string `json:"name"`
Size int64 `json:"size"`
Protected string `json:"protected"`
Timestamp string `json:"timestamp"`
Namespace struct {
Type string `json:"type"`
OriginalName string `json:"original_name"`
} `json:"namespace"`
}

// TODO: use go-ceph once https://github.com/ceph/go-ceph/issues/300 is available in a release.
func (rv *rbdVolume) listSnapshots(ctx context.Context, cr *util.Credentials) ([]snapshotInfo, error) {
// rbd snap ls <image> --pool=<pool-name> --all --format=json
var snapInfo []snapshotInfo
stdout, stderr, err := util.ExecCommand("rbd",
"-m", rv.Monitors,
"--id", cr.ID,
"--keyfile="+cr.KeyFile,
"-c", util.CephConfigPath,
"--format="+"json",
"snap",
"ls",
"--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+
": (2) No such file or directory") {
return snapInfo, ErrImageNotFound{rv.String(), err}
}
return snapInfo, err
}

err = json.Unmarshal(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, nil
}

0 comments on commit ed63d01

Please sign in to comment.