cephfs: add helper function to getVolumeOptions

added helper function to extract basic
details from the parameters related to volume
options.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna 2024-02-19 09:38:28 +01:00 committed by mergify[bot]
parent 6a4c45deeb
commit 86bf74bb5c

View File

@ -209,10 +209,32 @@ func fmtBackingSnapshotOptionMismatch(optName, expected, actual string) error {
optName, actual, expected) optName, actual, expected)
} }
// getVolumeOptions validates the basic required basic options provided in the
// volume parameters and extract the volumeOptions from volume parameters.
// It contains the following checks:
// - clusterID must be set
// - monitors must be set
// - fsName must be set.
func getVolumeOptions(vo map[string]string) (*VolumeOptions, error) {
opts := VolumeOptions{}
clusterData, err := GetClusterInformation(vo)
if err != nil {
return nil, err
}
opts.ClusterID = clusterData.ClusterID
opts.Monitors = strings.Join(clusterData.Monitors, ",")
opts.SubvolumeGroup = clusterData.CephFS.SubvolumeGroup
if err = extractOption(&opts.FsName, "fsName", vo); err != nil {
return nil, err
}
return &opts, nil
}
// NewVolumeOptions generates a new instance of volumeOptions from the provided // NewVolumeOptions generates a new instance of volumeOptions from the provided
// CSI request parameters. // CSI request parameters.
//
//nolint:gocyclo,cyclop // TODO: reduce complexity
func NewVolumeOptions( func NewVolumeOptions(
ctx context.Context, ctx context.Context,
requestName, requestName,
@ -222,20 +244,17 @@ func NewVolumeOptions(
cr *util.Credentials, cr *util.Credentials,
) (*VolumeOptions, error) { ) (*VolumeOptions, error) {
var ( var (
opts VolumeOptions opts *VolumeOptions
backingSnapshotBool string backingSnapshotBool string
err error err error
) )
volOptions := req.GetParameters() volOptions := req.GetParameters()
clusterData, err := GetClusterInformation(volOptions) opts, err = getVolumeOptions(volOptions)
if err != nil { if err != nil {
return nil, err return nil, err
} }
opts.ClusterID = clusterData.ClusterID
opts.Monitors = strings.Join(clusterData.Monitors, ",")
opts.SubvolumeGroup = clusterData.CephFS.SubvolumeGroup
opts.Owner = k8s.GetOwner(volOptions) opts.Owner = k8s.GetOwner(volOptions)
opts.BackingSnapshot = IsShallowVolumeSupported(req) opts.BackingSnapshot = IsShallowVolumeSupported(req)
@ -247,10 +266,6 @@ func NewVolumeOptions(
return nil, err return nil, err
} }
if err = extractOption(&opts.FsName, "fsName", volOptions); err != nil {
return nil, err
}
if err = extractOptionalOption(&opts.KernelMountOptions, "kernelMountOptions", volOptions); err != nil { if err = extractOptionalOption(&opts.KernelMountOptions, "kernelMountOptions", volOptions); err != nil {
return nil, err return nil, err
} }
@ -323,7 +338,7 @@ func NewVolumeOptions(
} }
} }
return &opts, nil return opts, nil
} }
// IsShallowVolumeSupported returns true only for ReadOnly volume requests // IsShallowVolumeSupported returns true only for ReadOnly volume requests