Skip to content

Commit

Permalink
feat(region): Check periodically whether Guest Template is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
rainzm committed May 22, 2020
1 parent 1265e5d commit f9b7c7e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
20 changes: 20 additions & 0 deletions pkg/apis/compute/guesttemplate_const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package compute

const (
GT_READY = "ready"
GT_INVALID = "invalid"
)
39 changes: 38 additions & 1 deletion pkg/compute/models/guest_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package models
import (
"context"
"fmt"
"time"

"yunion.io/x/jsonutils"
"yunion.io/x/log"
Expand Down Expand Up @@ -80,6 +81,8 @@ type SGuestTemplate struct {

// 其他配置信息
Content jsonutils.JSONObject `nullable:"false" list:"user" update:"user" create:"optional" json:"content"`

LastCheckTime time.Time
}

var GuestTemplateManager *SGuestTemplateManager
Expand Down Expand Up @@ -125,10 +128,19 @@ func (gtm *SGuestTemplateManager) ValidateCreateData(

func (gt *SGuestTemplate) PostCreate(ctx context.Context, userCred mcclient.TokenCredential,
ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
gt.SetStatus(userCred, "ready", "")
gt.SetStatus(userCred, computeapis.GT_READY, "")
gt.updateCheckTime()
logclient.AddActionLogWithContext(ctx, gt, logclient.ACT_CREATE, nil, userCred, true)
}

func (gt *SGuestTemplate) updateCheckTime() error {
_, err := db.Update(gt, func() error {
gt.LastCheckTime = time.Now()
return nil
})
return err
}

func (gt *SGuestTemplate) PostUpdate(ctx context.Context, userCred mcclient.TokenCredential,
query jsonutils.JSONObject, data jsonutils.JSONObject) {
logclient.AddActionLogWithContext(ctx, gt, logclient.ACT_UPDATE, nil, userCred, true)
Expand Down Expand Up @@ -749,3 +761,28 @@ func (g *SGuest) PerformSaveTemplate(ctx context.Context, userCred mcclient.Toke
}
return nil, nil
}

func (gm *SGuestTemplateManager) InspectAllTemplate(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
lastCheckTime := time.Now().Add(time.Duration(-options.Options.GuestTemplateCheckInterval) * time.Hour)
q := gm.Query().Equals("status", computeapis.GT_READY)
q = q.Filter(sqlchemy.OR(sqlchemy.IsNull(q.Field("last_check_time")), sqlchemy.LE(q.Field("last_check_time"),
lastCheckTime)))
gts := make([]SGuestTemplate, 0, 10)
err := db.FetchModelObjects(gm, q, &gts)
if err != nil {
log.Errorf("Unable to fetch all guest templates that need to check: %s", err.Error())
return
}
for i := range gts {
_, err := GuestManager.validateCreateData(ctx, userCred, gts[i].GetOwnerId(), jsonutils.NewDict(),
gts[i].Content.(*jsonutils.JSONDict))
if err == nil {
gts[i].updateCheckTime()
continue
}
// invalid
gts[i].updateCheckTime()
reason := fmt.Sprintf("During the inspection, the guest template is not available: %s", err.Error())
gts[i].SetStatus(userCred, computeapis.GT_INVALID, reason)
}
}
3 changes: 3 additions & 0 deletions pkg/compute/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ type ComputeOptions struct {

FetchEtcdServiceInfoAndUseEtcdLock bool `default:"true" help:"fetch etcd service info and use etcd lock"`

GuestTemplateCheckInterval int `help:"The interval between two consecutive inspections about Guest Template,
unit: h" default:"12"`

SCapabilityOptions
SASControllerOptions
common_options.CommonOptions
Expand Down
2 changes: 2 additions & 0 deletions pkg/compute/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func StartService() {
cron.AddJobEveryFewDays("SyncDBInstanceSkus", opts.SyncSkusDay, opts.SyncSkusHour, 0, 0, models.SyncDBInstanceSkus, true)
cron.AddJobEveryFewDays("SyncElasticCacheSkus", opts.SyncSkusDay, opts.SyncSkusHour, 0, 0, models.SyncElasticCacheSkus, true)
cron.AddJobEveryFewDays("StorageSnapshotsRecycle", 1, 2, 0, 0, models.StorageManager.StorageSnapshotsRecycle, false)

cron.AddJobEveryFewHour("InspectAllTemplate", 1, 0, 0, models.GuestTemplateManager.InspectAllTemplate, true)
go cron.Start2(ctx, electObj)

// init auto scaling controller
Expand Down

0 comments on commit f9b7c7e

Please sign in to comment.