Skip to content

Commit 2e42ee7

Browse files
authored
Initialize volume manager in WORKLOAD cluster mode (#3598)
Signed-off-by: Deepak Kinni <[email protected]>
1 parent d9a8919 commit 2e42ee7

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

pkg/syncer/syncer_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
cnsconfig "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/config"
4242
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/unittestcommon"
4343
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common"
44+
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common/commonco"
4445
csitypes "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/types"
4546
k8s "sigs.k8s.io/vsphere-csi-driver/v3/pkg/kubernetes"
4647
)
@@ -216,6 +217,7 @@ func TestSyncerWorkflows(t *testing.T) {
216217
dsList = append(dsList, datastoreInfoObj.Datastore.Reference())
217218
runTestMetadataSyncInformer(t)
218219
runTestFullSyncWorkflows(t)
220+
runTestCsiFullSync_WorkloadCluster(t)
219221
t.Log("TestSyncerWorkflows: end")
220222
}
221223

@@ -991,3 +993,47 @@ func TestGetVCForTopologySegments(t *testing.T) {
991993
})
992994
}
993995
}
996+
997+
// TestCsiFullSync_WorkloadCluster tests that CsiFullSync works correctly for WORKLOAD clusters
998+
// This test runs as a subtest within TestSyncerWorkflows to ensure proper initialization
999+
func runTestCsiFullSync_WorkloadCluster(t *testing.T) {
1000+
ctx, cancel := context.WithCancel(context.Background())
1001+
defer cancel()
1002+
1003+
// Initialize commonco.ContainerOrchestratorUtility for WORKLOAD cluster testing
1004+
// This is required for CsiFullSync to work properly with WORKLOAD clusters
1005+
fakeCommonCO, err := unittestcommon.GetFakeContainerOrchestratorInterface(common.Kubernetes)
1006+
if err != nil {
1007+
t.Fatalf("Failed to get fake CO interface: %v", err)
1008+
}
1009+
// Store the original value to restore later
1010+
originalCO := commonco.ContainerOrchestratorUtility
1011+
commonco.ContainerOrchestratorUtility = fakeCommonCO
1012+
defer func() {
1013+
commonco.ContainerOrchestratorUtility = originalCO
1014+
}()
1015+
1016+
// Create a WORKLOAD cluster metadataSyncer (note: volumeManagers map is NOT populated)
1017+
// This simulates the real WORKLOAD cluster setup where only volumeManager is set
1018+
workloadMetadataSyncer := &metadataSyncInformer{
1019+
clusterFlavor: cnstypes.CnsClusterFlavorWorkload,
1020+
volumeManager: volumeManager, // Use the initialized global volumeManager
1021+
host: virtualCenter.Config.Host,
1022+
configInfo: metadataSyncer.configInfo, // Use the global metadataSyncer's configInfo
1023+
coCommonInterface: metadataSyncer.coCommonInterface, // Use the global metadataSyncer's coCommonInterface
1024+
// Copy the Kubernetes listers from the global metadataSyncer
1025+
pvLister: metadataSyncer.pvLister,
1026+
pvcLister: metadataSyncer.pvcLister,
1027+
podLister: metadataSyncer.podLister,
1028+
// volumeManagers map is intentionally NOT populated to simulate real WORKLOAD cluster
1029+
}
1030+
1031+
// Test that CsiFullSync works for WORKLOAD cluster - this is the high-level test
1032+
// that verifies the entire full sync flow doesn't fail due to the regression
1033+
err = CsiFullSync(ctx, workloadMetadataSyncer, virtualCenter.Config.Host)
1034+
if err != nil {
1035+
t.Errorf("CsiFullSync failed for WORKLOAD cluster: %v", err)
1036+
}
1037+
1038+
t.Logf("CsiFullSync completed successfully for WORKLOAD cluster")
1039+
}

pkg/syncer/util.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,16 @@ func getVcHostAndVolumeManagerForVolumeID(ctx context.Context,
429429
log := logger.GetLogger(ctx)
430430
log.Debugf("Getting VC from in-memory map for volume %s", volumeID)
431431

432+
// For WORKLOAD cluster type, return the single volumeManager since
433+
// volumeManagers map is not populated
434+
if metadataSyncer.clusterFlavor == cnstypes.CnsClusterFlavorWorkload {
435+
if metadataSyncer.volumeManager == nil {
436+
return "", nil, logger.LogNewErrorf(log,
437+
"volume manager not initialized for WORKLOAD cluster")
438+
}
439+
return metadataSyncer.host, metadataSyncer.volumeManager, nil
440+
}
441+
432442
if len(metadataSyncer.configInfo.Cfg.VirtualCenter) == 1 {
433443
vCenter := metadataSyncer.configInfo.Cfg.Global.VCenterIP
434444
cnsVolumeMgr, volMgrFound := metadataSyncer.volumeManagers[vCenter]
@@ -530,6 +540,17 @@ func getVolManagerForVcHost(ctx context.Context, vc string,
530540
metadataSyncer *metadataSyncInformer) (volumes.Manager, error) {
531541
log := logger.GetLogger(ctx)
532542

543+
// For WORKLOAD cluster type, return the single volumeManager since
544+
// volumeManagers map is not populated
545+
if metadataSyncer.clusterFlavor == cnstypes.CnsClusterFlavorWorkload {
546+
if metadataSyncer.volumeManager == nil {
547+
return nil, logger.LogNewErrorf(log,
548+
"volume manager not initialized for WORKLOAD cluster")
549+
}
550+
return metadataSyncer.volumeManager, nil
551+
}
552+
553+
// For other cluster types (Vanilla, Guest), use the volumeManagers map
533554
cnsVolumeMgr, volMgrFound := metadataSyncer.volumeManagers[vc]
534555
if !volMgrFound {
535556
return nil, logger.LogNewErrorf(log,

0 commit comments

Comments
 (0)