better network checking

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-25 05:35:02 -05:00
parent 178974e42f
commit a4c54d6483
1 changed files with 36 additions and 13 deletions

View File

@ -157,10 +157,14 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) error {
return errors.New("not kvm") return errors.New("not kvm")
} }
if !updateNetwork(d, domcfg) { e, err = updateNetwork(d, domcfg)
if err != nil {
log.Info("updateNetwork() failed") log.Info("updateNetwork() failed")
return errors.New("updateNetwork() failed") return errors.New("updateNetwork() failed")
} }
if e != nil {
alle = append(alle, e)
}
if !updateDisk(d, domcfg) { if !updateDisk(d, domcfg) {
return errors.New("updateDisk() failed") return errors.New("updateDisk() failed")
@ -213,15 +217,19 @@ func updateMemory(d *DropletT, domcfg *libvirtxml.Domain) (*pb.Event, error) {
return e, nil return e, nil
} }
// returns false if something went wrong func updateNetwork(d *DropletT, domcfg *libvirtxml.Domain) (*pb.Event, error) {
func updateNetwork(d *DropletT, domcfg *libvirtxml.Domain) bool { var newEvent *pb.Event
if (d == nil) || (domcfg == nil) { if (d == nil) || (domcfg == nil) {
return false return nil, errors.New("domcfg == nil")
} }
var macs []string // mac address & bridge name
var macs map[string]string
macs = make(map[string]string)
// Iterate over the network interfaces and print the MAC addresses // Iterate over the network interfaces and print the MAC addresses
for _, iface := range domcfg.Devices.Interfaces { for _, iface := range domcfg.Devices.Interfaces {
var hwaddr string
var brname string
// fmt.Printf("iface: %+v\n", iface) // fmt.Printf("iface: %+v\n", iface)
// fmt.Printf("MAC: %+v\n", iface.MAC) // fmt.Printf("MAC: %+v\n", iface.MAC)
// fmt.Printf("Source: %+v\n", iface.Source) // fmt.Printf("Source: %+v\n", iface.Source)
@ -229,35 +237,50 @@ func updateNetwork(d *DropletT, domcfg *libvirtxml.Domain) bool {
// fmt.Printf("Model: %+v\n", iface.Model) // fmt.Printf("Model: %+v\n", iface.Model)
if iface.MAC != nil { if iface.MAC != nil {
// iface.MAC.Address = "aa:bb:aa:bb:aa:ff" // iface.MAC.Address = "aa:bb:aa:bb:aa:ff"
// fmt.Printf("MAC Address: %+v\n", iface.MAC)
// log.Info("Interface:", iface.Target, "MAC Address:", iface.MAC.Address) // log.Info("Interface:", iface.Target, "MAC Address:", iface.MAC.Address)
// fmt.Printf("source: %+v\n", iface.Source) // fmt.Printf("source: %+v\n", iface.Source)
macs = append(macs, iface.MAC.Address) hwaddr = iface.MAC.Address
} else { } else {
fmt.Printf("iface: %+v\n", iface)
fmt.Printf("Interface Target: %+v, MAC Address not available\n", iface.Target) fmt.Printf("Interface Target: %+v, MAC Address not available\n", iface.Target)
return nil, errors.New("network XML does not have a MAC Address")
} }
if iface.Source != nil {
if iface.Source.Bridge != nil {
brname = iface.Source.Bridge.Bridge
}
}
macs[hwaddr] = brname
} }
for _, mac := range macs { for mac, brname := range macs {
var found bool = false var found bool = false
for i, eth := range d.pb.Networks { // log.Info("XML has mac address:", mac, brname)
for _, eth := range d.pb.Networks {
if eth.Mac == mac { if eth.Mac == mac {
log.Verbose("OKAY. FOUND ETH:", i, eth.Mac, eth.Name) // log.Info("OKAY. FOUND ETH:", eth.Mac, eth.Name, brname)
found = true found = true
if eth.Name != brname {
log.Info("network changed bridge name:", eth.Mac, eth.Name, brname)
return nil, errors.New("bridge name changed")
}
} }
} }
if !found { if !found {
var eth *pb.Network var eth *pb.Network
eth = new(pb.Network) eth = new(pb.Network)
eth.Mac = mac eth.Mac = mac
eth.Name = "worldbr" if brname == "" {
brname = "worldbr"
}
eth.Name = brname
d.pb.Networks = append(d.pb.Networks, eth) d.pb.Networks = append(d.pb.Networks, eth)
me.changed = true newEvent = NewChangeEvent(d.pb, "Droplet NewNetwork", "", mac+" "+brname)
} }
} }
log.Verbose("mac addrs:", macs) log.Verbose("mac addrs:", macs)
return true return newEvent, nil
} }
/* from vm3-with-nvme-1.5GB-sec.xml /* from vm3-with-nvme-1.5GB-sec.xml