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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "io"
  10. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  11. "k8s.io/apimachinery/pkg/runtime"
  12. syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
  13. kyaml "k8s.io/apimachinery/pkg/util/yaml"
  14. "sigs.k8s.io/yaml"
  15. "strings"
  16. "time"
  17. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
  18. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
  19. "github.com/zeromicro/go-zero/core/logx"
  20. )
  21. type CommitGeneralTaskLogic struct {
  22. logx.Logger
  23. ctx context.Context
  24. svcCtx *svc.ServiceContext
  25. }
  26. func NewCommitGeneralTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommitGeneralTaskLogic {
  27. return &CommitGeneralTaskLogic{
  28. Logger: logx.WithContext(ctx),
  29. ctx: ctx,
  30. svcCtx: svcCtx,
  31. }
  32. }
  33. func (l *CommitGeneralTaskLogic) CommitGeneralTask(req *types.GeneralTaskReq) error {
  34. var yamlStr []string
  35. for _, s := range req.ReqBody {
  36. j2, err := yaml.YAMLToJSON([]byte(s))
  37. if err != nil {
  38. logx.Errorf("Failed to convert yaml to JSON, err: %v", err)
  39. return err
  40. }
  41. yamlStr = append(yamlStr, string(j2))
  42. }
  43. result := strings.Join(yamlStr, ",")
  44. //TODO The namespace is fixed to ns-admin for the time being. Later, the namespace is obtained based on the user
  45. taskModel := models.Task{
  46. Status: constants.Saved,
  47. Name: req.Name,
  48. CommitTime: time.Now(),
  49. NsID: "ns-admin",
  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 = ? and id in ?", req.AdapterId, 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.TaskId = uint(taskModel.Id)
  70. taskCloud.AdapterId = c.AdapterId
  71. taskCloud.ClusterId = c.Id
  72. taskCloud.ClusterName = c.Name
  73. taskCloud.Status = "Saved"
  74. taskCloud.YamlString = string(unString)
  75. taskCloud.Kind = sStruct.GetKind()
  76. taskCloud.Namespace = sStruct.GetNamespace()
  77. tx = l.svcCtx.DbEngin.Create(&taskCloud)
  78. if tx.Error != nil {
  79. logx.Errorf("CommitGeneralTask() create taskCloud => sql execution error: %v", err)
  80. return tx.Error
  81. }
  82. }
  83. }
  84. return nil
  85. }
  86. func UnMarshalK8sStruct(yamlString string) *unstructured.Unstructured {
  87. unstructuredObj := &unstructured.Unstructured{}
  88. d := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
  89. var err error
  90. for {
  91. var rawObj runtime.RawExtension
  92. err = d.Decode(&rawObj)
  93. if err == io.EOF {
  94. break
  95. }
  96. obj := &unstructured.Unstructured{}
  97. syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
  98. unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  99. if err != nil {
  100. logx.Errorf("UnMarshalK8sStruct() => Execution failure err:%v", err)
  101. }
  102. unstructuredObj = &unstructured.Unstructured{Object: unstructuredMap}
  103. // 命名空间为空 设置默认值
  104. if len(unstructuredObj.GetNamespace()) == 0 {
  105. unstructuredObj.SetNamespace("default")
  106. }
  107. }
  108. return unstructuredObj
  109. }

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.