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.7 kB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. "github.com/zeromicro/go-zero/core/logx"
  18. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/participant/cloud"
  19. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  20. container "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/cloud"
  21. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  22. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  23. cloud2 "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models/cloud"
  24. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  25. "k8s.io/apimachinery/pkg/util/json"
  26. "net/http"
  27. "strconv"
  28. "time"
  29. )
  30. type ContainerCreateLogic struct {
  31. logx.Logger
  32. ctx context.Context
  33. svcCtx *svc.ServiceContext
  34. }
  35. func NewContainerCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ContainerCreateLogic {
  36. return &ContainerCreateLogic{
  37. Logger: logx.WithContext(ctx),
  38. ctx: ctx,
  39. svcCtx: svcCtx,
  40. }
  41. }
  42. func (l *ContainerCreateLogic) ContainerCreate(req *container.CreateParam) (resp *ContainerCreateRespStruct, err error) {
  43. resp = &ContainerCreateRespStruct{}
  44. param := &cloud.CreateParam{
  45. Name: req.Name,
  46. Description: req.Description,
  47. ImagePullSecrets: req.ImagePullSecrets,
  48. ImageRegistry: req.ImageRegistry,
  49. ImageUsername: req.ImageUsername,
  50. ImagePassword: req.ImagePassword,
  51. Port: req.Port,
  52. Cpu: req.Cpu,
  53. Memory: req.Memory,
  54. Image: req.Image,
  55. Args: req.Args,
  56. MountPath: req.MountPath,
  57. Envs: req.Envs,
  58. NodePort: req.NodePort,
  59. ContainerGroupName: req.ContainerGroupName,
  60. CreateParameter: req.ContainerCreateParameter,
  61. }
  62. create, err := l.svcCtx.Cloud.ContainerCreate(req.ClusterId, param)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if create.Code != http.StatusOK {
  67. return nil, errors.New(create.Message)
  68. }
  69. marshal, err := json.Marshal(create.Data)
  70. if err != nil {
  71. return nil, err
  72. }
  73. // 构建主任务结构体
  74. taskModel := models.Task{
  75. Id: utils.GenSnowflakeID(),
  76. Status: constants.Saved,
  77. Description: req.Description,
  78. Name: req.Name,
  79. UserId: req.UserId,
  80. AdapterTypeDict: "0",
  81. CommitTime: time.Now(),
  82. }
  83. // 保存任务数据到数据库
  84. tx := l.svcCtx.DbEngin.Create(&taskModel)
  85. if tx.Error != nil {
  86. }
  87. var adapterId int64
  88. tx.Table("t_cluster").Select("adapter_id").Where("id=?", req.ClusterId).Find(&adapterId)
  89. // 构建cloud任务结构体
  90. cloudTaskModel := cloud2.TaskCloudModel{
  91. Id: utils.GenSnowflakeID(),
  92. TaskId: taskModel.Id,
  93. Name: req.Name,
  94. AdapterId: adapterId,
  95. Status: constants.Saved,
  96. Namespace: "default",
  97. UserId: req.UserId,
  98. ClusterId: req.ClusterId,
  99. BusinessCode: string(marshal),
  100. ResourceSpec: cloud2.ResourceSpec{
  101. CPU: req.Cpu,
  102. Memory: req.Memory,
  103. },
  104. }
  105. // 保存任务数据到数据库
  106. tx = l.svcCtx.DbEngin.Create(&cloudTaskModel)
  107. if tx.Error != nil {
  108. fmt.Println()
  109. return nil, tx.Error
  110. }
  111. resp.TaskId = strconv.FormatInt(taskModel.Id, 10)
  112. return resp, nil
  113. }
  114. type ContainerCreateRespStruct struct {
  115. TaskId string `json:"taskId"`
  116. }

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.