From 79904e8c2054f7682ac0103ea7cdfd02c8bda42c Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Wed, 1 Jan 2020 10:48:46 +0800 Subject: [PATCH] =?UTF-8?q?networks:=20=E7=A1=AE=E4=BF=9D=E5=A4=96?= =?UTF-8?q?=E9=83=A8network=E4=B9=9F=E6=9C=89ifname=5Fhint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/models/guestnetworks.go | 4 ++++ pkg/compute/models/networks.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/compute/models/guestnetworks.go b/pkg/compute/models/guestnetworks.go index c18c0ee961..bd67cad261 100644 --- a/pkg/compute/models/guestnetworks.go +++ b/pkg/compute/models/guestnetworks.go @@ -238,6 +238,9 @@ func (self *SGuestnetwork) getVirtualRand(width int, randomized bool) string { } func (self *SGuestnetwork) generateIfname(network *SNetwork, virtual bool, randomized bool) string { + // It may happen that external networks when synced can miss ifname hint + network.ensureIfnameHint() + pattern := regexp.MustCompile(`\W+`) nName := pattern.ReplaceAllString(network.IfnameHint, "") if len(nName) > MAX_IFNAME_SIZE-4 { @@ -254,6 +257,7 @@ func (self *SGuestnetwork) generateIfname(network *SNetwork, virtual bool, rando } func (man *SGuestnetworkManager) ifnameUsed(ifname string) bool { + // inviable names are always used if ifname == "" { return true } diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index d3b0a51339..b600dc9943 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -1163,6 +1163,24 @@ func isValidMaskLen(maskLen int64) bool { } } +func (self *SNetwork) ensureIfnameHint() { + if self.IfnameHint != "" { + return + } + hint, err := NetworkManager.newIfnameHint(self.Name) + if err != nil { + panic(errors.Wrap(err, "ensureIfnameHint: allocate hint")) + } + _, err = db.Update(self, func() error { + self.IfnameHint = hint + return nil + }) + if err != nil { + panic(errors.Wrap(err, "ensureIfnameHint: db update")) + } + log.Infof("network %s(%s): initialized ifname hint: %s", self.Name, self.Id, hint) +} + func (manager *SNetworkManager) newIfnameHint(hint string) (string, error) { isa := func(c byte) bool { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')