- /*
-
- Copyright (c) [2023] [pcm]
- [pcm-coordinator] is licensed under Mulan PSL v2.
- You can use this software according to the terms and conditions of the Mulan PSL v2.
- You may obtain a copy of Mulan PSL v2 at:
- http://license.coscl.org.cn/MulanPSL2
- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- See the Mulan PSL v2 for more details.
-
- */
-
- package scheduler
-
- import (
- "bytes"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/pkg/response"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/algorithm/providerPricing"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/strategies"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
- "gorm.io/gorm"
- "io"
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
- "k8s.io/apimachinery/pkg/runtime"
- syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
- kyaml "k8s.io/apimachinery/pkg/util/yaml"
- )
-
- type cloudScheduler struct {
- }
-
- func NewCloudScheduler() *cloudScheduler {
- return &cloudScheduler{}
- }
-
- func (cs *cloudScheduler) pickOptimalStrategy(task *providerPricing.Task, providers ...*providerPricing.Provider) (*providerPricing.Strategy, error) {
-
- //调度算法
- strategy := strategies.NewPricingStrategy(task, providers...)
- taskResult, err := strategies.ScheduleWithFullCollaboration(strategy, strategy.ProviderList)
- if err != nil {
- return nil, err
- }
- return taskResult.MaxscoreStrategy, nil
- }
-
- func (cs *cloudScheduler) getNewStructForDb(task *response.TaskInfo, resource string, participantId int64) (interface{}, error) {
- cloud := cs.UnMarshalK8sStruct(resource, task.TaskId, task.NsID)
- cloud.Id = utils.GenSnowflakeID()
- cloud.NsID = task.NsID
-
- cloud.ParticipantId = participantId
- return cloud, nil
- }
-
- func (cs *cloudScheduler) UnMarshalK8sStruct(yamlString string, taskId int64, nsID string) models.Cloud {
- var cloud models.Cloud
- d := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
- var err error
- for {
- var rawObj runtime.RawExtension
- err = d.Decode(&rawObj)
- if err == io.EOF {
- break
- }
- if err != nil {
- }
- obj := &unstructured.Unstructured{}
- syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
- if err != nil {
- }
-
- unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
- if err != nil {
- }
-
- unstructureObj := &unstructured.Unstructured{Object: unstructuredMap}
- if len(nsID) != 0 {
- unstructureObj.SetNamespace(nsID)
- }
- cloud = models.Cloud{
- TaskId: taskId,
- ApiVersion: unstructureObj.GetAPIVersion(),
- Name: unstructureObj.GetName(),
- Kind: unstructureObj.GetKind(),
- Namespace: unstructureObj.GetNamespace(),
- Status: "Saved",
- }
- // 命名空间为空 设置默认值
- if len(unstructureObj.GetNamespace()) == 0 {
- cloud.Namespace = "default"
- }
- //unstructureObj转成string
- unString, _ := unstructureObj.MarshalJSON()
- cloud.YamlString = string(unString)
- }
- return cloud
- }
-
- func (cs *cloudScheduler) genTaskAndProviders(task *response.TaskInfo, dbEngin *gorm.DB) (*providerPricing.Task, []*providerPricing.Provider) {
- var proParams []providerParams
- 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"
- dbEngin.Raw(sqlstr).Scan(&proParams)
-
- var providerList []*providerPricing.Provider
- for _, p := range proParams {
- provider := providerPricing.NewProvider(p.Participant_id, p.Cpu_avail, p.Mem_avail, p.Disk_avail, 0.0, 0.0, 0.0)
- providerList = append(providerList, provider)
- }
-
- //replicas := task.Metadata.(map[string]interface{})["spec"].(map[string]interface{})["replicas"].(float64)
- //t := algorithm.NewTask(0, int(replicas), 2, 75120000, 301214500, 1200, 2, 6, 2000)
-
- return nil, providerList
- }
|