2021-10-01 04:18:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
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 rbd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// isMigrationVolID validates if the passed in volID is a volumeID
|
|
|
|
// of a migrated volume.
|
|
|
|
func isMigrationVolID(volHash string) bool {
|
|
|
|
return strings.Contains(volHash, migIdentifier) &&
|
|
|
|
strings.Contains(volHash, migImageNamePrefix) && strings.Contains(volHash, migMonPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseMigrationVolID decodes the volume ID and generates a migrationVolID
|
|
|
|
// struct which consists of mon, image name, pool and clusterID information.
|
|
|
|
func parseMigrationVolID(vh string) (*migrationVolID, error) {
|
|
|
|
mh := &migrationVolID{}
|
|
|
|
handSlice := strings.Split(vh, migVolIDFieldSep)
|
|
|
|
if len(handSlice) < migVolIDTotalLength {
|
|
|
|
// its short of length in this case, so return error
|
|
|
|
return nil, ErrInvalidVolID
|
|
|
|
}
|
|
|
|
// Store pool
|
|
|
|
poolHash := strings.Join(handSlice[migVolIDSplitLength:], migVolIDFieldSep)
|
|
|
|
poolByte, dErr := hex.DecodeString(poolHash)
|
|
|
|
if dErr != nil {
|
|
|
|
return nil, ErrMissingPoolNameInVolID
|
|
|
|
}
|
|
|
|
mh.poolName = string(poolByte)
|
|
|
|
// Parse migration mons( for clusterID) and image
|
|
|
|
for _, field := range handSlice[:migVolIDSplitLength] {
|
|
|
|
switch {
|
|
|
|
case strings.Contains(field, migImageNamePrefix):
|
|
|
|
imageSli := strings.Split(field, migImageNamePrefix)
|
|
|
|
if len(imageSli) > 0 {
|
|
|
|
mh.imageName = migInTreeImagePrefix + imageSli[1]
|
|
|
|
}
|
|
|
|
case strings.Contains(field, migMonPrefix):
|
|
|
|
// ex: mons-7982de6a23b77bce50b1ba9f2e879cce
|
|
|
|
mh.clusterID = strings.Trim(field, migMonPrefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mh.imageName == "" {
|
|
|
|
return nil, ErrMissingImageNameInVolID
|
|
|
|
}
|
|
|
|
if mh.poolName == "" {
|
|
|
|
return nil, ErrMissingPoolNameInVolID
|
|
|
|
}
|
|
|
|
if mh.clusterID == "" {
|
|
|
|
return nil, ErrDecodeClusterIDFromMonsInVolID
|
|
|
|
}
|
|
|
|
|
|
|
|
return mh, nil
|
|
|
|
}
|
|
|
|
|
2021-10-25 09:17:12 +00:00
|
|
|
// deleteMigratedVolume get rbd volume details from the migration volID
|
2021-10-01 04:18:19 +00:00
|
|
|
// and delete the volume from the cluster, return err if there was an error on the process.
|
2021-10-25 09:17:12 +00:00
|
|
|
func deleteMigratedVolume(ctx context.Context, parsedMigHandle *migrationVolID, cr *util.Credentials) error {
|
2021-12-22 05:59:09 +00:00
|
|
|
rv, err := genVolFromMigVolID(ctx, parsedMigHandle, cr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-21 14:01:56 +00:00
|
|
|
defer rv.Destroy(ctx)
|
2024-07-05 06:46:09 +00:00
|
|
|
err = rv.Delete(ctx)
|
2021-12-22 05:59:09 +00:00
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to delete rbd image: %s, err: %v", rv, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// genVolFromMigVolID populate rbdVol struct from the migration volID.
|
|
|
|
func genVolFromMigVolID(ctx context.Context, migVolID *migrationVolID, cr *util.Credentials) (*rbdVolume, error) {
|
2021-10-25 09:17:12 +00:00
|
|
|
var err error
|
2021-10-01 04:18:19 +00:00
|
|
|
rv := &rbdVolume{}
|
|
|
|
|
|
|
|
// fill details to rv struct from parsed migration handle
|
2021-12-22 05:59:09 +00:00
|
|
|
rv.RbdImageName = migVolID.imageName
|
|
|
|
rv.Pool = migVolID.poolName
|
|
|
|
rv.ClusterID = migVolID.clusterID
|
2021-10-01 04:18:19 +00:00
|
|
|
rv.Monitors, err = util.Mons(util.CsiConfigFile, rv.ClusterID)
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to fetch monitors using clusterID: %s, err: %v", rv.ClusterID, err)
|
|
|
|
|
2021-12-22 05:59:09 +00:00
|
|
|
return nil, err
|
2021-10-01 04:18:19 +00:00
|
|
|
}
|
|
|
|
// connect to the volume.
|
|
|
|
err = rv.Connect(cr)
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to get connected to the rbd image : %s, err: %v", rv.RbdImageName, err)
|
|
|
|
|
2021-12-22 05:59:09 +00:00
|
|
|
return nil, err
|
2021-10-01 04:18:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 05:59:09 +00:00
|
|
|
return rv, nil
|
2021-10-01 04:18:19 +00:00
|
|
|
}
|