Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rbd: use ImageID where possible, add ParentID to prevent confusion #4273

Open
wants to merge 5 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions internal/rbd/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,9 +1017,10 @@ func cleanupRBDImage(ctx context.Context,
}
}

// Deleting rbd image
// Deleting rbd image, it isn't a failure if the image was deleted already.
log.DebugLog(ctx, "deleting image %s", rbdVol.RbdImageName)
if err = rbdVol.deleteImage(ctx); err != nil {
err = rbdVol.deleteImage(ctx)
if err != nil && !errors.Is(err, librbd.ErrNotFound) {
log.ErrorLog(ctx, "failed to delete rbd image: %s with error: %v",
rbdVol, err)

Expand Down Expand Up @@ -1173,6 +1174,7 @@ func (cs *ControllerServer) CreateSnapshot(

// Update the metadata on snapshot not on the original image
rbdVol.RbdImageName = rbdSnap.RbdSnapName
rbdVol.ImageID = vol.ImageID
rbdVol.ClusterName = cs.ClusterName

defer func() {
Expand Down
36 changes: 31 additions & 5 deletions internal/rbd/rbd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ type rbdImage struct {
NamePrefix string
// ParentName represents the parent image name of the image.
ParentName string
// ParentID represents the parent image ID of the image.
ParentID string
// Parent Pool is the pool that contains the parent image.
ParentPool string
// Cluster name
Expand Down Expand Up @@ -505,7 +507,14 @@ func (ri *rbdImage) open() (*librbd.Image, error) {
return nil, err
}

image, err := librbd.OpenImage(ri.ioctx, ri.RbdImageName, librbd.NoSnapshot)
var image *librbd.Image

// try to open by id, that works for images in trash too
if ri.ImageID != "" {
image, err = librbd.OpenImageById(ri.ioctx, ri.ImageID, librbd.NoSnapshot)
} else {
image, err = librbd.OpenImage(ri.ioctx, ri.RbdImageName, librbd.NoSnapshot)
}
if err != nil {
if errors.Is(err, librbd.ErrNotFound) {
err = util.JoinErrors(ErrImageNotFound, err)
Expand Down Expand Up @@ -704,6 +713,7 @@ func (ri *rbdImage) getCloneDepth(ctx context.Context) (uint, error) {
vol.Pool = ri.Pool
vol.Monitors = ri.Monitors
vol.RbdImageName = ri.RbdImageName
vol.ImageID = ri.ImageID
vol.RadosNamespace = ri.RadosNamespace
vol.conn = ri.conn.Copy()

Expand Down Expand Up @@ -736,6 +746,7 @@ func (ri *rbdImage) getCloneDepth(ctx context.Context) (uint, error) {
depth++
}
vol.RbdImageName = vol.ParentName
vol.ImageID = vol.ParentID
vol.Pool = vol.ParentPool
}
}
Expand Down Expand Up @@ -830,7 +841,9 @@ func (ri *rbdImage) flattenRbdImage(
_, err = ta.AddFlatten(admin.NewImageSpec(ri.Pool, ri.RadosNamespace, ri.RbdImageName))
rbdCephMgrSupported := isCephMgrSupported(ctx, ri.ClusterID, err)
if rbdCephMgrSupported {
if err != nil {
// it is not possible to flatten an image that is in trash (ErrNotFoun)
switch {
case err != nil && !errors.Is(err, librbd.ErrNotFound):
// discard flattening error if the image does not have any parent
rbdFlattenNoParent := fmt.Sprintf("Image %s/%s does not have a parent", ri.Pool, ri.RbdImageName)
if strings.Contains(err.Error(), rbdFlattenNoParent) {
Expand All @@ -839,11 +852,11 @@ func (ri *rbdImage) flattenRbdImage(
log.ErrorLog(ctx, "failed to add task flatten for %s : %v", ri, err)

return err
}
if forceFlatten || depth >= hardlimit {
case forceFlatten || depth >= hardlimit:
return fmt.Errorf("%w: flatten is in progress for image %s", ErrFlattenInProgress, ri.RbdImageName)
default:
log.DebugLog(ctx, "successfully added task to flatten image %q", ri)
}
log.DebugLog(ctx, "successfully added task to flatten image %q", ri)
}
if !rbdCephMgrSupported {
log.ErrorLog(
Expand Down Expand Up @@ -875,6 +888,9 @@ func (ri *rbdImage) getParentName() (string, error) {
return "", err
}

ri.ParentName = parentInfo.Image.ImageName
ri.ParentID = parentInfo.Image.ImageID

return parentInfo.Image.ImageName, nil
}

Expand Down Expand Up @@ -1588,6 +1604,11 @@ func (ri *rbdImage) getImageInfo() error {
// TODO: can rv.VolSize not be a uint64? Or initialize it to -1?
ri.VolSize = int64(imageInfo.Size)

ri.ImageID, err = image.GetId()
if err != nil {
return err
}

features, err := image.GetFeatures()
if err != nil {
return err
Expand All @@ -1601,11 +1622,13 @@ func (ri *rbdImage) getImageInfo() error {
// the parent is an error or not.
if errors.Is(err, librbd.ErrNotFound) {
ri.ParentName = ""
ri.ParentID = ""
} else {
return err
}
} else {
ri.ParentName = parentInfo.Image.ImageName
ri.ParentID = parentInfo.Image.ImageID
ri.ParentPool = parentInfo.Image.PoolName
}
// Get image creation time
Expand Down Expand Up @@ -1636,6 +1659,7 @@ func (ri *rbdImage) getParent() (*rbdImage, error) {
parentImage.Pool = ri.ParentPool
parentImage.RadosNamespace = ri.RadosNamespace
parentImage.RbdImageName = ri.ParentName
parentImage.ImageID = ri.ParentID

err = parentImage.getImageInfo()
if err != nil {
Expand Down Expand Up @@ -1692,6 +1716,7 @@ type rbdImageMetadataStash struct {
Pool string `json:"pool"`
RadosNamespace string `json:"radosNamespace"`
ImageName string `json:"image"`
ImageID string `json:"imageID"`
UnmapOptions string `json:"unmapOptions"`
NbdAccess bool `json:"accessType"`
Encrypted bool `json:"encrypted"`
Expand Down Expand Up @@ -1721,6 +1746,7 @@ func stashRBDImageMetadata(volOptions *rbdVolume, metaDataPath string) error {
Pool: volOptions.Pool,
RadosNamespace: volOptions.RadosNamespace,
ImageName: volOptions.RbdImageName,
ImageID: volOptions.ImageID,
Encrypted: volOptions.isBlockEncrypted(),
UnmapOptions: volOptions.UnmapOptions,
}
Expand Down
12 changes: 11 additions & 1 deletion internal/rbd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,17 @@ func generateVolFromSnap(rbdSnap *rbdSnapshot) *rbdVolume {
vol.JournalPool = rbdSnap.JournalPool
vol.RadosNamespace = rbdSnap.RadosNamespace
vol.RbdImageName = rbdSnap.RbdSnapName
vol.ImageID = rbdSnap.ImageID
vol.ParentName = rbdSnap.ParentName
vol.ParentID = rbdSnap.ParentID

// /!\ WARNING /!\
//
// Do not set the ImageID to the ID of the snapshot, a new image will
// be created based on the returned rbdVolume. If the ImageID is set to
// the ID of the snapshot, accessing the new image by ID will actually
// access the snapshot!
// vol.ImageID = rbdSnap.ImageID

// copyEncryptionConfig cannot be used here because the volume and the
// snapshot will have the same volumeID which cases the panic in
// copyEncryptionConfig function.
Expand Down