Skip to content

Commit fe6c644

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 fe6c644

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