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 4.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "k8s.io/apimachinery/pkg/util/json"
  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. UserName: req.UserName,
  81. AdapterTypeDict: "0",
  82. CommitTime: time.Now(),
  83. }
  84. // 保存任务数据到数据库
  85. tx := l.svcCtx.DbEngin.Create(&taskModel)
  86. if tx.Error != nil {
  87. }
  88. type ClusterInfo struct {
  89. Name string `json:"name"`
  90. AdapterId int64 `json:"adapter_id"`
  91. }
  92. var ci ClusterInfo
  93. tx.Table("t_cluster").Select("adapter_id,name").Where("id=?", req.ClusterId).Find(&ci)
  94. // 构建cloud任务结构体
  95. cloudTaskModel := cloud2.TaskCloudModel{
  96. BusinessCode: string(marshal),
  97. Id: utils.GenSnowflakeID(),
  98. TaskId: taskModel.Id,
  99. Name: req.Name,
  100. AdapterId: ci.AdapterId,
  101. Status: constants.Saved,
  102. Namespace: "default",
  103. UserId: req.UserId,
  104. ClusterId: req.ClusterId,
  105. ClusterName: ci.Name,
  106. ResourceSpec: cloud2.ResourceSpec{
  107. ResourceName: fmt.Sprintf("%s_cpu %s_mem %s", ci.Name, req.Cpu, req.Memory),
  108. ResourceType: constants.TaskTypeCloud,
  109. Specifications: map[string]interface{}{
  110. "CPU": req.Cpu,
  111. "Memory": req.Memory,
  112. },
  113. },
  114. }
  115. // 保存任务数据到数据库
  116. tx = l.svcCtx.DbEngin.Create(&cloudTaskModel)
  117. if tx.Error != nil {
  118. fmt.Println()
  119. return nil, tx.Error
  120. }
  121. resp.TaskId = strconv.FormatInt(taskModel.Id, 10)
  122. return resp, nil
  123. }
  124. type ContainerCreateRespStruct struct {
  125. TaskId string `json:"taskId"`
  126. }

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.