@@ -77,8 +77,12 @@ type TopologyNodeFinder struct {
7777}
7878
7979type instanceMapper interface {
80+ // Instances2NodeMap receives a list of SLURM node names and returns a map of
81+ // the service provider assigned compute instance IDs to the node names
8082 Instances2NodeMap (context.Context , []string ) (map [string ]string , error )
81- GetComputeInstancesRegion (context.Context ) (string , error )
83+ // GetInstancesRegions receives a list of SLURM node names and returns a map
84+ // of node names to their deployed regions
85+ GetInstancesRegions (context.Context , []string ) (map [string ]string , error )
8286}
8387
8488var (
@@ -114,22 +118,50 @@ func (eng *SlurmEngine) GetComputeInstances(ctx context.Context, environment eng
114118 return nil , err
115119 }
116120
121+ if len (nodes ) == 0 {
122+ return nil , nil
123+ }
124+
117125 i2n , err := instanceMapper .Instances2NodeMap (ctx , nodes )
118126 if err != nil {
119127 return nil , err
120128 }
121129 klog .V (4 ).Infof ("Detected instance map: %v" , i2n )
122130
123- region , err := instanceMapper .GetComputeInstancesRegion (ctx )
131+ nodeRegions , err := instanceMapper .GetInstancesRegions (ctx , nodes )
124132 if err != nil {
125133 return nil , err
126134 }
127- klog .V (4 ).Infof ("Detected region: %s" , region )
128135
129- return []topology.ComputeInstances {{
130- Region : region ,
131- Instances : i2n ,
132- }}, nil
136+ return aggregateComputeInstances (i2n , nodeRegions ), nil
137+ }
138+
139+ func aggregateComputeInstances (i2n , nodeRegions map [string ]string ) []topology.ComputeInstances {
140+ // regions maps region name to the corresponding index in "cis"
141+ regions := make (map [string ]int )
142+ cis := []topology.ComputeInstances {}
143+
144+ for instance , node := range i2n {
145+ region , ok := nodeRegions [node ]
146+ if ! ok {
147+ klog .Warningf ("Failed to find region for node %s" , node )
148+ continue
149+ }
150+ indx , ok := regions [region ]
151+ if ! ok {
152+ indx = len (regions )
153+ regions [region ] = indx
154+ cis = append (cis , topology.ComputeInstances {
155+ Region : region ,
156+ Instances : map [string ]string {instance : node },
157+ })
158+ } else {
159+ cis [indx ].Instances [instance ] = node
160+ }
161+ }
162+ klog .V (4 ).Infof ("Detected regions: %v" , regions )
163+
164+ return cis
133165}
134166
135167func GetNodeList (ctx context.Context ) ([]string , error ) {
0 commit comments