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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. //调度算法
  34. strategy := algo.NewK8sStrategy(task, providers...)
  35. taskResult, err := algo.ScheduleWithFullCollaboration(strategy, strategy.ProviderList)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return taskResult.MaxscoreStrategy, nil
  40. }
  41. func (cs *cloudScheduler) getNewStructForDb(task *response.TaskInfo, resource interface{}, participantId int64) (interface{}, error) {
  42. bytes, err := json.Marshal(resource)
  43. if err != nil {
  44. return nil, err
  45. }
  46. cloud := cs.UnMarshalK8sStruct(string(bytes), task.TaskId, task.NsID)
  47. cloud.Id = utils.GenSnowflakeID()
  48. cloud.YamlString = string(bytes)
  49. cloud.NsID = task.NsID
  50. cloud.ParticipantId = participantId
  51. return cloud, nil
  52. }
  53. func (cs *cloudScheduler) UnMarshalK8sStruct(yamlString string, taskId int64, nsID string) models.Cloud {
  54. var cloud models.Cloud
  55. d := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
  56. var err error
  57. for {
  58. var rawObj runtime.RawExtension
  59. err = d.Decode(&rawObj)
  60. if err == io.EOF {
  61. break
  62. }
  63. if err != nil {
  64. }
  65. obj := &unstructured.Unstructured{}
  66. syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
  67. if err != nil {
  68. }
  69. unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  70. if err != nil {
  71. }
  72. unstructureObj := &unstructured.Unstructured{Object: unstructuredMap}
  73. if len(nsID) != 0 {
  74. unstructureObj.SetNamespace(nsID)
  75. }
  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. replicas := task.Metadata.(map[string]interface{})["spec"].(map[string]interface{})["replicas"].(float64)
  101. t := algo.NewTask(0, int(replicas), 2, 75120000, 301214500, 1200, 2, 6, 2000)
  102. return t, providerList
  103. }

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.