@@ -63,6 +63,32 @@ import (
63
63
k8s "sigs.k8s.io/vsphere-csi-driver/v3/pkg/kubernetes"
64
64
)
65
65
66
+ // DatastoreRetriever interface abstracts vSphere datastore operations for testability
67
+ type DatastoreRetriever interface {
68
+ // GetCandidateDatastoresInCluster retrieves candidate datastores for a specific cluster
69
+ GetCandidateDatastoresInCluster (ctx context.Context , vc * cnsvsphere.VirtualCenter , clusterID string ,
70
+ includeVSANDirect bool ) ([]* cnsvsphere.DatastoreInfo , []* cnsvsphere.DatastoreInfo , error )
71
+ // GetSharedDatastoresInClusters retrieves shared datastores across multiple clusters
72
+ GetSharedDatastoresInClusters (ctx context.Context , clusterMorefs []string ,
73
+ vc * cnsvsphere.VirtualCenter ) ([]* cnsvsphere.DatastoreInfo , error )
74
+ }
75
+
76
+ // VSphereDatastoreRetriever implements DatastoreRetriever using real vSphere operations
77
+ type VSphereDatastoreRetriever struct {}
78
+
79
+ // GetCandidateDatastoresInCluster implements DatastoreRetriever interface
80
+ func (v * VSphereDatastoreRetriever ) GetCandidateDatastoresInCluster (ctx context.Context ,
81
+ vc * cnsvsphere.VirtualCenter , clusterID string , includeVSANDirect bool ) ([]* cnsvsphere.DatastoreInfo ,
82
+ []* cnsvsphere.DatastoreInfo , error ) {
83
+ return cnsvsphere .GetCandidateDatastoresInCluster (ctx , vc , clusterID , includeVSANDirect )
84
+ }
85
+
86
+ // GetSharedDatastoresInClusters implements DatastoreRetriever interface
87
+ func (v * VSphereDatastoreRetriever ) GetSharedDatastoresInClusters (ctx context.Context ,
88
+ clusterMorefs []string , vc * cnsvsphere.VirtualCenter ) ([]* cnsvsphere.DatastoreInfo , error ) {
89
+ return getSharedDatastoresInClusters (ctx , clusterMorefs , vc )
90
+ }
91
+
66
92
var (
67
93
// controllerVolumeTopologyInstance is a singleton instance of controllerVolumeTopology
68
94
// created for vanilla flavor.
@@ -164,6 +190,8 @@ type wcpControllerVolumeTopology struct {
164
190
k8sConfig * restclient.Config
165
191
// azInformer is an informer instance on the AvailabilityZone custom resource.
166
192
azInformer cache.SharedIndexInformer
193
+ // datastoreRetriever provides abstraction for vSphere datastore operations
194
+ datastoreRetriever DatastoreRetriever
167
195
}
168
196
169
197
// InitTopologyServiceInController returns a singleton implementation of the
@@ -279,8 +307,9 @@ func (c *K8sOrchestrator) InitTopologyServiceInController(ctx context.Context) (
279
307
return nil , err
280
308
}
281
309
wcpControllerVolumeTopologyInstance = & wcpControllerVolumeTopology {
282
- k8sConfig : config ,
283
- azInformer : * azInformer ,
310
+ k8sConfig : config ,
311
+ azInformer : * azInformer ,
312
+ datastoreRetriever : & VSphereDatastoreRetriever {},
284
313
}
285
314
}
286
315
} else {
@@ -1504,24 +1533,32 @@ func (volTopology *wcpControllerVolumeTopology) GetSharedDatastoresInTopology(ct
1504
1533
// Call GetCandidateDatastores for each cluster moref. Ignore the vsanDirectDatastores for now.
1505
1534
if ! isPodVMOnStretchedSupervisorEnabled {
1506
1535
// This code block assume we have 1 Cluster Per AZ
1507
- accessibleDs , _ , err := cnsvsphere .GetCandidateDatastoresInCluster (ctx , params .Vc , clusterMorefs [0 ], false )
1536
+ accessibleDs , _ , err := volTopology .datastoreRetriever .GetCandidateDatastoresInCluster (ctx ,
1537
+ params .Vc , clusterMorefs [0 ], false )
1508
1538
if err != nil {
1509
1539
return nil , logger .LogNewErrorf (log ,
1510
1540
"failed to find candidate datastores to place volume in cluster %q. Error: %v" ,
1511
1541
clusterMorefs [0 ], err )
1512
1542
}
1513
1543
params .TopoSegToDatastoresMap [zone ] = accessibleDs
1514
1544
sharedDatastores = append (sharedDatastores , accessibleDs ... )
1545
+ log .Debugf ("PodVMOnStretchedSupervisor Not Enabled. Topology: %+v, Zone: %s, " +
1546
+ "Topology Segments to Datastore Map: %+v, Datastores: %v" ,
1547
+ params .TopologyRequirement , zone , params .TopoSegToDatastoresMap , sharedDatastores )
1515
1548
} else {
1516
1549
// This code block adds support for multiple vSphere Clusters Per AZ
1517
1550
// sharedDatastores will be calculated for all clusters within AZ
1518
- sharedDatastoresForclusterMorefs , err := getSharedDatastoresInClusters (ctx , clusterMorefs , params .Vc )
1551
+ sharedDatastoresForclusterMorefs , err := volTopology .datastoreRetriever .GetSharedDatastoresInClusters (ctx ,
1552
+ clusterMorefs , params .Vc )
1519
1553
if err != nil {
1520
1554
return nil , logger .LogNewErrorf (log , "failed to get shared datastores " +
1521
1555
"for clusters: %v, err: %v" , clusterMorefs , err )
1522
1556
}
1523
1557
params .TopoSegToDatastoresMap [zone ] = sharedDatastoresForclusterMorefs
1524
1558
sharedDatastores = append (sharedDatastores , sharedDatastoresForclusterMorefs ... )
1559
+ log .Debugf ("PodVMOnStretchedSupervisor Enabled. Topology: %+v, Zone: %s, " +
1560
+ "Topology Segments to Datastore Map: %+v, Datastores: %v" ,
1561
+ params .TopologyRequirement , zone , params .TopoSegToDatastoresMap , sharedDatastores )
1525
1562
}
1526
1563
}
1527
1564
log .Infof ("Shared datastores %v for topologyRequirement: %+v" , sharedDatastores ,
0 commit comments