2018-03-05 11:59:47 +00:00
|
|
|
/*
|
2019-04-03 08:46:15 +00:00
|
|
|
Copyright 2018 The Ceph-CSI Authors.
|
2018-03-05 11:59:47 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cephfs
|
|
|
|
|
2018-03-22 13:11:51 +00:00
|
|
|
import (
|
2019-08-22 17:19:06 +00:00
|
|
|
"context"
|
2020-06-25 11:30:04 +00:00
|
|
|
"errors"
|
2018-03-22 13:11:51 +00:00
|
|
|
"fmt"
|
2018-04-13 12:21:15 +00:00
|
|
|
"strconv"
|
2020-05-14 13:20:11 +00:00
|
|
|
"strings"
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2020-01-24 16:26:56 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
2019-06-11 12:40:31 +00:00
|
|
|
|
2020-04-17 09:23:49 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2018-03-22 13:11:51 +00:00
|
|
|
)
|
2018-03-05 11:59:47 +00:00
|
|
|
|
|
|
|
type volumeOptions struct {
|
2020-01-24 16:26:56 +00:00
|
|
|
TopologyPools *[]util.TopologyConstrainedPool
|
|
|
|
TopologyRequirement *csi.TopologyRequirement
|
|
|
|
Topology map[string]string
|
|
|
|
RequestName string
|
|
|
|
NamePrefix string
|
|
|
|
Size int64
|
|
|
|
ClusterID string
|
|
|
|
FsName string
|
|
|
|
FscID int64
|
|
|
|
MetadataPool string
|
|
|
|
Monitors string `json:"monitors"`
|
|
|
|
Pool string `json:"pool"`
|
|
|
|
RootPath string `json:"rootPath"`
|
|
|
|
Mounter string `json:"mounter"`
|
|
|
|
ProvisionVolume bool `json:"provisionVolume"`
|
|
|
|
KernelMountOptions string `json:"kernelMountOptions"`
|
|
|
|
FuseMountOptions string `json:"fuseMountOptions"`
|
2020-04-29 10:03:08 +00:00
|
|
|
SubvolumeGroup string
|
2020-09-10 12:34:48 +00:00
|
|
|
Features []string
|
2020-08-05 10:10:04 +00:00
|
|
|
|
|
|
|
// conn is a connection to the Ceph cluster obtained from a ConnPool
|
|
|
|
conn *util.ClusterConnection
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect a CephFS volume to the Ceph cluster.
|
|
|
|
func (vo *volumeOptions) Connect(cr *util.Credentials) error {
|
|
|
|
if vo.conn != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := &util.ClusterConnection{}
|
|
|
|
if err := conn.Connect(vo.Monitors, cr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vo.conn = conn
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy cleans up the CephFS volume object and closes the connection to the
|
|
|
|
// Ceph cluster in case one was setup.
|
|
|
|
func (vo *volumeOptions) Destroy() {
|
|
|
|
if vo.conn != nil {
|
|
|
|
vo.conn.Destroy()
|
|
|
|
}
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func validateNonEmptyField(field, fieldName string) error {
|
|
|
|
if field == "" {
|
2019-02-04 05:14:37 +00:00
|
|
|
return fmt.Errorf("parameter '%s' cannot be empty", fieldName)
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
func extractOptionalOption(dest *string, optionLabel string, options map[string]string) error {
|
|
|
|
opt, ok := options[optionLabel]
|
|
|
|
if !ok {
|
|
|
|
// Option not found, no error as it is optional
|
|
|
|
return nil
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if err := validateNonEmptyField(opt, optionLabel); err != nil {
|
|
|
|
return err
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
*dest = opt
|
2018-04-13 12:21:15 +00:00
|
|
|
return nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func extractOption(dest *string, optionLabel string, options map[string]string) error {
|
2019-01-16 13:03:38 +00:00
|
|
|
opt, ok := options[optionLabel]
|
|
|
|
if !ok {
|
2019-02-04 05:14:37 +00:00
|
|
|
return fmt.Errorf("missing required field %s", optionLabel)
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
2019-01-16 13:03:38 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if err := validateNonEmptyField(opt, optionLabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:03:38 +00:00
|
|
|
*dest = opt
|
|
|
|
return nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 13:11:51 +00:00
|
|
|
func validateMounter(m string) error {
|
|
|
|
switch m {
|
2019-01-16 13:03:38 +00:00
|
|
|
case volumeMounterFuse:
|
|
|
|
case volumeMounterKernel:
|
2018-03-22 13:11:51 +00:00
|
|
|
default:
|
2019-02-04 05:14:37 +00:00
|
|
|
return fmt.Errorf("unknown mounter '%s'. Valid options are 'fuse' and 'kernel'", m)
|
2018-03-22 13:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
func extractMounter(dest *string, options map[string]string) error {
|
|
|
|
if err := extractOptionalOption(dest, "mounter", options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if *dest != "" {
|
|
|
|
if err := validateMounter(*dest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-14 13:20:11 +00:00
|
|
|
func getClusterInformation(options map[string]string) (*util.ClusterInfo, error) {
|
2019-05-28 19:03:18 +00:00
|
|
|
clusterID, ok := options["clusterID"]
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("clusterID must be set")
|
2020-05-14 13:20:11 +00:00
|
|
|
return nil, err
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateNonEmptyField(clusterID, "clusterID"); err != nil {
|
2020-05-14 13:20:11 +00:00
|
|
|
return nil, err
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 06:19:38 +00:00
|
|
|
monitors, err := util.Mons(util.CsiConfigFile, clusterID)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
err = fmt.Errorf("failed to fetch monitor list using clusterID (%s): %w", clusterID, err)
|
2020-05-14 13:20:11 +00:00
|
|
|
return nil, err
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 06:19:38 +00:00
|
|
|
subvolumeGroup, err := util.CephFSSubvolumeGroup(util.CsiConfigFile, clusterID)
|
2020-04-29 10:03:08 +00:00
|
|
|
if err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
err = fmt.Errorf("failed to fetch subvolumegroup using clusterID (%s): %w", clusterID, err)
|
2020-05-14 13:20:11 +00:00
|
|
|
return nil, err
|
2020-04-29 10:03:08 +00:00
|
|
|
}
|
2020-05-14 13:20:11 +00:00
|
|
|
clusterData := &util.ClusterInfo{
|
|
|
|
ClusterID: clusterID,
|
|
|
|
Monitors: strings.Split(monitors, ","),
|
|
|
|
}
|
|
|
|
clusterData.CephFS.SubvolumeGroup = subvolumeGroup
|
|
|
|
return clusterData, nil
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newVolumeOptions generates a new instance of volumeOptions from the provided
|
2020-07-19 12:21:03 +00:00
|
|
|
// CSI request parameters.
|
2020-01-24 16:26:56 +00:00
|
|
|
func newVolumeOptions(ctx context.Context, requestName string, req *csi.CreateVolumeRequest,
|
2020-10-01 07:10:10 +00:00
|
|
|
cr *util.Credentials) (*volumeOptions, error) {
|
2018-04-13 12:21:15 +00:00
|
|
|
var (
|
2019-01-29 05:49:16 +00:00
|
|
|
opts volumeOptions
|
|
|
|
err error
|
2018-04-13 12:21:15 +00:00
|
|
|
)
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2020-01-24 16:26:56 +00:00
|
|
|
volOptions := req.GetParameters()
|
2020-05-14 13:20:11 +00:00
|
|
|
clusterData, err := getClusterInformation(volOptions)
|
2020-01-24 16:26:56 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-01-21 14:21:03 +00:00
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2020-05-14 13:20:11 +00:00
|
|
|
opts.ClusterID = clusterData.ClusterID
|
|
|
|
opts.Monitors = strings.Join(clusterData.Monitors, ",")
|
|
|
|
opts.SubvolumeGroup = clusterData.CephFS.SubvolumeGroup
|
|
|
|
|
2019-08-05 15:40:48 +00:00
|
|
|
if err = extractOptionalOption(&opts.Pool, "pool", volOptions); err != nil {
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = extractMounter(&opts.Mounter, volOptions); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = extractOption(&opts.FsName, "fsName", volOptions); err != nil {
|
|
|
|
return nil, err
|
2018-03-09 16:05:19 +00:00
|
|
|
}
|
2019-01-29 05:49:16 +00:00
|
|
|
|
2019-08-28 09:47:12 +00:00
|
|
|
if err = extractOptionalOption(&opts.KernelMountOptions, "kernelMountOptions", volOptions); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = extractOptionalOption(&opts.FuseMountOptions, "fuseMountOptions", volOptions); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
opts.RequestName = requestName
|
|
|
|
|
2020-08-05 10:10:04 +00:00
|
|
|
err = opts.Connect(cr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-02 08:14:17 +00:00
|
|
|
opts.FscID, err = opts.getFscID(ctx)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
2018-03-09 16:05:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2020-10-02 11:18:24 +00:00
|
|
|
opts.MetadataPool, err = opts.getMetadataPool(ctx)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-24 16:26:56 +00:00
|
|
|
// store topology information from the request
|
|
|
|
opts.TopologyPools, opts.TopologyRequirement, err = util.GetTopologyFromRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: we need an API to fetch subvolume attributes (size/datapool and others), based
|
|
|
|
// on which we can evaluate which topology this belongs to.
|
|
|
|
// CephFS tracker: https://tracker.ceph.com/issues/44277
|
|
|
|
if opts.TopologyPools != nil {
|
|
|
|
return nil, errors.New("topology based provisioning is not supported for CephFS backed volumes")
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
opts.ProvisionVolume = true
|
|
|
|
|
2019-01-29 05:49:16 +00:00
|
|
|
return &opts, nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
// newVolumeOptionsFromVolID generates a new instance of volumeOptions and volumeIdentifier
|
2020-07-19 12:21:03 +00:00
|
|
|
// from the provided CSI VolumeID.
|
2019-08-22 17:19:06 +00:00
|
|
|
func newVolumeOptionsFromVolID(ctx context.Context, volID string, volOpt, secrets map[string]string) (*volumeOptions, *volumeIdentifier, error) {
|
2019-01-29 05:49:16 +00:00
|
|
|
var (
|
2019-05-28 19:03:18 +00:00
|
|
|
vi util.CSIIdentifier
|
|
|
|
volOptions volumeOptions
|
|
|
|
vid volumeIdentifier
|
2020-09-10 12:34:48 +00:00
|
|
|
info Subvolume
|
2019-05-28 19:03:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Decode the VolID first, to detect older volumes or pre-provisioned volumes
|
|
|
|
// before other errors
|
|
|
|
err := vi.DecomposeCSIID(volID)
|
|
|
|
if err != nil {
|
2020-07-10 00:14:39 +00:00
|
|
|
err = fmt.Errorf("error decoding volume ID (%s): %w", volID, err)
|
|
|
|
return nil, nil, util.JoinErrors(ErrInvalidVolID, err)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
volOptions.ClusterID = vi.ClusterID
|
|
|
|
vid.VolumeID = volID
|
|
|
|
volOptions.FscID = vi.LocationID
|
|
|
|
|
2020-08-10 06:19:38 +00:00
|
|
|
if volOptions.Monitors, err = util.Mons(util.CsiConfigFile, vi.ClusterID); err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to fetch monitor list using clusterID (%s): %w", vi.ClusterID, err)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 06:19:38 +00:00
|
|
|
if volOptions.SubvolumeGroup, err = util.CephFSSubvolumeGroup(util.CsiConfigFile, vi.ClusterID); err != nil {
|
2020-06-25 11:30:04 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to fetch subvolumegroup list using clusterID (%s): %w", vi.ClusterID, err)
|
2020-04-29 10:03:08 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 19:29:17 +00:00
|
|
|
cr, err := util.NewAdminCredentials(secrets)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-06-25 19:29:17 +00:00
|
|
|
defer cr.DeleteCredentials()
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2020-08-05 10:10:04 +00:00
|
|
|
err = volOptions.Connect(cr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-10-19 07:10:00 +00:00
|
|
|
// in case of an error, volOptions is not returned, release any
|
|
|
|
// resources that may have been allocated
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
volOptions.Destroy()
|
|
|
|
}
|
|
|
|
}()
|
2020-08-05 10:10:04 +00:00
|
|
|
|
2020-10-02 08:44:40 +00:00
|
|
|
volOptions.FsName, err = volOptions.getFsName(ctx)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-02 11:18:24 +00:00
|
|
|
volOptions.MetadataPool, err = volOptions.getMetadataPool(ctx)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:57:51 +00:00
|
|
|
// Connect to cephfs' default radosNamespace (csi)
|
|
|
|
j, err := volJournal.Connect(volOptions.Monitors, radosNamespace, cr)
|
2020-05-12 21:05:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer j.Destroy()
|
|
|
|
|
|
|
|
imageAttributes, err := j.GetImageAttributes(
|
|
|
|
ctx, volOptions.MetadataPool, vi.ObjectUUID, false)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-01-24 16:26:56 +00:00
|
|
|
volOptions.RequestName = imageAttributes.RequestName
|
|
|
|
vid.FsSubvolName = imageAttributes.ImageName
|
2019-05-28 19:03:18 +00:00
|
|
|
|
|
|
|
if volOpt != nil {
|
2019-08-05 15:40:48 +00:00
|
|
|
if err = extractOptionalOption(&volOptions.Pool, "pool", volOpt); err != nil {
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-28 09:47:12 +00:00
|
|
|
if err = extractOptionalOption(&volOptions.KernelMountOptions, "kernelMountOptions", volOpt); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = extractOptionalOption(&volOptions.FuseMountOptions, "fuseMountOptions", volOpt); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-29 10:03:08 +00:00
|
|
|
if err = extractOptionalOption(&volOptions.SubvolumeGroup, "subvolumeGroup", volOpt); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if err = extractMounter(&volOptions.Mounter, volOpt); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 02:48:46 +00:00
|
|
|
volOptions.ProvisionVolume = true
|
2020-09-10 12:34:48 +00:00
|
|
|
|
|
|
|
info, err = getSubVolumeInfo(ctx, &volOptions, cr, volumeID(vid.FsSubvolName))
|
|
|
|
if err == nil {
|
|
|
|
volOptions.RootPath = info.Path
|
|
|
|
volOptions.Features = info.Features
|
2019-06-08 05:06:03 +00:00
|
|
|
}
|
2020-09-10 12:34:48 +00:00
|
|
|
|
|
|
|
if errors.Is(err, ErrInvalidCommand) {
|
|
|
|
volOptions.RootPath, err = getVolumeRootPathCeph(ctx, &volOptions, cr, volumeID(vid.FsSubvolName))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &volOptions, &vid, err
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 04:55:14 +00:00
|
|
|
// newVolumeOptionsFromMonitorList generates a new instance of volumeOptions and
|
2020-07-19 12:21:03 +00:00
|
|
|
// volumeIdentifier from the provided CSI volume context.
|
2020-07-12 04:55:14 +00:00
|
|
|
func newVolumeOptionsFromMonitorList(volID string, options, secrets map[string]string) (*volumeOptions, *volumeIdentifier, error) {
|
2019-05-28 19:03:18 +00:00
|
|
|
var (
|
|
|
|
opts volumeOptions
|
|
|
|
vid volumeIdentifier
|
2019-01-29 05:49:16 +00:00
|
|
|
provisionVolumeBool string
|
|
|
|
err error
|
|
|
|
)
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2020-07-12 04:55:14 +00:00
|
|
|
// Check if monitors is part of the options
|
2019-05-28 19:03:18 +00:00
|
|
|
if err = extractOption(&opts.Monitors, "monitors", options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if there are mon values in secret and if so override option retrieved monitors from
|
|
|
|
// monitors in the secret
|
2019-06-01 21:26:42 +00:00
|
|
|
mon, err := util.GetMonValFromSecret(secrets)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err == nil && len(mon) > 0 {
|
|
|
|
opts.Monitors = mon
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = extractOption(&provisionVolumeBool, "provisionVolume", options); err != nil {
|
|
|
|
return nil, nil, err
|
2019-01-29 05:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 12:21:15 +00:00
|
|
|
if opts.ProvisionVolume, err = strconv.ParseBool(provisionVolumeBool); err != nil {
|
2020-07-13 03:56:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to parse provisionVolume: %w", err)
|
2018-03-09 16:05:19 +00:00
|
|
|
}
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2018-04-13 12:21:15 +00:00
|
|
|
if opts.ProvisionVolume {
|
2019-05-28 19:03:18 +00:00
|
|
|
if err = extractOption(&opts.Pool, "pool", options); err != nil {
|
|
|
|
return nil, nil, err
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
|
2019-07-10 10:50:04 +00:00
|
|
|
opts.RootPath = getVolumeRootPathCephDeprecated(volumeID(volID))
|
2018-04-13 12:21:15 +00:00
|
|
|
} else {
|
2019-05-28 19:03:18 +00:00
|
|
|
if err = extractOption(&opts.RootPath, "rootPath", options); err != nil {
|
|
|
|
return nil, nil, err
|
2018-04-13 12:21:15 +00:00
|
|
|
}
|
2018-03-22 13:11:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if err = extractMounter(&opts.Mounter, options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vid.FsSubvolName = volID
|
|
|
|
vid.VolumeID = volID
|
|
|
|
|
|
|
|
return &opts, &vid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newVolumeOptionsFromStaticVolume generates a new instance of volumeOptions and
|
|
|
|
// volumeIdentifier from the provided CSI volume context, if the provided context is
|
2020-07-19 12:21:03 +00:00
|
|
|
// detected to be a statically provisioned volume.
|
2019-05-28 19:03:18 +00:00
|
|
|
func newVolumeOptionsFromStaticVolume(volID string, options map[string]string) (*volumeOptions, *volumeIdentifier, error) {
|
|
|
|
var (
|
|
|
|
opts volumeOptions
|
|
|
|
vid volumeIdentifier
|
|
|
|
staticVol bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
val, ok := options["staticVolume"]
|
|
|
|
if !ok {
|
2020-07-10 00:14:39 +00:00
|
|
|
return nil, nil, ErrNonStaticVolume
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if staticVol, err = strconv.ParseBool(val); err != nil {
|
2020-07-13 03:56:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to parse preProvisionedVolume: %w", err)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !staticVol {
|
2020-07-10 00:14:39 +00:00
|
|
|
return nil, nil, ErrNonStaticVolume
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Volume is static, and ProvisionVolume carries bool stating if it was provisioned, hence
|
|
|
|
// store NOT of static boolean
|
|
|
|
opts.ProvisionVolume = !staticVol
|
|
|
|
|
2020-05-14 13:20:11 +00:00
|
|
|
clusterData, err := getClusterInformation(options)
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-14 13:20:11 +00:00
|
|
|
opts.ClusterID = clusterData.ClusterID
|
|
|
|
opts.Monitors = strings.Join(clusterData.Monitors, ",")
|
|
|
|
opts.SubvolumeGroup = clusterData.CephFS.SubvolumeGroup
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if err = extractOption(&opts.RootPath, "rootPath", options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = extractOption(&opts.FsName, "fsName", options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = extractMounter(&opts.Mounter, options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vid.FsSubvolName = opts.RootPath
|
|
|
|
vid.VolumeID = volID
|
|
|
|
|
|
|
|
return &opts, &vid, nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
2020-08-03 18:53:14 +00:00
|
|
|
|
|
|
|
// newSnapshotOptionsFromID generates a new instance of volumeOptions and snapshotIdentifier
|
|
|
|
// from the provided CSI VolumeID.
|
|
|
|
func newSnapshotOptionsFromID(ctx context.Context, snapID string, cr *util.Credentials) (*volumeOptions, *snapshotInfo, *snapshotIdentifier, error) {
|
|
|
|
var (
|
|
|
|
vi util.CSIIdentifier
|
|
|
|
volOptions volumeOptions
|
|
|
|
sid snapshotIdentifier
|
|
|
|
)
|
|
|
|
// Decode the snapID first, to detect pre-provisioned snapshot before other errors
|
|
|
|
err := vi.DecomposeCSIID(snapID)
|
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, ErrInvalidVolID
|
|
|
|
}
|
|
|
|
volOptions.ClusterID = vi.ClusterID
|
|
|
|
sid.SnapshotID = snapID
|
|
|
|
volOptions.FscID = vi.LocationID
|
|
|
|
|
2020-08-10 06:19:38 +00:00
|
|
|
if volOptions.Monitors, err = util.Mons(util.CsiConfigFile, vi.ClusterID); err != nil {
|
2020-08-03 18:53:14 +00:00
|
|
|
return &volOptions, nil, &sid, fmt.Errorf("failed to fetch monitor list using clusterID (%s): %w", vi.ClusterID, err)
|
|
|
|
}
|
|
|
|
|
2020-08-10 06:19:38 +00:00
|
|
|
if volOptions.SubvolumeGroup, err = util.CephFSSubvolumeGroup(util.CsiConfigFile, vi.ClusterID); err != nil {
|
2020-08-03 18:53:14 +00:00
|
|
|
return &volOptions, nil, &sid, fmt.Errorf("failed to fetch subvolumegroup list using clusterID (%s): %w", vi.ClusterID, err)
|
|
|
|
}
|
|
|
|
|
2020-10-19 07:08:57 +00:00
|
|
|
err = volOptions.Connect(cr)
|
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
// in case of an error, volOptions is returned, but callers may not
|
|
|
|
// expect to need to call Destroy() on it. So, make sure to release any
|
|
|
|
// resources that may have been allocated
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
volOptions.Destroy()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-10-02 08:44:40 +00:00
|
|
|
volOptions.FsName, err = volOptions.getFsName(ctx)
|
2020-08-03 18:53:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
|
2020-10-02 11:18:24 +00:00
|
|
|
volOptions.MetadataPool, err = volOptions.getMetadataPool(ctx)
|
2020-08-03 18:53:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:57:51 +00:00
|
|
|
// Connect to cephfs' default radosNamespace (csi)
|
|
|
|
j, err := snapJournal.Connect(volOptions.Monitors, radosNamespace, cr)
|
2020-08-03 18:53:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
defer j.Destroy()
|
|
|
|
|
|
|
|
imageAttributes, err := j.GetImageAttributes(
|
|
|
|
ctx, volOptions.MetadataPool, vi.ObjectUUID, true)
|
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
// storing request name in snapshotshot Identifier
|
|
|
|
sid.RequestName = imageAttributes.RequestName
|
|
|
|
sid.FsSnapshotName = imageAttributes.ImageName
|
|
|
|
sid.FsSubvolName = imageAttributes.SourceName
|
|
|
|
|
2020-09-16 13:49:10 +00:00
|
|
|
subvolInfo, err := getSubVolumeInfo(ctx, &volOptions, cr, volumeID(sid.FsSubvolName))
|
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
volOptions.Features = subvolInfo.Features
|
|
|
|
|
2020-08-03 18:53:14 +00:00
|
|
|
info, err := getSnapshotInfo(ctx, &volOptions, cr, volumeID(sid.FsSnapshotName), volumeID(sid.FsSubvolName))
|
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &volOptions, &info, &sid, nil
|
|
|
|
}
|