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.2 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. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/pkg/response"
  16. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
  17. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/algorithm/providerPricing"
  18. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/strategies"
  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 *providerPricing.Task, providers ...*providerPricing.Provider) (*providerPricing.Strategy, error) {
  33. //调度算法
  34. strategy := strategies.NewPricingStrategy(task, providers...)
  35. taskResult, err := strategies.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 string, participantId int64) (interface{}, error) {
  42. cloud := cs.UnMarshalK8sStruct(resource, task.TaskId, task.NsID)
  43. cloud.Id = utils.GenSnowflakeID()
  44. cloud.NsID = task.NsID
  45. cloud.ParticipantId = participantId
  46. return cloud, nil
  47. }
  48. func (cs *cloudScheduler) UnMarshalK8sStruct(yamlString string, taskId int64, nsID string) models.Cloud {
  49. var cloud models.Cloud
  50. d := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
  51. var err error
  52. for {
  53. var rawObj runtime.RawExtension
  54. err = d.Decode(&rawObj)
  55. if err == io.EOF {
  56. break
  57. }
  58. if err != nil {
  59. }
  60. obj := &unstructured.Unstructured{}
  61. syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
  62. if err != nil {
  63. }
  64. unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  65. if err != nil {
  66. }
  67. unstructureObj := &unstructured.Unstructured{Object: unstructuredMap}
  68. if len(nsID) != 0 {
  69. unstructureObj.SetNamespace(nsID)
  70. }
  71. cloud = models.Cloud{
  72. TaskId: taskId,
  73. ApiVersion: unstructureObj.GetAPIVersion(),
  74. Name: unstructureObj.GetName(),
  75. Kind: unstructureObj.GetKind(),
  76. Namespace: unstructureObj.GetNamespace(),
  77. Status: "Saved",
  78. }
  79. // 命名空间为空 设置默认值
  80. if len(unstructureObj.GetNamespace()) == 0 {
  81. cloud.Namespace = "default"
  82. }
  83. //unstructureObj转成string
  84. unString, _ := unstructureObj.MarshalJSON()
  85. cloud.YamlString = string(unString)
  86. }
  87. return cloud
  88. }
  89. func (cs *cloudScheduler) genTaskAndProviders(task *response.TaskInfo, dbEngin *gorm.DB) (*providerPricing.Task, []*providerPricing.Provider) {
  90. var proParams []providerParams
  91. 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"
  92. dbEngin.Raw(sqlstr).Scan(&proParams)
  93. var providerList []*providerPricing.Provider
  94. for _, p := range proParams {
  95. provider := providerPricing.NewProvider(p.Participant_id, p.Cpu_avail, p.Mem_avail, p.Disk_avail, 0.0, 0.0, 0.0)
  96. providerList = append(providerList, provider)
  97. }
  98. //replicas := task.Metadata.(map[string]interface{})["spec"].(map[string]interface{})["replicas"].(float64)
  99. //t := algorithm.NewTask(0, int(replicas), 2, 75120000, 301214500, 1200, 2, 6, 2000)
  100. return nil, providerList
  101. }

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.