@@ -66,6 +66,8 @@ const (
66
66
annCSIvSphereVolumeAccessibleTopology = "csi.vsphere.volume-accessible-topology"
67
67
)
68
68
69
+ var k8sNewClient = k8s .NewClient
70
+
69
71
// CsiFullSync reconciles volume metadata on a vanilla k8s cluster with volume
70
72
// metadata on CNS.
71
73
func CsiFullSync (ctx context.Context , metadataSyncer * metadataSyncInformer , vc string ) error {
@@ -224,6 +226,31 @@ func CsiFullSync(ctx context.Context, metadataSyncer *metadataSyncInformer, vc s
224
226
}
225
227
}
226
228
229
+ // Iterate over all the file volume PVCs and check if file share export paths are added as annotations
230
+ // on it. If not added, then add file share export path annotations on such PVCs.
231
+ if metadataSyncer .clusterFlavor == cnstypes .CnsClusterFlavorWorkload {
232
+ k8sClient , err := k8sNewClient (ctx )
233
+ if err != nil {
234
+ log .Errorf ("FullSync for VC %s: Failed to create kubernetes client. Err: %+v" , vc , err )
235
+ return err
236
+ }
237
+ for _ , pv := range k8sPVs {
238
+ if IsFileVolume (pv ) {
239
+ if pvc , ok := pvToPVCMap [pv .Name ]; ok {
240
+ // Check if file share export path is available as annotation on PVC
241
+ if pvc .ObjectMeta .Annotations [common .Nfsv3ExportPathAnnotationKey ] == "" {
242
+ err = setFileShareAnnotationsOnPVC (ctx , k8sClient , metadataSyncer .volumeManager , pvc )
243
+ if err != nil {
244
+ log .Errorf ("FullSync for VC %s: Failed to add file share export path annotations " +
245
+ "for PVC %s, Err: %v. Continuing for other PVCs." , pvc .Name , err )
246
+ continue
247
+ }
248
+ }
249
+ }
250
+ }
251
+ }
252
+ }
253
+
227
254
var queryAllResult * cnstypes.CnsQueryResult
228
255
if metadataSyncer .configInfo .Cfg .Global .ClusterID != "" {
229
256
// Cluster ID is removed from vSphere Config Secret post 9.0 release in Supervisor
@@ -630,6 +657,57 @@ func patchVolumeAccessibleTopologyToPVC(ctx context.Context, k8sClient clientset
630
657
return nil
631
658
}
632
659
660
+ func setFileShareAnnotationsOnPVC (ctx context.Context , k8sClient clientset.Interface ,
661
+ volumeManager volumes.Manager , pvc * v1.PersistentVolumeClaim ) error {
662
+ log := logger .GetLogger (ctx )
663
+ log .Infof ("setFileShareAnnotationsOnPVC: Setting file share annotation for pvc: %q, namespace: %q" ,
664
+ pvc .Name , pvc .Namespace )
665
+
666
+ pv , err := k8sClient .CoreV1 ().PersistentVolumes ().Get (ctx , pvc .Spec .VolumeName , metav1.GetOptions {})
667
+ if err != nil {
668
+ log .Errorf ("setFileShareAnnotationsOnPVC: Failed to get PV: %q , Err: %v." ,
669
+ pvc .Spec .VolumeName , err )
670
+ return err
671
+ }
672
+
673
+ // Make a QueryVolume call to fetch file share export paths.
674
+ querySelection := cnstypes.CnsQuerySelection {
675
+ Names : []string {
676
+ string (cnstypes .QuerySelectionNameTypeVolumeType ),
677
+ string (cnstypes .QuerySelectionNameTypeBackingObjectDetails ),
678
+ },
679
+ }
680
+ volume , err := common .QueryVolumeByID (ctx , volumeManager , pv .Spec .CSI .VolumeHandle , & querySelection )
681
+ if err != nil {
682
+ log .Errorf ("setFileShareAnnotationsOnPVC: Error while performing QueryVolume on volume %s, Err: %+v" ,
683
+ pv .Spec .CSI .VolumeHandle , err )
684
+ }
685
+ vSANFileBackingDetails := volume .BackingObjectDetails .(* cnstypes.CnsVsanFileShareBackingDetails )
686
+ accessPoints := make (map [string ]string )
687
+ for _ , kv := range vSANFileBackingDetails .AccessPoints {
688
+ if kv .Key == common .Nfsv3AccessPointKey {
689
+ pvc .Annotations [common .Nfsv3ExportPathAnnotationKey ] = kv .Value
690
+ } else if kv .Key == common .Nfsv4AccessPointKey {
691
+ pvc .Annotations [common .Nfsv4ExportPathAnnotationKey ] = kv .Value
692
+ }
693
+ accessPoints [kv .Key ] = kv .Value
694
+ }
695
+ log .Debugf ("setFileShareAnnotationsOnPVC: Access point details for pvc: %q, namespace: %q are %+v" ,
696
+ pvc .Name , pvc .Namespace , accessPoints )
697
+
698
+ // Update PVC to add annotation on it
699
+ pvc , err = k8sClient .CoreV1 ().PersistentVolumeClaims (pvc .Namespace ).Update (ctx , pvc ,
700
+ metav1.UpdateOptions {})
701
+ if err != nil {
702
+ log .Errorf ("setFileShareAnnotationsOnPVC: Error updating PVC %q in namespace %q, Err: %v" ,
703
+ pvc .Name , pvc .Namespace , err )
704
+ return err
705
+ }
706
+ log .Infof ("setFileShareAnnotationsOnPVC: Added file share export paths annotation successfully on PVC %q, " +
707
+ "namespce %q" , pvc .Name , pvc .Namespace )
708
+ return nil
709
+ }
710
+
633
711
// cleanUpVolumeInfoCrDeletionMap removes volumes from the VolumeInfo CR deletion map
634
712
// if the volume is present in the K8s cluster.
635
713
// This may happen if is not yet created PV by the time full sync
0 commit comments