Skip to content

Commit b657780

Browse files
committed
baremetal: Use static libvirt library to destroy cluster
Currently we link to a C-based dynamic library (via the libvirt-go module) to connect to libvirt for the purposes of destroying a baremetal cluster - which in fact only deletes the storage pools used by the bootstrap (and is only used to clean up in cases where the installation fails, since otherwise terraform will clean up when the bootstrap is torn down). The Terraform provider that actually creates the bootstrap uses a pure-Golang library (go-libvirt). By switching to that same library here we can avoid the need to dynamically link to libvirt.
1 parent a6b7c08 commit b657780

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

pkg/destroy/baremetal/baremetal.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
package baremetal
55

66
import (
7-
"github.com/libvirt/libvirt-go"
7+
"net/url"
8+
9+
libvirt "github.com/digitalocean/go-libvirt"
810
"github.com/pkg/errors"
911
"github.com/sirupsen/logrus"
1012

@@ -24,17 +26,27 @@ type ClusterUninstaller struct {
2426
func (o *ClusterUninstaller) Run() (*types.ClusterQuota, error) {
2527
o.Logger.Debug("Deleting bare metal resources")
2628

27-
// FIXME: close the connection
28-
conn, err := libvirt.NewConnect(o.LibvirtURI)
29+
uri, err := url.Parse(o.LibvirtURI)
2930
if err != nil {
30-
return nil, errors.Wrap(err, "failed to connect to Libvirt daemon")
31+
return nil, err
3132
}
32-
err = o.deleteStoragePool(conn)
33+
34+
virt, err := libvirt.ConnectToURI(uri)
3335
if err != nil {
34-
return nil, errors.Wrap(err, "failed to clean baremetal bootstrap storage pool")
36+
return nil, err
3537
}
38+
defer func() {
39+
if err := virt.Disconnect(); err != nil {
40+
if o.Logger != nil {
41+
o.Logger.Warn("failed to disconnect from libvirt", err)
42+
}
43+
}
44+
}()
3645

37-
o.Logger.Debug("FIXME: delete resources!")
46+
err = o.deleteStoragePool(virt)
47+
if err != nil {
48+
return nil, errors.Wrap(err, "failed to clean baremetal bootstrap storage pool")
49+
}
3850

3951
return nil, nil
4052
}
@@ -51,47 +63,45 @@ func New(logger logrus.FieldLogger, metadata *types.ClusterMetadata) (providers.
5163

5264
// deleteStoragePool destroys, deletes and undefines any storagePool left behind during the creation
5365
// of the bootstrap VM
54-
func (o *ClusterUninstaller) deleteStoragePool(conn *libvirt.Connect) error {
66+
func (o *ClusterUninstaller) deleteStoragePool(virt *libvirt.Libvirt) error {
5567
o.Logger.Debug("Deleting baremetal bootstrap volumes")
5668

5769
pName := o.InfraID + "-bootstrap"
58-
pool, err := conn.LookupStoragePoolByName(pName)
70+
pool, err := virt.StoragePoolLookupByName(pName)
5971
if err != nil {
6072
o.Logger.Warnf("Unable to get storage pool %s: %s", pName, err)
6173
return nil
6274
}
63-
defer pool.Free()
6475

6576
// delete vols
66-
vols, err := pool.ListAllStorageVolumes(0)
77+
vols, err := virt.StoragePoolListVolumes(pool, 0)
6778
if err != nil {
6879
o.Logger.Warnf("Unable to get volumes in storage pool %s: %s", pName, err)
6980
return nil
7081
}
7182

72-
for _, vol := range vols {
73-
defer vol.Free()
74-
vName, err := vol.GetName()
83+
for _, vName := range vols {
84+
vol, err := virt.StorageVolLookupByName(pool, vName)
7585
if err != nil {
7686
o.Logger.Warnf("Unable to get volume %s in storage pool %s: %s", vName, pName, err)
7787
return nil
7888
}
79-
if err := vol.Delete(0); err != nil {
89+
if err := virt.StorageVolDelete(vol, 0); err != nil {
8090
o.Logger.Warnf("Unable to delete volume %s in storage pool %s: %s", vName, pName, err)
8191
return nil
8292
}
8393
o.Logger.WithField("volume", vName).Info("Deleted volume")
8494
}
8595

86-
if err := pool.Destroy(); err != nil {
96+
if err := virt.StoragePoolDestroy(pool); err != nil {
8797
o.Logger.Warnf("Unable to destroy storage pool %s: %s", pName, err)
8898
}
8999

90-
if err := pool.Delete(0); err != nil {
100+
if err := virt.StoragePoolDelete(pool, 0); err != nil {
91101
o.Logger.Warnf("Unable to delete storage pool %s: %s", pName, err)
92102
}
93103

94-
if err := pool.Undefine(); err != nil {
104+
if err := virt.StoragePoolUndefine(pool); err != nil {
95105
o.Logger.Warnf("Unable to undefine storage pool %s: %s", pName, err)
96106
}
97107
o.Logger.WithField("pool", pName).Info("Deleted pool")

0 commit comments

Comments
 (0)