Signed-off-by: jagger <cossjie@foxmail.com>

Former-commit-id: 0d2a0bae21
This commit is contained in:
jagger 2024-04-30 16:07:53 +08:00
parent dd5d975df2
commit 61338174e8
5 changed files with 79 additions and 51 deletions

View File

@ -5,8 +5,8 @@ Port: 8999
Timeout: 50000 Timeout: 50000
DB: DB:
DataSource: root:uJpLd6u-J?HC1@(47.92.88.143:3306)/pcm?parseTime=true&loc=Local DataSource: root:uJpLd6u-J?HC1@(10.206.0.12:3306)/pcm?parseTime=true&loc=Local
# DataSource: root:uJpLd6u-J?HC1@(47.92.88.143:3306)/pcm?parseTime=true&loc=Local # DataSource: root:uJpLd6u-J?HC1@(47.92.88.143:3306)/pcm?parseTime=true&loc=Local
Redis: Redis:
Host: 10.206.0.12:6379 Host: 10.206.0.12:6379
Pass: redisPW123 Pass: redisPW123

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"github.com/pkg/errors" "github.com/pkg/errors"
clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/api/client"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers/option" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers/option"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
@ -15,7 +16,6 @@ import (
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml" syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
kyaml "k8s.io/apimachinery/pkg/util/yaml" kyaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/yaml"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -41,71 +41,98 @@ func NewCommitGeneralTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext)
} }
func (l *CommitGeneralTaskLogic) CommitGeneralTask(req *types.GeneralTaskReq) error { func (l *CommitGeneralTaskLogic) CommitGeneralTask(req *types.GeneralTaskReq) error {
var yamlStr []string tx := l.svcCtx.DbEngin.Begin()
for _, s := range req.ReqBody { // 执行回滚或者提交操作
j2, err := yaml.YAMLToJSON([]byte(s)) defer func() {
if err != nil { if p := recover(); p != nil {
logx.Errorf("Failed to convert yaml to JSON, err: %v", err) tx.Rollback()
return err logx.Error(p)
} else if tx.Error != nil {
logx.Info("rollback, error", tx.Error)
tx.Rollback()
} else {
tx = tx.Commit()
logx.Info("commit success")
} }
yamlStr = append(yamlStr, string(j2)) }()
} //TODO adapter
result := strings.Join(yamlStr, ",") adapterId, _ := strconv.ParseUint(req.AdapterIds[0], 10, 64)
//TODO The namespace is fixed to ns-admin for the time being. Later, the namespace is obtained based on the user
taskModel := models.Task{
Status: constants.Saved,
Name: req.Name,
CommitTime: time.Now(),
YamlString: "[" + result + "]",
}
// Save the task data to the database
tx := l.svcCtx.DbEngin.Create(&taskModel)
if tx.Error != nil {
return tx.Error
}
var clusters []*models.CloudModel var clusters []*models.CloudModel
err := l.svcCtx.DbEngin.Raw("SELECT * FROM `t_cluster` where adapter_id in ? and id in ?", req.AdapterIds, req.ClusterIds).Scan(&clusters).Error err := tx.Raw("SELECT * FROM `t_cluster` where adapter_id in ? and id in ?", req.AdapterIds, req.ClusterIds).Scan(&clusters).Error
if err != nil { if err != nil {
logx.Errorf("CommitGeneralTask() => sql execution error: %v", err) logx.Errorf("CommitGeneralTask() => sql execution error: %v", err)
return errors.Errorf("the cluster does not match the drive resources. Check the data") return errors.Errorf("the cluster does not match the drive resources. Check the data")
} }
taskCloud := cloud.TaskCloudModel{} taskCloud := cloud.TaskCloudModel{}
//TODO 执行策略返回集群跟 Replica
opt := &option.CloudOption{} opt := &option.CloudOption{}
utils.Convert(&req, &opt) utils.Convert(&req, &opt)
sc, err := schedulers.NewCloudScheduler(l.ctx, "", l.svcCtx.Scheduler, opt, l.svcCtx.DbEngin, l.svcCtx.PromClient) sc, _ := schedulers.NewCloudScheduler(l.ctx, "", l.svcCtx.Scheduler, opt, tx, l.svcCtx.PromClient)
if err != nil {
return err
}
results, err := l.svcCtx.Scheduler.AssignAndSchedule(sc) results, err := l.svcCtx.Scheduler.AssignAndSchedule(sc)
if err != nil { if err != nil {
logx.Errorf("AssignAndSchedule() => execution error: %v", err)
return err return err
} }
rs := (results).([]*schedulers.CloudResult) rs := (results).([]*schedulers.CloudResult)
var synergyStatus int64
if len(rs) > 1 {
synergyStatus = 1
}
var strategy int64
sqlStr := `select t_dict_item.item_value
from t_dict
left join t_dict_item on t_dict.id = t_dict_item.dict_id
where item_text = ?
and t_dict.dict_code = 'schedule_Strategy'`
//查询调度策略
err = tx.Raw(sqlStr, req.Strategy).Scan(&strategy).Error
taskModel := models.Task{
Id: utils.GenSnowflakeID(),
Status: constants.Pending,
Name: req.Name,
CommitTime: time.Now(),
YamlString: strings.Join(req.ReqBody, "\n---\n"),
TaskTypeDict: 0,
SynergyStatus: synergyStatus,
Strategy: strategy,
}
var taskClouds []cloud.TaskCloudModel
for _, r := range rs { for _, r := range rs {
for _, s := range req.ReqBody { for _, s := range req.ReqBody {
sStruct := UnMarshalK8sStruct(s, int64(r.Replica)) sStruct := UnMarshalK8sStruct(s, int64(r.Replica))
unString, _ := sStruct.MarshalJSON() unString, _ := sStruct.MarshalJSON()
taskCloud.Id = utils.GenSnowflakeIDUint() taskCloud.Id = utils.GenSnowflakeIDUint()
taskCloud.TaskId = uint(taskModel.Id) taskCloud.TaskId = uint(taskModel.Id)
adapterId, _ := strconv.ParseUint(req.AdapterIds[0], 10, 64)
clusterId, _ := strconv.ParseUint(r.ClusterId, 10, 64) clusterId, _ := strconv.ParseUint(r.ClusterId, 10, 64)
taskCloud.AdapterId = uint(adapterId) taskCloud.AdapterId = uint(adapterId)
taskCloud.ClusterId = uint(clusterId) taskCloud.ClusterId = uint(clusterId)
taskCloud.ClusterName = r.ClusterName taskCloud.ClusterName = r.ClusterName
taskCloud.Status = "Pending" taskCloud.Status = constants.Pending
taskCloud.YamlString = string(unString) taskCloud.YamlString = string(unString)
taskCloud.Kind = sStruct.GetKind() taskCloud.Kind = sStruct.GetKind()
taskCloud.Namespace = sStruct.GetNamespace() taskCloud.Namespace = sStruct.GetNamespace()
tx = l.svcCtx.DbEngin.Create(&taskCloud) taskClouds = append(taskClouds, taskCloud)
if tx.Error != nil {
logx.Errorf("CommitGeneralTask() create taskCloud => sql execution error: %v", err)
return tx.Error
}
} }
} }
adapterName := ""
tx.Table("t_adapter").Select("name").Where("id=?", adapterId).Find(&adapterName)
noticeInfo := clientCore.NoticeInfo{
AdapterId: int64(adapterId),
AdapterName: adapterName,
NoticeType: "create",
TaskName: req.Name,
Incident: "任务创建中",
CreatedTime: time.Now(),
}
db := tx.Table("task").Create(&taskModel)
db = tx.Table("task_cloud").Create(&taskClouds)
db = tx.Table("t_notice").Create(&noticeInfo)
if db.Error != nil {
logx.Errorf("Task creation failure, err: %v", db.Error)
return errors.New("task creation failure")
}
return nil return nil
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/api/client" clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/api/client"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models/cloud"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
"gorm.io/gorm" "gorm.io/gorm"
@ -54,7 +55,7 @@ func (l *PullTaskInfoLogic) PullTaskInfo(req *clientCore.PullTaskInfoReq) (*clie
} }
} }
case 0: case 0:
var cloudModelList []models.Cloud var cloudModelList []cloud.TaskCloudModel
err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &cloudModelList) err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &cloudModelList)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -26,4 +26,5 @@ const (
WaitRestart = "WaitRestart" WaitRestart = "WaitRestart"
WaitPause = "WaitPause" WaitPause = "WaitPause"
WaitStart = "WaitStart" WaitStart = "WaitStart"
Pending = "Pending"
) )

View File

@ -6,18 +6,17 @@ import (
) )
type TaskCloudModel struct { type TaskCloudModel struct {
Id uint `json:"id" gorm:"primarykey;not null;comment:id"` Id uint `json:"id" gorm:"primarykey;not null;comment:id"`
TaskId uint `json:"taskId" gorm:"not null;comment:task表id"` TaskId uint `json:"taskId" gorm:"not null;comment:task表id"`
AdapterId uint `json:"adapterId" gorm:"not null;comment:适配器id"` AdapterId uint `json:"adapterId" gorm:"not null;comment:适配器id"`
ClusterId uint `json:"clusterId" gorm:"not null;comment:集群id"` ClusterId uint `json:"clusterId" gorm:"not null;comment:集群id"`
ClusterName string `json:"clusterName" gorm:"not null;comment:集群名称"` ClusterName string `json:"clusterName" gorm:"not null;comment:集群名称"`
Kind string `json:"kind" gorm:"comment:种类"` Kind string `json:"kind" gorm:"comment:种类"`
Status string `json:"status" gorm:"comment:状态"` Status string `json:"status" gorm:"comment:状态"`
StartTime time.Time `json:"startTime" gorm:"comment:开始时间"` StartTime *time.Time `json:"startTime" gorm:"comment:开始时间"`
YamlString string `json:"yamlString" gorm:"not null;comment:入参"` YamlString string `json:"yamlString" gorm:"not null;comment:入参"`
Result string `json:"result" gorm:"comment:运行结果"` Result string `json:"result" gorm:"comment:运行结果"`
Namespace string `json:"namespace" gorm:"comment:命名空间"` Namespace string `json:"namespace" gorm:"comment:命名空间"`
Replica int `json:"replica" gorm:"not null;comment:副本数"`
base.BaseModel base.BaseModel
} }