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.
|
|
|
|
*/
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
package store
|
2018-03-05 11:59:47 +00:00
|
|
|
|
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"
|
2022-04-06 13:26:07 +00:00
|
|
|
"path"
|
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
|
|
|
|
2023-12-18 11:04:31 +00:00
|
|
|
cephcsi "github.com/ceph/ceph-csi/api/deploy/kubernetes"
|
2022-02-15 12:11:09 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/cephfs/core"
|
2021-08-25 06:46:03 +00:00
|
|
|
cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors"
|
2021-09-16 13:47:57 +00:00
|
|
|
fsutil "github.com/ceph/ceph-csi/internal/cephfs/util"
|
2022-08-12 14:31:08 +00:00
|
|
|
kmsapi "github.com/ceph/ceph-csi/internal/kms"
|
2020-04-17 09:23:49 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2022-08-12 14:31:08 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/k8s"
|
2021-09-06 12:20:55 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2018-03-22 13:11:51 +00:00
|
|
|
)
|
2018-03-05 11:59:47 +00:00
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
const (
|
|
|
|
cephfsDefaultEncryptionType = util.EncryptionTypeFile
|
|
|
|
)
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
type VolumeOptions struct {
|
2022-02-15 12:11:09 +00:00
|
|
|
core.SubVolume
|
2022-04-06 13:26:07 +00:00
|
|
|
|
|
|
|
RequestName string
|
|
|
|
NamePrefix string
|
|
|
|
ClusterID string
|
|
|
|
MetadataPool string
|
2020-10-09 11:38:59 +00:00
|
|
|
// ReservedID represents the ID reserved for a subvolume
|
2022-04-06 13:26:07 +00:00
|
|
|
ReservedID string
|
|
|
|
Monitors string `json:"monitors"`
|
|
|
|
RootPath string `json:"rootPath"`
|
|
|
|
Mounter string `json:"mounter"`
|
|
|
|
BackingSnapshotRoot string // Snapshot root relative to RootPath.
|
|
|
|
BackingSnapshotID string
|
|
|
|
KernelMountOptions string `json:"kernelMountOptions"`
|
|
|
|
FuseMountOptions string `json:"fuseMountOptions"`
|
2022-03-31 13:29:33 +00:00
|
|
|
NetNamespaceFilePath string
|
2022-04-06 13:26:07 +00:00
|
|
|
TopologyPools *[]util.TopologyConstrainedPool
|
|
|
|
TopologyRequirement *csi.TopologyRequirement
|
|
|
|
Topology map[string]string
|
|
|
|
FscID int64
|
2020-08-05 10:10:04 +00:00
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
// Encryption provides access to optional VolumeEncryption functions
|
|
|
|
Encryption *util.VolumeEncryption
|
|
|
|
// Owner is the creator (tenant, Kubernetes Namespace) of the volume
|
|
|
|
Owner string
|
|
|
|
|
2020-08-05 10:10:04 +00:00
|
|
|
// conn is a connection to the Ceph cluster obtained from a ConnPool
|
|
|
|
conn *util.ClusterConnection
|
2022-04-06 13:26:07 +00:00
|
|
|
|
|
|
|
ProvisionVolume bool `json:"provisionVolume"`
|
|
|
|
BackingSnapshot bool `json:"backingSnapshot"`
|
2020-08-05 10:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect a CephFS volume to the Ceph cluster.
|
2021-09-16 13:47:57 +00:00
|
|
|
func (vo *VolumeOptions) Connect(cr *util.Credentials) error {
|
2020-08-05 10:10:04 +00:00
|
|
|
if vo.conn != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := &util.ClusterConnection{}
|
|
|
|
if err := conn.Connect(vo.Monitors, cr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vo.conn = conn
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-05 10:10:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy cleans up the CephFS volume object and closes the connection to the
|
|
|
|
// Ceph cluster in case one was setup.
|
2021-09-16 13:47:57 +00:00
|
|
|
func (vo *VolumeOptions) Destroy() {
|
2020-08-05 10:10:04 +00:00
|
|
|
if vo.conn != nil {
|
|
|
|
vo.conn.Destroy()
|
|
|
|
}
|
2022-08-12 14:31:08 +00:00
|
|
|
if vo.IsEncrypted() {
|
|
|
|
vo.Encryption.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
|
2021-07-22 05:45:17 +00:00
|
|
|
|
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
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-01-16 13:03:38 +00:00
|
|
|
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 {
|
2021-09-16 14:46:07 +00:00
|
|
|
case "fuse":
|
|
|
|
case "kernel":
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-06 05:55:00 +00:00
|
|
|
func (v *VolumeOptions) DetectMounter(options map[string]string) error {
|
|
|
|
return extractMounter(&v.Mounter, options)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-12-18 11:04:31 +00:00
|
|
|
func GetClusterInformation(options map[string]string) (*cephcsi.ClusterInfo, error) {
|
2019-05-28 19:03:18 +00:00
|
|
|
clusterID, ok := options["clusterID"]
|
|
|
|
if !ok {
|
2024-04-04 08:50:20 +00:00
|
|
|
err := errors.New("clusterID must be set")
|
2021-07-22 05:45:17 +00:00
|
|
|
|
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)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
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)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-05-14 13:20:11 +00:00
|
|
|
return nil, err
|
2020-04-29 10:03:08 +00:00
|
|
|
}
|
2023-12-18 11:04:31 +00:00
|
|
|
clusterData := &cephcsi.ClusterInfo{
|
2020-05-14 13:20:11 +00:00
|
|
|
ClusterID: clusterID,
|
|
|
|
Monitors: strings.Split(monitors, ","),
|
|
|
|
}
|
|
|
|
clusterData.CephFS.SubvolumeGroup = subvolumeGroup
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-05-14 13:20:11 +00:00
|
|
|
return clusterData, nil
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
// GetConnection returns the cluster connection.
|
|
|
|
func (vo *VolumeOptions) GetConnection() *util.ClusterConnection {
|
|
|
|
return vo.conn
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
func fmtBackingSnapshotOptionMismatch(optName, expected, actual string) error {
|
|
|
|
return fmt.Errorf("%s option mismatch with backing snapshot: got %s, expected %s",
|
|
|
|
optName, actual, expected)
|
|
|
|
}
|
|
|
|
|
2024-02-19 08:38:28 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
// NewVolumeOptions generates a new instance of volumeOptions from the provided
|
2020-07-19 12:21:03 +00:00
|
|
|
// CSI request parameters.
|
2022-07-28 12:05:33 +00:00
|
|
|
func NewVolumeOptions(
|
|
|
|
ctx context.Context,
|
|
|
|
requestName,
|
|
|
|
clusterName string,
|
|
|
|
setMetadata bool,
|
|
|
|
req *csi.CreateVolumeRequest,
|
2022-06-01 10:17:19 +00:00
|
|
|
cr *util.Credentials,
|
|
|
|
) (*VolumeOptions, error) {
|
2018-04-13 12:21:15 +00:00
|
|
|
var (
|
2024-02-19 08:38:28 +00:00
|
|
|
opts *VolumeOptions
|
2022-04-06 13:26:07 +00:00
|
|
|
backingSnapshotBool string
|
|
|
|
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()
|
2024-02-19 08:38:28 +00:00
|
|
|
opts, err = getVolumeOptions(volOptions)
|
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
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
opts.Owner = k8s.GetOwner(volOptions)
|
2023-02-07 09:06:36 +00:00
|
|
|
opts.BackingSnapshot = IsShallowVolumeSupported(req)
|
2020-05-14 13:20:11 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-19 06:03:19 +00:00
|
|
|
if err = extractOptionalOption(&opts.NamePrefix, "volumeNamePrefix", volOptions); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if err = extractOptionalOption(&backingSnapshotBool, "backingSnapshot", volOptions); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
if err = opts.InitKMS(ctx, volOptions, req.GetSecrets()); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to init KMS: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if backingSnapshotBool != "" {
|
|
|
|
if opts.BackingSnapshot, err = strconv.ParseBool(backingSnapshotBool); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse backingSnapshot: %w", 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
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
fs := core.NewFileSystem(opts.conn)
|
2022-01-25 05:30:16 +00:00
|
|
|
opts.FscID, err = fs.GetFscID(ctx, opts.FsName)
|
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
|
|
|
|
2022-01-25 05:30:16 +00:00
|
|
|
opts.MetadataPool, err = fs.GetMetadataPool(ctx, opts.FsName)
|
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
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if opts.BackingSnapshot {
|
|
|
|
if req.GetVolumeContentSource() == nil || req.GetVolumeContentSource().GetSnapshot() == nil {
|
|
|
|
return nil, errors.New("backingSnapshot option requires snapshot volume source")
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.BackingSnapshotID = req.GetVolumeContentSource().GetSnapshot().GetSnapshotId()
|
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
err = opts.populateVolumeOptionsFromBackingSnapshot(ctx, cr, req.GetSecrets(), clusterName, setMetadata)
|
2022-04-06 13:26:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 08:38:28 +00:00
|
|
|
return opts, nil
|
2019-01-29 05:49:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 09:06:36 +00:00
|
|
|
// IsShallowVolumeSupported returns true only for ReadOnly volume requests
|
|
|
|
// with datasource as snapshot.
|
|
|
|
func IsShallowVolumeSupported(req *csi.CreateVolumeRequest) bool {
|
2024-04-04 08:50:20 +00:00
|
|
|
isRO := IsVolumeCreateRO(req.GetVolumeCapabilities())
|
2023-02-07 09:06:36 +00:00
|
|
|
|
|
|
|
return isRO && (req.GetVolumeContentSource() != nil && req.GetVolumeContentSource().GetSnapshot() != nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsVolumeCreateRO(caps []*csi.VolumeCapability) bool {
|
|
|
|
for _, cap := range caps {
|
2024-04-04 08:50:20 +00:00
|
|
|
if cap.GetAccessMode() != nil {
|
|
|
|
switch cap.GetAccessMode().GetMode() { //nolint:exhaustive // only check what we want
|
2023-02-07 09:06:36 +00:00
|
|
|
case csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY,
|
|
|
|
csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
// newVolumeOptionsFromVolID generates a new instance of volumeOptions and VolumeIdentifier
|
2020-07-19 12:21:03 +00:00
|
|
|
// from the provided CSI VolumeID.
|
2023-06-02 09:49:22 +00:00
|
|
|
//
|
2023-06-02 08:59:52 +00:00
|
|
|
//nolint:gocyclo,cyclop // TODO: reduce complexity
|
2021-09-16 13:47:57 +00:00
|
|
|
func NewVolumeOptionsFromVolID(
|
2021-06-25 10:18:59 +00:00
|
|
|
ctx context.Context,
|
|
|
|
volID string,
|
2022-06-01 10:17:19 +00:00
|
|
|
volOpt, secrets map[string]string,
|
2022-06-14 13:23:29 +00:00
|
|
|
clusterName string,
|
2022-07-28 12:05:33 +00:00
|
|
|
setMetadata bool,
|
2022-06-01 10:17:19 +00:00
|
|
|
) (*VolumeOptions, *VolumeIdentifier, error) {
|
2019-01-29 05:49:16 +00:00
|
|
|
var (
|
2019-05-28 19:03:18 +00:00
|
|
|
vi util.CSIIdentifier
|
2021-09-16 13:47:57 +00:00
|
|
|
volOptions VolumeOptions
|
|
|
|
vid VolumeIdentifier
|
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)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-08-25 06:46:03 +00:00
|
|
|
return nil, nil, util.JoinErrors(cerrors.ErrInvalidVolID, err)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
volOptions.ClusterID = vi.ClusterID
|
|
|
|
vid.VolumeID = volID
|
2022-02-15 12:11:09 +00:00
|
|
|
volOptions.VolID = volID
|
2019-05-28 19:03:18 +00:00
|
|
|
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
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
fs := core.NewFileSystem(volOptions.conn)
|
2022-01-25 05:30:16 +00:00
|
|
|
volOptions.FsName, err = fs.GetFsName(ctx, volOptions.FscID)
|
2019-05-28 19:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-25 05:30:16 +00:00
|
|
|
volOptions.MetadataPool, err = fs.GetMetadataPool(ctx, volOptions.FsName)
|
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)
|
2021-09-16 13:47:57 +00:00
|
|
|
j, err := VolJournal.Connect(volOptions.Monitors, fsutil.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
|
2022-08-12 14:31:08 +00:00
|
|
|
volOptions.Owner = imageAttributes.Owner
|
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
|
|
|
|
}
|
2022-08-12 14:31:08 +00:00
|
|
|
|
|
|
|
if err = volOptions.InitKMS(ctx, volOpt, secrets); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if imageAttributes.BackingSnapshotID != "" || volOptions.BackingSnapshotID != "" {
|
|
|
|
volOptions.BackingSnapshot = true
|
|
|
|
volOptions.BackingSnapshotID = imageAttributes.BackingSnapshotID
|
|
|
|
}
|
|
|
|
|
2020-01-23 02:48:46 +00:00
|
|
|
volOptions.ProvisionVolume = true
|
2022-02-15 12:11:09 +00:00
|
|
|
volOptions.SubVolume.VolID = vid.FsSubvolName
|
2022-04-06 13:26:07 +00:00
|
|
|
|
|
|
|
if volOptions.BackingSnapshot {
|
2022-08-12 14:31:08 +00:00
|
|
|
err = volOptions.populateVolumeOptionsFromBackingSnapshot(ctx, cr, secrets, clusterName, setMetadata)
|
2022-04-06 13:26:07 +00:00
|
|
|
} else {
|
2022-07-28 12:05:33 +00:00
|
|
|
err = volOptions.populateVolumeOptionsFromSubvolume(ctx, clusterName, setMetadata)
|
2022-04-06 13:26:07 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
if volOpt == nil && imageAttributes.KmsID != "" && volOptions.Encryption == nil {
|
|
|
|
err = volOptions.ConfigureEncryption(ctx, imageAttributes.KmsID, secrets)
|
|
|
|
if err != nil {
|
|
|
|
return &volOptions, &vid, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
return &volOptions, &vid, err
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:05:33 +00:00
|
|
|
func (vo *VolumeOptions) populateVolumeOptionsFromSubvolume(
|
|
|
|
ctx context.Context,
|
|
|
|
clusterName string,
|
|
|
|
setMetadata bool,
|
|
|
|
) error {
|
|
|
|
vol := core.NewSubVolume(vo.conn, &vo.SubVolume, vo.ClusterID, clusterName, setMetadata)
|
2022-04-06 13:26:07 +00:00
|
|
|
|
|
|
|
var info *core.Subvolume
|
2022-02-15 12:11:09 +00:00
|
|
|
info, err := vol.GetSubVolumeInfo(ctx)
|
2020-09-10 12:34:48 +00:00
|
|
|
if err == nil {
|
2022-04-06 13:26:07 +00:00
|
|
|
vo.RootPath = info.Path
|
|
|
|
vo.Features = info.Features
|
|
|
|
vo.Size = info.BytesQuota
|
2019-06-08 05:06:03 +00:00
|
|
|
}
|
2020-09-10 12:34:48 +00:00
|
|
|
|
2021-08-25 06:46:03 +00:00
|
|
|
if errors.Is(err, cerrors.ErrInvalidCommand) {
|
2022-04-06 13:26:07 +00:00
|
|
|
vo.RootPath, err = vol.GetVolumeRootPathCeph(ctx)
|
2020-09-10 12:34:48 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vo *VolumeOptions) populateVolumeOptionsFromBackingSnapshot(
|
|
|
|
ctx context.Context,
|
|
|
|
cr *util.Credentials,
|
2022-08-12 14:31:08 +00:00
|
|
|
secrets map[string]string,
|
2022-06-14 13:23:29 +00:00
|
|
|
clusterName string,
|
2022-07-28 12:05:33 +00:00
|
|
|
setMetadata bool,
|
2022-04-06 13:26:07 +00:00
|
|
|
) error {
|
|
|
|
// As of CephFS snapshot v2 API, snapshots may be found in two locations:
|
|
|
|
//
|
|
|
|
// (a) /volumes/<volume group>/<subvolume>/.snap/<snapshot>/<UUID>
|
|
|
|
// (b) /volumes/<volume group>/<subvolume>/<UUID>/.snap/_<snapshot>_<snapshot inode number>
|
|
|
|
|
|
|
|
if !vo.ProvisionVolume {
|
|
|
|
// Case (b)
|
|
|
|
//
|
|
|
|
// If the volume is not provisioned by us, we assume that we have access only
|
|
|
|
// to snapshot's parent volume root. In this case, o.RootPath is expected to
|
|
|
|
// be already set in the volume context.
|
|
|
|
|
|
|
|
// BackingSnapshotRoot cannot be determined at this stage, because the
|
|
|
|
// full directory name is not known (see snapshot path format for case
|
|
|
|
// (b) above). RootPath/.snap must be traversed in order to find out
|
|
|
|
// the snapshot directory name.
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:05:33 +00:00
|
|
|
parentBackingSnapVolOpts, _, snapID, err := NewSnapshotOptionsFromID(ctx,
|
2022-08-12 14:31:08 +00:00
|
|
|
vo.BackingSnapshotID, cr, secrets, clusterName, setMetadata)
|
2022-04-06 13:26:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to retrieve backing snapshot %s: %w", vo.BackingSnapshotID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that backing snapshot parent's volume options match the context.
|
|
|
|
// Snapshot-backed volume inherits all its parent's (parent of the snapshot) options.
|
|
|
|
|
|
|
|
if vo.ClusterID != parentBackingSnapVolOpts.ClusterID {
|
|
|
|
return fmtBackingSnapshotOptionMismatch("clusterID", vo.ClusterID, parentBackingSnapVolOpts.ClusterID)
|
|
|
|
}
|
|
|
|
|
2023-08-09 11:16:45 +00:00
|
|
|
// Pool parameter is optional and only used to set 'pool_layout' argument for
|
|
|
|
// subvolume and subvolume clone create commands.
|
|
|
|
// Setting this to empty since it is not used with Snapshot-backed volume.
|
|
|
|
vo.Pool = ""
|
2022-04-06 13:26:07 +00:00
|
|
|
|
|
|
|
if vo.MetadataPool != parentBackingSnapVolOpts.MetadataPool {
|
|
|
|
return fmtBackingSnapshotOptionMismatch("MetadataPool", vo.MetadataPool, parentBackingSnapVolOpts.MetadataPool)
|
|
|
|
}
|
|
|
|
|
|
|
|
if vo.FsName != parentBackingSnapVolOpts.FsName {
|
|
|
|
return fmtBackingSnapshotOptionMismatch("fsName", vo.FsName, parentBackingSnapVolOpts.FsName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if vo.SubvolumeGroup != parentBackingSnapVolOpts.SubvolumeGroup {
|
|
|
|
return fmtBackingSnapshotOptionMismatch("SubvolumeGroup", vo.SubvolumeGroup, parentBackingSnapVolOpts.SubvolumeGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
vo.Features = parentBackingSnapVolOpts.Features
|
|
|
|
vo.Size = parentBackingSnapVolOpts.Size
|
|
|
|
|
|
|
|
// For case (a) (o.ProvisionVolume==true is assumed), snapshot root path
|
|
|
|
// can be built out of subvolume root path, which is in following format:
|
|
|
|
//
|
|
|
|
// /volumes/<volume group>/<subvolume>/<subvolume UUID>
|
|
|
|
|
|
|
|
subvolRoot, subvolUUID := path.Split(parentBackingSnapVolOpts.RootPath)
|
|
|
|
|
|
|
|
vo.RootPath = subvolRoot
|
|
|
|
vo.BackingSnapshotRoot = path.Join(".snap", snapID.FsSnapshotName, subvolUUID)
|
|
|
|
|
|
|
|
return nil
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
// NewVolumeOptionsFromMonitorList generates a new instance of VolumeOptions and
|
|
|
|
// VolumeIdentifier from the provided CSI volume context.
|
|
|
|
func NewVolumeOptionsFromMonitorList(
|
2021-06-25 10:18:59 +00:00
|
|
|
volID string,
|
2022-06-01 10:17:19 +00:00
|
|
|
options, secrets map[string]string,
|
|
|
|
) (*VolumeOptions, *VolumeIdentifier, error) {
|
2019-05-28 19:03:18 +00:00
|
|
|
var (
|
2021-09-16 13:47:57 +00:00
|
|
|
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)
|
2024-04-04 08:50:20 +00:00
|
|
|
if err == nil && mon != "" {
|
2019-05-28 19:03:18 +00:00
|
|
|
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
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
opts.RootPath = core.GetVolumeRootPathCephDeprecated(fsutil.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
|
|
|
}
|
|
|
|
|
2021-08-02 14:57:11 +00:00
|
|
|
if err = extractOptionalOption(&opts.KernelMountOptions, "kernelMountOptions", options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = extractOptionalOption(&opts.FuseMountOptions, "fuseMountOptions", options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if err = extractMounter(&opts.Mounter, options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if err = extractOptionalOption(&opts.BackingSnapshotID, "backingSnapshotID", options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
opts.Owner = k8s.GetOwner(options)
|
|
|
|
if err = opts.InitKMS(context.TODO(), options, secrets); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
vid.FsSubvolName = volID
|
|
|
|
vid.VolumeID = volID
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if opts.BackingSnapshotID != "" {
|
|
|
|
opts.BackingSnapshot = true
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
return &opts, &vid, nil
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
// 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.
|
2021-09-16 13:47:57 +00:00
|
|
|
func NewVolumeOptionsFromStaticVolume(
|
2021-06-25 10:18:59 +00:00
|
|
|
volID string,
|
2022-08-12 14:31:08 +00:00
|
|
|
options, secrets map[string]string,
|
2022-06-01 10:17:19 +00:00
|
|
|
) (*VolumeOptions, *VolumeIdentifier, error) {
|
2019-05-28 19:03:18 +00:00
|
|
|
var (
|
2021-09-16 13:47:57 +00:00
|
|
|
opts VolumeOptions
|
|
|
|
vid VolumeIdentifier
|
2019-05-28 19:03:18 +00:00
|
|
|
staticVol bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
val, ok := options["staticVolume"]
|
|
|
|
if !ok {
|
2021-08-25 06:46:03 +00:00
|
|
|
return nil, nil, cerrors.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 {
|
2021-08-25 06:46:03 +00:00
|
|
|
return nil, nil, cerrors.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
|
|
|
|
|
2021-09-16 13:47:57 +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
|
2022-08-12 14:31:08 +00:00
|
|
|
opts.Owner = k8s.GetOwner(options)
|
2020-05-14 13:20:11 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if err = extractOption(&opts.RootPath, "rootPath", options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-12 07:56:06 +00:00
|
|
|
if err = extractOptionalOption(&opts.FsName, "fsName", options); err != nil {
|
2019-05-28 19:03:18 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:14:58 +00:00
|
|
|
if err = extractOptionalOption(&opts.KernelMountOptions, "kernelMountOptions", options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = extractOptionalOption(&opts.FuseMountOptions, "fuseMountOptions", options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = extractOptionalOption(&opts.SubvolumeGroup, "subvolumeGroup", options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if err = extractMounter(&opts.Mounter, options); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
if err = opts.InitKMS(context.TODO(), options, secrets); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
vid.FsSubvolName = opts.RootPath
|
|
|
|
vid.VolumeID = volID
|
|
|
|
|
2022-04-06 13:26:07 +00:00
|
|
|
if opts.BackingSnapshotID != "" {
|
|
|
|
opts.BackingSnapshot = true
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
return &opts, &vid, nil
|
2018-03-05 11:59:47 +00:00
|
|
|
}
|
2020-08-03 18:53:14 +00:00
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
// NewSnapshotOptionsFromID generates a new instance of volumeOptions and SnapshotIdentifier
|
2020-08-03 18:53:14 +00:00
|
|
|
// from the provided CSI VolumeID.
|
2021-09-16 13:47:57 +00:00
|
|
|
func NewSnapshotOptionsFromID(
|
2021-06-25 10:18:59 +00:00
|
|
|
ctx context.Context,
|
|
|
|
snapID string,
|
2022-06-01 10:17:19 +00:00
|
|
|
cr *util.Credentials,
|
2022-08-12 14:31:08 +00:00
|
|
|
secrets map[string]string,
|
2022-06-14 13:23:29 +00:00
|
|
|
clusterName string,
|
2022-07-28 12:05:33 +00:00
|
|
|
setMetadata bool,
|
2022-06-01 10:17:19 +00:00
|
|
|
) (*VolumeOptions, *core.SnapshotInfo, *SnapshotIdentifier, error) {
|
2020-08-03 18:53:14 +00:00
|
|
|
var (
|
|
|
|
vi util.CSIIdentifier
|
2021-09-16 13:47:57 +00:00
|
|
|
volOptions VolumeOptions
|
|
|
|
sid SnapshotIdentifier
|
2020-08-03 18:53:14 +00:00
|
|
|
)
|
|
|
|
// Decode the snapID first, to detect pre-provisioned snapshot before other errors
|
|
|
|
err := vi.DecomposeCSIID(snapID)
|
|
|
|
if err != nil {
|
2021-08-25 06:46:03 +00:00
|
|
|
return &volOptions, nil, &sid, cerrors.ErrInvalidVolID
|
2020-08-03 18:53:14 +00:00
|
|
|
}
|
|
|
|
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 {
|
2021-06-25 10:18:59 +00:00
|
|
|
return &volOptions, nil, &sid, fmt.Errorf(
|
|
|
|
"failed to fetch monitor list using clusterID (%s): %w",
|
|
|
|
vi.ClusterID,
|
|
|
|
err)
|
2020-08-03 18:53:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 06:19:38 +00:00
|
|
|
if volOptions.SubvolumeGroup, err = util.CephFSSubvolumeGroup(util.CsiConfigFile, vi.ClusterID); err != nil {
|
2021-06-25 10:18:59 +00:00
|
|
|
return &volOptions, nil, &sid, fmt.Errorf(
|
|
|
|
"failed to fetch subvolumegroup list using clusterID (%s): %w",
|
|
|
|
vi.ClusterID,
|
|
|
|
err)
|
2020-08-03 18:53:14 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
fs := core.NewFileSystem(volOptions.conn)
|
2022-01-25 05:30:16 +00:00
|
|
|
volOptions.FsName, err = fs.GetFsName(ctx, volOptions.FscID)
|
2020-08-03 18:53:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
|
2022-01-25 05:30:16 +00:00
|
|
|
volOptions.MetadataPool, err = fs.GetMetadataPool(ctx, volOptions.FsName)
|
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)
|
2021-09-16 13:47:57 +00:00
|
|
|
j, err := SnapJournal.Connect(volOptions.Monitors, fsutil.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
|
|
|
|
}
|
2021-04-15 10:49:37 +00:00
|
|
|
// storing request name in snapshot Identifier
|
2020-08-03 18:53:14 +00:00
|
|
|
sid.RequestName = imageAttributes.RequestName
|
|
|
|
sid.FsSnapshotName = imageAttributes.ImageName
|
|
|
|
sid.FsSubvolName = imageAttributes.SourceName
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
volOptions.SubVolume.VolID = sid.FsSubvolName
|
2022-08-12 14:31:08 +00:00
|
|
|
volOptions.Owner = imageAttributes.Owner
|
2022-07-28 12:05:33 +00:00
|
|
|
vol := core.NewSubVolume(volOptions.conn, &volOptions.SubVolume, volOptions.ClusterID, clusterName, setMetadata)
|
2022-02-15 12:11:09 +00:00
|
|
|
|
2022-08-12 14:31:08 +00:00
|
|
|
if imageAttributes.KmsID != "" && volOptions.Encryption == nil {
|
|
|
|
err = volOptions.ConfigureEncryption(ctx, imageAttributes.KmsID, secrets)
|
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
subvolInfo, err := vol.GetSubVolumeInfo(ctx)
|
2020-09-16 13:49:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
volOptions.Features = subvolInfo.Features
|
2022-01-10 04:56:28 +00:00
|
|
|
volOptions.Size = subvolInfo.BytesQuota
|
2022-04-06 13:26:07 +00:00
|
|
|
volOptions.RootPath = subvolInfo.Path
|
2022-07-28 08:44:52 +00:00
|
|
|
snap := core.NewSnapshot(volOptions.conn, sid.FsSnapshotName,
|
2022-07-28 12:26:55 +00:00
|
|
|
volOptions.ClusterID, clusterName, setMetadata, &volOptions.SubVolume)
|
2022-02-15 12:11:09 +00:00
|
|
|
info, err := snap.GetSnapshotInfo(ctx)
|
2020-08-03 18:53:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return &volOptions, nil, &sid, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &volOptions, &info, &sid, nil
|
|
|
|
}
|
2021-09-06 12:20:55 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
// SnapshotOption is a struct that holds the information about the snapshot.
|
|
|
|
type SnapshotOption struct {
|
|
|
|
ReservedID string // ID reserved for the snapshot.
|
|
|
|
RequestName string // Request name of the snapshot.
|
|
|
|
ClusterID string // Cluster ID of to identify ceph cluster connection information.
|
|
|
|
Monitors string // Monitors of the ceph cluster.
|
|
|
|
NamePrefix string // Name prefix of the snapshot.
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenSnapFromOptions(ctx context.Context, req *csi.CreateSnapshotRequest) (*SnapshotOption, error) {
|
|
|
|
cephfsSnap := &SnapshotOption{}
|
2021-09-06 12:20:55 +00:00
|
|
|
cephfsSnap.RequestName = req.GetName()
|
|
|
|
snapOptions := req.GetParameters()
|
|
|
|
|
2021-09-07 06:05:11 +00:00
|
|
|
clusterID, err := util.GetClusterID(snapOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cephfsSnap.Monitors, cephfsSnap.ClusterID, err = util.GetMonsAndClusterID(ctx, clusterID, false)
|
2021-09-06 12:20:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed getting mons (%s)", err)
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if namePrefix, ok := snapOptions["snapshotNamePrefix"]; ok {
|
|
|
|
cephfsSnap.NamePrefix = namePrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
return cephfsSnap, nil
|
|
|
|
}
|
2022-08-12 14:31:08 +00:00
|
|
|
|
|
|
|
func parseEncryptionOpts(volOptions map[string]string) (string, util.EncryptionType, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
ok bool
|
|
|
|
encrypted, kmsID string
|
|
|
|
)
|
|
|
|
encrypted, ok = volOptions["encrypted"]
|
|
|
|
if !ok {
|
|
|
|
return "", util.EncryptionTypeNone, nil
|
|
|
|
}
|
|
|
|
kmsID, err = util.FetchEncryptionKMSID(encrypted, volOptions["encryptionKMSID"])
|
|
|
|
if err != nil {
|
|
|
|
return "", util.EncryptionTypeInvalid, err
|
|
|
|
}
|
|
|
|
|
|
|
|
encType := util.FetchEncryptionType(volOptions, cephfsDefaultEncryptionType)
|
|
|
|
|
|
|
|
return kmsID, encType, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEncrypted returns true if volOptions enables file encryption.
|
|
|
|
func IsEncrypted(ctx context.Context, volOptions map[string]string) (bool, error) {
|
|
|
|
_, encType, err := parseEncryptionOpts(volOptions)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return encType == util.EncryptionTypeFile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyEncryptionConfig copies passphrases and initializes a fresh
|
|
|
|
// Encryption struct if necessary from (vo, vID) to (cp, cpVID).
|
2024-03-04 15:13:31 +00:00
|
|
|
func (vo *VolumeOptions) CopyEncryptionConfig(ctx context.Context, cp *VolumeOptions, vID, cpVID string) error {
|
2022-08-12 14:31:08 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if !vo.IsEncrypted() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if vID == cpVID {
|
|
|
|
return fmt.Errorf("BUG: %v and %v have the same VolID %q "+
|
|
|
|
"set!? Call stack: %s", vo, cp, vID, util.CallStack())
|
|
|
|
}
|
|
|
|
|
|
|
|
if cp.Encryption == nil {
|
|
|
|
cp.Encryption, err = util.NewVolumeEncryption(vo.Encryption.GetID(), vo.Encryption.KMS)
|
|
|
|
if errors.Is(err, util.ErrDEKStoreNeeded) {
|
2024-03-04 15:13:31 +00:00
|
|
|
_, err := vo.Encryption.KMS.GetSecret(ctx, "")
|
2022-08-12 14:31:08 +00:00
|
|
|
if errors.Is(err, kmsapi.ErrGetSecretUnsupported) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vo.Encryption.KMS.RequiresDEKStore() == kmsapi.DEKStoreIntegrated {
|
2024-03-04 15:13:31 +00:00
|
|
|
passphrase, err := vo.Encryption.GetCryptoPassphrase(ctx, vID)
|
2022-08-12 14:31:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch passphrase for %q (%+v): %w",
|
|
|
|
vID, vo, err)
|
|
|
|
}
|
|
|
|
|
2024-03-04 15:13:31 +00:00
|
|
|
err = cp.Encryption.StoreCryptoPassphrase(ctx, cpVID, passphrase)
|
2022-08-12 14:31:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to store passphrase for %q (%+v): %w",
|
|
|
|
cpVID, cp, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigureEncryption initializes the Ceph CSI key management from
|
|
|
|
// kmsID and credentials. Sets vo.Encryption on success.
|
|
|
|
func (vo *VolumeOptions) ConfigureEncryption(
|
|
|
|
ctx context.Context,
|
|
|
|
kmsID string,
|
|
|
|
credentials map[string]string,
|
|
|
|
) error {
|
|
|
|
kms, err := kmsapi.GetKMS(vo.Owner, kmsID, credentials)
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "get KMS failed %+v: %v", vo, err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vo.Encryption, err = util.NewVolumeEncryption(kmsID, kms)
|
|
|
|
|
|
|
|
if errors.Is(err, util.ErrDEKStoreNeeded) {
|
|
|
|
// fscrypt uses secrets directly from the KMS.
|
|
|
|
// Therefore we do not support an additional DEK
|
|
|
|
// store. Since not all "metadata" KMS support
|
|
|
|
// GetSecret, test for support here. Postpone any
|
|
|
|
// other error handling
|
2024-03-04 15:13:31 +00:00
|
|
|
_, err := vo.Encryption.KMS.GetSecret(ctx, "")
|
2022-08-12 14:31:08 +00:00
|
|
|
if errors.Is(err, kmsapi.ErrGetSecretUnsupported) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitKMS initialized the Ceph CSI key management by parsing the
|
|
|
|
// configuration from volume options + credentials. Sets vo.Encryption
|
|
|
|
// on success.
|
|
|
|
func (vo *VolumeOptions) InitKMS(
|
|
|
|
ctx context.Context,
|
|
|
|
volOptions, credentials map[string]string,
|
|
|
|
) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
kmsID, encType, err := parseEncryptionOpts(volOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if encType == util.EncryptionTypeNone {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if encType != util.EncryptionTypeFile {
|
|
|
|
return fmt.Errorf("unsupported encryption type %v. only supported type is 'file'", encType)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = vo.ConfigureEncryption(ctx, kmsID, credentials)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid encryption kms configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vo *VolumeOptions) IsEncrypted() bool {
|
|
|
|
return vo.Encryption != nil
|
|
|
|
}
|