@@ -6,6 +6,7 @@ package placement
66
77import (
88 "context"
9+ "fmt"
910
1011 "github.com/vmware/govmomi/find"
1112 "github.com/vmware/govmomi/vim25"
@@ -23,30 +24,49 @@ func GroupPlacement(
2324 namespace , childRPName string ,
2425 configSpecs []vimtypes.VirtualMachineConfigSpec ) (map [string ]Result , error ) {
2526
26- candidates , resourcePoolToZoneName , err := getPlacementCandidates (ctx , client , vcClient , "" , namespace , childRPName )
27+ candidates , err := getPlacementCandidates (ctx , client , vcClient , "" , namespace , childRPName )
2728 if err != nil {
28- return nil , err
29+ return nil , fmt . Errorf ( "failed to get placement candidates: %w" , err )
2930 }
3031
3132 if len (candidates ) == 0 {
3233 return nil , ErrNoPlacementCandidates
3334 }
3435
35- recommendations , err := getGroupPlacementRecommendations (ctx , vcClient , finder , candidates , configSpecs )
36+ needDatastorePlacement := pkgcfg .FromContext (ctx ).Features .FastDeploy
37+ recommendations , err := getGroupPlacementRecommendations (
38+ ctx ,
39+ vcClient ,
40+ finder ,
41+ candidates ,
42+ configSpecs ,
43+ needDatastorePlacement )
3644 if err != nil {
3745 return nil , err
3846 }
3947
40- results := map [string ]Result {}
48+ resourcePoolToZoneName := make (map [string ]string , len (candidates ))
49+ for zoneName , rpMoIDs := range candidates {
50+ for _ , rpMoID := range rpMoIDs {
51+ resourcePoolToZoneName [rpMoID ] = zoneName
52+ }
53+ }
54+
55+ results := make (map [string ]Result , len (recommendations ))
4156 for vmName , recommendation := range recommendations {
42- if pkgcfg . FromContext ( ctx ). Features . FastDeploy {
57+ if needDatastorePlacement {
4358 // Get the name and type of the datastores.
4459 if err := getDatastoreProperties (ctx , vcClient , & recommendation ); err != nil {
4560 return nil , err
4661 }
4762 }
4863
49- zoneName := resourcePoolToZoneName [recommendation .PoolMoRef .Value ]
64+ zoneName , ok := resourcePoolToZoneName [recommendation .PoolMoRef .Value ]
65+ if ! ok {
66+ // This should never happen: placement returned a non-candidate RP.
67+ return nil , fmt .Errorf ("no zone assignment for ResourcePool %s" ,
68+ recommendation .PoolMoRef .Value )
69+ }
5070
5171 result := Result {
5272 ZoneName : zoneName ,
@@ -66,7 +86,8 @@ func getGroupPlacementRecommendations(
6686 vcClient * vim25.Client ,
6787 finder * find.Finder ,
6888 candidates map [string ][]string ,
69- configSpecs []vimtypes.VirtualMachineConfigSpec ) (map [string ]Recommendation , error ) {
89+ configSpecs []vimtypes.VirtualMachineConfigSpec ,
90+ needDatastorePlacement bool ) (map [string ]Recommendation , error ) {
7091
7192 var candidateRPMoRefs []vimtypes.ManagedObjectReference
7293
@@ -80,12 +101,11 @@ func getGroupPlacementRecommendations(
80101 }
81102 }
82103
83- return ClusterPlaceVMForCreate (
104+ return getClusterPlacementRecommendations (
84105 ctx ,
85106 vcClient ,
86107 finder ,
87108 candidateRPMoRefs ,
88109 configSpecs ,
89- false ,
90- true )
110+ needDatastorePlacement )
91111}
0 commit comments