Skip to content

Commit

Permalink
Fix minikube-net network failures for KVM driver
Browse files Browse the repository at this point in the history
  • Loading branch information
prezha committed Nov 9, 2020
1 parent 18b0c00 commit 1771d61
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
8 changes: 0 additions & 8 deletions pkg/drivers/kvm/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,6 @@ func (d *Driver) Restart() error {

// Start a host
func (d *Driver) Start() (err error) {
// if somebody/something deleted the network in the meantime,
// we might need to recreate it. It's (nearly) a noop if the network exists.
log.Info("Creating network...")
err = d.createNetwork()
if err != nil {
return errors.Wrap(err, "creating network")
}

// this call ensures that all networks are active
log.Info("Ensuring networks are active...")
err = d.ensureNetwork()
Expand Down
31 changes: 30 additions & 1 deletion pkg/drivers/kvm/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io/ioutil"
"strings"
"text/template"
"time"

"github.com/docker/machine/libmachine/log"
libvirt "github.com/libvirt/libvirt-go"
Expand All @@ -46,6 +47,9 @@ const networkTmpl = `
</network>
`

// waiting time for libvirt ops to settle
const nap = 100 * time.Microsecond

// setupNetwork ensures that the network with `name` is started (active)
// and has the autostart feature set.
func setupNetwork(conn *libvirt.Connect, name string) error {
Expand Down Expand Up @@ -99,8 +103,24 @@ func (d *Driver) ensureNetwork() error {

// Start the private network
log.Infof("Ensuring network %s is active", d.PrivateNetwork)
// retry once to recreate the network, but only if is not used by another minikube instance
if err := setupNetwork(conn, d.PrivateNetwork); err != nil {
return err
log.Debugf("Network %s is inoperable, will try to recreate it: %v", d.PrivateNetwork, err)
if err := d.deleteNetwork(); err != nil {
return errors.Wrapf(err, "deleting inoperable network %s", d.PrivateNetwork)
}
log.Debugf("Successfully deleted %s network", d.PrivateNetwork)
time.Sleep(nap)
if err := d.createNetwork(); err != nil {
return errors.Wrapf(err, "recreating inoperable network %s", d.PrivateNetwork)
}
log.Debugf("Successfully recreated %s network", d.PrivateNetwork)
time.Sleep(nap)
if err := setupNetwork(conn, d.PrivateNetwork); err != nil {
return err
}
log.Debugf("Successfully activated %s network", d.PrivateNetwork)
time.Sleep(nap)
}

return nil
Expand Down Expand Up @@ -179,15 +199,24 @@ func (d *Driver) deleteNetwork() error {

// when we reach this point, it means it is safe to delete the network
log.Debugf("Trying to destroy network %s...", d.PrivateNetwork)
// cannot destroy an inactive network - try to activate it first
active, err := network.IsActive()
if err == nil && !active {
log.Debugf("Trying to reactivate network %s first...", d.PrivateNetwork)
_ = network.Create()
time.Sleep(nap)
}
err = network.Destroy()
if err != nil {
return errors.Wrap(err, "network destroy")
}
time.Sleep(nap)
log.Debugf("Trying to undefine network %s...", d.PrivateNetwork)
err = network.Undefine()
if err != nil {
return errors.Wrap(err, "network undefine")
}
time.Sleep(nap)

return nil
}
Expand Down

0 comments on commit 1771d61

Please sign in to comment.