Skip to content

Commit

Permalink
Merge pull request #10479 from prezha/fix-kvm-network-cleanup
Browse files Browse the repository at this point in the history
improve kvm network delete/cleanup
  • Loading branch information
medyagh authored Feb 20, 2021
2 parents 5e90254 + a5f75b4 commit 6bf3c23
Showing 1 changed file with 13 additions and 46 deletions.
59 changes: 13 additions & 46 deletions pkg/drivers/kvm/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (d *Driver) deleteNetwork() error {
log.Warnf("Network %s does not exist. Skipping deletion", d.PrivateNetwork)
return nil
}
return errors.Wrapf(err, "failed looking for network %s", d.PrivateNetwork)
return errors.Wrapf(err, "failed looking up network %s", d.PrivateNetwork)
}
defer func() { _ = network.Free() }()
log.Debugf("Network %s exists", d.PrivateNetwork)
Expand All @@ -236,58 +236,25 @@ func (d *Driver) deleteNetwork() error {

// when we reach this point, it means it is safe to delete the network

// cannot destroy an inactive network - try to activate it first
log.Debugf("Trying to reactivate network %s first (if needed)...", d.PrivateNetwork)
activate := func() error {
log.Debugf("Trying to delete network %s...", d.PrivateNetwork)
delete := func() error {
active, err := network.IsActive()
if err == nil && active {
return nil
}
if err != nil {
return err
}
// inactive, try to activate
if err := network.Create(); err != nil {
return err
}
return errors.Errorf("needs confirmation") // confirm in the next cycle
}
if err := retry.Local(activate, 10*time.Second); err != nil {
log.Debugf("Reactivating network %s failed, will continue anyway...", d.PrivateNetwork)
}

log.Debugf("Trying to destroy network %s...", d.PrivateNetwork)
destroy := func() error {
if err := network.Destroy(); err != nil {
return err
}
active, err := network.IsActive()
if err == nil && !active {
return nil
}
return errors.Errorf("retrying %v", err)
}
if err := retry.Local(destroy, 10*time.Second); err != nil {
return errors.Wrap(err, "destroying network")
}

log.Debugf("Trying to undefine network %s...", d.PrivateNetwork)
undefine := func() error {
if err := network.Undefine(); err != nil {
return err
}
netp, err := conn.LookupNetworkByName(d.PrivateNetwork)
if netp != nil {
_ = netp.Free()
}
if lvErr(err).Code == libvirt.ERR_NO_NETWORK {
return nil
if active {
log.Debugf("Destroying active network %s", d.PrivateNetwork)
if err := network.Destroy(); err != nil {
return err
}
}
return errors.Errorf("retrying %v", err)
log.Debugf("Undefining inactive network %s", d.PrivateNetwork)
return network.Undefine()
}
if err := retry.Local(undefine, 10*time.Second); err != nil {
return errors.Wrap(err, "undefining network")
if err := retry.Local(delete, 10*time.Second); err != nil {
return errors.Wrap(err, "deleting network")
}
log.Debugf("Network %s deleted", d.PrivateNetwork)

return nil
}
Expand Down

0 comments on commit 6bf3c23

Please sign in to comment.