Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start the default network for the kvm2 driver. #2806

Merged
merged 1 commit into from
May 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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