@@ -77,134 +77,20 @@ func (ms *machineScope) create() error {
7777 if err != nil {
7878 return errors .Wrap (err , "error getting VM ignition" )
7979 }
80- optionalVMParams := ovirtC .CreateVMParams ().MustWithInitializationParameters (string (ignition ), ms .machine .Name )
81-
82- if ms .machineProviderSpec .VMType != "" {
83- optionalVMParams = optionalVMParams .MustWithVMType (ovirtC .VMType (ms .machineProviderSpec .VMType ))
84- }
85- if ms .machineProviderSpec .InstanceTypeId != "" {
86- optionalVMParams = optionalVMParams .MustWithInstanceTypeID (ovirtC .InstanceTypeID (ms .machineProviderSpec .InstanceTypeId ))
87- } else {
88- // Add CPU
89- if ms .machineProviderSpec .CPU != nil {
90- optionalVMParams = optionalVMParams .MustWithCPUParameters (uint (ms .machineProviderSpec .CPU .Cores ),
91- uint (ms .machineProviderSpec .CPU .Sockets ),
92- uint (ms .machineProviderSpec .CPU .Threads ))
93- }
94-
95- if ms .machineProviderSpec .MemoryMB > 0 {
96- optionalVMParams = optionalVMParams .MustWithMemory (int64 (bytesInMB ) * int64 (ms .machineProviderSpec .MemoryMB ))
97- }
98-
99- if ms .machineProviderSpec .GuaranteedMemoryMB > 0 {
100- optionalMemoryPolicy := ovirtC .NewMemoryPolicyParameters ().MustWithGuaranteed (int64 (bytesInMB ) * int64 (ms .machineProviderSpec .GuaranteedMemoryMB ))
101- optionalVMParams = optionalVMParams .WithMemoryPolicy (optionalMemoryPolicy )
102- }
103- }
104-
105- optionalPlacementPolicy := ovirtC .NewVMPlacementPolicyParameters ()
106- isAutoPinning := ms .machineProviderSpec .AutoPinningPolicy != "" && ms .machineProviderSpec .AutoPinningPolicy != "none"
107- if isAutoPinning {
108- hosts , err := ms .ovirtClient .ListHosts (ovirtC .ContextStrategy (ms .Context ))
109- if err != nil {
110- return errors .Wrap (err , "error Listing hosts" )
111- }
112- hostIDs := make ([]ovirtC.HostID , 0 )
113- for _ , host := range hosts {
114- if string (host .ClusterID ()) == clusterId {
115- hostIDs = append (hostIDs , host .ID ())
116- }
117- }
118- if len (hostIDs ) > 0 {
119- optionalPlacementPolicy , err = optionalPlacementPolicy .WithHostIDs (hostIDs )
120- if err != nil {
121- return errors .Wrapf (err , "failed to create placement policy parameters with host IDs: %v" , hostIDs )
122- }
123- }
124- }
125-
126- if ms .machineProviderSpec .Hugepages > 0 {
127- optionalVMParams = optionalVMParams .MustWithHugePages (ovirtC .VMHugePages (ms .machineProviderSpec .Hugepages ))
128- }
129-
13080 // CREATE VM from a template
13181 templateName := ms .machineProviderSpec .TemplateName
132-
133- temp , err := ms .ovirtClient .GetTemplateByName (templateName , ovirtC .ContextStrategy (ms .Context ))
134-
82+ template , err := ms .ovirtClient .GetTemplateByName (templateName , ovirtC .ContextStrategy (ms .Context ))
13583 if err != nil {
13684 return errors .Wrapf (err , "error finding template name %s." , templateName )
13785 }
13886
139- // Handle Sparse disks and Format
140- if ms .machineProviderSpec .Sparse != nil || ms .machineProviderSpec .Format != "" {
141- tempDiskAttachment , err := ms .ovirtClient .ListTemplateDiskAttachments (temp .ID (), ovirtC .ContextStrategy (ms .Context ))
142- if err != nil {
143- return errors .Wrapf (err , "failed to fetch template %s disk attachments from oVirt Engine" ,
144- ms .machineProviderSpec .TemplateName )
145- }
146-
147- diskParams := []ovirtC.OptionalVMDiskParameters {}
148- for _ , diskAttachment := range tempDiskAttachment {
149- diskBuilder , _ := ovirtC .NewBuildableVMDiskParameters (diskAttachment .DiskID ())
150- if ms .machineProviderSpec .Sparse != nil {
151- diskBuilder .MustWithSparse (* ms .machineProviderSpec .Sparse )
152- }
153-
154- if ms .machineProviderSpec .Format != "" {
155- diskBuilder .MustWithFormat (ovirtC .ImageFormat (ms .machineProviderSpec .Format ))
156- }
157-
158- diskParams = append (diskParams , diskBuilder )
159- }
160- optionalVMParams = optionalVMParams .MustWithDisks (diskParams )
161- }
162-
163- // Handle Disk Clone
164- if ms .machineProviderSpec .Clone != nil {
165- optionalVMParams = optionalVMParams .MustWithClone (* ms .machineProviderSpec .Clone )
166- } else {
167- if ms .machineProviderSpec .VMType == string (ovirtC .VMTypeDesktop ) {
168- optionalVMParams = optionalVMParams .MustWithClone (false )
169- } else {
170- optionalVMParams = optionalVMParams .MustWithClone (true )
171- }
172- }
173-
174- if ms .machineProviderSpec .StorageDomainId != "" {
175- tempDiskAttachment , err := ms .ovirtClient .ListTemplateDiskAttachments (temp .ID (), ovirtC .ContextStrategy (ms .Context ))
176- if err != nil {
177- return errors .Wrapf (err , "failed to fetch template %s disk attachments from oVirt Engine" ,
178- ms .machineProviderSpec .TemplateName )
179- }
180- optionalVMParams = optionalVMParams .MustWithDisks ([]ovirtC.OptionalVMDiskParameters {
181- ovirtC .MustNewBuildableVMDiskParameters (tempDiskAttachment [0 ].DiskID ()).MustWithStorageDomainID (ovirtC .StorageDomainID (ms .machineProviderSpec .StorageDomainId )),
182- })
183- }
184-
185- vmAffinity := ovirtC .VMAffinityMigratable
186- // apply high_performance rules
187- // see: https://access.redhat.com/documentation/en-us/red_hat_virtualization/4.4/html-single/virtual_machine_management_guide/index?extIdCarryOver=true&sc_cid=701f2000001Css5AAC#Automatic_High_Performance_Configuration_Settings
188- if ms .machineProviderSpec .VMType == string (ovirtC .VMTypeHighPerformance ) {
189- vmAffinity = ovirtC .VMAffinityUserMigratable
190- optionalVMParams .WithSoundcardEnabled (false )
191- optionalVMParams .WithSerialConsole (true )
192-
193- cpuMode := ovirtC .NewVMCPUParams ()
194- cpuMode = cpuMode .MustWithMode (ovirtC .CPUModeHostPassthrough )
195- optionalVMParams = optionalVMParams .MustWithCPU (cpuMode )
196-
197- memPolicy := ovirtC .NewMemoryPolicyParameters ()
198- memPolicy = memPolicy .MustWithBallooning (false )
199- optionalVMParams = optionalVMParams .WithMemoryPolicy (memPolicy )
200-
87+ optionalVMParams , err := ms .buildOptionalVMParameters (string (ignition ), template .ID ())
88+ if err != nil {
89+ return errors .Wrapf (err , "error building parameters for VM creation" )
20190 }
20291
203- optionalPlacementPolicy = optionalPlacementPolicy .MustWithAffinity (vmAffinity )
204- optionalVMParams = optionalVMParams .WithPlacementPolicy (optionalPlacementPolicy )
205-
20692 instance , err := ms .ovirtClient .CreateVM (ovirtC .ClusterID (clusterId ),
207- temp .ID (),
93+ template .ID (),
20894 ms .machine .Name ,
20995 optionalVMParams , ovirtC .ContextStrategy (ms .Context ))
21096
@@ -294,7 +180,7 @@ func (ms *machineScope) create() error {
294180 }
295181 }
296182
297- if isAutoPinning {
183+ if ms . isAutoPinning () {
298184 err = ms .ovirtClient .AutoOptimizeVMCPUPinningSettings (instance .ID (), true , ovirtC .ContextStrategy (ms .Context ))
299185 if err != nil {
300186 return err
@@ -545,3 +431,129 @@ func (ms *machineScope) reconcileMachineAnnotations(status string, id string) {
545431 ms .machine .ObjectMeta .Annotations [InstanceStatusAnnotationKey ] = status
546432 ms .machine .ObjectMeta .Annotations [utils .OvirtIDAnnotationKey ] = id
547433}
434+
435+ func (ms * machineScope ) buildOptionalVMParameters (ignition string , templateID ovirtC.TemplateID ) (ovirtC.BuildableVMParameters , error ) {
436+ optionalVMParams := ovirtC .CreateVMParams ()
437+ optionalVMParams = optionalVMParams .MustWithInitializationParameters (ignition , ms .machine .Name )
438+
439+ if ms .machineProviderSpec .VMType != "" {
440+ optionalVMParams = optionalVMParams .MustWithVMType (ovirtC .VMType (ms .machineProviderSpec .VMType ))
441+ }
442+ if ms .machineProviderSpec .InstanceTypeId != "" {
443+ optionalVMParams = optionalVMParams .MustWithInstanceTypeID (ovirtC .InstanceTypeID (ms .machineProviderSpec .InstanceTypeId ))
444+ } else {
445+ // Add CPU
446+ if ms .machineProviderSpec .CPU != nil {
447+ optionalVMParams = optionalVMParams .MustWithCPUParameters (
448+ uint (ms .machineProviderSpec .CPU .Cores ),
449+ uint (ms .machineProviderSpec .CPU .Threads ),
450+ uint (ms .machineProviderSpec .CPU .Sockets ),
451+ )
452+ }
453+
454+ if ms .machineProviderSpec .MemoryMB > 0 {
455+ optionalVMParams = optionalVMParams .MustWithMemory (int64 (bytesInMB ) * int64 (ms .machineProviderSpec .MemoryMB ))
456+ }
457+
458+ if ms .machineProviderSpec .GuaranteedMemoryMB > 0 {
459+ optionalMemoryPolicy := ovirtC .NewMemoryPolicyParameters ().MustWithGuaranteed (int64 (bytesInMB ) * int64 (ms .machineProviderSpec .GuaranteedMemoryMB ))
460+ optionalVMParams = optionalVMParams .WithMemoryPolicy (optionalMemoryPolicy )
461+ }
462+ }
463+
464+ optionalPlacementPolicy := ovirtC .NewVMPlacementPolicyParameters ()
465+ if ms .isAutoPinning () {
466+ hosts , err := ms .ovirtClient .ListHosts (ovirtC .ContextStrategy (ms .Context ))
467+ if err != nil {
468+ return nil , errors .Wrap (err , "error Listing hosts" )
469+ }
470+ hostIDs := make ([]ovirtC.HostID , 0 )
471+ for _ , host := range hosts {
472+ if string (host .ClusterID ()) == ms .machineProviderSpec .ClusterId {
473+ hostIDs = append (hostIDs , host .ID ())
474+ }
475+ }
476+ if len (hostIDs ) > 0 {
477+ optionalPlacementPolicy , err = optionalPlacementPolicy .WithHostIDs (hostIDs )
478+ if err != nil {
479+ return nil , errors .Wrapf (err , "failed to create placement policy parameters with host IDs: %v" , hostIDs )
480+ }
481+ }
482+ }
483+
484+ if ms .machineProviderSpec .Hugepages > 0 {
485+ optionalVMParams = optionalVMParams .MustWithHugePages (ovirtC .VMHugePages (ms .machineProviderSpec .Hugepages ))
486+ }
487+
488+ // Handle Sparse disks and Format
489+ if ms .machineProviderSpec .Sparse != nil || ms .machineProviderSpec .Format != "" {
490+ tempDiskAttachment , err := ms .ovirtClient .ListTemplateDiskAttachments (templateID , ovirtC .ContextStrategy (ms .Context ))
491+ if err != nil {
492+ return nil , errors .Wrapf (err , "failed to fetch template %s disk attachments from oVirt Engine" ,
493+ ms .machineProviderSpec .TemplateName )
494+ }
495+
496+ diskParams := []ovirtC.OptionalVMDiskParameters {}
497+ for _ , diskAttachment := range tempDiskAttachment {
498+ diskBuilder , _ := ovirtC .NewBuildableVMDiskParameters (diskAttachment .DiskID ())
499+ if ms .machineProviderSpec .Sparse != nil {
500+ diskBuilder .MustWithSparse (* ms .machineProviderSpec .Sparse )
501+ }
502+
503+ if ms .machineProviderSpec .Format != "" {
504+ diskBuilder .MustWithFormat (ovirtC .ImageFormat (ms .machineProviderSpec .Format ))
505+ }
506+
507+ diskParams = append (diskParams , diskBuilder )
508+ }
509+ optionalVMParams = optionalVMParams .MustWithDisks (diskParams )
510+ }
511+
512+ // Handle Disk Clone
513+ if ms .machineProviderSpec .Clone != nil {
514+ optionalVMParams = optionalVMParams .MustWithClone (* ms .machineProviderSpec .Clone )
515+ } else {
516+ if ms .machineProviderSpec .VMType == string (ovirtC .VMTypeDesktop ) {
517+ optionalVMParams = optionalVMParams .MustWithClone (false )
518+ } else {
519+ optionalVMParams = optionalVMParams .MustWithClone (true )
520+ }
521+ }
522+
523+ if ms .machineProviderSpec .StorageDomainId != "" {
524+ tempDiskAttachment , err := ms .ovirtClient .ListTemplateDiskAttachments (templateID , ovirtC .ContextStrategy (ms .Context ))
525+ if err != nil {
526+ return nil , errors .Wrapf (err , "failed to fetch template %s disk attachments from oVirt Engine" ,
527+ ms .machineProviderSpec .TemplateName )
528+ }
529+ optionalVMParams = optionalVMParams .MustWithDisks ([]ovirtC.OptionalVMDiskParameters {
530+ ovirtC .MustNewBuildableVMDiskParameters (tempDiskAttachment [0 ].DiskID ()).MustWithStorageDomainID (ovirtC .StorageDomainID (ms .machineProviderSpec .StorageDomainId )),
531+ })
532+ }
533+
534+ vmAffinity := ovirtC .VMAffinityMigratable
535+ // apply high_performance rules
536+ // see: https://access.redhat.com/documentation/en-us/red_hat_virtualization/4.4/html-single/virtual_machine_management_guide/index?extIdCarryOver=true&sc_cid=701f2000001Css5AAC#Automatic_High_Performance_Configuration_Settings
537+ if ms .machineProviderSpec .VMType == string (ovirtC .VMTypeHighPerformance ) {
538+ vmAffinity = ovirtC .VMAffinityUserMigratable
539+ optionalVMParams .WithSoundcardEnabled (false )
540+ optionalVMParams .WithSerialConsole (true )
541+
542+ cpuMode := ovirtC .NewVMCPUParams ()
543+ cpuMode = cpuMode .MustWithMode (ovirtC .CPUModeHostPassthrough )
544+ optionalVMParams = optionalVMParams .MustWithCPU (cpuMode )
545+
546+ memPolicy := ovirtC .NewMemoryPolicyParameters ()
547+ memPolicy = memPolicy .MustWithBallooning (false )
548+ optionalVMParams = optionalVMParams .WithMemoryPolicy (memPolicy )
549+ }
550+
551+ optionalPlacementPolicy = optionalPlacementPolicy .MustWithAffinity (vmAffinity )
552+ optionalVMParams = optionalVMParams .WithPlacementPolicy (optionalPlacementPolicy )
553+
554+ return optionalVMParams , nil
555+ }
556+
557+ func (ms * machineScope ) isAutoPinning () bool {
558+ return ms .machineProviderSpec .AutoPinningPolicy != "" && ms .machineProviderSpec .AutoPinningPolicy != "none"
559+ }
0 commit comments