mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-05-06 13:42:10 +08:00
fix: vpcagent sync inconsistent networkaddress guestnetwork id (#24657)
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
@@ -995,6 +995,12 @@ func (manager *SGuestnetworkManager) DeleteGuestNics(ctx context.Context, userCr
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "GetIsolatedDeviceByNetworkIndex")
|
||||
}
|
||||
if dev != nil {
|
||||
err = guest.detachIsolateDevice(ctx, userCred, dev)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "detachIsolateDevice %s", dev.GetName())
|
||||
}
|
||||
}
|
||||
net, _ := gn.GetNetwork()
|
||||
if !gotypes.IsNil(net) && (regutils.MatchIP4Addr(gn.IpAddr) || regutils.MatchIP6Addr(gn.Ip6Addr)) {
|
||||
net.updateDnsRecord(&gn, false)
|
||||
@@ -1005,16 +1011,10 @@ func (manager *SGuestnetworkManager) DeleteGuestNics(ctx context.Context, userCr
|
||||
}
|
||||
err = gn.Delete(ctx, userCred)
|
||||
if err != nil {
|
||||
log.Errorf("%s", err)
|
||||
log.Errorf("guest network %s delete fail %s", gn.GetDetailedString(), err)
|
||||
return errors.Wrapf(err, "Delete %s", gn.GetDetailedString())
|
||||
}
|
||||
gn.LogDetachEvent(ctx, userCred, guest, net)
|
||||
if dev != nil {
|
||||
err = guest.detachIsolateDevice(ctx, userCred, dev)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !gotypes.IsNil(net) {
|
||||
if reserve && regutils.MatchIP4Addr(gn.IpAddr) {
|
||||
ReservedipManager.ReserveIP(ctx, userCred, net, gn.IpAddr, "Delete to reserve", api.AddressTypeIPv4)
|
||||
|
||||
@@ -34,6 +34,8 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/auth"
|
||||
ctxutils "yunion.io/x/onecloud/pkg/util/ctx"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
)
|
||||
|
||||
@@ -75,6 +77,12 @@ type SNetworkAddress struct {
|
||||
|
||||
func (man *SNetworkAddressManager) InitializeData() error {
|
||||
go man.delayedWorkManager.Start(context.Background())
|
||||
{
|
||||
err := man.cleanDeletedGuestnetworkSubIPs()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "cleanDeletedGuestnetworkSubIPs")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -698,3 +706,24 @@ func (manager *SNetworkAddressManager) fetchSubIpsQuery(parentType api.TNetworkA
|
||||
subIPQ = subIPQ.GroupBy(subIPQ.Field("parent_id"))
|
||||
return subIPQ
|
||||
}
|
||||
|
||||
func (manager *SNetworkAddressManager) cleanDeletedGuestnetworkSubIPs() error {
|
||||
ctx := ctxutils.CtxWithTime()
|
||||
userCred := auth.AdminCredential()
|
||||
gnQ := GuestnetworkManager.Query("row_id").SubQuery()
|
||||
q := manager.Query().Equals("parent_type", api.NetworkAddressParentTypeGuestnetwork)
|
||||
q = q.LeftJoin(gnQ, sqlchemy.Equals(q.Field("parent_id"), gnQ.Field("row_id")))
|
||||
q = q.Filter(sqlchemy.IsNull(gnQ.Field("row_id")))
|
||||
netaddrs := make([]SNetworkAddress, 0)
|
||||
err := db.FetchModelObjects(manager, q, &netaddrs)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "FetchModelObjects")
|
||||
}
|
||||
for i := range netaddrs {
|
||||
na := &netaddrs[i]
|
||||
if err := db.DeleteModel(ctx, userCred, na); err != nil {
|
||||
return errors.Wrapf(err, "DeleteModel")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -573,12 +573,13 @@ func (set Guestnetworks) joinNetworkAddresses(subEntries NetworkAddresses) bool
|
||||
if !ok {
|
||||
log.Errorf("cannot find guestnetwork row id %s of network address %s", parentId, subEntry.Id)
|
||||
correct = false
|
||||
} else {
|
||||
if guestnetwork.SubIPs == nil {
|
||||
guestnetwork.SubIPs = NetworkAddresses{}
|
||||
}
|
||||
guestnetwork.SubIPs[subEntry.Id] = subEntry
|
||||
subEntry.Guestnetwork = guestnetwork
|
||||
}
|
||||
if guestnetwork.SubIPs == nil {
|
||||
guestnetwork.SubIPs = NetworkAddresses{}
|
||||
}
|
||||
guestnetwork.SubIPs[subEntry.Id] = subEntry
|
||||
subEntry.Guestnetwork = guestnetwork
|
||||
}
|
||||
}
|
||||
return correct
|
||||
|
||||
Reference in New Issue
Block a user