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.

cloud_service.go 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package service
  2. import (
  3. "fmt"
  4. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/config"
  5. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/database"
  6. cloudservice "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/cloud"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/collector"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  9. "strconv"
  10. "sync"
  11. )
  12. const (
  13. PcmK8s = "k8s"
  14. )
  15. type CloudService struct {
  16. CloudExecutorAdapterMap map[string]collector.CloudCollector
  17. Storage *database.CloudStorage
  18. LocalCache map[string]interface{}
  19. Conf *config.Config
  20. TaskSyncLock sync.Mutex
  21. }
  22. // NewCloudService 创建并初始化CloudService实例
  23. func NewCloudService(conf *config.Config, storages *database.CloudStorage, localCache map[string]interface{}) (*CloudService, error) {
  24. cloudService := &CloudService{
  25. CloudExecutorAdapterMap: make(map[string]collector.CloudCollector),
  26. Storage: storages,
  27. LocalCache: localCache,
  28. Conf: conf,
  29. }
  30. if err := cloudService.initAdapters(); err != nil {
  31. return nil, err
  32. }
  33. return cloudService, nil
  34. }
  35. // initAdapters 初始化所有适配器
  36. func (s *CloudService) initAdapters() error {
  37. adapters, err := s.loadAdapters()
  38. if err != nil {
  39. return err
  40. }
  41. for _, adapter := range adapters {
  42. if err := s.processAdapter(*adapter); err != nil {
  43. return err
  44. }
  45. }
  46. return nil
  47. }
  48. // loadAdapters 从存储中加载适配器
  49. func (s *CloudService) loadAdapters() ([]*types.AdapterInfo, error) {
  50. return s.Storage.GetAdaptersByType("0")
  51. }
  52. // processAdapter 处理单个适配器
  53. func (s *CloudService) processAdapter(adapter types.AdapterInfo) error {
  54. if adapter.Id == "" {
  55. return nil
  56. }
  57. executor, err := s.createExecutor(adapter)
  58. if err != nil {
  59. return err
  60. }
  61. if executor != nil {
  62. s.CloudExecutorAdapterMap[adapter.Id] = executor
  63. }
  64. return nil
  65. }
  66. // createExecutor 根据适配器类型创建对应的执行器
  67. func (s *CloudService) createExecutor(adapter types.AdapterInfo) (collector.CloudCollector, error) {
  68. switch adapter.Nickname {
  69. case PcmK8s:
  70. return s.CreateK8sExecutor(adapter)
  71. // 可以在这里添加其他类型的适配器
  72. default:
  73. return nil, nil // 或者返回错误,取决于业务需求
  74. }
  75. }
  76. // CreateSlurmExecutor 创建Slurm执行器
  77. func (s *CloudService) CreateK8sExecutor(adapter types.AdapterInfo) (collector.CloudCollector, error) {
  78. id, err := strconv.ParseInt(adapter.Id, 10, 64)
  79. if err != nil {
  80. return nil, fmt.Errorf("failed to parse adapter ID %s: %v", adapter.Id, err)
  81. }
  82. return cloudservice.NewCloud(adapter.Server, id, adapter.Nickname), nil
  83. }

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.