rbd: update PV/PVC metadata on a reattach of PV

Example if a PVC was delete by setting `persistentVolumeReclaimPolicy` as
`Retain` on PV, and PV is reattached to a new PVC, we make sure to update
PV/PVC image metadata on a PV reattach.

Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
This commit is contained in:
Prasanna Kumar Kalever 2022-02-21 16:54:24 +05:30 committed by mergify[bot]
parent 0119d69ab2
commit ae5925f04c
3 changed files with 37 additions and 2 deletions

View File

@ -177,7 +177,13 @@ func (r ReconcilePersistentVolume) reconcilePV(ctx context.Context, obj runtime.
} }
defer cr.DeleteCredentials() defer cr.DeleteCredentials()
rbdVolID, err := rbd.RegenerateJournal(pv.Spec.CSI.VolumeAttributes, volumeHandler, requestName, pvcNamespace, cr) rbdVolID, err := rbd.RegenerateJournal(
pv.Spec.CSI.VolumeAttributes,
pv.Spec.ClaimRef.Name,
volumeHandler,
requestName,
pvcNamespace,
cr)
if err != nil { if err != nil {
log.ErrorLogMsg("failed to regenerate journal %s", err) log.ErrorLogMsg("failed to regenerate journal %s", err)

View File

@ -23,6 +23,7 @@ import (
"github.com/ceph/ceph-csi/internal/journal" "github.com/ceph/ceph-csi/internal/journal"
"github.com/ceph/ceph-csi/internal/util" "github.com/ceph/ceph-csi/internal/util"
"github.com/ceph/ceph-csi/internal/util/k8s"
"github.com/ceph/ceph-csi/internal/util/log" "github.com/ceph/ceph-csi/internal/util/log"
) )
@ -533,9 +534,13 @@ func undoVolReservation(ctx context.Context, rbdVol *rbdVolume, cr *util.Credent
// Generate new volume Handler // Generate new volume Handler
// The volume handler won't remain same as its contains poolID,clusterID etc // The volume handler won't remain same as its contains poolID,clusterID etc
// which are not same across clusters. // which are not same across clusters.
// nolint:gocyclo,cyclop // TODO: reduce complexity
func RegenerateJournal( func RegenerateJournal(
volumeAttributes map[string]string, volumeAttributes map[string]string,
volumeID, requestName, owner string, claimName,
volumeID,
requestName,
owner string,
cr *util.Credentials) (string, error) { cr *util.Credentials) (string, error) {
ctx := context.Background() ctx := context.Background()
var ( var (
@ -598,16 +603,24 @@ func RegenerateJournal(
if err != nil { if err != nil {
return "", err return "", err
} }
if imageData != nil { if imageData != nil {
rbdVol.ReservedID = imageData.ImageUUID rbdVol.ReservedID = imageData.ImageUUID
rbdVol.ImageID = imageData.ImageAttributes.ImageID rbdVol.ImageID = imageData.ImageAttributes.ImageID
rbdVol.Owner = imageData.ImageAttributes.Owner rbdVol.Owner = imageData.ImageAttributes.Owner
rbdVol.RbdImageName = imageData.ImageAttributes.ImageName
if rbdVol.ImageID == "" { if rbdVol.ImageID == "" {
err = rbdVol.storeImageID(ctx, j) err = rbdVol.storeImageID(ctx, j)
if err != nil { if err != nil {
return "", err return "", err
} }
} }
// Update Metadata on reattach of the same old PV
parameters := k8s.PrepareVolumeMetadata(claimName, rbdVol.Owner, "")
err = rbdVol.setVolumeMetadata(parameters)
if err != nil {
return "", fmt.Errorf("failed to set volume metadata: %w", err)
}
// As the omap already exists for this image ID return nil. // As the omap already exists for this image ID return nil.
rbdVol.VolID, err = util.GenerateVolID(ctx, rbdVol.Monitors, cr, imagePoolID, rbdVol.Pool, rbdVol.VolID, err = util.GenerateVolID(ctx, rbdVol.Monitors, cr, imagePoolID, rbdVol.Pool,
rbdVol.ClusterID, rbdVol.ReservedID, volIDVersion) rbdVol.ClusterID, rbdVol.ReservedID, volIDVersion)

View File

@ -63,3 +63,19 @@ func GetVolumeMetadata(parameters map[string]string) map[string]string {
return newParam return newParam
} }
// PrepareVolumeMetadata return PV/PVC/PVCNamespace metadata based on inputs.
func PrepareVolumeMetadata(pvcName, pvcNamespace, pvName string) map[string]string {
newParam := map[string]string{}
if pvcName != "" {
newParam[pvcNameKey] = pvcName
}
if pvcNamespace != "" {
newParam[pvcNamespaceKey] = pvcNamespace
}
if pvName != "" {
newParam[pvNameKey] = pvName
}
return newParam
}