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.

ScheduleCloud.go 2.2 kB

2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package kq
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
  8. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
  9. "gitlink.org.cn/jcce-pcm/pcm-coordinator/model"
  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. )
  16. /*
  17. *
  18. Listening to the payment flow status change notification message queue
  19. */
  20. type ScheduleCloudMq struct {
  21. ctx context.Context
  22. svcCtx *svc.ServiceContext
  23. }
  24. func NewScheduleCloudMq(ctx context.Context, svcCtx *svc.ServiceContext) *ScheduleCloudMq {
  25. return &ScheduleCloudMq{
  26. ctx: ctx,
  27. svcCtx: svcCtx,
  28. }
  29. }
  30. func UnMarshalK8sStruct(yamlString string, taskId int64) model.Cloud {
  31. var cloud model.Cloud
  32. d := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
  33. var err error
  34. for {
  35. var rawObj runtime.RawExtension
  36. err = d.Decode(&rawObj)
  37. if err == io.EOF {
  38. break
  39. }
  40. if err != nil {
  41. }
  42. obj := &unstructured.Unstructured{}
  43. syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
  44. if err != nil {
  45. }
  46. unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  47. if err != nil {
  48. }
  49. unstructureObj := &unstructured.Unstructured{Object: unstructuredMap}
  50. cloud = model.Cloud{
  51. TaskId: taskId,
  52. ApiVersion: unstructureObj.GetAPIVersion(),
  53. Name: unstructureObj.GetName(),
  54. Kind: unstructureObj.GetKind(),
  55. Namespace: unstructureObj.GetNamespace(),
  56. Status: "Saved",
  57. ServiceName: "kubeNative",
  58. }
  59. }
  60. return cloud
  61. }
  62. func (l *ScheduleCloudMq) Consume(_, val string) error {
  63. var task *types.TaskInfo
  64. json.Unmarshal([]byte(val), &task)
  65. // 构建提交作业到云算的结构体
  66. bytes, err := json.Marshal(task.Metadata)
  67. if err != nil {
  68. return err
  69. }
  70. cloud := UnMarshalK8sStruct(string(bytes), task.TaskId)
  71. cloud.YamlString = string(bytes)
  72. // 存储数据
  73. tx := l.svcCtx.DbEngin.Create(&cloud)
  74. if tx.Error != nil {
  75. logx.Error(tx.Error)
  76. return tx.Error
  77. }
  78. return nil
  79. }

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.