From ae5925f04cdce9b83425a7f8547be74779de8f63 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Mon, 21 Feb 2022 16:54:24 +0530 Subject: [PATCH] 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 --- .../persistentvolume/persistentvolume.go | 8 +++++++- internal/rbd/rbd_journal.go | 15 ++++++++++++++- internal/util/k8s/parameters.go | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/internal/controller/persistentvolume/persistentvolume.go b/internal/controller/persistentvolume/persistentvolume.go index e6957d88c..4bf46a7dd 100644 --- a/internal/controller/persistentvolume/persistentvolume.go +++ b/internal/controller/persistentvolume/persistentvolume.go @@ -177,7 +177,13 @@ func (r ReconcilePersistentVolume) reconcilePV(ctx context.Context, obj runtime. } 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 { log.ErrorLogMsg("failed to regenerate journal %s", err) diff --git a/internal/rbd/rbd_journal.go b/internal/rbd/rbd_journal.go index 4e2d03c6f..a8907ac7e 100644 --- a/internal/rbd/rbd_journal.go +++ b/internal/rbd/rbd_journal.go @@ -23,6 +23,7 @@ import ( "github.com/ceph/ceph-csi/internal/journal" "github.com/ceph/ceph-csi/internal/util" + "github.com/ceph/ceph-csi/internal/util/k8s" "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 // The volume handler won't remain same as its contains poolID,clusterID etc // which are not same across clusters. +// nolint:gocyclo,cyclop // TODO: reduce complexity func RegenerateJournal( volumeAttributes map[string]string, - volumeID, requestName, owner string, + claimName, + volumeID, + requestName, + owner string, cr *util.Credentials) (string, error) { ctx := context.Background() var ( @@ -598,16 +603,24 @@ func RegenerateJournal( if err != nil { return "", err } + if imageData != nil { rbdVol.ReservedID = imageData.ImageUUID rbdVol.ImageID = imageData.ImageAttributes.ImageID rbdVol.Owner = imageData.ImageAttributes.Owner + rbdVol.RbdImageName = imageData.ImageAttributes.ImageName if rbdVol.ImageID == "" { err = rbdVol.storeImageID(ctx, j) if err != nil { 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. rbdVol.VolID, err = util.GenerateVolID(ctx, rbdVol.Monitors, cr, imagePoolID, rbdVol.Pool, rbdVol.ClusterID, rbdVol.ReservedID, volIDVersion) diff --git a/internal/util/k8s/parameters.go b/internal/util/k8s/parameters.go index e5f36cb6b..db2ee82de 100644 --- a/internal/util/k8s/parameters.go +++ b/internal/util/k8s/parameters.go @@ -63,3 +63,19 @@ func GetVolumeMetadata(parameters map[string]string) map[string]string { 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 +}