You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

containercreatelogic.go 3.6 kB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package cloud
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. "net/http"
  18. "strconv"
  19. "time"
  20. "github.com/zeromicro/go-zero/core/logx"
  21. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/participant/cloud"
  22. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  23. container "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/cloud"
  24. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  25. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  26. cloud2 "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models/cloud"
  27. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  28. )
  29. type ContainerCreateLogic struct {
  30. logx.Logger
  31. ctx context.Context
  32. svcCtx *svc.ServiceContext
  33. }
  34. func NewContainerCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ContainerCreateLogic {
  35. return &ContainerCreateLogic{
  36. Logger: logx.WithContext(ctx),
  37. ctx: ctx,
  38. svcCtx: svcCtx,
  39. }
  40. }
  41. func (l *ContainerCreateLogic) ContainerCreate(req *container.CreateParam) (resp *ContainerCreateRespStruct, err error) {
  42. resp = &ContainerCreateRespStruct{}
  43. param := &cloud.CreateParam{
  44. Name: req.Name,
  45. Description: req.Description,
  46. ImagePullSecrets: req.ImagePullSecrets,
  47. ImageRegistry: req.ImageRegistry,
  48. ImageUsername: req.ImageUsername,
  49. ImagePassword: req.ImagePassword,
  50. Port: req.Port,
  51. Cpu: req.Cpu,
  52. Memory: req.Memory,
  53. Image: req.Image,
  54. Args: req.Args,
  55. MountPath: req.MountPath,
  56. Envs: req.Envs,
  57. NodePort: req.NodePort,
  58. ContainerGroupName: req.ContainerGroupName,
  59. CreateParameter: req.ContainerCreateParameter,
  60. }
  61. create, err := l.svcCtx.Cloud.ContainerCreate(req.ClusterId, param)
  62. if err != nil {
  63. return nil, err
  64. }
  65. if create.Code != http.StatusOK {
  66. return nil, errors.New(create.Message)
  67. }
  68. // 构建主任务结构体
  69. taskModel := models.Task{
  70. Id: utils.GenSnowflakeID(),
  71. Status: constants.Saved,
  72. Description: req.Description,
  73. Name: req.Name,
  74. UserId: req.UserId,
  75. UserName: req.UserName,
  76. AdapterTypeDict: "0",
  77. CommitTime: time.Now(),
  78. }
  79. // 保存任务数据到数据库
  80. tx := l.svcCtx.DbEngin.Create(&taskModel)
  81. if tx.Error != nil {
  82. }
  83. var adapterId int64
  84. tx.Table("t_cluster").Select("adapter_id").Where("id=?", req.ClusterId).Find(&adapterId)
  85. // 构建cloud任务结构体
  86. cloudTaskModel := cloud2.TaskCloudModel{
  87. Id: utils.GenSnowflakeID(),
  88. TaskId: taskModel.Id,
  89. Name: req.Name,
  90. AdapterId: adapterId,
  91. Status: constants.Saved,
  92. Namespace: "default",
  93. UserId: req.UserId,
  94. ClusterId: req.ClusterId,
  95. ResourceSpec: cloud2.ResourceSpec{
  96. CPU: req.Cpu,
  97. Memory: req.Memory,
  98. },
  99. }
  100. // 保存任务数据到数据库
  101. tx = l.svcCtx.DbEngin.Create(&cloudTaskModel)
  102. if tx.Error != nil {
  103. fmt.Println()
  104. return nil, tx.Error
  105. }
  106. resp.TaskId = strconv.FormatInt(taskModel.Id, 10)
  107. return resp, nil
  108. }
  109. type ContainerCreateRespStruct struct {
  110. TaskId string `json:"taskId"`
  111. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.