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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package cloud
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. schsdk "gitlink.org.cn/cloudream/common/sdks/scheduler"
  7. "gitlink.org.cn/cloudream/common/utils/http2"
  8. "gitlink.org.cn/cloudream/common/utils/serder"
  9. )
  10. //type CreateParam struct {
  11. // ClusterId string `json:"clusterId,omitempty"`
  12. // ContainerGroupName string `json:"containerGroupName"`
  13. // Name string `json:"name"`
  14. // Image string `json:"image"`
  15. // Cpu string `json:"cpu,omitempty"`
  16. // Memory string `json:"memory,omitempty"`
  17. // Port int32 `json:"port,omitempty"`
  18. // NodePort int32 `json:"nodePort,omitempty"`
  19. // MountPath string `json:"mountPath,omitempty"`
  20. // Args []string `json:"args,omitempty"`
  21. // Envs []struct {
  22. // Name string `json:"name,omitempty"`
  23. // Value string `json:"value,omitempty"`
  24. // } `json:"envs,omitempty"`
  25. // ContainerCreateParameter ContainerCreateParameter `json:"containerCreateParameter,omitempty"`
  26. //}
  27. type CreateCloudJobReq struct {
  28. ClusterId schsdk.ClusterID `json:"clusterId,omitempty"`
  29. ContainerGroupName string `json:"containerGroupName"`
  30. Name string `json:"name"`
  31. Description string `json:"description"`
  32. Image string `json:"image"`
  33. Cpu string `json:"cpu,omitempty"`
  34. Memory string `json:"memory,omitempty"`
  35. Port int32 `json:"port,omitempty"`
  36. NodePort int32 `json:"nodePort,omitempty"`
  37. MountPath string `json:"mountPath,omitempty"`
  38. Args []string `json:"args,omitempty"`
  39. Envs []interface{} `json:"envs,omitempty"`
  40. Capacity int32 `json:"capacity,omitempty"`
  41. }
  42. // type T2 struct {
  43. // Code int `json:"code"`
  44. // Msg string `json:"msg"`
  45. // Data struct {
  46. // Command interface{} `json:"Command"`
  47. // Args []string `json:"args"`
  48. // ContainerPorts struct {
  49. // NodePort int `json:"nodePort"`
  50. // Port int `json:"port"`
  51. // } `json:"containerPorts"`
  52. // Image string `json:"image"`
  53. // Limits struct {
  54. // Cpu string `json:"cpu"`
  55. // Memory string `json:"memory"`
  56. // } `json:"limits"`
  57. // Name string `json:"name"`
  58. // } `json:"data"`
  59. // TraceId string `json:"traceId"`
  60. // }
  61. type CreateCloudResp struct {
  62. TaskID TaskID `json:"taskId"`
  63. }
  64. func (c *Client) CreateJob(req CreateCloudJobReq, token string) (*CreateCloudResp, error) {
  65. targetUrl, err := url.JoinPath(c.baseURL, "/cloud/container/create")
  66. if err != nil {
  67. return nil, err
  68. }
  69. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  70. Body: req,
  71. Header: map[string]string{
  72. "Authorization": token,
  73. },
  74. })
  75. if err != nil {
  76. return nil, err
  77. }
  78. // 打印resp.Body内容
  79. //body, err := io.ReadAll(resp.Body)
  80. //if err != nil {
  81. // println("Error reading response body:", err)
  82. //}
  83. //println("Response Body:", string(body))
  84. contType := resp.Header.Get("Content-Type")
  85. if strings.Contains(contType, http2.ContentTypeJSON) {
  86. var codeResp respons2[CreateCloudResp]
  87. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  88. return nil, fmt.Errorf("parsing response: %w", err)
  89. }
  90. if codeResp.Code == ResponseCodeOK {
  91. return &codeResp.Data, nil
  92. }
  93. return nil, fmt.Errorf("error: %s", codeResp.Message)
  94. }
  95. return nil, fmt.Errorf("unknow response content type: %s", contType)
  96. }