Skip to content

Commit 98a961b

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 98a961b

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,34 @@ 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.QuerySelectionNameTypeVolumeType),
1512+
string(cnstypes.QuerySelectionNameTypeBackingObjectDetails),
1513+
},
1514+
}
1515+
volume, err := common.QueryVolumeByID(ctx, c.manager.VolumeManager,
1516+
volumeInfo.VolumeID.Id, &querySelection)
1517+
if err != nil {
1518+
log.Errorf("error while performing QueryVolume on volume %s, error: %+v",
1519+
volumeInfo.VolumeID.Id, err)
1520+
}
1521+
vSANFileBackingDetails := volume.BackingObjectDetails.(*cnstypes.CnsVsanFileShareBackingDetails)
1522+
accessPoints := make(map[string]string)
1523+
for _, kv := range vSANFileBackingDetails.AccessPoints {
1524+
if kv.Key == common.Nfsv3AccessPointKey {
1525+
attributes[common.Nfsv3ExportPathAnnotationKey] = kv.Value
1526+
} else if kv.Key == common.Nfsv4AccessPointKey {
1527+
attributes[common.Nfsv4ExportPathAnnotationKey] = kv.Value
1528+
}
1529+
accessPoints[kv.Key] = kv.Value
1530+
}
1531+
log.Debugf("Access point details for volume: %s, namespace: %s are %+v", req.Name,
1532+
req.Parameters[common.AttributePvcNamespace], accessPoints)
1533+
15061534
resp := &csi.CreateVolumeResponse{
15071535
Volume: &csi.Volume{
15081536
VolumeId: volumeInfo.VolumeID.Id,

pkg/syncer/fullsync.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,31 @@ 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+
err = setFileShareAnnotationsOnPVC(ctx, k8sClient, metadataSyncer.volumeManager, pvc)
241+
if err != nil {
242+
log.Errorf("FullSync for VC %s: Failed to add file share export path annotations "+
243+
"for PVC %s, Err: %v. Continuing for other PVCs.", pvc.Name, err)
244+
continue
245+
}
246+
}
247+
}
248+
}
249+
}
250+
}
251+
227252
var queryAllResult *cnstypes.CnsQueryResult
228253
if metadataSyncer.configInfo.Cfg.Global.ClusterID != "" {
229254
// Cluster ID is removed from vSphere Config Secret post 9.0 release in Supervisor
@@ -630,6 +655,58 @@ func patchVolumeAccessibleTopologyToPVC(ctx context.Context, k8sClient clientset
630655
return nil
631656
}
632657

658+
func setFileShareAnnotationsOnPVC(ctx context.Context, k8sClient clientset.Interface,
659+
volumeManager volumes.Manager, pvc *v1.PersistentVolumeClaim) error {
660+
log := logger.GetLogger(ctx)
661+
log.Infof("setFileShareAnnotationsOnPVC: Setting file share annotation for PVC: %q, namespace: %q",
662+
pvc.Name, pvc.Namespace)
663+
664+
namespacedPvcName := pvc.Namespace + "/" + pvc.Name
665+
volumeId, ok := commonco.ContainerOrchestratorUtility.GetVolumeIDFromPVCName(namespacedPvcName)
666+
if !ok {
667+
errmsg := fmt.Sprintf("failed to get volumeId for PVC: %q, namespace: %q", pvc.Name, pvc.Namespace)
668+
log.Errorf("setFileShareAnnotationsOnPVC: %s", errmsg)
669+
return errors.New(errmsg)
670+
}
671+
672+
// Make a QueryVolume call to fetch file share export paths.
673+
querySelection := cnstypes.CnsQuerySelection{
674+
Names: []string{
675+
string(cnstypes.QuerySelectionNameTypeVolumeType),
676+
string(cnstypes.QuerySelectionNameTypeBackingObjectDetails),
677+
},
678+
}
679+
volume, err := common.QueryVolumeByID(ctx, volumeManager, volumeId, &querySelection)
680+
if err != nil {
681+
log.Errorf("setFileShareAnnotationsOnPVC: Error while performing QueryVolume on volume %s, Err: %+v",
682+
volumeId, err)
683+
}
684+
vSANFileBackingDetails := volume.BackingObjectDetails.(*cnstypes.CnsVsanFileShareBackingDetails)
685+
accessPoints := make(map[string]string)
686+
for _, kv := range vSANFileBackingDetails.AccessPoints {
687+
if kv.Key == common.Nfsv3AccessPointKey {
688+
pvc.Annotations[common.Nfsv3ExportPathAnnotationKey] = kv.Value
689+
} else if kv.Key == common.Nfsv4AccessPointKey {
690+
pvc.Annotations[common.Nfsv4ExportPathAnnotationKey] = kv.Value
691+
}
692+
accessPoints[kv.Key] = kv.Value
693+
}
694+
log.Debugf("setFileShareAnnotationsOnPVC: Access point details for PVC: %q, namespace: %q are %+v",
695+
pvc.Name, pvc.Namespace, accessPoints)
696+
697+
// Update PVC to add annotation on it
698+
pvc, err = k8sClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Update(ctx, pvc,
699+
metav1.UpdateOptions{})
700+
if err != nil {
701+
log.Errorf("setFileShareAnnotationsOnPVC: Error updating PVC %q in namespace %q, Err: %v",
702+
pvc.Name, pvc.Namespace, err)
703+
return err
704+
}
705+
log.Infof("setFileShareAnnotationsOnPVC: Added file share export paths annotation successfully on PVC %q, "+
706+
"namespce %q", pvc.Name, pvc.Namespace)
707+
return nil
708+
}
709+
633710
// cleanUpVolumeInfoCrDeletionMap removes volumes from the VolumeInfo CR deletion map
634711
// if the volume is present in the K8s cluster.
635712
// 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)