99 "flag"
1010 "log"
1111 "os"
12+ "strconv"
1213 "text/template"
1314
1415 bmoe2e "github.com/metal3-io/baremetal-operator/test/e2e"
@@ -133,35 +134,31 @@ func CreateVolume(conn *libvirt.Connect, volumeName, poolName, poolPath string,
133134 return nil
134135}
135136
136- // CreateLibvirtVM creates a new virtual machine with the given name,
137- // network name, and MAC address. It first creates a qcow2 file with a size
138- // of 3GB and defines it in the baremetal-e2e storage pool. The function then connects
139- // to the libvirt daemon and uses a template to generate the VM's XML configuration.
140- // If the domain is successfully defined and created, the virtual machine is
141- // started. Errors during qcow2 file creation, volume creation, libvirt connection,
142- // template rendering, or domain creation are returned.
143- func CreateLibvirtVM (conn * libvirt.Connect , name , networkName , macAddress string ) error {
137+ // CreateLibvirtVM creates a new virtual machine based on the bmc details.
138+ // The VM is defined only, not started. Two volumes are also created for the VM.
139+ func CreateLibvirtVM (conn * libvirt.Connect , bmc * bmoe2e.BMC ) error {
140+ if err := ReserveIPAddresses (conn , bmc .Name , bmc .Networks ); err != nil {
141+ log .Printf ("Error occurred: %v\n " , err )
142+ return err
143+ }
144144 poolName := "baremetal-e2e"
145145 poolPath := "/tmp/pool_oo"
146-
147- if err := CreateVolume (conn , name + "-1" , poolName , poolPath , 20 ); err != nil { //nolint: mnd
146+ if err := CreateVolume (conn , bmc .Name + "-1" , poolName , poolPath , 20 ); err != nil { //nolint: mnd
148147 return err
149148 }
150149
151- if err := CreateVolume (conn , name + "-2" , poolName , poolPath , 20 ); err != nil { //nolint: mnd
150+ if err := CreateVolume (conn , bmc . Name + "-2" , poolName , poolPath , 20 ); err != nil { //nolint: mnd
152151 return err
153152 }
154153
155154 data := struct {
156- Name string
157- Network string
158- MacAddress string
159- PoolPath string
155+ Name string
156+ Networks []bmoe2e.Network
157+ PoolPath string
160158 }{
161- Name : name ,
162- Network : networkName ,
163- MacAddress : macAddress ,
164- PoolPath : poolPath ,
159+ Name : bmc .Name ,
160+ Networks : bmc .Networks ,
161+ PoolPath : poolPath ,
165162 }
166163
167164 vmCfg , err := RenderTemplate ("templates/VM.xml.tpl" , data )
@@ -182,17 +179,9 @@ func CreateLibvirtVM(conn *libvirt.Connect, name, networkName, macAddress string
182179 return nil
183180}
184181
185- // CreateLibvirtVMWithReservedIPAddress creates a VM with the given MAC address, name, IP address
186- // and adds a DHCP host entry on the given network.
187- //
188- // It will return an error if the network does not exist, or if creating the VM
189- // or adding the DHCP host entry fails.
190- func CreateLibvirtVMWithReservedIPAddress (conn * libvirt.Connect , macAddress , name , ipAddress , networkName string ) error {
191- network , err := conn .LookupNetworkByName (networkName )
192- if err != nil {
193- return err
194- }
195-
182+ // updateNetwork is a helper function for CreateLibvirtVMWithReservedIPAddress.
183+ // It updates the network with a DHCP host entry.
184+ func updateNetwork (network * libvirt.Network , macAddress , name , ipAddress string ) error {
196185 xmlTpl , err := template .New ("xml" ).Parse ("<host mac='{{ .MacAddress }}' name='{{ .Name }}' ip='{{ .IPAddress }}' />" )
197186
198187 if err != nil {
@@ -229,9 +218,24 @@ func CreateLibvirtVMWithReservedIPAddress(conn *libvirt.Connect, macAddress, nam
229218 log .Printf ("Error occurred: %v\n " , err )
230219 return err
231220 }
232- if err = CreateLibvirtVM (conn , name , networkName , macAddress ); err != nil {
233- log .Printf ("Error occurred: %v\n " , err )
234- return err
221+ return nil
222+ }
223+
224+ // ReserveIPAddresses adds a DHCP host entry for all networks that
225+ // specify an IP address.
226+ func ReserveIPAddresses (conn * libvirt.Connect , hostName string , networks []bmoe2e.Network ) error {
227+ for i , net := range networks {
228+ // Checking if this network has IP specified.
229+ if net .IPAddress != "" {
230+ network , err := conn .LookupNetworkByName (net .Name )
231+ if err != nil {
232+ return err
233+ }
234+ err = updateNetwork (network , net .MacAddress , hostName + "-" + strconv .Itoa (i ), net .IPAddress )
235+ if err != nil {
236+ return err
237+ }
238+ }
235239 }
236240 return nil
237241}
@@ -252,9 +256,15 @@ func main() {
252256 bmcs := []bmoe2e.BMC {}
253257 if * configFile == "" {
254258 bmc := bmoe2e.BMC {
255- IPAddress : * ipAddress ,
256259 BootMacAddress : * macAddress ,
257260 Name : * name ,
261+ Networks : []bmoe2e.Network {
262+ {
263+ Name : * networkName ,
264+ MacAddress : * macAddress ,
265+ IPAddress : * ipAddress ,
266+ },
267+ },
258268 }
259269 bmcs = append (bmcs , bmc )
260270 } else {
@@ -272,16 +282,9 @@ func main() {
272282 defer conn .Close ()
273283
274284 for _ , bmc := range bmcs {
275- if bmc .IPAddress != "" {
276- if err = CreateLibvirtVMWithReservedIPAddress (conn , bmc .BootMacAddress , bmc .Name , bmc .IPAddress , * networkName ); err != nil {
277- log .Printf ("Error occurred: %v\n " , err )
278- break
279- }
280- } else {
281- if err = CreateLibvirtVM (conn , bmc .Name , * networkName , bmc .BootMacAddress ); err != nil {
282- log .Printf ("Error occurred: %v\n " , err )
283- break
284- }
285+ if err = CreateLibvirtVM (conn , & bmc ); err != nil {
286+ log .Printf ("Error occurred: %v\n " , err )
287+ break
285288 }
286289 }
287290}
0 commit comments