simple unique mac

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-30 12:30:34 -05:00
parent 410015c33e
commit 2a18f506c7
2 changed files with 13 additions and 11 deletions

View File

@ -38,19 +38,16 @@ func create(w http.ResponseWriter, r *http.Request) error {
fmt.Fprintln(w, result) fmt.Fprintln(w, result)
return errors.New(result) return errors.New(result)
} }
if d.Uuid == "" { if d.Uuid == "" {
u := uuid.New() u := uuid.New()
d.Uuid = u.String() d.Uuid = u.String()
} }
return nil
if len(d.Networks) == 0 { if len(d.Networks) == 0 {
mac, err := getNewMac()
if err != nil {
return err
}
var newNet *pb.Network var newNet *pb.Network
newNet = new(pb.Network) newNet = new(pb.Network)
newNet.Mac = mac newNet.Mac = getNewMac()
d.Networks = append(d.Networks, newNet) d.Networks = append(d.Networks, newNet)
// d.AddDefaultNetwork(mac) // d.AddDefaultNetwork(mac)
} }

View File

@ -15,6 +15,7 @@ package main
import ( import (
"errors" "errors"
"fmt"
"path/filepath" "path/filepath"
"github.com/google/uuid" "github.com/google/uuid"
@ -133,7 +134,7 @@ func ValidateDiskFilenames(cluster *pb.Cluster) []*pb.Event {
return alle return alle
} }
func getNewMac() (string, error) { func getNewMac() string {
// mac address map to check for duplicates // mac address map to check for duplicates
var macs map[string]string var macs map[string]string
macs = make(map[string]string) macs = make(map[string]string)
@ -145,21 +146,25 @@ func getNewMac() (string, error) {
// UUID already exists // UUID already exists
log.Info("duplicate MAC", n.Mac, macs[n.Mac]) log.Info("duplicate MAC", n.Mac, macs[n.Mac])
log.Info("duplicate MAC", n.Mac, d.Hostname) log.Info("duplicate MAC", n.Mac, d.Hostname)
return "", errors.New("duplicate MAC: " + d.Hostname + " and " + macs[n.Mac]) return ""
} }
macs[n.Mac] = d.Hostname macs[n.Mac] = d.Hostname
} }
} }
var mac string = "22:22:22:22:22:09" var i int = 9
var mac string
for { for {
mac = fmt.Sprintf("22:22:22:22:22:%02d", i)
if _, ok := macs[mac]; ok { if _, ok := macs[mac]; ok {
log.Info("MAC already defined", mac, macs[mac]) log.Info("MAC already defined", mac, macs[mac])
i += 1
continue continue
} }
return mac, nil log.Info("using new MAC:", mac)
return mac
} }
return mac, nil return ""
} }
// runs on startup. dies if there are duplicates // runs on startup. dies if there are duplicates