Skip to content

Commit

Permalink
Rework patch that gets the VM IP address using libvirt API
Browse files Browse the repository at this point in the history
Changed the code to use the native libvirt API provided by the official
Go bindings.

Signed-off-by: Flavio Castelli <[email protected]>
  • Loading branch information
flavio committed May 1, 2017
1 parent 86938fc commit f720110
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,21 +593,20 @@ func (d *Driver) getMAC() (string, error) {
}

func (d *Driver) getIPByMACFromAPI(mac string) (string, error) {
network, err := d.conn.LookupNetworkByName(d.PrivateNetwork)
interfaces, err := d.VM.ListAllInterfaceAddresses(uint(libvirt.DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE))
if err != nil {
log.Errorf("Failed to lookup network %s", d.PrivateNetwork)
return "", err
}
leases, err := network.GetDHCPLeases()
if err != nil {
log.Warnf("Failed to retrieve DHCP leases from libvirt: %v", err)
return "", err
}
for _, lease := range leases {
if strings.ToLower(mac) == strings.ToLower(lease.GetMACAddress()) {
return lease.GetIPAddress(), nil
for _, domainInterface := range interfaces {
if strings.ToUpper(domainInterface.Hwaddr) == strings.ToUpper(mac) {
// An interface can have multiple addresses (eg: ipv4 and ipv6)
// Just returns the first one right now...
for _, addr := range domainInterface.Addrs {
return addr.Addr, nil
}
}
}

return "", errors.New("failed to match IP for MAC address")
}

Expand Down Expand Up @@ -693,9 +692,9 @@ func (d *Driver) GetIP() (string, error) {
}

methods := []ipLookupFunc{
d.getIPByMACFromAPI,
d.getIPByMACFromLeaseFile,
d.getIPByMacFromSettings,
d.getIPByMACFromAPI,
}
for _, method := range methods {
ip, err := method(mac)
Expand Down

0 comments on commit f720110

Please sign in to comment.