| @@ -1,10 +1,9 @@ | |||||
| package cloud | package cloud | ||||
| import ( | import ( | ||||
| "github.com/zeromicro/go-zero/rest/httpx" | |||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/cloud" | "gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/cloud" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | ||||
| "io" | |||||
| "k8s.io/apimachinery/pkg/util/json" | |||||
| "net/http" | "net/http" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | ||||
| @@ -14,17 +13,10 @@ import ( | |||||
| func ContainerDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | func ContainerDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||||
| return func(w http.ResponseWriter, r *http.Request) { | return func(w http.ResponseWriter, r *http.Request) { | ||||
| var req container.DeleteParam | 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) | result.ParamErrorResult(r, w, err) | ||||
| return | return | ||||
| } | } | ||||
| if err = json.Unmarshal(body, &req); err != nil { | |||||
| result.ParamErrorResult(r, w, err) | |||||
| return | |||||
| } | |||||
| l := cloud.NewContainerDeleteLogic(r.Context(), svcCtx) | l := cloud.NewContainerDeleteLogic(r.Context(), svcCtx) | ||||
| resp, err := l.ContainerDelete(&req) | resp, err := l.ContainerDelete(&req) | ||||
| result.HttpResult(r, w, resp, err) | result.HttpResult(r, w, resp, err) | ||||
| @@ -1,11 +1,10 @@ | |||||
| package cloud | package cloud | ||||
| import ( | 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/internal/logic/cloud" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | ||||
| "io" | |||||
| "net/http" | "net/http" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | ||||
| @@ -15,16 +14,10 @@ import ( | |||||
| func ContainerLogHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | func ContainerLogHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||||
| return func(w http.ResponseWriter, r *http.Request) { | return func(w http.ResponseWriter, r *http.Request) { | ||||
| var req container.LogParam | 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) | result.ParamErrorResult(r, w, err) | ||||
| return | return | ||||
| } | } | ||||
| if err = json.Unmarshal(body, &req); err != nil { | |||||
| result.ParamErrorResult(r, w, err) | |||||
| return | |||||
| } | |||||
| l := cloud.NewContainerLogLogic(r.Context(), svcCtx) | l := cloud.NewContainerLogLogic(r.Context(), svcCtx) | ||||
| resp, err := l.ContainerLog(&req) | resp, err := l.ContainerLog(&req) | ||||
| result.HttpResult(r, w, resp, err) | result.HttpResult(r, w, resp, err) | ||||
| @@ -1,11 +1,10 @@ | |||||
| package cloud | package cloud | ||||
| import ( | 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/internal/logic/cloud" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" | ||||
| "io" | |||||
| "net/http" | "net/http" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" | ||||
| @@ -15,16 +14,10 @@ import ( | |||||
| func ContainerMetricsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | func ContainerMetricsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||||
| return func(w http.ResponseWriter, r *http.Request) { | return func(w http.ResponseWriter, r *http.Request) { | ||||
| var req container.MetricsParam | 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) | result.ParamErrorResult(r, w, err) | ||||
| return | return | ||||
| } | } | ||||
| if err = json.Unmarshal(body, &req); err != nil { | |||||
| result.ParamErrorResult(r, w, err) | |||||
| return | |||||
| } | |||||
| l := cloud.NewContainerMetricsLogic(r.Context(), svcCtx) | l := cloud.NewContainerMetricsLogic(r.Context(), svcCtx) | ||||
| resp, err := l.ContainerMetrics(&req) | resp, err := l.ContainerMetrics(&req) | ||||
| result.HttpResult(r, w, resp, err) | result.HttpResult(r, w, resp, err) | ||||
| @@ -397,7 +397,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | |||||
| { | { | ||||
| // 删除容器 | // 删除容器 | ||||
| Method: http.MethodDelete, | Method: http.MethodDelete, | ||||
| Path: "/cloud/container/delete", | |||||
| Path: "/cloud/container/delete/:id", | |||||
| Handler: cloud.ContainerDeleteHandler(serverCtx), | Handler: cloud.ContainerDeleteHandler(serverCtx), | ||||
| }, | }, | ||||
| { | { | ||||
| @@ -409,13 +409,13 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | |||||
| { | { | ||||
| // 获取容器日志 | // 获取容器日志 | ||||
| Method: http.MethodGet, | Method: http.MethodGet, | ||||
| Path: "/cloud/container/log", | |||||
| Path: "/cloud/container/log/:id", | |||||
| Handler: cloud.ContainerLogHandler(serverCtx), | Handler: cloud.ContainerLogHandler(serverCtx), | ||||
| }, | }, | ||||
| { | { | ||||
| // 获取容器监控数据 | // 获取容器监控数据 | ||||
| Method: http.MethodGet, | Method: http.MethodGet, | ||||
| Path: "/cloud/container/metrics", | |||||
| Path: "/cloud/container/metrics/:id", | |||||
| Handler: cloud.ContainerMetricsHandler(serverCtx), | Handler: cloud.ContainerMetricsHandler(serverCtx), | ||||
| }, | }, | ||||
| }, | }, | ||||
| @@ -18,10 +18,14 @@ import ( | |||||
| "context" | "context" | ||||
| "errors" | "errors" | ||||
| "github.com/zeromicro/go-zero/core/logx" | "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/svc" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | |||||
| container "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/cloud" | container "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/cloud" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" | "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" | ||||
| "k8s.io/apimachinery/pkg/util/json" | |||||
| "net/http" | "net/http" | ||||
| "strconv" | |||||
| ) | ) | ||||
| type ContainerDeleteLogic struct { | 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) { | 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{} | 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 == "" { | 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 { | if err != nil { | ||||
| return nil, err | return nil, err | ||||
| } | } | ||||
| @@ -53,5 +74,12 @@ func (l *ContainerDeleteLogic) ContainerDelete(req *container.DeleteParam) (resp | |||||
| } | } | ||||
| taskCloud.Status = "Deleted" | taskCloud.Status = "Deleted" | ||||
| l.svcCtx.DbEngin.Updates(taskCloud) | l.svcCtx.DbEngin.Updates(taskCloud) | ||||
| task.Status = "Deleted" | |||||
| l.svcCtx.DbEngin.Updates(task) | |||||
| return | return | ||||
| } | } | ||||
| type DeleteContainerParam struct { | |||||
| RequestId string `json:"RequestId"` | |||||
| ContainerGroupId string `json:"ContainerGroupId"` | |||||
| } | |||||
| @@ -18,9 +18,14 @@ import ( | |||||
| "context" | "context" | ||||
| "errors" | "errors" | ||||
| "github.com/zeromicro/go-zero/core/logx" | "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/svc" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | |||||
| container "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/cloud" | 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" | "net/http" | ||||
| "strconv" | |||||
| ) | ) | ||||
| type ContainerLogLogic struct { | 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) { | 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) | get, err := l.svcCtx.Cloud.ContainerLog(req.ClusterId, req) | ||||
| if err != nil { | if err != nil { | ||||
| return nil, err | return nil, err | ||||
| @@ -18,9 +18,14 @@ import ( | |||||
| "context" | "context" | ||||
| "errors" | "errors" | ||||
| "github.com/zeromicro/go-zero/core/logx" | "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/svc" | ||||
| "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" | |||||
| container "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/cloud" | 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" | "net/http" | ||||
| "strconv" | |||||
| ) | ) | ||||
| type ContainerMetricsLogic struct { | 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) { | 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) | get, err := l.svcCtx.Cloud.ContainerMetrics(req.ClusterId, req) | ||||
| if err != nil { | if err != nil { | ||||
| return nil, err | 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"` | Name string `json:"name,omitempty"` | ||||
| GetParameter interface{} `json:"getParameter,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 { | 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"` | Name string `json:"name,optional"` | ||||
| DeleteParameter interface{} `json:"deleteParameter,omitempty"` | |||||
| DeleteParameter interface{} `json:"deleteParameter,optional"` | |||||
| } | } | ||||
| func (k K8sDeleteParam) ContainerDeleteParameter() { | func (k K8sDeleteParam) ContainerDeleteParameter() { | ||||
| @@ -91,14 +91,16 @@ type EciGetParam struct { | |||||
| ContainerGroupName string `json:"containerGroupName,omitempty"` | ContainerGroupName string `json:"containerGroupName,omitempty"` | ||||
| } | } | ||||
| type LogParam struct { | 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 { | 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() { | func (k K8sLogParam) ContainerLogParam() { | ||||