Skip to content

Commit

Permalink
feat: update domain daily trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
askyrie authored and SongZhen0704 committed Aug 16, 2024
1 parent f1a7764 commit 4a3d675
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 26 deletions.
25 changes: 25 additions & 0 deletions cli/ctl/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ func RegisterCloudCommand() *cobra.Command {
}
cloud.AddCommand(task)

trigger := &cobra.Command{
Use: "trigger domain-lcuuid",
Short: "trigger domain",
Example: "deepflow-ctl cloud trigger domain-lcuuid",
Run: func(cmd *cobra.Command, args []string) {
triggerDomain(cmd, args)
},
}
cloud.AddCommand(trigger)

return cloud
}

Expand Down Expand Up @@ -145,3 +155,18 @@ func getTask(cmd *cobra.Command, args []string) {
}
common.PrettyPrint(resp.Get("DATA"))
}

func triggerDomain(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "must specify domain-lcuuid.")
return
}
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/trigger-domain/%s/", server.IP, server.Port, args[0])
resp, err := common.CURLResponseRawJson("GET", url, []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
common.PrettyPrint(resp)
}
58 changes: 53 additions & 5 deletions server/controller/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"sync"
"time"

Expand All @@ -41,10 +42,12 @@ import (
var log = logging.MustGetLogger("cloud")

type Cloud struct {
synchronizing bool
cfg config.CloudConfig
cCtx context.Context
cCancel context.CancelFunc
mutex sync.RWMutex
triggerTime time.Time
basicInfo model.BasicInfo
resource model.Resource
platform platform.Platform
Expand All @@ -60,10 +63,20 @@ func NewCloud(domain mysql.Domain, cfg config.CloudConfig, ctx context.Context)
return nil
}

// maybe all types will be supported later
var triggerTime time.Time
if domain.Type == common.QINGCLOUD || domain.Type == common.QINGCLOUD_PRIVATE {
triggerTime, err = time.ParseInLocation("15:04", cfg.QingCloudConfig.DailyTriggerTime, time.Local)
if err != nil {
log.Errorf("parse qing config daily trigger time failed: (%s)", err.Error())
}
}

log.Infof("cloud task (%s) init success", domain.Name)

cCtx, cCancel := context.WithCancel(ctx)
return &Cloud{
triggerTime: triggerTime,
basicInfo: model.BasicInfo{
Lcuuid: domain.Lcuuid,
Name: domain.Name,
Expand Down Expand Up @@ -159,6 +172,11 @@ func (c *Cloud) getCloudGatherInterval() int {
}

func (c *Cloud) getCloudData() {
log.Infof("cloud (%s) assemble data starting", c.basicInfo.Name)

c.synchronizing = true
defer func() { c.synchronizing = false }()

var cResource model.Resource
var cloudCost float64
if c.basicInfo.Type != common.KUBERNETES {
Expand Down Expand Up @@ -201,6 +219,7 @@ func (c *Cloud) getCloudData() {
c.resource.ErrorMessage = cResource.ErrorMessage
log.Warningf("get cloud (%s) data, verify is (false), error state (%d), error message (%s)", c.basicInfo.Name, cResource.ErrorState, cResource.ErrorMessage)
}
log.Infof("cloud (%s) assemble data complete", c.basicInfo.Name)
}

func (c *Cloud) sendStatsd(cloudCost float64) {
Expand All @@ -210,15 +229,40 @@ func (c *Cloud) sendStatsd(cloudCost float64) {
statsd.MetaStatsd.RegisterStatsdTable(c)
}

func (c *Cloud) ClientTrigger() error {
if c.synchronizing {
return fmt.Errorf("cloud (%s) is synchronizing, please try again later", c.basicInfo.Name)
}
go c.getCloudData()
return nil
}

func (c *Cloud) dailyTrigger() bool {
if c.triggerTime.IsZero() {
return true
}

now := time.Now()
dailyTime := time.Date(now.Year(), now.Month(), now.Day(), c.triggerTime.Hour(), c.triggerTime.Minute(), 0, 0, time.Local)
timeSub := now.Sub(dailyTime)
if timeSub >= 0 && timeSub <= time.Minute {
return true
}
log.Infof("now is not trigger time (%s), task (%s) not running", dailyTime.Format(common.GO_BIRTHDAY), c.basicInfo.Name)
return false
}

func (c *Cloud) run() {
log.Infof("cloud (%s) started", c.basicInfo.Name)

if err := c.platform.CheckAuth(); err != nil {
log.Errorf("cloud (%+v) check auth failed", c.basicInfo)
}
log.Infof("cloud (%s) assemble data starting", c.basicInfo.Name)
c.getCloudData()
log.Infof("cloud (%s) assemble data complete", c.basicInfo.Name)

// execute immediately upon startup
if c.dailyTrigger() {
c.getCloudData()
}

cloudGatherInterval := c.getCloudGatherInterval()
ticker := time.NewTicker(time.Second * time.Duration(cloudGatherInterval))
Expand All @@ -227,9 +271,13 @@ func (c *Cloud) run() {
for {
select {
case <-ticker.C:
log.Infof("cloud (%s) assemble data starting", c.basicInfo.Name)
if c.synchronizing {
continue
}
if !c.dailyTrigger() {
continue
}
c.getCloudData()
log.Infof("cloud (%s) assemble data complete", c.basicInfo.Name)
case <-c.cCtx.Done():
log.Infof("cloud (%s) stopped", c.basicInfo.Name)
return
Expand Down
21 changes: 0 additions & 21 deletions server/controller/cloud/qingcloud/qingcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type QingCloud struct {
httpTimeout int
MaxRetries uint
RetryDuration uint
DailyTriggerTime time.Time

defaultVPCName string
defaultVxnetName string
Expand Down Expand Up @@ -114,15 +113,6 @@ func NewQingCloud(domain mysql.Domain, cfg cloudconfig.CloudConfig) (*QingCloud,
url = "https://api.qingcloud.com"
}

var dailyTriggerTime time.Time
if cfg.QingCloudConfig.DailyTriggerTime != "" {
dailyTriggerTime, err = time.ParseInLocation("15:04", cfg.QingCloudConfig.DailyTriggerTime, time.Local)
if err != nil {
log.Errorf("parse qing config daily trigger time failed: (%s)", err.Error())
return nil, err
}
}

return &QingCloud{
Uuid: domain.Lcuuid,
// TODO: display_name后期需要修改为uuid_generate
Expand All @@ -137,7 +127,6 @@ func NewQingCloud(domain mysql.Domain, cfg cloudconfig.CloudConfig) (*QingCloud,
MaxRetries: cfg.QingCloudConfig.MaxRetries,
RetryDuration: cfg.QingCloudConfig.RetryDuration,
DisableSyncLBListener: cfg.QingCloudConfig.DisableSyncLBListener,
DailyTriggerTime: dailyTriggerTime,

defaultVPCName: domain.Name + "_default_vpc",
defaultVxnetName: "vxnet-0",
Expand Down Expand Up @@ -329,16 +318,6 @@ func (q *QingCloud) GetStatter() statsd.StatsdStatter {
func (q *QingCloud) GetCloudData() (model.Resource, error) {
var resource model.Resource

if !q.DailyTriggerTime.IsZero() {
now := time.Now()
triggerTime := time.Date(now.Year(), now.Month(), now.Day(), q.DailyTriggerTime.Hour(), q.DailyTriggerTime.Minute(), 0, 0, time.Local)
timeSub := now.Sub(triggerTime)
if timeSub < time.Second || timeSub > time.Minute {
log.Infof("now is not the trigger time (%s), the task is not running", triggerTime.Format(common.GO_BIRTHDAY))
return resource, nil
}
}

// every tasks must init
q.CloudStatsd = statsd.NewCloudStatsd()

Expand Down
9 changes: 9 additions & 0 deletions server/controller/http/router/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (d *Debug) RegisterTo(e *gin.Engine) {
e.GET("/v1/tasks/", getCloudBasicInfos(d.m))
e.GET("/v1/tasks/:lcuuid/", getCloudBasicInfo(d.m))
e.GET("/v1/info/:lcuuid/", getCloudResource(d.m))
e.GET("/v1/trigger-domain/:lcuuid/", triggerDomain(d.m))
e.GET("/v1/genesis/:type/", getGenesisSyncData(d.g, true))
e.GET("/v1/sync/:type/", getGenesisSyncData(d.g, false))
e.GET("/v1/agent-stats/:ipOrID/", getAgentStats(d.g))
Expand Down Expand Up @@ -80,6 +81,14 @@ func getCloudResource(m *manager.Manager) gin.HandlerFunc {
})
}

func triggerDomain(m *manager.Manager) gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
lcuuid := c.Param("lcuuid")
err := service.TriggerDomain(lcuuid, m)
JsonResponse(c, nil, err)
})
}

func getKubernetesGatherBasicInfos(m *manager.Manager) gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
data, err := service.GetKubernetesGatherBasicInfos(c.Param("lcuuid"), m)
Expand Down
4 changes: 4 additions & 0 deletions server/controller/http/service/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func GetCloudResource(lcuuid string, m *manager.Manager) (resp cloudmodel.Resour
}
}

func TriggerDomain(lcuuid string, m *manager.Manager) error {
return m.TriggerDomain(lcuuid)
}

func GetKubernetesGatherBasicInfos(lcuuid string, m *manager.Manager) (resp []kubernetes_gather_model.KubernetesGatherBasicInfo, err error) {
response, err := m.GetKubernetesGatherBasicInfos(lcuuid)
return response, err
Expand Down
10 changes: 10 additions & 0 deletions server/controller/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ func (m *Manager) GetCloudResource(lcuuid string) (model.Resource, error) {
return cloudResource, nil
}

func (m *Manager) TriggerDomain(lcuuid string) error {
m.mutex.RLock()
defer m.mutex.RUnlock()
task, ok := m.taskMap[lcuuid]
if !ok {
return fmt.Errorf("domain (%s) not found", lcuuid)
}
return task.Cloud.ClientTrigger()
}

func (m *Manager) GetKubernetesGatherBasicInfos(lcuuid string) ([]kubernetes_gather_model.KubernetesGatherBasicInfo, error) {
var k8sGatherBasicInfos []kubernetes_gather_model.KubernetesGatherBasicInfo

Expand Down

0 comments on commit 4a3d675

Please sign in to comment.