| @@ -1,10 +1,9 @@ | |||
| package cloud | |||
| import ( | |||
| "github.com/zeromicro/go-zero/rest/httpx" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/cloud" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | |||
| "io" | |||
| "k8s.io/apimachinery/pkg/util/json" | |||
| "net/http" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | |||
| @@ -14,17 +13,10 @@ import ( | |||
| func ContainerDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | |||
| return func(w http.ResponseWriter, r *http.Request) { | |||
| var req container.DeleteParam | |||
| body, err := io.ReadAll(r.Body) | |||
| if err != nil { | |||
| if err := httpx.Parse(r, &req); err != nil { | |||
| result.ParamErrorResult(r, w, err) | |||
| return | |||
| } | |||
| if err = json.Unmarshal(body, &req); err != nil { | |||
| result.ParamErrorResult(r, w, err) | |||
| return | |||
| } | |||
| l := cloud.NewContainerDeleteLogic(r.Context(), svcCtx) | |||
| resp, err := l.ContainerDelete(&req) | |||
| result.HttpResult(r, w, resp, err) | |||
| @@ -1,11 +1,10 @@ | |||
| package cloud | |||
| import ( | |||
| "encoding/json" | |||
| "github.com/zeromicro/go-zero/rest/httpx" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/cloud" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | |||
| "io" | |||
| "net/http" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | |||
| @@ -15,16 +14,10 @@ import ( | |||
| func ContainerLogHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | |||
| return func(w http.ResponseWriter, r *http.Request) { | |||
| var req container.LogParam | |||
| body, err := io.ReadAll(r.Body) | |||
| if err != nil { | |||
| if err := httpx.Parse(r, &req); err != nil { | |||
| result.ParamErrorResult(r, w, err) | |||
| return | |||
| } | |||
| if err = json.Unmarshal(body, &req); err != nil { | |||
| result.ParamErrorResult(r, w, err) | |||
| return | |||
| } | |||
| l := cloud.NewContainerLogLogic(r.Context(), svcCtx) | |||
| resp, err := l.ContainerLog(&req) | |||
| result.HttpResult(r, w, resp, err) | |||
| @@ -1,11 +1,10 @@ | |||
| package cloud | |||
| import ( | |||
| "encoding/json" | |||
| "github.com/zeromicro/go-zero/rest/httpx" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/cloud" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | |||
| "io" | |||
| "net/http" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | |||
| @@ -15,16 +14,10 @@ import ( | |||
| func ContainerMetricsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | |||
| return func(w http.ResponseWriter, r *http.Request) { | |||
| var req container.MetricsParam | |||
| body, err := io.ReadAll(r.Body) | |||
| if err != nil { | |||
| if err := httpx.Parse(r, &req); err != nil { | |||
| result.ParamErrorResult(r, w, err) | |||
| return | |||
| } | |||
| if err = json.Unmarshal(body, &req); err != nil { | |||
| result.ParamErrorResult(r, w, err) | |||
| return | |||
| } | |||
| l := cloud.NewContainerMetricsLogic(r.Context(), svcCtx) | |||
| resp, err := l.ContainerMetrics(&req) | |||
| result.HttpResult(r, w, resp, err) | |||
| @@ -397,7 +397,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | |||
| { | |||
| // 删除容器 | |||
| Method: http.MethodDelete, | |||
| Path: "/cloud/container/delete", | |||
| Path: "/cloud/container/delete/:id", | |||
| Handler: cloud.ContainerDeleteHandler(serverCtx), | |||
| }, | |||
| { | |||
| @@ -409,13 +409,13 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | |||
| { | |||
| // 获取容器日志 | |||
| Method: http.MethodGet, | |||
| Path: "/cloud/container/log", | |||
| Path: "/cloud/container/log/:id", | |||
| Handler: cloud.ContainerLogHandler(serverCtx), | |||
| }, | |||
| { | |||
| // 获取容器监控数据 | |||
| Method: http.MethodGet, | |||
| Path: "/cloud/container/metrics", | |||
| Path: "/cloud/container/metrics/:id", | |||
| Handler: cloud.ContainerMetricsHandler(serverCtx), | |||
| }, | |||
| }, | |||
| @@ -18,10 +18,14 @@ import ( | |||
| "context" | |||
| "errors" | |||
| "github.com/zeromicro/go-zero/core/logx" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/participant/cloud" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | |||
| container "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/cloud" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" | |||
| "k8s.io/apimachinery/pkg/util/json" | |||
| "net/http" | |||
| "strconv" | |||
| ) | |||
| type ContainerDeleteLogic struct { | |||
| @@ -39,12 +43,29 @@ func NewContainerDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *C | |||
| } | |||
| func (l *ContainerDeleteLogic) ContainerDelete(req *container.DeleteParam) (resp interface{}, err error) { | |||
| task := &types.TaskModel{} | |||
| l.svcCtx.DbEngin.Table("task").Where("id", req.Id).Scan(&task) | |||
| if task.Name == "" { | |||
| return nil, errors.New("任务不存在") | |||
| } | |||
| taskCloud := models.TaskCloud{} | |||
| l.svcCtx.DbEngin.Model(models.TaskAi{}).Where("id", req.Id).Scan(&taskCloud) | |||
| l.svcCtx.DbEngin.Table("task_cloud").Where("task_id", req.Id).Scan(&taskCloud) | |||
| if taskCloud.Name == "" { | |||
| return nil, errors.New("任务不存在") | |||
| return nil, errors.New("通算任务不存在") | |||
| } | |||
| create, err := l.svcCtx.Cloud.ContainerDelete(req.ClusterId, req) | |||
| if taskCloud.BusinessCode != "" && taskCloud.BusinessCode != "null" { | |||
| var eciRequestParam cloud.EciRequestParam | |||
| err := json.Unmarshal([]byte(taskCloud.BusinessCode), &eciRequestParam) | |||
| if err != nil { | |||
| logx.Errorf("Failed to unmarshal BusinessCode: %v", err) | |||
| return nil, err | |||
| } | |||
| req.Name = eciRequestParam.ContainerGroupId | |||
| } else { | |||
| req.Name = taskCloud.Name | |||
| } | |||
| req.ClusterId = strconv.FormatInt(taskCloud.ClusterId, 10) | |||
| create, err := l.svcCtx.Cloud.ContainerDelete(strconv.FormatInt(taskCloud.ClusterId, 10), req) | |||
| if err != nil { | |||
| return nil, err | |||
| } | |||
| @@ -53,5 +74,12 @@ func (l *ContainerDeleteLogic) ContainerDelete(req *container.DeleteParam) (resp | |||
| } | |||
| taskCloud.Status = "Deleted" | |||
| l.svcCtx.DbEngin.Updates(taskCloud) | |||
| task.Status = "Deleted" | |||
| l.svcCtx.DbEngin.Updates(task) | |||
| return | |||
| } | |||
| type DeleteContainerParam struct { | |||
| RequestId string `json:"RequestId"` | |||
| ContainerGroupId string `json:"ContainerGroupId"` | |||
| } | |||
| @@ -18,9 +18,14 @@ import ( | |||
| "context" | |||
| "errors" | |||
| "github.com/zeromicro/go-zero/core/logx" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/participant/cloud" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | |||
| container "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/cloud" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" | |||
| "k8s.io/apimachinery/pkg/util/json" | |||
| "net/http" | |||
| "strconv" | |||
| ) | |||
| type ContainerLogLogic struct { | |||
| @@ -38,6 +43,30 @@ func NewContainerLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Cont | |||
| } | |||
| func (l *ContainerGetLogic) ContainerLog(req *container.LogParam) (resp interface{}, err error) { | |||
| task := &types.TaskModel{} | |||
| l.svcCtx.DbEngin.Table("task").Where("id", req.Id).Scan(&task) | |||
| if task.Name == "" { | |||
| return nil, errors.New("任务不存在") | |||
| } | |||
| taskCloud := models.TaskCloud{} | |||
| l.svcCtx.DbEngin.Table("task_cloud").Where("task_id", req.Id).Scan(&taskCloud) | |||
| if taskCloud.Name == "" { | |||
| return nil, errors.New("通算任务不存在") | |||
| } | |||
| if taskCloud.BusinessCode != "" && taskCloud.BusinessCode != "null" { | |||
| var eciRequestParam cloud.EciRequestParam | |||
| err := json.Unmarshal([]byte(taskCloud.BusinessCode), &eciRequestParam) | |||
| if err != nil { | |||
| logx.Errorf("Failed to unmarshal BusinessCode: %v", err) | |||
| return nil, err | |||
| } | |||
| eciLogParam := cloud.EciLogParam{ | |||
| ContainerGroupId: eciRequestParam.ContainerGroupId, | |||
| } | |||
| req.ContainerLogParameter = eciLogParam | |||
| } | |||
| req.Name = taskCloud.Name | |||
| req.ClusterId = strconv.FormatInt(taskCloud.ClusterId, 10) | |||
| get, err := l.svcCtx.Cloud.ContainerLog(req.ClusterId, req) | |||
| if err != nil { | |||
| return nil, err | |||
| @@ -18,9 +18,14 @@ import ( | |||
| "context" | |||
| "errors" | |||
| "github.com/zeromicro/go-zero/core/logx" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/participant/cloud" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | |||
| container "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/cloud" | |||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" | |||
| "k8s.io/apimachinery/pkg/util/json" | |||
| "net/http" | |||
| "strconv" | |||
| ) | |||
| type ContainerMetricsLogic struct { | |||
| @@ -38,6 +43,28 @@ func NewContainerMetricsLogic(ctx context.Context, svcCtx *svc.ServiceContext) * | |||
| } | |||
| func (l *ContainerMetricsLogic) ContainerMetrics(req *container.MetricsParam) (resp interface{}, err error) { | |||
| task := &types.TaskModel{} | |||
| l.svcCtx.DbEngin.Table("task").Where("id", req.Id).Scan(&task) | |||
| if task.Name == "" { | |||
| return nil, errors.New("任务不存在") | |||
| } | |||
| taskCloud := models.TaskCloud{} | |||
| l.svcCtx.DbEngin.Table("task_cloud").Where("task_id", req.Id).Scan(&taskCloud) | |||
| if taskCloud.Name == "" { | |||
| return nil, errors.New("通算任务不存在") | |||
| } | |||
| if taskCloud.BusinessCode != "" && taskCloud.BusinessCode != "null" { | |||
| var eciRequestParam cloud.EciRequestParam | |||
| err := json.Unmarshal([]byte(taskCloud.BusinessCode), &eciRequestParam) | |||
| if err != nil { | |||
| logx.Errorf("Failed to unmarshal BusinessCode: %v", err) | |||
| return nil, err | |||
| } | |||
| req.Name = eciRequestParam.ContainerGroupId | |||
| } else { | |||
| req.Name = taskCloud.Name | |||
| } | |||
| req.ClusterId = strconv.FormatInt(taskCloud.ClusterId, 10) | |||
| get, err := l.svcCtx.Cloud.ContainerMetrics(req.ClusterId, req) | |||
| if err != nil { | |||
| return nil, err | |||
| @@ -0,0 +1,16 @@ | |||
| package cloud | |||
| type ContainerCreateParameter interface { | |||
| ContainerCreateParam() | |||
| } | |||
| type ContainerDeleteParameter interface { | |||
| ContainerDeleteParam() | |||
| } | |||
| type ContainerGetParameter interface { | |||
| ContainerGetParam() | |||
| } | |||
| type ContainerLogParameter interface { | |||
| ContainerLogParam() | |||
| } | |||
| @@ -41,3 +41,16 @@ type GetParam struct { | |||
| Name string `json:"name,omitempty"` | |||
| GetParameter interface{} `json:"getParameter,omitempty"` | |||
| } | |||
| type LogParam struct { | |||
| Name string `json:"name,omitempty"` | |||
| GetParameter interface{} `json:"getParameter,omitempty"` | |||
| } | |||
| type EciLogParam struct { | |||
| ContainerGroupId string `json:"containerGroupId"` | |||
| } | |||
| type EciRequestParam struct { | |||
| RequestId string `json:"RequestId"` | |||
| ContainerGroupId string `json:"ContainerGroupId"` | |||
| } | |||
| @@ -51,10 +51,10 @@ func (e EciCreateParam) ContainerCreateParam() { | |||
| // 删除容器参数 | |||
| type DeleteParam struct { | |||
| Id string `json:"id"` | |||
| ClusterId string `json:"clusterId,omitempty"` | |||
| Id string `path:"id"` | |||
| ClusterId string `json:"clusterId,optional"` | |||
| Name string `json:"name,optional"` | |||
| DeleteParameter interface{} `json:"deleteParameter,omitempty"` | |||
| DeleteParameter interface{} `json:"deleteParameter,optional"` | |||
| } | |||
| func (k K8sDeleteParam) ContainerDeleteParameter() { | |||
| @@ -91,14 +91,16 @@ type EciGetParam struct { | |||
| ContainerGroupName string `json:"containerGroupName,omitempty"` | |||
| } | |||
| type LogParam struct { | |||
| ClusterId string `json:"clusterId"` | |||
| Name string `json:"name,omitempty"` | |||
| ContainerLogParameter interface{} `json:"containerLogParameter,omitempty"` | |||
| Id string `path:"id"` | |||
| ClusterId string `json:"clusterId,optional"` | |||
| Name string `json:"name,optional"` | |||
| ContainerLogParameter interface{} `json:"containerLogParameter,optional"` | |||
| } | |||
| type MetricsParam struct { | |||
| ClusterId string `json:"clusterId"` | |||
| Name string `json:"name,omitempty"` | |||
| Id string `path:"id"` | |||
| ClusterId string `json:"clusterId,optional"` | |||
| Name string `json:"name,optional"` | |||
| } | |||
| func (k K8sLogParam) ContainerLogParam() { | |||