external-provisioner icon indicating copy to clipboard operation
external-provisioner copied to clipboard

Add PVC Annotations + Labels to PV

Open speedfl opened this issue 3 years ago • 0 comments

Problem

Correlate PV with a context is almost impossible. The only solution is to create StorageClass with Contextual Data (Cost Center / Customer / Environment) and then correlate the PV with the StorageClass. This results in a duplication of StorageClass while its purpose is the "how to store data" (Azure Disk, AWS EBS...) and not the "for who/what data are stored".

Proposal

We should have an elegant way to Add PVC / Labels annotations on PV such as Cost Center, Customer, Billing ID... https://github.com/kubernetes-csi/external-provisioner/blob/master/pkg/controller/controller.go

Possible Solution:

  • StorageClass object would contain field to mention the tag to forward to PV (preferred)
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: ebs-sc
provisioner: some-provisionner
fromPVCToPV: # I am bad finding names :D 
  labels:
    - label-one
    - label-two
  annotations:
    - annotation-one
    - annotation-two
parameters:
pv := &v1.PersistentVolume{
    ObjectMeta: metav1.ObjectMeta{
        Name: pvName,
    },
    Spec: v1.PersistentVolumeSpec{
        AccessModes:  options.PVC.Spec.AccessModes,
        MountOptions: options.StorageClass.MountOptions,
        Capacity: v1.ResourceList{
            v1.ResourceName(v1.ResourceStorage): bytesToQuantity(respCap),
        },
        // TODO wait for CSI VolumeSource API
        PersistentVolumeSource: v1.PersistentVolumeSource{
            CSI: result.csiPVSource,
        },
    },
}

if options.StorageClass.fromPVCToPV != nil {
    options.StorageClass.fromPVCToPV.Annotations != nil {
        for _, key := range options.StorageClass.fromPVCToPV.Annotations {

            if options.PVC.ObjectMeta.Annotations != nil && val, ok := appSet.ObjectMeta.Annotations[key]; ok {
                metav1.SetMetaDataAnnotation(&pv.ObjectMeta, key, val)
            }
        }
    }
   // same for labels
}

Other

There is another issue to forward the Annotations/Labels to CreateVolumeRequest which goes in this direction: https://github.com/kubernetes-csi/external-provisioner/issues/86

speedfl avatar Jul 28 '22 13:07 speedfl