rbd: introduce appendDeviceTypeAndOptions()

Factor out --device-type and --options formatting.

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
This commit is contained in:
Ilya Dryomov 2020-08-21 19:00:06 +02:00 committed by mergify[bot]
parent d3f31187fc
commit c2493686b7

View File

@ -208,6 +208,20 @@ func attachRBDImage(ctx context.Context, volOptions *rbdVolume, cr *util.Credent
return devicePath, err return devicePath, err
} }
func appendDeviceTypeAndOptions(cmdArgs []string, isNbd bool, userOptions string) []string {
accessType := accessTypeKRbd
if isNbd {
accessType = accessTypeNbd
}
cmdArgs = append(cmdArgs, "--device-type", accessType)
if userOptions != "" {
cmdArgs = append(cmdArgs, "--options", userOptions)
}
return cmdArgs
}
func createPath(ctx context.Context, volOpt *rbdVolume, cr *util.Credentials) (string, error) { func createPath(ctx context.Context, volOpt *rbdVolume, cr *util.Credentials) (string, error) {
isNbd := false isNbd := false
imagePath := volOpt.String() imagePath := volOpt.String()
@ -222,22 +236,15 @@ func createPath(ctx context.Context, volOpt *rbdVolume, cr *util.Credentials) (s
} }
// Choose access protocol // Choose access protocol
accessType := accessTypeKRbd
if volOpt.Mounter == rbdTonbd && hasNBD { if volOpt.Mounter == rbdTonbd && hasNBD {
isNbd = true isNbd = true
accessType = accessTypeNbd
} }
// Update options with device type selection mapArgs = appendDeviceTypeAndOptions(mapArgs, isNbd, volOpt.MapOptions)
mapArgs = append(mapArgs, "--device-type", accessType)
if volOpt.readOnly { if volOpt.readOnly {
mapArgs = append(mapArgs, "--read-only") mapArgs = append(mapArgs, "--read-only")
} }
if volOpt.MapOptions != "" {
mapArgs = append(mapArgs, "--options", volOpt.MapOptions)
}
// Execute map // Execute map
stdout, stderr, err := util.ExecCommand(ctx, rbd, mapArgs...) stdout, stderr, err := util.ExecCommand(ctx, rbd, mapArgs...)
if err != nil { if err != nil {
@ -310,14 +317,8 @@ func detachRBDImageOrDeviceSpec(ctx context.Context, imageOrDeviceSpec string, i
} }
} }
accessType := accessTypeKRbd unmapArgs := []string{"unmap", imageOrDeviceSpec}
if isNbd { unmapArgs = appendDeviceTypeAndOptions(unmapArgs, isNbd, unmapOptions)
accessType = accessTypeNbd
}
unmapArgs := []string{"unmap", "--device-type", accessType, imageOrDeviceSpec}
if unmapOptions != "" {
unmapArgs = append(unmapArgs, "--options", unmapOptions)
}
_, stderr, err := util.ExecCommand(ctx, rbd, unmapArgs...) _, stderr, err := util.ExecCommand(ctx, rbd, unmapArgs...)
if err != nil { if err != nil {