Skip to content

Commit b0994a5

Browse files
nixpanicmergify[bot]
authored andcommitted
rbd: split mirror functions off into rbdMrror type
Signed-off-by: Niels de Vos <ndevos@ibm.com>
1 parent af4431f commit b0994a5

File tree

3 files changed

+63
-43
lines changed

3 files changed

+63
-43
lines changed

internal/rbd/controllerserver.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,14 @@ func (cs *ControllerServer) DeleteVolume(
10241024
func cleanupRBDImage(ctx context.Context,
10251025
rbdVol *rbdVolume, cr *util.Credentials,
10261026
) (*csi.DeleteVolumeResponse, error) {
1027-
info, err := rbdVol.GetMirroringInfo(ctx)
1027+
rm, err := rbdVol.ToMirror()
1028+
if err != nil {
1029+
log.ErrorLog(ctx, err.Error())
1030+
1031+
return nil, status.Error(codes.Internal, err.Error())
1032+
}
1033+
1034+
info, err := rm.GetMirroringInfo(ctx)
10281035
if err != nil {
10291036
log.ErrorLog(ctx, err.Error())
10301037

@@ -1043,7 +1050,7 @@ func cleanupRBDImage(ctx context.Context,
10431050
// the image on all the remote (secondary) clusters will get
10441051
// auto-deleted. This helps in garbage collecting the OMAP, PVC and PV
10451052
// objects after failback operation.
1046-
sts, rErr := rbdVol.GetGlobalMirroringStatus(ctx)
1053+
sts, rErr := rm.GetGlobalMirroringStatus(ctx)
10471054
if rErr != nil {
10481055
return nil, status.Error(codes.Internal, rErr.Error())
10491056
}

internal/rbd/group.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,3 @@ func (rv *rbdVolume) GetVolumeGroupID(ctx context.Context, resolver types.Volume
101101

102102
return resolver.MakeVolumeGroupID(ctx, info.PoolID, info.Name)
103103
}
104-
105-
func (rv *rbdVolume) ToMirror() (types.Mirror, error) {
106-
return rv, nil
107-
}

internal/rbd/mirror.go

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,82 +66,99 @@ func (rv *rbdVolume) HandleParentImageExistence(
6666
if err != nil {
6767
return fmt.Errorf("failed to get parent of image %s: %w", rv, err)
6868
}
69-
parentMirroringInfo, err := parent.GetMirroringInfo(ctx)
69+
70+
pm, err := parent.ToMirror()
71+
if err != nil {
72+
return fmt.Errorf("failed to convert parent image %s to mirror type: %w", parent, err)
73+
}
74+
75+
parentMirroringInfo, err := pm.GetMirroringInfo(ctx)
7076
if err != nil {
7177
return fmt.Errorf(
7278
"failed to get mirroring info of parent %q of image %q: %w",
73-
parent, rv, err)
79+
pm, rv, err)
7480
}
7581
if parentMirroringInfo.GetState() != librbd.MirrorImageEnabled.String() {
7682
return fmt.Errorf("%w: failed to enable mirroring on image %q: "+
7783
"parent image %q is not enabled for mirroring",
78-
rbderrors.ErrFailedPrecondition, rv, parent)
84+
rbderrors.ErrFailedPrecondition, rv, pm)
7985
}
8086

8187
return nil
8288
}
8389

90+
// rbdMirror is an extended rbdImage type that implements the types.Mirror interface.
91+
type rbdMirror struct {
92+
rbdImage
93+
}
94+
8495
// check that rbdVolume implements the types.Mirror interface.
85-
var _ types.Mirror = &rbdVolume{}
96+
var _ types.Mirror = &rbdMirror{}
97+
98+
func (ri *rbdImage) ToMirror() (types.Mirror, error) {
99+
return &rbdMirror{
100+
rbdImage: *ri,
101+
}, nil
102+
}
86103

87104
// EnableMirroring enables mirroring on an image.
88-
func (ri *rbdImage) EnableMirroring(_ context.Context, mode librbd.ImageMirrorMode) error {
89-
image, err := ri.open()
105+
func (rm *rbdMirror) EnableMirroring(_ context.Context, mode librbd.ImageMirrorMode) error {
106+
image, err := rm.open()
90107
if err != nil {
91-
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
108+
return fmt.Errorf("failed to open image %q with error: %w", rm, err)
92109
}
93110
defer image.Close()
94111

95112
err = image.MirrorEnable(mode)
96113
if err != nil {
97-
return fmt.Errorf("failed to enable mirroring on %q with error: %w", ri, err)
114+
return fmt.Errorf("failed to enable mirroring on %q with error: %w", rm, err)
98115
}
99116

100117
return nil
101118
}
102119

103120
// DisableMirroring disables mirroring on an image.
104-
func (ri *rbdImage) DisableMirroring(_ context.Context, force bool) error {
105-
image, err := ri.open()
121+
func (rm *rbdMirror) DisableMirroring(_ context.Context, force bool) error {
122+
image, err := rm.open()
106123
if err != nil {
107-
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
124+
return fmt.Errorf("failed to open image %q with error: %w", rm, err)
108125
}
109126
defer image.Close()
110127

111128
err = image.MirrorDisable(force)
112129
if err != nil {
113-
return fmt.Errorf("failed to disable mirroring on %q with error: %w", ri, err)
130+
return fmt.Errorf("failed to disable mirroring on %q with error: %w", rm, err)
114131
}
115132

116133
return nil
117134
}
118135

119136
// GetMirroringInfo gets mirroring information of an image.
120-
func (ri *rbdImage) GetMirroringInfo(_ context.Context) (types.MirrorInfo, error) {
121-
image, err := ri.open()
137+
func (rm *rbdMirror) GetMirroringInfo(_ context.Context) (types.MirrorInfo, error) {
138+
image, err := rm.open()
122139
if err != nil {
123-
return nil, fmt.Errorf("failed to open image %q with error: %w", ri, err)
140+
return nil, fmt.Errorf("failed to open image %q with error: %w", rm, err)
124141
}
125142
defer image.Close()
126143

127144
info, err := image.GetMirrorImageInfo()
128145
if err != nil {
129-
return nil, fmt.Errorf("failed to get mirroring info of %q with error: %w", ri, err)
146+
return nil, fmt.Errorf("failed to get mirroring info of %q with error: %w", rm, err)
130147
}
131148

132149
return ImageStatus{MirrorImageInfo: info}, nil
133150
}
134151

135152
// Promote promotes image to primary.
136-
func (ri *rbdImage) Promote(_ context.Context, force bool) error {
137-
image, err := ri.open()
153+
func (rm *rbdMirror) Promote(_ context.Context, force bool) error {
154+
image, err := rm.open()
138155
if err != nil {
139-
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
156+
return fmt.Errorf("failed to open image %q with error: %w", rm, err)
140157
}
141158
defer image.Close()
142159
err = image.MirrorPromote(force)
143160
if err != nil {
144-
return fmt.Errorf("failed to promote image %q with error: %w", ri, err)
161+
return fmt.Errorf("failed to promote image %q with error: %w", rm, err)
145162
}
146163

147164
return nil
@@ -150,13 +167,13 @@ func (ri *rbdImage) Promote(_ context.Context, force bool) error {
150167
// ForcePromote promotes image to primary with force option with 2 minutes
151168
// timeout. If there is no response within 2 minutes,the rbd CLI process will be
152169
// killed and an error is returned.
153-
func (rv *rbdVolume) ForcePromote(ctx context.Context, cr *util.Credentials) error {
170+
func (rm *rbdMirror) ForcePromote(ctx context.Context, cr *util.Credentials) error {
154171
promoteArgs := []string{
155172
"mirror", "image", "promote",
156-
rv.String(),
173+
rm.String(),
157174
"--force",
158175
"--id", cr.ID,
159-
"-m", rv.Monitors,
176+
"-m", rm.Monitors,
160177
"--keyfile=" + cr.KeyFile,
161178
}
162179
_, stderr, err := util.ExecCommandWithTimeout(
@@ -167,41 +184,41 @@ func (rv *rbdVolume) ForcePromote(ctx context.Context, cr *util.Credentials) err
167184
promoteArgs...,
168185
)
169186
if err != nil {
170-
return fmt.Errorf("failed to promote image %q with error: %w", rv, err)
187+
return fmt.Errorf("failed to promote image %q with error: %w", rm, err)
171188
}
172189

173190
if stderr != "" {
174-
return fmt.Errorf("failed to promote image %q with stderror: %s", rv, stderr)
191+
return fmt.Errorf("failed to promote image %q with stderror: %s", rm, stderr)
175192
}
176193

177194
return nil
178195
}
179196

180197
// Demote demotes image to secondary.
181-
func (ri *rbdImage) Demote(_ context.Context) error {
182-
image, err := ri.open()
198+
func (rm *rbdMirror) Demote(_ context.Context) error {
199+
image, err := rm.open()
183200
if err != nil {
184-
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
201+
return fmt.Errorf("failed to open image %q with error: %w", rm, err)
185202
}
186203
defer image.Close()
187204
err = image.MirrorDemote()
188205
if err != nil {
189-
return fmt.Errorf("failed to demote image %q with error: %w", ri, err)
206+
return fmt.Errorf("failed to demote image %q with error: %w", rm, err)
190207
}
191208

192209
return nil
193210
}
194211

195212
// Resync resync image to correct the split-brain.
196-
func (ri *rbdImage) Resync(_ context.Context) error {
197-
image, err := ri.open()
213+
func (rm *rbdMirror) Resync(_ context.Context) error {
214+
image, err := rm.open()
198215
if err != nil {
199-
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
216+
return fmt.Errorf("failed to open image %q with error: %w", rm, err)
200217
}
201218
defer image.Close()
202219
err = image.MirrorResync()
203220
if err != nil {
204-
return fmt.Errorf("failed to resync image %q with error: %w", ri, err)
221+
return fmt.Errorf("failed to resync image %q with error: %w", rm, err)
205222
}
206223

207224
// If we issued a resync, return a non-final error as image needs to be recreated
@@ -211,15 +228,15 @@ func (ri *rbdImage) Resync(_ context.Context) error {
211228
}
212229

213230
// GetGlobalMirroringStatus get the mirroring status of an image.
214-
func (ri *rbdImage) GetGlobalMirroringStatus(_ context.Context) (types.GlobalStatus, error) {
215-
image, err := ri.open()
231+
func (rm *rbdMirror) GetGlobalMirroringStatus(_ context.Context) (types.GlobalStatus, error) {
232+
image, err := rm.open()
216233
if err != nil {
217-
return nil, fmt.Errorf("failed to open image %q with error: %w", ri, err)
234+
return nil, fmt.Errorf("failed to open image %q with error: %w", rm, err)
218235
}
219236
defer image.Close()
220237
statusInfo, err := image.GetGlobalMirrorStatus()
221238
if err != nil {
222-
return nil, fmt.Errorf("failed to get image mirroring status %q with error: %w", ri, err)
239+
return nil, fmt.Errorf("failed to get image mirroring status %q with error: %w", rm, err)
223240
}
224241

225242
return GlobalMirrorStatus{GlobalMirrorImageStatus: statusInfo}, nil

0 commit comments

Comments
 (0)