Skip to content

Commit

Permalink
Start the default network for the kvm2 driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlorenc authored and dlorenc committed May 11, 2018
1 parent fe971d8 commit b54950f
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions pkg/drivers/kvm/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ const networkTmpl = `
</network>
`

func setupNetwork(conn *libvirt.Connect, name string) error {
n, err := conn.LookupNetworkByName(defaultNetworkName)
if err != nil {
return errors.Wrapf(err, "checking network %s", name)
}
autostart, err := n.GetAutostart()
if err != nil {
return errors.Wrapf(err, "checking network %s autostart", name)
}
if !autostart {
if err := n.SetAutostart(true); err != nil {
return errors.Wrapf(err, "setting autostart for network %s", name)
}
}

active, err := n.IsActive()
if err != nil {
return errors.Wrapf(err, "checking network status for %s", name)
}
if !active {
if err := n.Create(); err != nil {
return errors.Wrapf(err, "starting network %s", name)
}
}
return nil

}

func (d *Driver) createNetwork() error {
if d.MAC == "" {
mac, err := randomMAC()
Expand All @@ -59,11 +87,16 @@ func (d *Driver) createNetwork() error {

tmpl := template.Must(template.New("network").Parse(networkTmpl))
var networkXML bytes.Buffer
err = tmpl.Execute(&networkXML, d)
if err != nil {
if err := tmpl.Execute(&networkXML, d); err != nil {
return errors.Wrap(err, "executing network template")
}

// Start the default network
log.Infof("Setting up network %s", defaultNetworkName)
if err := setupNetwork(conn, defaultNetworkName); err != nil {
return err
}

//Check if network already exists
if _, err := conn.LookupNetworkByName(d.PrivateNetwork); err == nil {
return nil
Expand All @@ -73,14 +106,13 @@ func (d *Driver) createNetwork() error {
if err != nil {
return errors.Wrapf(err, "defining network from xml: %s", networkXML.String())
}
err = network.SetAutostart(true)
if err != nil {
return errors.Wrap(err, "setting network to autostart")
if err := network.Create(); err != nil {
return errors.Wrapf(err, "creating network %s", d.PrivateNetwork)
}

err = network.Create()
if err != nil {
return errors.Wrap(err, "creating network")
log.Infof("Setting up network %s", d.PrivateNetwork)
if err := setupNetwork(conn, d.PrivateNetwork); err != nil {
return err
}

return nil
Expand Down

0 comments on commit b54950f

Please sign in to comment.