Skip to content

Commit c32e756

Browse files
committed
Add file share export paths to volume attributes so that CSI provisioner can add them as PVC annotations
1 parent 08a7bd8 commit c32e756

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

pkg/csi/service/common/constants.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,18 @@ const (
144144
// UnknownVolumeType is assigned to CNS volumes whose type couldn't be determined.
145145
UnknownVolumeType = "UNKNOWN"
146146

147+
// Nfsv3AccessPointKey is the key for NFSv3 access point.
148+
Nfsv3AccessPointKey = "NFSv3"
149+
147150
// Nfsv4AccessPointKey is the key for NFSv4 access point.
148151
Nfsv4AccessPointKey = "NFSv4.1"
149152

153+
// Nfsv3ExportPathAnnotationKey specifies the NFSv3 export path annotation key on PVC
154+
Nfsv3ExportPathAnnotationKey = "csi.vsphere.exportpath.nfs3"
155+
156+
// Nfsv4ExportPathAnnotationKey specifies the NFSv4.1 export path annotation key on PVC
157+
Nfsv4ExportPathAnnotationKey = "csi.vsphere.exportpath.nfs41"
158+
150159
// Nfsv4AccessPoint is the access point of file volume.
151160
Nfsv4AccessPoint = "Nfsv4AccessPoint"
152161

pkg/csi/service/wcp/controller.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,31 @@ func (c *controller) createFileVolume(ctx context.Context, req *csi.CreateVolume
15031503
attributes := make(map[string]string)
15041504
attributes[common.AttributeDiskType] = common.DiskTypeFileVolume
15051505

1506+
// Return NFSv3 and NFSv4.1 export paths in volume context attributes.
1507+
// CSI provisioner will add these export paths as PVC annotations.
1508+
// Make a QueryVolume call to fetch file share export paths.
1509+
querySelection := cnstypes.CnsQuerySelection{
1510+
Names: []string{
1511+
string(cnstypes.QuerySelectionNameTypeBackingObjectDetails),
1512+
},
1513+
}
1514+
volume, err := common.QueryVolumeByID(ctx, c.manager.VolumeManager,
1515+
volumeInfo.VolumeID.Id, &querySelection)
1516+
if err != nil {
1517+
log.Errorf("error while performing QueryVolume on volume %s, error: %+v",
1518+
volumeInfo.VolumeID.Id, err)
1519+
}
1520+
vSANFileBackingDetails := volume.BackingObjectDetails.(*cnstypes.CnsVsanFileShareBackingDetails)
1521+
for _, kv := range vSANFileBackingDetails.AccessPoints {
1522+
if kv.Key == common.Nfsv3AccessPointKey {
1523+
attributes[common.Nfsv3ExportPathAnnotationKey] = kv.Value
1524+
} else if kv.Key == common.Nfsv4AccessPointKey {
1525+
attributes[common.Nfsv4ExportPathAnnotationKey] = kv.Value
1526+
}
1527+
}
1528+
log.Debugf("Access point details for volume: %s, namespace: %s are %+v", req.Name,
1529+
req.Parameters[common.AttributePvcNamespace], attributes)
1530+
15061531
resp := &csi.CreateVolumeResponse{
15071532
Volume: &csi.Volume{
15081533
VolumeId: volumeInfo.VolumeID.Id,

pkg/syncer/fullsync.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,32 @@ func CsiFullSync(ctx context.Context, metadataSyncer *metadataSyncInformer, vc s
224224
}
225225
}
226226

227+
// Iterate over all the file volume PVCs and check if file share export paths are added as annotations
228+
// on it. If not added, then add file share export path annotations on such PVCs.
229+
if metadataSyncer.clusterFlavor == cnstypes.CnsClusterFlavorWorkload {
230+
k8sClient, err := k8sNewClient(ctx)
231+
if err != nil {
232+
log.Errorf("FullSync for VC %s: Failed to create kubernetes client. Err: %+v", vc, err)
233+
return err
234+
}
235+
for _, pv := range k8sPVs {
236+
if IsFileVolume(pv) {
237+
if pvc, ok := pvToPVCMap[pv.Name]; ok {
238+
// Check if file share export path is available as annotation on PVC
239+
if pvc.ObjectMeta.Annotations[common.Nfsv3ExportPathAnnotationKey] == "" ||
240+
pvc.ObjectMeta.Annotations[common.Nfsv4ExportPathAnnotationKey] == "" {
241+
err = setFileShareAnnotationsOnPVC(ctx, k8sClient, metadataSyncer.volumeManager, pvc)
242+
if err != nil {
243+
log.Errorf("FullSync for VC %s: Failed to add file share export path annotations "+
244+
"for PVC %s, Err: %v. Continuing for other PVCs.", vc, pvc.Name, err)
245+
continue
246+
}
247+
}
248+
}
249+
}
250+
}
251+
}
252+
227253
var queryAllResult *cnstypes.CnsQueryResult
228254
if metadataSyncer.configInfo.Cfg.Global.ClusterID != "" {
229255
// Cluster ID is removed from vSphere Config Secret post 9.0 release in Supervisor
@@ -630,6 +656,59 @@ func patchVolumeAccessibleTopologyToPVC(ctx context.Context, k8sClient clientset
630656
return nil
631657
}
632658

659+
// setFileShareAnnotationsOnPVC sets file share export paths as annotation on file volume PVC if it
660+
// is not already added. In case of upgrade from old version to VC 9.1 compatible CSI driver, this
661+
// will add these annotations on all existing file volume PVCs.
662+
func setFileShareAnnotationsOnPVC(ctx context.Context, k8sClient clientset.Interface,
663+
volumeManager volumes.Manager, pvc *v1.PersistentVolumeClaim) error {
664+
log := logger.GetLogger(ctx)
665+
log.Infof("setFileShareAnnotationsOnPVC: Setting file share annotation for PVC: %q, namespace: %q",
666+
pvc.Name, pvc.Namespace)
667+
668+
pv, err := k8sClient.CoreV1().PersistentVolumes().Get(ctx, pvc.Spec.VolumeName, metav1.GetOptions{})
669+
if err != nil {
670+
log.Errorf("setFileShareAnnotationsOnPVC: failed to get PV for PVC: %q, namespace: %q",
671+
pvc.Name, pvc.Namespace)
672+
return err
673+
}
674+
675+
// Make a QueryVolume call to fetch file share export paths.
676+
querySelection := cnstypes.CnsQuerySelection{
677+
Names: []string{
678+
string(cnstypes.QuerySelectionNameTypeBackingObjectDetails),
679+
},
680+
}
681+
volume, err := common.QueryVolumeByID(ctx, volumeManager, pv.Spec.CSI.VolumeHandle, &querySelection)
682+
if err != nil {
683+
log.Errorf("setFileShareAnnotationsOnPVC: Error while performing QueryVolume on volume %s, Err: %+v",
684+
pv.Spec.CSI.VolumeHandle, err)
685+
}
686+
vSANFileBackingDetails := volume.BackingObjectDetails.(*cnstypes.CnsVsanFileShareBackingDetails)
687+
accessPoints := make(map[string]string)
688+
for _, kv := range vSANFileBackingDetails.AccessPoints {
689+
if kv.Key == common.Nfsv3AccessPointKey {
690+
pvc.Annotations[common.Nfsv3ExportPathAnnotationKey] = kv.Value
691+
} else if kv.Key == common.Nfsv4AccessPointKey {
692+
pvc.Annotations[common.Nfsv4ExportPathAnnotationKey] = kv.Value
693+
}
694+
accessPoints[kv.Key] = kv.Value
695+
}
696+
log.Debugf("setFileShareAnnotationsOnPVC: Access point details for PVC: %q, namespace: %q are %+v",
697+
pvc.Name, pvc.Namespace, accessPoints)
698+
699+
// Update PVC to add annotation on it
700+
pvc, err = k8sClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Update(ctx, pvc,
701+
metav1.UpdateOptions{})
702+
if err != nil {
703+
log.Errorf("setFileShareAnnotationsOnPVC: Error updating PVC %q in namespace %q, Err: %v",
704+
pvc.Name, pvc.Namespace, err)
705+
return err
706+
}
707+
log.Infof("setFileShareAnnotationsOnPVC: Added file share export paths annotation successfully on PVC %q, "+
708+
"namespce %q", pvc.Name, pvc.Namespace)
709+
return nil
710+
}
711+
633712
// cleanUpVolumeInfoCrDeletionMap removes volumes from the VolumeInfo CR deletion map
634713
// if the volume is present in the K8s cluster.
635714
// This may happen if is not yet created PV by the time full sync

pkg/syncer/syncer_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,15 @@ func runTestCsiFullSync_WorkloadCluster(t *testing.T) {
10131013
commonco.ContainerOrchestratorUtility = originalCO
10141014
}()
10151015

1016+
// Store the original K8sNewclient to restore later
1017+
origK8sClient := k8sNewClient
1018+
defer func() {
1019+
k8sNewClient = origK8sClient
1020+
}()
1021+
k8sNewClient = func(ctx context.Context) (clientset.Interface, error) {
1022+
return k8sclient, nil
1023+
}
1024+
10161025
// Create a WORKLOAD cluster metadataSyncer (note: volumeManagers map is NOT populated)
10171026
// This simulates the real WORKLOAD cluster setup where only volumeManager is set
10181027
workloadMetadataSyncer := &metadataSyncInformer{

0 commit comments

Comments
 (0)