From a4c54d6483e1baa181d3f72da10bafb0f4344605 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 25 Oct 2024 05:35:02 -0500 Subject: [PATCH] better network checking Signed-off-by: Jeff Carr --- addDroplet.go | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/addDroplet.go b/addDroplet.go index 9c2c266..ba2701c 100644 --- a/addDroplet.go +++ b/addDroplet.go @@ -157,10 +157,14 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) error { return errors.New("not kvm") } - if !updateNetwork(d, domcfg) { + e, err = updateNetwork(d, domcfg) + if err != nil { log.Info("updateNetwork() failed") return errors.New("updateNetwork() failed") } + if e != nil { + alle = append(alle, e) + } if !updateDisk(d, domcfg) { return errors.New("updateDisk() failed") @@ -213,15 +217,19 @@ func updateMemory(d *DropletT, domcfg *libvirtxml.Domain) (*pb.Event, error) { return e, nil } -// returns false if something went wrong -func updateNetwork(d *DropletT, domcfg *libvirtxml.Domain) bool { +func updateNetwork(d *DropletT, domcfg *libvirtxml.Domain) (*pb.Event, error) { + var newEvent *pb.Event 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 for _, iface := range domcfg.Devices.Interfaces { + var hwaddr string + var brname string // fmt.Printf("iface: %+v\n", iface) // fmt.Printf("MAC: %+v\n", iface.MAC) // 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) if iface.MAC != nil { // 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) // fmt.Printf("source: %+v\n", iface.Source) - macs = append(macs, iface.MAC.Address) + hwaddr = iface.MAC.Address } else { + fmt.Printf("iface: %+v\n", iface) 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 - 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 { - log.Verbose("OKAY. FOUND ETH:", i, eth.Mac, eth.Name) + // log.Info("OKAY. FOUND ETH:", eth.Mac, eth.Name, brname) 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 { var eth *pb.Network eth = new(pb.Network) eth.Mac = mac - eth.Name = "worldbr" + if brname == "" { + brname = "worldbr" + } + eth.Name = brname d.pb.Networks = append(d.pb.Networks, eth) - me.changed = true + newEvent = NewChangeEvent(d.pb, "Droplet NewNetwork", "", mac+" "+brname) } } log.Verbose("mac addrs:", macs) - return true + return newEvent, nil } /* from vm3-with-nvme-1.5GB-sec.xml