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

Former-commit-id: 0dfcf5d24e7865fd86bc81278a150c1740da623f
This commit is contained in:
jagger 2024-04-10 09:17:47 +08:00
parent 571e7603c7
commit 5d03296353
5 changed files with 132 additions and 6 deletions

View File

@ -4,14 +4,21 @@ import (
"github.com/zeromicro/go-zero/rest/httpx"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/cloud"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
"net/http"
)
func CommitGeneralTaskHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GeneralTaskReq
if err := httpx.Parse(r, &req); err != nil {
result.ParamErrorResult(r, w, err)
return
}
l := cloud.NewCommitGeneralTaskLogic(r.Context(), svcCtx)
err := l.CommitGeneralTask()
err := l.CommitGeneralTask(&req)
result.HttpResult(r, w, nil, err)
}
}

View File

@ -1,10 +1,17 @@
package cloud
import (
"bytes"
"context"
"github.com/pkg/errors"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models/cloud"
"io"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
kyaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/yaml"
"strings"
"time"
@ -54,13 +61,58 @@ func (l *CommitGeneralTaskLogic) CommitGeneralTask(req *types.GeneralTaskReq) er
return tx.Error
}
var clusterIds []int64
l.svcCtx.DbEngin.Raw("SELECT id FROM `t_cluster` where adapter_id = ? and id in ?", req.AdapterId, req.ClusterIds).Scan(&clusterIds)
if len(clusterIds) == 0 || clusterIds == nil {
var clusters []*models.CloudModel
err := l.svcCtx.DbEngin.Raw("SELECT * FROM `t_cluster` where adapter_id = ? and id in ?", req.AdapterId, req.ClusterIds).Scan(&clusters).Error
if err != nil {
logx.Errorf("CommitGeneralTask() => sql execution error: %v", err)
return errors.Errorf("the cluster does not match the drive resources. Check the data")
}
//保存数据到cloud表
taskCloud := cloud.TaskCloudModel{}
//TODO 执行策略返回集群跟 Replica
for _, c := range clusters {
for _, s := range req.ReqBody {
sStruct := UnMarshalK8sStruct(s)
unString, _ := sStruct.MarshalJSON()
taskCloud.TaskId = uint(taskModel.Id)
taskCloud.AdapterId = c.AdapterId
taskCloud.ClusterId = c.Id
taskCloud.ClusterName = c.Name
taskCloud.Status = "Saved"
taskCloud.YamlString = string(unString)
taskCloud.Kind = sStruct.GetKind()
taskCloud.Namespace = sStruct.GetNamespace()
tx = l.svcCtx.DbEngin.Create(&taskCloud)
if tx.Error != nil {
logx.Errorf("CommitGeneralTask() create taskCloud => sql execution error: %v", err)
return tx.Error
}
}
}
return nil
}
func UnMarshalK8sStruct(yamlString string) *unstructured.Unstructured {
unstructuredObj := &unstructured.Unstructured{}
d := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
var err error
for {
var rawObj runtime.RawExtension
err = d.Decode(&rawObj)
if err == io.EOF {
break
}
obj := &unstructured.Unstructured{}
syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
logx.Errorf("UnMarshalK8sStruct() => Execution failure err:%v", err)
}
unstructuredObj = &unstructured.Unstructured{Object: unstructuredMap}
// 命名空间为空 设置默认值
if len(unstructuredObj.GetNamespace()) == 0 {
unstructuredObj.SetNamespace("default")
}
}
return unstructuredObj
}

View File

@ -0,0 +1,14 @@
package base
import (
"gorm.io/gorm"
"time"
)
type BaseModel struct {
DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"-"` // 删除时间
CreatedBy uint `gorm:"created_by;comment:创建人" json:"createdBy"` //创建人
CreatedTime time.Time `gorm:"comment:创建时间" json:"-"` // 创建时间
UpdatedBy uint `gorm:"updated_by;comment:更新人" json:"UpdatedBy"` //创建人
UpdatedTime time.Time `gorm:"comment:更新时间" json:"-"` // 更新时间
}

View File

@ -0,0 +1,26 @@
package cloud
import (
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models/base"
"time"
)
type TaskCloudModel struct {
Id uint `json:"id" gorm:"primarykey;not null;comment:id"`
TaskId uint `json:"taskId" gorm:"not null;comment:task表id"`
AdapterId uint `json:"adapterId" gorm:"not null;comment:适配器id"`
ClusterId uint `json:"clusterId" gorm:"not null;comment:集群id"`
ClusterName string `json:"clusterName" gorm:"not null;comment:集群名称"`
Kind string `json:"kind" gorm:"comment:种类"`
Status string `json:"status" gorm:"comment:状态"`
StartTime time.Time `json:"startTime" gorm:"comment:开始时间"`
YamlString string `json:"yamlString" gorm:"not null;comment:入参"`
Result string `json:"result" gorm:"comment:运行结果"`
Namespace string `json:"namespace" gorm:"comment:命名空间"`
Replica int `json:"replica" gorm:"not null;comment:副本数"`
base.BaseModel
}
func (TaskCloudModel) TableName() string {
return "task_cloud"
}

27
pkg/models/cloud_model.go Normal file
View File

@ -0,0 +1,27 @@
package models
import "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models/base"
type CloudModel struct {
Id uint `json:"id,omitempty" gorm:"id"`
AdapterId uint `json:"adapterId,omitempty" gorm:"adapter_id"`
Name string `json:"name,omitempty" gorm:"name"`
Nickname string `json:"nickname,omitempty" gorm:"nickname"`
Description string `json:"description,omitempty" gorm:"description"`
Server string `json:"server,omitempty" gorm:"server"`
MonitorServer string `json:"monitorServer,omitempty" gorm:"monitor_server"`
Username string `json:"username,omitempty" gorm:"username"`
Password string `json:"password,omitempty" gorm:"password"`
Token string `json:"token,omitempty" gorm:"token"`
Ak string `json:"ak,omitempty" gorm:"ak"`
Sk string `json:"sk,omitempty" gorm:"sk"`
Region string `json:"region,omitempty" gorm:"region"`
ProjectId string `json:"projectId,omitempty" gorm:"project_id"`
Version string `json:"version,omitempty" gorm:"version"`
Label string `json:"label,omitempty" gorm:"label"`
OwnerId uint `json:"ownerId,omitempty" gorm:"owner_id"`
AuthType int `json:"authType,omitempty" gorm:"auth_type"`
ProducerDict string `json:"producerDict,omitempty" gorm:"producer_dict"`
RegionDict string `json:"regionDict,omitempty" gorm:"region_dict"`
base.BaseModel
}