Skip to content

Commit

Permalink
rbd/go-ceph: drop rbdStatus() in favour of InUse() to get watchers
Browse files Browse the repository at this point in the history
Signed-off-by: Niels de Vos <[email protected]>
  • Loading branch information
nixpanic committed Dec 4, 2019
1 parent e473276 commit cc3c927
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 deletions.
4 changes: 2 additions & 2 deletions pkg/rbd/rbd_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ func waitForrbdImage(ctx context.Context, backoff wait.Backoff, volOptions *rbdV
imagePath := fmt.Sprintf("%s/%s", volOptions.Pool, image)

err := wait.ExponentialBackoff(backoff, func() (bool, error) {
used, rbdOutput, err := rbdStatus(ctx, volOptions, cr)
used, err := volOptions.InUse(cr)
if err != nil {
return false, fmt.Errorf("fail to check rbd image status with: (%v), rbd output: (%s)", err, rbdOutput)
return false, fmt.Errorf("fail to check rbd image status with: (%v)", err)
}
if (volOptions.DisableInUseChecks) && (used) {
klog.V(2).Info(util.Log(ctx, "valid multi-node attach requested, ignoring watcher in-use result"))
Expand Down
38 changes: 10 additions & 28 deletions pkg/rbd/rbd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,38 +167,20 @@ func (rv *rbdVolume) getIoctx(cr *util.Credentials) (*rados.IOContext, error) {
return ioctx, nil
}

// rbdStatus checks if there is watcher on the image.
// It returns true if there is a watcher on the image, otherwise returns false.
func rbdStatus(ctx context.Context, pOpts *rbdVolume, cr *util.Credentials) (bool, string, error) {
var output string
var cmd []byte

image := pOpts.RbdImageName

klog.V(4).Infof(util.Log(ctx, "rbd: status %s using mon %s, pool %s"), image, pOpts.Monitors, pOpts.Pool)
args := []string{"status", image, "--pool", pOpts.Pool, "-m", pOpts.Monitors, "--id", cr.ID, "--keyfile=" + cr.KeyFile}
cmd, err := execCommand("rbd", args)
output = string(cmd)

if err, ok := err.(*exec.Error); ok {
if err.Err == exec.ErrNotFound {
klog.Errorf(util.Log(ctx, "rbd cmd not found"))
// fail fast if command not found
return false, output, err
}
// InUse returns true in case the RBD image has watchers
func (rv *rbdVolume) InUse(cr *util.Credentials) (bool, error) {
ioctx, err := rv.getIoctx(cr)
if err != nil {
return true, err
}

// If command never succeed, returns its last error.
image := librbd.GetImage(ioctx, rv.RbdImageName)
watchers, err := image.HasWatchers()
if err != nil {
return false, output, err
return true, err
}

if strings.Contains(output, imageWatcherStr) {
klog.V(4).Infof(util.Log(ctx, "rbd: watchers on %s: %s"), image, output)
return true, output, nil
}
klog.Warningf(util.Log(ctx, "rbd: no watchers on %s"), image)
return false, output, nil
return watchers != 0, nil
}

// rbdManagerTaskDelete adds a ceph manager task to delete an rbd image, thus deleting
Expand Down Expand Up @@ -232,7 +214,7 @@ func rbdManagerTaskDeleteImage(ctx context.Context, pOpts *rbdVolume, cr *util.C

// deleteImage deletes a ceph image with provision and volume options.
func deleteImage(ctx context.Context, pOpts *rbdVolume, cr *util.Credentials) error {
found, _, err := rbdStatus(ctx, pOpts, cr)
found, err := pOpts.InUse(cr)
if err != nil {
return err
}
Expand Down

0 comments on commit cc3c927

Please sign in to comment.