rbd: add set/Get VolumeMetadata() utility function

Define and use PV and PVC metadata keys used by external provisioner.
The CSI external-provisioner (v1.6.0+) introduces the
--extra-create-metadata flag, which automatically sets map<string, string>
parameters in the CSI CreateVolumeRequest.

Add utility functions to set/Get PV/PVC/PVCNamespace metadata on image

Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
This commit is contained in:
Prasanna Kumar Kalever 2022-02-22 17:53:45 +05:30 committed by mergify[bot]
parent 7b2aef0d81
commit 4d750ed0e5
2 changed files with 34 additions and 1 deletions

View File

@ -28,6 +28,7 @@ import (
"time" "time"
"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"
"github.com/ceph/go-ceph/rados" "github.com/ceph/go-ceph/rados"
@ -1920,3 +1921,15 @@ func genVolFromVolIDWithMigration(
return rv, err return rv, err
} }
// setVolumeMetadata set PV/PVC/PVCNamespace metadata on RBD image.
func (rv *rbdVolume) setVolumeMetadata(parameters map[string]string) error {
for k, v := range k8s.GetVolumeMetadata(parameters) {
err := rv.SetMetadata(k, v)
if err != nil {
return fmt.Errorf("failed to set metadata key %q, value %q on image: %w", k, v, err)
}
}
return nil
}

View File

@ -23,7 +23,12 @@ import (
// to the driver on CreateVolumeRequest/CreateSnapshotRequest calls. // to the driver on CreateVolumeRequest/CreateSnapshotRequest calls.
const ( const (
csiParameterPrefix = "csi.storage.k8s.io/" csiParameterPrefix = "csi.storage.k8s.io/"
pvcNamespaceKey = "csi.storage.k8s.io/pvc/namespace"
// PV and PVC metadata keys used by external provisioner as part of
// create requests as parameters, when `extra-create-metadata` is true.
pvcNameKey = "csi.storage.k8s.io/pvc/name"
pvcNamespaceKey = "csi.storage.k8s.io/pvc/namespace"
pvNameKey = "csi.storage.k8s.io/pv/name"
) )
// RemoveCSIPrefixedParameters removes parameters prefixed with csiParameterPrefix. // RemoveCSIPrefixedParameters removes parameters prefixed with csiParameterPrefix.
@ -43,3 +48,18 @@ func RemoveCSIPrefixedParameters(param map[string]string) map[string]string {
func GetOwner(param map[string]string) string { func GetOwner(param map[string]string) string {
return param[pvcNamespaceKey] return param[pvcNamespaceKey]
} }
// GetVolumeMetadata filter parameters, only return PV/PVC/PVCNamespace metadata.
func GetVolumeMetadata(parameters map[string]string) map[string]string {
keys := []string{pvcNameKey, pvcNamespaceKey, pvNameKey}
newParam := map[string]string{}
for k, v := range parameters {
for _, key := range keys {
if strings.Contains(k, key) {
newParam[k] = v
}
}
}
return newParam
}