@@ -187,15 +187,20 @@ func CsiFullSync(ctx context.Context, metadataSyncer *metadataSyncInformer, vc s
187187 return err
188188 }
189189
190- // Iterate through all the k8sPVs to find all PVs with node affinity missing and
191- // patch such PVs and their corresponding PVCs with topology discovered
192- if metadataSyncer .clusterFlavor == cnstypes .CnsClusterFlavorWorkload &&
193- IsWorkloadDomainIsolationSupported {
194- k8sClient , err := k8s .NewClient (ctx )
190+ // Create k8sClient once for workload cluster operations to optimize performance
191+ var k8sClient clientset.Interface
192+ if metadataSyncer .clusterFlavor == cnstypes .CnsClusterFlavorWorkload {
193+ k8sClient , err = k8s .NewClient (ctx )
195194 if err != nil {
196195 log .Errorf ("FullSync for VC %s: Failed to create kubernetes client. Err: %+v" , vc , err )
197196 return err
198197 }
198+ }
199+
200+ // Iterate through all the k8sPVs to find all PVs with node affinity missing and
201+ // patch such PVs and their corresponding PVCs with topology discovered
202+ if metadataSyncer .clusterFlavor == cnstypes .CnsClusterFlavorWorkload &&
203+ IsWorkloadDomainIsolationSupported {
199204 var pvWithMissingNodeAffinityList [](* v1.PersistentVolume )
200205 for _ , pv := range k8sPVs {
201206 if pv .Spec .NodeAffinity == nil {
@@ -226,11 +231,7 @@ func CsiFullSync(ctx context.Context, metadataSyncer *metadataSyncInformer, vc s
226231 // Iterate over all the file volume PVCs and check if file share export paths are added as annotations
227232 // on it. If not added, then add file share export path annotations on such PVCs.
228233 if metadataSyncer .clusterFlavor == cnstypes .CnsClusterFlavorWorkload {
229- k8sClient , err := k8sNewClient (ctx )
230- if err != nil {
231- log .Errorf ("FullSync for VC %s: Failed to create kubernetes client. Err: %+v" , vc , err )
232- return err
233- }
234+ // Reuse the existing k8sClient instead of creating a new one
234235 for _ , pv := range k8sPVs {
235236 if IsFileVolume (pv ) {
236237 if pvc , ok := pvToPVCMap [pv .Name ]; ok {
0 commit comments