Merge pull request #13787 from swordqiu/automated-cherry-pick-of-#13785-upstream-release-3.8

Automated cherry pick of #13785: fix: instance group with classic network cannot associate with eip
This commit is contained in:
Zexi Li
2022-03-26 19:00:31 +08:00
committed by GitHub
4 changed files with 45 additions and 2 deletions

View File

@@ -52,7 +52,7 @@ const (
)
var (
EIP_ASSOCIATE_VALID_TYPES = []string{EIP_ASSOCIATE_TYPE_SERVER, EIP_ASSOCIATE_TYPE_NAT_GATEWAY}
EIP_ASSOCIATE_VALID_TYPES = []string{EIP_ASSOCIATE_TYPE_SERVER, EIP_ASSOCIATE_TYPE_NAT_GATEWAY, EIP_ASSOCIATE_TYPE_INSTANCE_GROUP}
)
type ElasticipListInput struct {

View File

@@ -1159,7 +1159,47 @@ func (self *SElasticip) PerformAssociate(ctx context.Context, userCred mcclient.
return input, httperrors.NewInputParameterError("server and eip are not managed by the same provider")
}
input.InstanceExternalId = server.ExternalId
case api.EIP_ASSOCIATE_TYPE_INSTANCE_GROUP:
grpObj, err := GroupManager.FetchByIdOrName(userCred, input.InstanceId)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return input, httperrors.NewResourceNotFoundError("instance group %s not found", input.InstanceId)
}
return input, httperrors.NewGeneralError(err)
}
group := grpObj.(*SGroup)
lockman.LockObject(ctx, group)
defer lockman.ReleaseObject(ctx, group)
net, err := group.isEipAssociable()
if err != nil {
return input, errors.Wrap(err, "grp.isEipAssociable")
}
if net.Id == self.NetworkId {
return input, httperrors.NewInputParameterError("cannot associate eip with same network")
}
eipZone, _ := self.GetZone()
if eipZone != nil {
insZone, _ := net.GetZone()
if eipZone.Id != insZone.Id {
return input, httperrors.NewInputParameterError("cannot associate eip and instance in different zone")
}
}
case api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY:
natgwObj, err := NatGatewayManager.FetchByIdOrName(userCred, input.InstanceId)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return input, httperrors.NewResourceNotFoundError("nat gateway %s not found", input.InstanceId)
}
return input, httperrors.NewGeneralError(err)
}
natgw := natgwObj.(*SNatGateway)
lockman.LockObject(ctx, natgw)
defer lockman.ReleaseObject(ctx, natgw)
}
return input, self.StartEipAssociateInstanceTask(ctx, userCred, input, "")

View File

@@ -696,6 +696,9 @@ func (grp *SGroup) isEipAssociable() (*SNetwork, error) {
if net == nil {
return nil, errors.Wrap(httperrors.ErrInvalidStatus, "group no attached network")
}
if !IsOneCloudVpcResource(net) {
return nil, errors.Wrap(httperrors.ErrInvalidStatus, "group network is not a VPC network")
}
gns, err := grp.getGroupnetworks()
if err != nil {

View File

@@ -1640,7 +1640,7 @@ func (self *SKVMRegionDriver) RequestAssociatEip(ctx context.Context, userCred m
return nil, errors.Wrapf(err, "db.FetchModelObjects")
}
if len(groupnics) == 0 {
return nil, errors.Errorf("guest has no nics to associate eip")
return nil, errors.Errorf("instance group has no nics to associate eip")
}
}