Merge pull request #14300 from ioito/hotfix/qx-eip-associate-type

fix(region): eip associate type
This commit is contained in:
Zexi Li
2022-05-25 09:53:53 +08:00
committed by GitHub
18 changed files with 95 additions and 70 deletions
+13 -17
View File
@@ -352,13 +352,9 @@ func (self *SElasticip) GetShortDesc(ctx context.Context) *jsonutils.JSONDict {
}
func (manager *SElasticipManager) SyncEips(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, region *SCloudregion, eips []cloudprovider.ICloudEIP, syncOwnerId mcclient.IIdentityProvider) compare.SyncResult {
// ownerProjId := projectId
lockman.LockRawObject(ctx, manager.KeywordPlural(), region.Id)
defer lockman.ReleaseRawObject(ctx, manager.KeywordPlural(), region.Id)
lockman.LockRawObject(ctx, "elasticip", region.Id)
defer lockman.ReleaseRawObject(ctx, "elasticip", region.Id)
// localEips := make([]SElasticip, 0)
// remoteEips := make([]cloudprovider.ICloudEIP, 0)
syncResult := compare.SyncResult{}
dbEips, err := region.GetElasticIps(provider.Id, api.EIP_MODE_STANDALONE_EIP)
@@ -490,8 +486,6 @@ func (self *SElasticip) SyncInstanceWithCloudEip(ctx context.Context, userCred m
func (self *SElasticip) SyncWithCloudEip(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, ext cloudprovider.ICloudEIP, syncOwnerId mcclient.IIdentityProvider) error {
diff, err := db.UpdateWithLock(ctx, self, func() error {
// self.Name = ext.GetName()
if bandwidth := ext.GetBandwidth(); bandwidth != 0 {
self.Bandwidth = bandwidth
}
@@ -500,6 +494,7 @@ func (self *SElasticip) SyncWithCloudEip(ctx context.Context, userCred mcclient.
self.Status = ext.GetStatus()
self.ExternalId = ext.GetGlobalId()
self.IsEmulated = ext.IsEmulated()
self.AssociateType = ext.GetAssociationType()
if chargeType := ext.GetInternetChargeType(); len(chargeType) > 0 {
self.ChargeType = chargeType
@@ -532,10 +527,10 @@ func (self *SElasticip) SyncWithCloudEip(ctx context.Context, userCred mcclient.
})
}
err = self.SyncInstanceWithCloudEip(ctx, userCred, ext)
if err != nil {
return errors.Wrap(err, "fail to sync associated instance of EIP")
}
//err = self.SyncInstanceWithCloudEip(ctx, userCred, ext)
//if err != nil {
// return errors.Wrap(err, "fail to sync associated instance of EIP")
//}
syncVirtualResourceMetadata(ctx, userCred, self, ext)
// eip有绑定资源,并且绑定资源是项目资源,eip项目信息跟随绑定资源
@@ -560,6 +555,7 @@ func (manager *SElasticipManager) newFromCloudEip(ctx context.Context, userCred
eip.ManagerId = provider.Id
eip.CloudregionId = region.Id
eip.ChargeType = extEip.GetInternetChargeType()
eip.AssociateType = extEip.GetAssociationType()
if len(eip.ChargeType) == 0 {
eip.ChargeType = api.EIP_CHARGE_TYPE_BY_TRAFFIC
}
@@ -596,10 +592,10 @@ func (manager *SElasticipManager) newFromCloudEip(ctx context.Context, userCred
return nil, errors.Wrapf(err, "newFromCloudEip")
}
err = eip.SyncInstanceWithCloudEip(ctx, userCred, extEip)
if err != nil {
return nil, errors.Wrap(err, "fail to sync associated instance of EIP")
}
//err = eip.SyncInstanceWithCloudEip(ctx, userCred, extEip)
//if err != nil {
// return nil, errors.Wrap(err, "fail to sync associated instance of EIP")
//}
syncVirtualResourceMetadata(ctx, userCred, &eip, extEip)
@@ -1738,7 +1734,7 @@ func (manager *SElasticipManager) TotalCount(scope rbacutils.TRbacScope, ownerId
q3sq := manager.Query().SubQuery()
q3 := q3sq.Query(
sqlchemy.COUNT("eip_used_count", q3sq.Field("id")),
).Equals("mode", api.EIP_MODE_STANDALONE_EIP).IsNotEmpty("associate_id")
).Equals("mode", api.EIP_MODE_STANDALONE_EIP).IsNotEmpty("associate_type")
q3 = manager.usageQ(scope, ownerId, q3, rangeObjs, providers, brands, cloudEnv, policyResult)
err := q1.First(&usage)
+10 -14
View File
@@ -16,9 +16,9 @@ package tasks
import (
"context"
"fmt"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
@@ -35,11 +35,11 @@ func init() {
taskman.RegisterTask(EipSyncstatusTask{})
}
func (self *EipSyncstatusTask) taskFail(ctx context.Context, eip *models.SElasticip, msg jsonutils.JSONObject) {
eip.SetStatus(self.UserCred, api.EIP_STATUS_UNKNOWN, msg.String())
db.OpsLog.LogEvent(eip, db.ACT_SYNC_STATUS, msg, self.GetUserCred())
logclient.AddActionLogWithStartable(self, eip, logclient.ACT_SYNC_STATUS, msg, self.UserCred, false)
self.SetStageFailed(ctx, msg)
func (self *EipSyncstatusTask) taskFail(ctx context.Context, eip *models.SElasticip, err error) {
eip.SetStatus(self.UserCred, api.EIP_STATUS_UNKNOWN, err.Error())
db.OpsLog.LogEvent(eip, db.ACT_SYNC_STATUS, err, self.GetUserCred())
logclient.AddActionLogWithStartable(self, eip, logclient.ACT_SYNC_STATUS, err, self.UserCred, false)
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
}
func (self *EipSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
@@ -47,29 +47,25 @@ func (self *EipSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneMod
extEip, err := eip.GetIEip(ctx)
if err != nil {
msg := fmt.Sprintf("fail to find ieip for eip %s", err)
self.taskFail(ctx, eip, jsonutils.NewString(msg))
self.taskFail(ctx, eip, errors.Wrapf(err, "eip.GetIEip"))
return
}
err = extEip.Refresh()
if err != nil {
msg := fmt.Sprintf("fail to refresh eip status %s", err)
self.taskFail(ctx, eip, jsonutils.NewString(msg))
self.taskFail(ctx, eip, errors.Wrapf(err, "extEip.Refresh"))
return
}
err = eip.SyncWithCloudEip(ctx, self.UserCred, eip.GetCloudprovider(), extEip, nil)
if err != nil {
msg := fmt.Sprintf("fail to sync eip status %s", err)
self.taskFail(ctx, eip, jsonutils.NewString(msg))
self.taskFail(ctx, eip, errors.Wrapf(err, "SyncWithCloudEip"))
return
}
err = eip.SyncInstanceWithCloudEip(ctx, self.UserCred, extEip)
if err != nil {
msg := fmt.Sprintf("fail to sync eip status %s", err)
self.taskFail(ctx, eip, jsonutils.NewString(msg))
self.taskFail(ctx, eip, errors.Wrapf(err, "SyncInstanceWithCloudEip"))
return
}
+2 -2
View File
@@ -162,14 +162,14 @@ func (self *SEipAddress) GetMode() string {
func (self *SEipAddress) GetAssociationType() string {
switch self.InstanceType {
case EIP_INSTANCE_TYPE_ECS:
case EIP_INSTANCE_TYPE_ECS, "NetworkInterface":
return api.EIP_ASSOCIATE_TYPE_SERVER
case EIP_INSTANCE_TYPE_NAT:
return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
case EIP_INTANNCE_TYPE_SLB:
return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
default:
return "unsupported"
return self.InstanceType
}
}
+2 -3
View File
@@ -152,15 +152,14 @@ func (self *SEipAddress) GetMode() string {
func (self *SEipAddress) GetAssociationType() string {
switch self.InstanceType {
case EIP_INSTANCE_TYPE_ECS:
case EIP_INSTANCE_TYPE_ECS, "NetworkInterface":
return api.EIP_ASSOCIATE_TYPE_SERVER
case EIP_INSTANCE_TYPE_NAT:
return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
case EIP_INTANNCE_TYPE_SLB:
return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
default:
//log.Fatalf("unsupported type: %s", self.InstanceType)
return "unsupported"
return self.InstanceType
}
}
+3 -1
View File
@@ -113,9 +113,11 @@ func (self *SEipAddress) GetAssociationType() string {
switch net.InterfaceType {
case "nat_gateway":
return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
default:
return net.InterfaceType
}
}
return api.EIP_ASSOCIATE_TYPE_SERVER
return ""
}
func (self *SEipAddress) GetAssociationExternalId() string {
+4 -1
View File
@@ -74,7 +74,10 @@ func (self *SClassicEipAddress) GetAssociationExternalId() string {
}
func (self *SClassicEipAddress) GetAssociationType() string {
return api.EIP_ASSOCIATE_TYPE_SERVER
if len(self.instanceId) > 0 {
return api.EIP_ASSOCIATE_TYPE_SERVER
}
return ""
}
func (self *SClassicEipAddress) GetBandwidth() int {
+17 -13
View File
@@ -23,6 +23,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
billing_api "yunion.io/x/onecloud/pkg/apis/billing"
api "yunion.io/x/onecloud/pkg/apis/compute"
@@ -169,21 +170,28 @@ func (region *SRegion) DissociateEip(eipId string) error {
}
func (self *SEipAddress) GetAssociationExternalId() string {
interfaceId := self.Properties.IPConfiguration.ID
if len(interfaceId) > 0 && strings.Index(interfaceId, "/ipConfigurations/") > 0 {
interfaceId = strings.Split(interfaceId, "/ipConfigurations/")[0]
nic, err := self.region.GetNetworkInterface(interfaceId)
if err != nil {
log.Errorf("Failt to find NetworkInterface for eip %s nic %s", self.Name, interfaceId)
return ""
if self.GetAssociationType() == api.EIP_ASSOCIATE_TYPE_SERVER {
info := strings.Split(self.Properties.IPConfiguration.ID, "/")
nic, _ := self.region.GetNetworkInterface(strings.Join(info[:len(info)-2], "/"))
if nic != nil {
return strings.ToLower(nic.Properties.VirtualMachine.ID)
}
return strings.ToLower(nic.Properties.VirtualMachine.ID)
}
return ""
}
func (self *SEipAddress) GetAssociationType() string {
return api.EIP_ASSOCIATE_TYPE_SERVER
if len(self.Properties.IPConfiguration.ID) == 0 {
return ""
}
if info := strings.Split(self.Properties.IPConfiguration.ID, "/"); len(info) > 7 {
resType := strings.ToLower(info[6])
if utils.IsInStringArray(resType, []string{"networkinterfaces"}) {
return api.EIP_ASSOCIATE_TYPE_SERVER
}
return resType
}
return ""
}
func (self *SEipAddress) GetBandwidth() int {
@@ -214,10 +222,6 @@ func (self *SEipAddress) GetMode() string {
if self.IsEmulated() {
return api.EIP_MODE_INSTANCE_PUBLICIP
}
nic, err := self.region.GetNetworkInterface(self.Properties.IPConfiguration.ID)
if err == nil && len(nic.Properties.VirtualMachine.ID) > 0 {
return api.EIP_MODE_INSTANCE_PUBLICIP
}
return api.EIP_MODE_STANDALONE_EIP
}
+9
View File
@@ -23,6 +23,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/netutils"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudprovider"
@@ -393,6 +394,10 @@ func (region *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
if len(eips[i].GetIpAddr()) == 0 {
continue
}
_, err := netutils.NewIPV4Addr(eips[i].GetIpAddr())
if err != nil {
continue
}
eips[i].region = region
ieips = append(ieips, &eips[i])
}
@@ -400,6 +405,10 @@ func (region *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
if len(classicEips[i].GetIpAddr()) == 0 {
continue
}
_, err := netutils.NewIPV4Addr(eips[i].GetIpAddr())
if err != nil {
continue
}
classicEips[i].region = region
ieips = append(ieips, &classicEips[i])
}
+4 -1
View File
@@ -67,7 +67,10 @@ func (self *SEip) GetINetworkId() string {
}
func (self *SEip) GetAssociationType() string {
return api.EIP_ASSOCIATE_TYPE_SERVER
if len(self.InstanceId) > 0 {
return api.EIP_ASSOCIATE_TYPE_SERVER
}
return ""
}
func (self *SEip) GetAssociationExternalId() string {
+1 -1
View File
@@ -100,7 +100,7 @@ func (e *SEip) GetAssociationType() string {
case "elb":
return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
default:
return "unsupported"
return e.BindType
}
}
+4 -1
View File
@@ -122,7 +122,10 @@ func (addr *SAddress) GetINetworkId() string {
}
func (addr *SAddress) GetAssociationType() string {
return api.EIP_ASSOCIATE_TYPE_SERVER
if len(addr.GetAssociationExternalId()) > 0 {
return api.EIP_ASSOCIATE_TYPE_SERVER
}
return ""
}
func (addr *SAddress) GetAssociationExternalId() string {
+1 -2
View File
@@ -179,8 +179,7 @@ func (self *SEipAddress) GetAssociationType() string {
case "network:nat_gateway":
return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
default:
log.Infof("eip %s associate type: %s", self.ID, port.DeviceOwner)
return api.EIP_ASSOCIATE_TYPE_SERVER
return port.DeviceOwner
}
}
+1 -2
View File
@@ -179,8 +179,7 @@ func (self *SEipAddress) GetAssociationType() string {
case "network:nat_gateway":
return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
default:
log.Infof("eip %s associate type: %s", self.ID, port.DeviceOwner)
return api.EIP_ASSOCIATE_TYPE_SERVER
return port.DeviceOwner
}
}
+1 -1
View File
@@ -74,7 +74,7 @@ func (e *SEip) GetAssociationType() string {
case "lb":
return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
default:
return api.EIP_ASSOCIATE_TYPE_UNKNOWN
return e.InstanceType
}
}
+4 -1
View File
@@ -167,7 +167,10 @@ func (eip *SEipAddress) GetMode() string {
}
func (eip *SEipAddress) GetAssociationType() string {
return api.EIP_ASSOCIATE_TYPE_SERVER
if len(eip.GetAssociationExternalId()) > 0 {
return api.EIP_ASSOCIATE_TYPE_SERVER
}
return ""
}
func (eip *SEipAddress) GetAssociationExternalId() string {
+4 -1
View File
@@ -142,7 +142,10 @@ func (self *SEipAddress) GetAssociationType() string {
return instanceType
}
}
return api.EIP_ASSOCIATE_TYPE_UNKNOWN
if info := strings.Split(self.InstanceId, "-"); len(info) > 0 {
return info[0]
}
return self.InstanceId
}
return ""
}
+11 -8
View File
@@ -157,18 +157,21 @@ func (self *SEip) GetMode() string {
}
func (self *SEip) GetAssociationType() string {
return api.EIP_ASSOCIATE_TYPE_SERVER
switch self.Resource.ResourceType {
case "uhost":
return api.EIP_ASSOCIATE_TYPE_SERVER
case "natgw":
return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
case "ulb":
return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
default:
return self.Resource.ResourceType
}
}
// 已绑定的资源类型, 枚举值为: uhost, 云主机;natgw:NAT网关;ulb:负载均衡器;upm: 物理机; hadoophost: 大数据集群;fortresshost:堡垒机;udockhost:容器;udhost:私有专区主机;vpngw:IPSec VPN;ucdr:云灾备;dbaudit:数据库审计。
func (self *SEip) GetAssociationExternalId() string {
if self.Resource.ResourceType == "uhost" {
return self.Resource.ResourceID
} else if self.Resource.ResourceType != "" {
log.Warningf("GetAssociationExternalId bind with %s %s.expect uhost", self.Resource.ResourceType, self.Resource.ResourceID)
}
return ""
return self.Resource.ResourceID
}
func (self *SEip) GetBandwidth() int {
+4 -1
View File
@@ -101,7 +101,10 @@ func (eip *SEipAddress) GetMode() string {
}
func (eip *SEipAddress) GetAssociationType() string {
return api.EIP_ASSOCIATE_TYPE_SERVER
if len(eip.GetAssociationExternalId()) > 0 {
return api.EIP_ASSOCIATE_TYPE_SERVER
}
return ""
}
func (eip *SEipAddress) GetAssociationExternalId() string {