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.

cloudScheduler.go 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 scheduler
  13. import (
  14. "bytes"
  15. "encoding/json"
  16. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/pkg/response"
  17. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
  18. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/algo"
  19. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
  20. "gorm.io/gorm"
  21. "io"
  22. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
  25. kyaml "k8s.io/apimachinery/pkg/util/yaml"
  26. )
  27. type cloudScheduler struct {
  28. }
  29. func NewCloudScheduler() *cloudScheduler {
  30. return &cloudScheduler{}
  31. }
  32. func (cs *cloudScheduler) pickOptimalStrategy(task *algo.Task, providers ...*algo.Provider) (*algo.Strategy, error) {
  33. //参数为空返回 nil
  34. if len(providers) == 0 || task == nil {
  35. return nil, nil
  36. }
  37. //调度算法
  38. strategy := algo.NewK8sStrategy(task, providers...)
  39. taskResult, err := algo.ScheduleWithFullCollaboration(strategy, strategy.ProviderList)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return taskResult.MaxscoreStrategy, nil
  44. }
  45. func (cs *cloudScheduler) getNewStructForDb(task *response.TaskInfo, participantId int64) (interface{}, error) {
  46. bytes, err := json.Marshal(task.Metadata)
  47. if err != nil {
  48. return nil, err
  49. }
  50. cloud := cs.UnMarshalK8sStruct(string(bytes), task.TaskId)
  51. cloud.Id = utils.GenSnowflakeID()
  52. cloud.YamlString = string(bytes)
  53. cloud.ParticipantId = participantId
  54. return cloud, nil
  55. }
  56. func (cs *cloudScheduler) UnMarshalK8sStruct(yamlString string, taskId int64) models.Cloud {
  57. var cloud models.Cloud
  58. d := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
  59. var err error
  60. for {
  61. var rawObj runtime.RawExtension
  62. err = d.Decode(&rawObj)
  63. if err == io.EOF {
  64. break
  65. }
  66. if err != nil {
  67. }
  68. obj := &unstructured.Unstructured{}
  69. syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
  70. if err != nil {
  71. }
  72. unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  73. if err != nil {
  74. }
  75. unstructureObj := &unstructured.Unstructured{Object: unstructuredMap}
  76. cloud = models.Cloud{
  77. TaskId: taskId,
  78. ApiVersion: unstructureObj.GetAPIVersion(),
  79. Name: unstructureObj.GetName(),
  80. Kind: unstructureObj.GetKind(),
  81. Namespace: unstructureObj.GetNamespace(),
  82. Status: "Saved",
  83. }
  84. // 命名空间为空 设置默认值
  85. if len(unstructureObj.GetNamespace()) == 0 {
  86. cloud.Namespace = "default"
  87. }
  88. }
  89. return cloud
  90. }
  91. func (cs *cloudScheduler) genTaskAndProviders(task *response.TaskInfo, dbEngin *gorm.DB) (*algo.Task, []*algo.Provider) {
  92. var proParams []providerParams
  93. sqlstr := "SELECT SUM(a.disk_avail) as disk_avail,SUM(a.mem_avail) as mem_avail,SUM(a.cpu_total * a.cpu_usable) as cpu_avail,participant_id from (SELECT * from sc_node_avail_info where created_time in (SELECT MAX(created_time) as time from sc_node_avail_info where deleted_flag = 0 GROUP BY participant_id,node_name)) a GROUP BY a.participant_id"
  94. dbEngin.Raw(sqlstr).Scan(&proParams)
  95. var providerList []*algo.Provider
  96. for _, p := range proParams {
  97. provider := algo.NewProvider(p.Participant_id, p.Cpu_avail, p.Mem_avail, p.Disk_avail, 0.0, 0.0, 0.0)
  98. providerList = append(providerList, provider)
  99. }
  100. t := algo.NewTask(0, 1, 2, 75120000, 301214500, 1200, 2, 6, 2000)
  101. return t, providerList
  102. }

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.