mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-10-31 02:38:07 +08:00
Merge pull request #5978 from rainzm/fix/auto_scaling8
Fix/Auto Scaling
This commit is contained in:
@@ -56,10 +56,11 @@ const (
|
||||
TIMER_TYPE_MONTH = "month"
|
||||
|
||||
// 加入中 和 加入失败 的不算是 ScalingGroup 的机器
|
||||
SG_GUEST_STATUS_JOINING = "joining" // 加入中
|
||||
SG_GUEST_STATUS_READY = "ready" // 正常
|
||||
SG_GUEST_STATUS_REMOVING = "removing" // 移除中
|
||||
SG_GUEST_STATUS_REMOVE_FAILED = "remove_failed" // 移除失败
|
||||
SG_GUEST_STATUS_JOINING = "joining" // 加入中
|
||||
SG_GUEST_STATUS_READY = "ready" // 正常
|
||||
SG_GUEST_STATUS_REMOVING = "removing" // 移除中
|
||||
SG_GUEST_STATUS_REMOVE_FAILED = "remove_failed" // 移除失败
|
||||
SG_GUEST_STATUS_PENDING_REMOVE = "pending_remove" // 机器进入回收站
|
||||
|
||||
// 只有ready状态是正常的
|
||||
SG_STATUS_READY = "ready" // 正常
|
||||
|
||||
@@ -17,7 +17,7 @@ package compute
|
||||
import "yunion.io/x/onecloud/pkg/apis"
|
||||
|
||||
type ScalingPolicyDetails struct {
|
||||
apis.StatusStandaloneResourceDetails
|
||||
apis.VirtualResourceDetails
|
||||
ScalingGroupResourceInfo
|
||||
SScalingPolicy
|
||||
// 定时方式触发
|
||||
@@ -29,7 +29,7 @@ type ScalingPolicyDetails struct {
|
||||
}
|
||||
|
||||
type ScalingPolicyCreateInput struct {
|
||||
apis.StandaloneResourceCreateInput
|
||||
apis.VirtualResourceCreateInput
|
||||
apis.EnabledBaseResourceCreateInput
|
||||
|
||||
// description: scaling_group ID or Name
|
||||
@@ -67,7 +67,7 @@ type ScalingPolicyCreateInput struct {
|
||||
}
|
||||
|
||||
type ScalingPolicyListInput struct {
|
||||
apis.StatusStandaloneResourceListInput
|
||||
apis.VirtualResourceListInput
|
||||
apis.EnabledResourceBaseListInput
|
||||
|
||||
// description: scaling group
|
||||
|
||||
@@ -1737,7 +1737,7 @@ type SScalingGroupResourceBase struct {
|
||||
|
||||
// SScalingPolicy is an autogenerated struct via yunion.io/x/onecloud/pkg/compute/models.SScalingPolicy.
|
||||
type SScalingPolicy struct {
|
||||
apis.SStatusStandaloneResourceBase
|
||||
apis.SVirtualResourceBase
|
||||
SScalingGroupResourceBase
|
||||
apis.SEnabledResourceBase
|
||||
TriggerType string `json:"trigger_type"`
|
||||
|
||||
@@ -170,21 +170,12 @@ func (manager *SGuestManager) FetchCustomizeColumns(
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(fields) == 0 || fields.Contains("scaling_status") {
|
||||
if len(fields) == 0 || fields.Contains("scaling_group") {
|
||||
sggs := fetchScalingGroupGuest(guestIds...)
|
||||
if sggs != nil && len(sggs) != 0 {
|
||||
for i := range rows {
|
||||
if sgg, ok := sggs[guestIds[i]]; ok {
|
||||
rows[i].ScalingStatus = sgg.GuestStatus
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if fields.Contains("scaling_group_id") {
|
||||
sggs := fetchScalingGroupGuest(guestIds...)
|
||||
if sggs != nil && len(sggs) != 0 {
|
||||
for i := range rows {
|
||||
if sgg, ok := sggs[guestIds[i]]; ok {
|
||||
rows[i].ScalingGroupId = sgg.ScalingGroupId
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,7 +211,7 @@ func (manager *SGuestManager) ListItemFilter(
|
||||
q = q.In("id", diskQ.SubQuery())
|
||||
}
|
||||
|
||||
scalingGroupQ := ScalingGroupGuestManager.Query("guest_id").Snapshot()
|
||||
scalingGroupQ := ScalingGroupGuestManager.Query("guest_id").NotEquals("guest_status", api.SG_GUEST_STATUS_PENDING_REMOVE).Snapshot()
|
||||
scalingGroupQ, err = manager.SScalingGroupResourceBaseManager.ListItemFilter(ctx, scalingGroupQ, userCred, query.ScalingGroupFilterListInput)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SScaligGroupResourceBaseManager.ListItemFilter")
|
||||
@@ -4605,13 +4605,13 @@ func (self *SGuest) GetIVM() (cloudprovider.ICloudVM, error) {
|
||||
return ihost.GetIVMById(self.ExternalId)
|
||||
}
|
||||
|
||||
func (self *SGuest) DetachScalingGroup(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
func (self *SGuest) PendingDetachScalingGroup() error {
|
||||
sggs, err := ScalingGroupGuestManager.Fetch("", self.GetId())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range sggs {
|
||||
sggs[i].Detach(ctx, userCred)
|
||||
sggs[i].SetGuestStatus(api.SG_GUEST_STATUS_PENDING_REMOVE)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -379,7 +379,7 @@ func (sgm *SScalingGroupManager) FetchCustomizeColumns(
|
||||
|
||||
func (sg *SScalingGroup) GuestNumber() (int, error) {
|
||||
q := GuestManager.Query().In("id", ScalingGroupGuestManager.Query("guest_id").Equals("scaling_group_id",
|
||||
sg.Id).SubQuery()).IsFalse("pending_deleted")
|
||||
sg.Id).NotEquals("guest_status", api.SG_GUEST_STATUS_PENDING_REMOVE).SubQuery()).IsFalse("pending_deleted")
|
||||
return q.CountWithError()
|
||||
}
|
||||
|
||||
|
||||
@@ -32,18 +32,17 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
"yunion.io/x/onecloud/pkg/util/rbacutils"
|
||||
"yunion.io/x/onecloud/pkg/util/stringutils2"
|
||||
)
|
||||
|
||||
type SScalingPolicyManager struct {
|
||||
db.SStatusStandaloneResourceBaseManager
|
||||
db.SVirtualResourceBaseManager
|
||||
SScalingGroupResourceBaseManager
|
||||
db.SEnabledResourceBaseManager
|
||||
}
|
||||
|
||||
type SScalingPolicy struct {
|
||||
db.SStatusStandaloneResourceBase
|
||||
db.SVirtualResourceBase
|
||||
SScalingGroupResourceBase
|
||||
db.SEnabledResourceBase
|
||||
|
||||
@@ -65,7 +64,7 @@ var ScalingPolicyManager *SScalingPolicyManager
|
||||
|
||||
func init() {
|
||||
ScalingPolicyManager = &SScalingPolicyManager{
|
||||
SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
|
||||
SVirtualResourceBaseManager: db.NewVirtualResourceBaseManager(
|
||||
SScalingPolicy{},
|
||||
"scalingpolicies_tbl",
|
||||
"scalingpolicy",
|
||||
@@ -78,7 +77,7 @@ func init() {
|
||||
func (spm *SScalingPolicyManager) ValidateListConditions(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
var err error
|
||||
query, err = spm.SStandaloneResourceBaseManager.ValidateListConditions(ctx, userCred, query)
|
||||
query, err = spm.SVirtualResourceBaseManager.ValidateListConditions(ctx, userCred, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -91,7 +90,7 @@ func (spm *SScalingPolicyManager) ValidateListConditions(ctx context.Context, us
|
||||
func (spm *SScalingPolicyManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential, input api.ScalingPolicyListInput) (*sqlchemy.SQuery, error) {
|
||||
var err error
|
||||
q, err = spm.SStatusStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, input.StatusStandaloneResourceListInput)
|
||||
q, err = spm.SVirtualResourceBaseManager.ListItemFilter(ctx, q, userCred, input.VirtualResourceListInput)
|
||||
if err != nil {
|
||||
return q, err
|
||||
}
|
||||
@@ -110,7 +109,7 @@ func (spm *SScalingPolicyManager) ListItemFilter(ctx context.Context, q *sqlchem
|
||||
}
|
||||
|
||||
func (spm *SScalingPolicyManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
|
||||
q, err := spm.SStatusStandaloneResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
q, err := spm.SVirtualResourceBaseManager.QueryDistinctExtraField(q, field)
|
||||
if err == nil {
|
||||
return q, nil
|
||||
}
|
||||
@@ -127,7 +126,7 @@ func (spm *SScalingPolicyManager) FilterByParentId(q *sqlchemy.SQuery, parentId
|
||||
|
||||
func (spm *SScalingPolicyManager) OrderByExtraFields(ctx context.Context, q *sqlchemy.SQuery,
|
||||
userCred mcclient.TokenCredential, query api.ScalingPolicyListInput) (*sqlchemy.SQuery, error) {
|
||||
return spm.SStatusStandaloneResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.StatusStandaloneResourceListInput)
|
||||
return spm.SVirtualResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.VirtualResourceListInput)
|
||||
}
|
||||
|
||||
func (sgm *SScalingPolicy) GetExtraDetails(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
@@ -144,7 +143,7 @@ func (spm *SScalingPolicyManager) FetchCustomizeColumns(
|
||||
isList bool,
|
||||
) []api.ScalingPolicyDetails {
|
||||
rows := make([]api.ScalingPolicyDetails, len(objs))
|
||||
statusRows := spm.SStatusStandaloneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
statusRows := spm.SVirtualResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
sgRows := spm.SScalingGroupResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
|
||||
var err error
|
||||
for i := range rows {
|
||||
@@ -152,7 +151,7 @@ func (spm *SScalingPolicyManager) FetchCustomizeColumns(
|
||||
if err != nil {
|
||||
log.Errorf("SScalingPolicy.getMoreDetails error: %s", err)
|
||||
}
|
||||
rows[i].StatusStandaloneResourceDetails = statusRows[i]
|
||||
rows[i].VirtualResourceDetails = statusRows[i]
|
||||
rows[i].ScalingGroupResourceInfo = sgRows[i]
|
||||
}
|
||||
return rows
|
||||
@@ -200,8 +199,8 @@ func (spm *SScalingPolicyManager) ValidateCreateData(ctx context.Context, userCr
|
||||
api.ScalingPolicyCreateInput, error) {
|
||||
log.Debugf("insert validateCreateData")
|
||||
var err error
|
||||
input.StandaloneResourceCreateInput, err = spm.SStandaloneResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query,
|
||||
input.StandaloneResourceCreateInput)
|
||||
input.VirtualResourceCreateInput, err = spm.SVirtualResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query,
|
||||
input.VirtualResourceCreateInput)
|
||||
if err != nil {
|
||||
return input, err
|
||||
}
|
||||
@@ -514,35 +513,3 @@ func (sg *SScalingPolicy) PostCreate(ctx context.Context, userCred mcclient.Toke
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
func (spm *SScalingPolicyManager) NamespaceScope() rbacutils.TRbacScope {
|
||||
return rbacutils.ScopeProject
|
||||
}
|
||||
|
||||
func (spm *SScalingPolicyManager) ResourceScope() rbacutils.TRbacScope {
|
||||
return rbacutils.ScopeProject
|
||||
}
|
||||
|
||||
func (spm *SScalingPolicyManager) FilterByOwner(q *sqlchemy.SQuery, owner mcclient.IIdentityProvider, scope rbacutils.TRbacScope) *sqlchemy.SQuery {
|
||||
if owner != nil {
|
||||
switch scope {
|
||||
case rbacutils.ScopeProject, rbacutils.ScopeDomain:
|
||||
scalingGroupQ := ScalingGroupManager.Query("id", "domain_id").SubQuery()
|
||||
q = q.Join(scalingGroupQ, sqlchemy.Equals(q.Field("scaling_group_id"), scalingGroupQ.Field("id")))
|
||||
q = q.Filter(sqlchemy.Equals(scalingGroupQ.Field("domain_id"), owner.GetProjectDomainId()))
|
||||
}
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
func (spm *SScalingPolicyManager) FetchOwnerId(ctx context.Context, data jsonutils.JSONObject) (mcclient.IIdentityProvider, error) {
|
||||
return db.FetchDomainInfo(ctx, data)
|
||||
}
|
||||
|
||||
func (sp *SScalingPolicy) GetOwnerId() mcclient.IIdentityProvider {
|
||||
scalingGroup := sp.GetScalingGroup()
|
||||
if scalingGroup != nil {
|
||||
return scalingGroup.GetOwnerId()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -402,12 +402,8 @@ func (sa *SScalingAlarm) ValidateCreateData(input api.ScalingPolicyCreateInput)
|
||||
return input, nil
|
||||
}
|
||||
|
||||
var notificationID = ""
|
||||
|
||||
func (spm *SScalingPolicyManager) NotificationID(session *mcclient.ClientSession) (string, error) {
|
||||
if len(notificationID) != 0 {
|
||||
return notificationID, nil
|
||||
}
|
||||
var notificationID = ""
|
||||
params := jsonutils.NewDict()
|
||||
params.Set("type", jsonutils.NewString(monapi.AlertNotificationTypeAutoScaling))
|
||||
|
||||
@@ -422,7 +418,7 @@ func (spm *SScalingPolicyManager) NotificationID(session *mcclient.ClientSession
|
||||
// To create new one
|
||||
conTrue, conFalse := true, false
|
||||
ncinput := monapi.NotificationCreateInput{
|
||||
Name: "autoscaling",
|
||||
Name: fmt.Sprintf("as-%s-%s", session.GetDomainName(), session.GetProjectName()),
|
||||
Type: monapi.AlertNotificationTypeAutoScaling,
|
||||
IsDefault: false,
|
||||
SendReminder: &conFalse,
|
||||
@@ -433,7 +429,7 @@ func (spm *SScalingPolicyManager) NotificationID(session *mcclient.ClientSession
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "Notification.Create")
|
||||
}
|
||||
notificationID, _ := ret.GetString("id")
|
||||
notificationID, _ = ret.GetString("id")
|
||||
return notificationID, nil
|
||||
}
|
||||
|
||||
@@ -442,7 +438,7 @@ func (sa *SScalingAlarm) Register(ctx context.Context, userCred mcclient.TokenCr
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
session := auth.GetAdminSession(ctx, "", "")
|
||||
session := auth.GetSession(ctx, userCred, "", "")
|
||||
notificationID, err := ScalingPolicyManager.NotificationID(session)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "ScalingPolicyManager.NotificationID")
|
||||
|
||||
@@ -77,7 +77,7 @@ func (sgg *SScalingGroupGuest) Detach(ctx context.Context, userCred mcclient.Tok
|
||||
func (sggm *SScalingGroupGuestManager) Fetch(scalingGroupId, guestId string) ([]SScalingGroupGuest, error) {
|
||||
|
||||
sggs := make([]SScalingGroupGuest, 0)
|
||||
q := sggm.Query()
|
||||
q := sggm.Query().NotEquals("guest_status", compute.SG_GUEST_STATUS_PENDING_REMOVE)
|
||||
if len(scalingGroupId) != 0 {
|
||||
q = q.Equals("scaling_group_id", scalingGroupId)
|
||||
}
|
||||
|
||||
@@ -182,8 +182,6 @@ func (self *GuestDeleteTask) OnSyncConfigComplete(ctx context.Context, obj db.IS
|
||||
|
||||
// try to leave all groups
|
||||
guest.LeaveAllGroups(ctx, self.UserCred)
|
||||
// detach
|
||||
guest.DetachScalingGroup(ctx, self.UserCred)
|
||||
isPurge := jsonutils.QueryBoolean(self.Params, "purge", false)
|
||||
overridePendingDelete := jsonutils.QueryBoolean(self.Params, "override_pending_delete", false)
|
||||
|
||||
@@ -193,6 +191,8 @@ func (self *GuestDeleteTask) OnSyncConfigComplete(ctx context.Context, obj db.IS
|
||||
return
|
||||
}
|
||||
log.Debugf("XXXXXXX Do guest pending delete... XXXXXXX")
|
||||
// pending detach
|
||||
guest.PendingDetachScalingGroup()
|
||||
guestStatus, _ := self.Params.GetString("guest_status")
|
||||
if !utils.IsInStringArray(guestStatus, []string{
|
||||
api.VM_SCHEDULE_FAILED, api.VM_NETWORK_FAILED, api.VM_DISK_FAILED,
|
||||
|
||||
@@ -356,7 +356,7 @@ Loop:
|
||||
case compute.SG_GUEST_STATUS_REMOVE_FAILED:
|
||||
succeedList.Delete(sggs[i].GetId())
|
||||
failedList = append(failedList, fmt.Sprintf("remove instance '%s' failed", sggs[i].GetId()))
|
||||
case compute.SG_GUEST_STATUS_READY, compute.SG_GUEST_STATUS_REMOVING:
|
||||
case compute.SG_GUEST_STATUS_READY, compute.SG_GUEST_STATUS_REMOVING, compute.SG_GUEST_STATUS_PENDING_REMOVE:
|
||||
waitList = append(waitList, sggs[i].GetId())
|
||||
default:
|
||||
log.Errorf("unkown guest status for ScalingGroupGuest '%s'", sggs[i].GetId())
|
||||
|
||||
Reference in New Issue
Block a user