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.

commitgeneraltasklogic.go 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package cloud
  2. import (
  3. "bytes"
  4. "context"
  5. "github.com/pkg/errors"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models/cloud"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  10. "io"
  11. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  12. "k8s.io/apimachinery/pkg/runtime"
  13. syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
  14. kyaml "k8s.io/apimachinery/pkg/util/yaml"
  15. "sigs.k8s.io/yaml"
  16. "strings"
  17. "time"
  18. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
  19. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
  20. "github.com/zeromicro/go-zero/core/logx"
  21. )
  22. type CommitGeneralTaskLogic struct {
  23. logx.Logger
  24. ctx context.Context
  25. svcCtx *svc.ServiceContext
  26. }
  27. func NewCommitGeneralTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommitGeneralTaskLogic {
  28. return &CommitGeneralTaskLogic{
  29. Logger: logx.WithContext(ctx),
  30. ctx: ctx,
  31. svcCtx: svcCtx,
  32. }
  33. }
  34. func (l *CommitGeneralTaskLogic) CommitGeneralTask(req *types.GeneralTaskReq) error {
  35. var yamlStr []string
  36. for _, s := range req.ReqBody {
  37. j2, err := yaml.YAMLToJSON([]byte(s))
  38. if err != nil {
  39. logx.Errorf("Failed to convert yaml to JSON, err: %v", err)
  40. return err
  41. }
  42. yamlStr = append(yamlStr, string(j2))
  43. }
  44. result := strings.Join(yamlStr, ",")
  45. //TODO The namespace is fixed to ns-admin for the time being. Later, the namespace is obtained based on the user
  46. taskModel := models.Task{
  47. Status: constants.Saved,
  48. Name: req.Name,
  49. CommitTime: time.Now(),
  50. YamlString: "[" + result + "]",
  51. }
  52. // Save the task data to the database
  53. tx := l.svcCtx.DbEngin.Create(&taskModel)
  54. if tx.Error != nil {
  55. return tx.Error
  56. }
  57. var clusters []*models.CloudModel
  58. err := l.svcCtx.DbEngin.Raw("SELECT * FROM `t_cluster` where adapter_id in ? and id in ?", req.AdapterIds, req.ClusterIds).Scan(&clusters).Error
  59. if err != nil {
  60. logx.Errorf("CommitGeneralTask() => sql execution error: %v", err)
  61. return errors.Errorf("the cluster does not match the drive resources. Check the data")
  62. }
  63. taskCloud := cloud.TaskCloudModel{}
  64. //TODO 执行策略返回集群跟 Replica
  65. for _, c := range clusters {
  66. for _, s := range req.ReqBody {
  67. sStruct := UnMarshalK8sStruct(s)
  68. unString, _ := sStruct.MarshalJSON()
  69. taskCloud.Id = utils.GenSnowflakeIDUint()
  70. taskCloud.TaskId = uint(taskModel.Id)
  71. taskCloud.AdapterId = c.AdapterId
  72. taskCloud.ClusterId = c.Id
  73. taskCloud.ClusterName = c.Name
  74. taskCloud.Status = "Pending"
  75. taskCloud.YamlString = string(unString)
  76. taskCloud.Kind = sStruct.GetKind()
  77. taskCloud.Namespace = sStruct.GetNamespace()
  78. tx = l.svcCtx.DbEngin.Create(&taskCloud)
  79. if tx.Error != nil {
  80. logx.Errorf("CommitGeneralTask() create taskCloud => sql execution error: %v", err)
  81. return tx.Error
  82. }
  83. }
  84. }
  85. return nil
  86. }
  87. func UnMarshalK8sStruct(yamlString string) *unstructured.Unstructured {
  88. unstructuredObj := &unstructured.Unstructured{}
  89. d := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
  90. var err error
  91. for {
  92. var rawObj runtime.RawExtension
  93. err = d.Decode(&rawObj)
  94. if err == io.EOF {
  95. break
  96. }
  97. obj := &unstructured.Unstructured{}
  98. syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
  99. unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  100. if err != nil {
  101. logx.Errorf("UnMarshalK8sStruct() => Execution failure err:%v", err)
  102. }
  103. unstructuredObj = &unstructured.Unstructured{Object: unstructuredMap}
  104. // 命名空间为空 设置默认值
  105. if len(unstructuredObj.GetNamespace()) == 0 {
  106. unstructuredObj.SetNamespace("default")
  107. }
  108. //设置副本数
  109. if unstructuredObj.GetKind() == "Deployment" || unstructuredObj.GetKind() == "StatefulSet" {
  110. unstructured.SetNestedField(
  111. unstructuredObj.Object,
  112. int64(6),
  113. "spec", "replicas",
  114. )
  115. }
  116. }
  117. return unstructuredObj
  118. }

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.