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.go 5.0 kB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package cloud
  2. import (
  3. "fmt"
  4. "github.com/go-resty/resty/v2"
  5. "github.com/pkg/errors"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/database"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/cloud"
  8. "k8s.io/apimachinery/pkg/util/json"
  9. "net/http"
  10. "sync"
  11. )
  12. const CreateContainer = "/cloud/container/create"
  13. const DeleteContainer = "/cloud/container/delete"
  14. const GetContainer = "/cloud/container/get"
  15. const LogContainer = "/cloud/container/log"
  16. const MetricsContainer = "/cloud/container/metrics"
  17. const ModelBind = "/cloud/model/bind"
  18. type Cloud struct {
  19. store *database.CloudStorage
  20. idAddr sync.Map
  21. }
  22. func New(store *database.CloudStorage, adapterId string) (*Cloud, error) {
  23. if store == nil {
  24. return nil, errors.New("store cannot be nil")
  25. }
  26. a := &Cloud{
  27. store: store,
  28. }
  29. css, err := store.GetClustersByAdapterId(adapterId)
  30. if err != nil {
  31. return nil, fmt.Errorf("failed to get clusters: %w", err)
  32. }
  33. for _, info := range css.List {
  34. a.idAddr.Store(info.Id, info.Server)
  35. }
  36. InitClient()
  37. return a, nil
  38. }
  39. func (c *Cloud) ContainerCreate(platformId string, param *CreateParam) (resp *Resp, err error) {
  40. addr, ok := c.GetServerAddrById(platformId)
  41. if !ok {
  42. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  43. }
  44. respErr := &RespErr{}
  45. _, err = Request(addr+CreateContainer, http.MethodPost, func(req *resty.Request) {
  46. req.SetQueryParams(map[string]string{
  47. "pfId": platformId,
  48. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  49. })
  50. if err != nil {
  51. return nil, err
  52. }
  53. return resp, nil
  54. }
  55. func (c *Cloud) ContainerDelete(platformId string, param *cloud.DeleteParam) (resp *Resp, err error) {
  56. marshal, err := json.Marshal(param)
  57. if err != nil {
  58. return nil, err
  59. }
  60. println(string(marshal))
  61. addr, ok := c.GetServerAddrById(platformId)
  62. if !ok {
  63. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  64. }
  65. respErr := &RespErr{}
  66. _, err = Request(addr+DeleteContainer, http.MethodPost, func(req *resty.Request) {
  67. req.SetQueryParams(map[string]string{
  68. "pfId": platformId,
  69. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  70. })
  71. if err != nil {
  72. return nil, err
  73. }
  74. return
  75. }
  76. func (c *Cloud) ContainerGet(platformId string, param *GetParam) (resp *Resp, err error) {
  77. addr, ok := c.GetServerAddrById(platformId)
  78. if !ok {
  79. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  80. }
  81. respErr := &RespErr{}
  82. _, err = Request(addr+GetContainer, http.MethodPost, func(req *resty.Request) {
  83. req.SetQueryParams(map[string]string{
  84. "pfId": platformId,
  85. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  86. })
  87. if err != nil {
  88. return nil, err
  89. }
  90. return resp, nil
  91. }
  92. func (c *Cloud) BindModel(platformId string, filePath string, fileName string) (resp *Resp, err error) {
  93. addr, ok := c.GetServerAddrById(platformId)
  94. if !ok {
  95. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  96. }
  97. respErr := &RespErr{}
  98. _, err = Request(addr+ModelBind, http.MethodPut, func(req *resty.Request) {
  99. req.SetQueryParams(map[string]string{
  100. "filePath": filePath,
  101. "fileName": fileName,
  102. }).SetError(&respErr).SetResult(&resp)
  103. })
  104. if err != nil {
  105. return nil, err
  106. }
  107. marshal, err := json.Marshal(resp.Data)
  108. if err != nil {
  109. return nil, err
  110. }
  111. fmt.Printf(string(marshal))
  112. return resp, nil
  113. }
  114. func (c *Cloud) ContainerLog(platformId string, param *cloud.LogParam) (resp *Resp, err error) {
  115. addr, ok := c.GetServerAddrById(platformId)
  116. if !ok {
  117. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  118. }
  119. bytes, err := json.Marshal(param)
  120. if err != nil {
  121. return nil, err
  122. }
  123. fmt.Printf(string(bytes))
  124. respErr := &RespErr{}
  125. asd, err := Request(addr+LogContainer, http.MethodPost, func(req *resty.Request) {
  126. req.SetQueryParams(map[string]string{
  127. "pfId": platformId,
  128. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  129. })
  130. if err != nil {
  131. return nil, err
  132. }
  133. json.Marshal(asd)
  134. marshal, err := json.Marshal(resp.Data)
  135. if err != nil {
  136. return nil, err
  137. }
  138. fmt.Printf(string(marshal))
  139. return resp, nil
  140. }
  141. func (c *Cloud) GetServerAddrById(id string) (string, bool) {
  142. val, ok := c.idAddr.Load(id)
  143. if !ok {
  144. return "", false
  145. }
  146. addr, ok := val.(string)
  147. if !ok {
  148. return "", false
  149. }
  150. return addr, true
  151. }
  152. func (c *Cloud) ContainerMetrics(platformId string, param *cloud.MetricsParam) (resp *Resp, err error) {
  153. addr, ok := c.GetServerAddrById(platformId)
  154. if !ok {
  155. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  156. }
  157. bytes, err := json.Marshal(param)
  158. if err != nil {
  159. return nil, err
  160. }
  161. fmt.Printf(string(bytes))
  162. respErr := &RespErr{}
  163. asd, err := Request(addr+MetricsContainer, http.MethodPost, func(req *resty.Request) {
  164. req.SetQueryParams(map[string]string{
  165. "pfId": platformId,
  166. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  167. })
  168. if err != nil {
  169. return nil, err
  170. }
  171. json.Marshal(asd)
  172. marshal, err := json.Marshal(resp.Data)
  173. if err != nil {
  174. return nil, err
  175. }
  176. fmt.Printf(string(marshal))
  177. return resp, nil
  178. }

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.