From 1192272f8806bed216bcd43da8a863a219c22a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Thu, 3 Sep 2026 18:27:27 +0800 Subject: [PATCH] fix(region): vmware vm nic sync (#25508) --- pkg/compute/models/guests.go | 11 ++++++++++- pkg/compute/models/hosts.go | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 51c6d61a73..e9ccc6dc30 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -4358,13 +4358,22 @@ func (self *SGuest) SyncVMNics( ip := commonext[i].GetIP() ip6 := commonext[i].GetIP6() if len(ip) > 0 { - if !network.Contains(ip) { + needRebind := network == nil || !network.Contains(ip) + // ESXi: force rebind if current network is not under the same manager wire + if !needRebind && host.HostType == api.HOST_TYPE_ESXI { + wire, _ := network.GetWire() + if wire == nil || wire.ManagerId != host.ManagerId { + needRebind = true + } + } + if needRebind { localNet, err := getCloudNicNetwork(ctx, commonext[i], host, ipList, i) if err != nil { return errors.Wrapf(err, "getCloudNicNetwork") } commondb[i].NetworkId = localNet.Id commondb[i].IpAddr = ip + commondb[i].Ip6Addr = ip6 } else { commondb[i].IpAddr = ip commondb[i].Ip6Addr = ip6 diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 1905a9ca83..2c32520133 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -3325,6 +3325,23 @@ func (hh *SHost) SyncHostVMs(ctx context.Context, userCred mcclient.TokenCredent func (hh *SHost) getNetworkOfIPOnHost(ctx context.Context, ipAddr string) (*SNetwork, error) { netInterfaces := hh.GetHostNetInterfaces() + // VMware: only associate networks under wires of the same manager_id + if hh.HostType == api.HOST_TYPE_ESXI { + if len(hh.ManagerId) == 0 { + return nil, fmt.Errorf("ESXi host %s has empty manager_id, cannot resolve network for IP %s", hh.Id, ipAddr) + } + for _, netInterface := range netInterfaces { + wire := netInterface.GetWire() + if wire == nil || wire.ManagerId != hh.ManagerId { + continue + } + network, err := netInterface.GetCandidateNetworkForIp(ctx, nil, nil, rbacscope.ScopeNone, ipAddr) + if err == nil && network != nil { + return network, nil + } + } + return nil, fmt.Errorf("IP %s not reachable on ESXi host %s under manager %s", ipAddr, hh.Id, hh.ManagerId) + } for _, netInterface := range netInterfaces { network, err := netInterface.GetCandidateNetworkForIp(ctx, nil, nil, rbacscope.ScopeNone, ipAddr) if err == nil && network != nil {