Skip to content

Commit

Permalink
change RBDInfo to include RBD Image IDs
Browse files Browse the repository at this point in the history
This commit makes the following changes:

- RBDInfo struct is renamed to RBDImageInfo.
- RBDImageInfo's Parent field becomes optional.
- RBDImageInfo has an ID field.

Signed-off-by: Ryotaro Banno <[email protected]>
  • Loading branch information
ushitora-anqou committed Dec 12, 2024
1 parent d66511b commit 51d3960
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 34 deletions.
15 changes: 10 additions & 5 deletions internal/ceph/ceph.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import (
"time"
)

type RBDInfo struct {
ParentPool string
ParentImage string
ParentSnap string
type RBDImageInfo struct {
ID string `json:"id"`
Parent *RBDImageInfoParent `json:"parent,omitempty"`
}

type RBDImageInfoParent struct {
Pool string `json:"pool"`
Image string `json:"image"`
Snapshot string `json:"snapshot"`
}

type RBDTimeStamp struct {
Expand All @@ -35,7 +40,7 @@ type RBDSnapshot struct {

type CephCmd interface {
RBDClone(pool, srcImage, srcSnap, dstImage, features string) error
RBDInfo(pool, image string) (*RBDInfo, error)
RBDInfo(pool, image string) (*RBDImageInfo, error)
RBDLs(pool string) ([]string, error)
RBDRm(pool, image string) error
RBDSnapCreate(pool, image, snap string) error
Expand Down
28 changes: 4 additions & 24 deletions internal/ceph/rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ import (
"fmt"
)

type rbdInfoParentJS struct {
Pool string `json:"pool"`
Image string `json:"image"`
Snapshot string `json:"snapshot"`
}

type rbdInfoJS struct {
Parent *rbdInfoParentJS `json:"parent,omitempty"`
}

// RBDClone clones an RBD image from a snapshot with specified features.
func (c *cephCmdImpl) RBDClone(pool, srcImage, srcSnap, dstImage, features string) error {
src := fmt.Sprintf("%s/%s@%s", pool, srcImage, srcSnap)
Expand All @@ -32,28 +22,18 @@ func (c *cephCmdImpl) RBDClone(pool, srcImage, srcSnap, dstImage, features strin
}

// RBDInfo gets information about an RBD image.
func (c *cephCmdImpl) RBDInfo(pool, image string) (*RBDInfo, error) {
func (c *cephCmdImpl) RBDInfo(pool, image string) (*RBDImageInfo, error) {
out, err := c.command.execute("rbd", "info", "--format", "json", fmt.Sprintf("%s/%s", pool, image))
if err != nil {
return nil, fmt.Errorf("failed to get RBD info: %v", err)
}

infoJS := &rbdInfoJS{}
err = json.Unmarshal(out, infoJS)
imageInfo := &RBDImageInfo{}
err = json.Unmarshal(out, imageInfo)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal RBD info: %v", err)
}

if infoJS.Parent == nil {
return nil, fmt.Errorf("RBD info parent field is empty")
}
info := &RBDInfo{
ParentPool: infoJS.Parent.Pool,
ParentImage: infoJS.Parent.Image,
ParentSnap: infoJS.Parent.Snapshot,
}

return info, nil
return imageInfo, nil
}

// RBDLs lists RBD images in a pool.
Expand Down
6 changes: 3 additions & 3 deletions internal/ceph/rbd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ var _ = Describe("CephCmd.RBDInfo", func() {
cmd := mockedCephCmd(m)
info, err := cmd.RBDInfo("pool", "image")
Expect(err).ToNot(HaveOccurred())
Expect(info.ParentPool).To(Equal("pool"))
Expect(info.ParentImage).To(Equal("csi-vol-39ca122a-88e1-44b6-aa2b-cae64fb383db"))
Expect(info.ParentSnap).To(Equal("test-snap"))
Expect(info.Parent.Pool).To(Equal("pool"))
Expect(info.Parent.Image).To(Equal("csi-vol-39ca122a-88e1-44b6-aa2b-cae64fb383db"))
Expect(info.Parent.Snapshot).To(Equal("test-snap"))
})

It("should return an error, if the command failed", func() {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/internal/testutil/fake_rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (f *fakeRBD) RBDClone(pool, srcImage, srcSnap, dstImage, features string) e
return nil
}

func (f *fakeRBD) RBDInfo(pool, image string) (*ceph.RBDInfo, error) {
func (f *fakeRBD) RBDInfo(pool, image string) (*ceph.RBDImageInfo, error) {
return nil, nil
}

Expand Down
5 changes: 4 additions & 1 deletion internal/controller/mantlerestore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,11 @@ func (r *MantleRestoreReconciler) cloneImageFromBackup(ctx context.Context, rest
if err != nil {
return fmt.Errorf("failed to get RBD info: %v", err)
}
if info.Parent == nil {
return fmt.Errorf("failed to get RBD info: parent field is empty")
}

if info.ParentPool == restore.Status.Pool && info.ParentImage == bkImage && info.ParentSnap == backup.Name {
if info.Parent.Pool == restore.Status.Pool && info.Parent.Image == bkImage && info.Parent.Snapshot == backup.Name {
logger.Info("image already exists", "image", r.restoringRBDImageName(restore))
return nil
} else {
Expand Down

0 comments on commit 51d3960

Please sign in to comment.