@@ -191,16 +191,24 @@ func (r *VirtualMachineGroupReconciler) createOrUpdateVMG(ctx context.Context, c
191191 return reconcile.Result {RequeueAfter : reconciliationDelay }, nil
192192 }
193193
194- // Generate all the members of the VirtualMachineGroup.
195- members := make ([]vmoprv1.GroupMember , 0 , len (currentVSphereMachines ))
196- // Sort the VSphereMachines by name for consistent ordering
197- sort .Slice (currentVSphereMachines , func (i , j int ) bool {
198- return currentVSphereMachines [i ].Name < currentVSphereMachines [j ].Name
194+ // Generate VM names according to the naming strategy set on the VSphereMachine.
195+ vmNames := make ([]string , 0 , len (currentVSphereMachines ))
196+ for _ , machine := range currentVSphereMachines {
197+ name , err := GenerateVirtualMachineName (machine .Name , machine .Spec .NamingStrategy )
198+ if err != nil {
199+ return reconcile.Result {}, err
200+ }
201+ vmNames = append (vmNames , name )
202+ }
203+ // Sort the VM names alphabetically for consistent ordering
204+ sort .Slice (vmNames , func (i , j int ) bool {
205+ return vmNames [i ] < vmNames [j ]
199206 })
200207
201- for _ , vm := range currentVSphereMachines {
208+ members := make ([]vmoprv1.GroupMember , 0 , len (currentVSphereMachines ))
209+ for _ , name := range vmNames {
202210 members = append (members , vmoprv1.GroupMember {
203- Name : vm . Name ,
211+ Name : name ,
204212 Kind : "VirtualMachine" ,
205213 })
206214 }
@@ -210,9 +218,14 @@ func (r *VirtualMachineGroupReconciler) createOrUpdateVMG(ctx context.Context, c
210218 return reconcile.Result {}, errors .Errorf ("Cluster Topology is not defined %s/%s" ,
211219 cluster .Namespace , cluster .Name )
212220 }
213- mds := cluster .Spec .Topology .Workers .MachineDeployments
214- mdNames := make ([]string , 0 , len (mds ))
215- for _ , md := range mds {
221+ machineDeployments := & clusterv1.MachineDeploymentList {}
222+ if err := r .Client .List (ctx , machineDeployments ,
223+ client .InNamespace (cluster .Namespace ),
224+ client.MatchingLabels {clusterv1 .ClusterNameLabel : cluster .Name }); err != nil {
225+ return reconcile.Result {}, err
226+ }
227+ mdNames := []string {}
228+ for _ , md := range machineDeployments .Items {
216229 mdNames = append (mdNames , md .Name )
217230 }
218231
@@ -401,7 +414,7 @@ func getCurrentVSphereMachines(ctx context.Context, kubeClient client.Client, cl
401414 return result , nil
402415}
403416
404- // GenerateVMGPlacementLabels returns labels per MachineDeployment which contain zone info for placed VMs for day-2 operationss
417+ // GenerateVMGPlacementLabels returns labels per MachineDeployment which contain zone info for placed VMs for day-2 operations.
405418func GenerateVMGPlacementLabels (ctx context.Context , vmg * vmoprv1.VirtualMachineGroup , machineDeployments []string ) (map [string ]string , error ) {
406419 log := ctrl .LoggerFrom (ctx )
407420 labels := make (map [string ]string )
@@ -428,6 +441,7 @@ func GenerateVMGPlacementLabels(ctx context.Context, vmg *vmoprv1.VirtualMachine
428441 }
429442
430443 // Check if VM belongs to a Machine Deployment by name (e.g. cluster-1-np-1-vm-xxx contains np-1)
444+ // TODO: Establish membership via the machine deployment name label
431445 if strings .Contains (member .Name , md ) {
432446 // Get the VM placement information by member status.
433447 if member .Placement == nil {
@@ -448,3 +462,20 @@ func GenerateVMGPlacementLabels(ctx context.Context, vmg *vmoprv1.VirtualMachine
448462
449463 return labels , nil
450464}
465+
466+ // TODO: de-dup this logic with vmopmachine.go
467+ // GenerateVirtualMachineName generates the name of a VirtualMachine based on the naming strategy.
468+ func GenerateVirtualMachineName (machineName string , namingStrategy * vmwarev1.VirtualMachineNamingStrategy ) (string , error ) {
469+ // Per default the name of the VirtualMachine should be equal to the Machine name (this is the same as "{{ .machine.name }}")
470+ if namingStrategy == nil || namingStrategy .Template == nil {
471+ // Note: No need to trim to max length in this case as valid Machine names will also be valid VirtualMachine names.
472+ return machineName , nil
473+ }
474+
475+ name , err := infrautilv1 .GenerateMachineNameFromTemplate (machineName , namingStrategy .Template )
476+ if err != nil {
477+ return "" , errors .Wrap (err , "failed to generate name for VirtualMachine" )
478+ }
479+
480+ return name , nil
481+ }
0 commit comments