From 5fda85f8315bec4b43615890793e321a574d264e Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Mon, 22 Mar 2021 18:42:56 +0800 Subject: [PATCH 1/2] region: attachnetwork: allow attach by network name isValidNetworkInfo allows names. Otherwise the code will panic when a named was passed and nil was returned by the fetch call --- pkg/compute/models/guest_actions.go | 2 +- pkg/compute/models/guests.go | 2 +- pkg/compute/models/networks.go | 4 ++-- pkg/scheduler/algorithm/predicates/quota_predicate.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index 44da8ef1e5..b91bde09f1 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -2259,7 +2259,7 @@ func (self *SGuest) PerformAttachnetwork(ctx context.Context, userCred mcclient. if err != nil { return nil, err } - if IsExitNetworkInfo(input.Nets[i]) { + if IsExitNetworkInfo(userCred, input.Nets[i]) { enicCnt = count // ebw = input.BwLimit } else { diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 657fae5c74..7a63ee5c96 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -1753,7 +1753,7 @@ func getGuestResourceRequirements( eBw := 0 iBw := 0 for _, netConfig := range input.Networks { - if IsExitNetworkInfo(netConfig) { + if IsExitNetworkInfo(userCred, netConfig) { eNicCnt += 1 eBw += netConfig.BwLimit } else { diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index 907cddabc8..5ca523c7c5 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -1052,9 +1052,9 @@ func isValidNetworkInfo(userCred mcclient.TokenCredential, netConfig *api.Networ return nil } -func IsExitNetworkInfo(netConfig *api.NetworkConfig) bool { +func IsExitNetworkInfo(userCred mcclient.TokenCredential, netConfig *api.NetworkConfig) bool { if len(netConfig.Network) > 0 { - netObj, _ := NetworkManager.FetchById(netConfig.Network) + netObj, _ := NetworkManager.FetchByIdOrName(userCred, netConfig.Network) net := netObj.(*SNetwork) if net.IsExitNetwork() { return true diff --git a/pkg/scheduler/algorithm/predicates/quota_predicate.go b/pkg/scheduler/algorithm/predicates/quota_predicate.go index a9be32fe6d..992712c6c8 100644 --- a/pkg/scheduler/algorithm/predicates/quota_predicate.go +++ b/pkg/scheduler/algorithm/predicates/quota_predicate.go @@ -59,7 +59,7 @@ func fetchGuestUsageFromSchedInfo(s *api.SchedInfo) (computemodels.SQuota, compu iNicCnt := 0 for _, netConfig := range s.Networks { - if computemodels.IsExitNetworkInfo(netConfig) { + if computemodels.IsExitNetworkInfo(s.UserCred, netConfig) { eNicCnt += 1 } else { iNicCnt += 1 From 5ede377c823444451fb8fd9f1b687ccb0ec291b4 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Mon, 22 Mar 2021 19:17:51 +0800 Subject: [PATCH 2/2] region: fix setting nic index When a guest has two nics, changing ip address of the 1st will cause the new nic after detach then attach to be "1", colliding with the original nic of index 1 --- pkg/compute/models/guests.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 7a63ee5c96..d485fed311 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -2702,12 +2702,26 @@ func (self *SGuest) getAttach2NetworkCount(net *SNetwork) (int, error) { return q.CountWithError() } -func (self *SGuest) getMaxNicIndex() int8 { +func (self *SGuest) getUsableNicIndex() int8 { nics, err := self.GetNetworks("") if err != nil { return -1 } - return int8(len(nics)) + maxIndex := int8(len(nics)) + for i := int8(0); i <= maxIndex; i++ { + found := true + for j := range nics { + if nics[j].Index == i { + found = false + break + } + } + if found { + return i + } + } + panic(fmt.Sprintf("cannot find usable nic index for guest %s(%s)", + self.Name, self.Id)) } func (self *SGuest) setOSProfile(ctx context.Context, userCred mcclient.TokenCredential, profile jsonutils.JSONObject) error { @@ -2848,7 +2862,7 @@ func (self *SGuest) attach2NetworkOnce( nicDriver = args.nicDriver ) if index < 0 { - index = self.getMaxNicIndex() + index = self.getUsableNicIndex() } if nicDriver == "" { osProf := self.GetOSProfile()