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.

job.go 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package cloud
  2. import (
  3. "fmt"
  4. schsdk "gitlink.org.cn/cloudream/common/sdks/scheduler"
  5. "gitlink.org.cn/cloudream/common/utils/http2"
  6. "gitlink.org.cn/cloudream/common/utils/serder"
  7. "net/url"
  8. "strings"
  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. }
  41. // type T2 struct {
  42. // Code int `json:"code"`
  43. // Msg string `json:"msg"`
  44. // Data struct {
  45. // Command interface{} `json:"Command"`
  46. // Args []string `json:"args"`
  47. // ContainerPorts struct {
  48. // NodePort int `json:"nodePort"`
  49. // Port int `json:"port"`
  50. // } `json:"containerPorts"`
  51. // Image string `json:"image"`
  52. // Limits struct {
  53. // Cpu string `json:"cpu"`
  54. // Memory string `json:"memory"`
  55. // } `json:"limits"`
  56. // Name string `json:"name"`
  57. // } `json:"data"`
  58. // TraceId string `json:"traceId"`
  59. // }
  60. type CreateCloudResp struct {
  61. Command interface{} `json:"Command"`
  62. Args []string `json:"args"`
  63. ContainerPorts map[string]int64 `json:"containerPorts"`
  64. Image string `json:"image"`
  65. Limits map[string]string `json:"limits"`
  66. Name string `json:"name"`
  67. TaskID TaskID `json:"taskId"`
  68. }
  69. func (c *Client) CreateJob(req CreateCloudJobReq, token string) (*CreateCloudResp, error) {
  70. targetUrl, err := url.JoinPath(c.baseURL, "/cloud/container/create")
  71. if err != nil {
  72. return nil, err
  73. }
  74. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  75. Body: req,
  76. Header: map[string]string{
  77. "Authorization": token,
  78. },
  79. })
  80. if err != nil {
  81. return nil, err
  82. }
  83. // 打印resp.Body内容
  84. //body, err := io.ReadAll(resp.Body)
  85. //if err != nil {
  86. // println("Error reading response body:", err)
  87. //}
  88. //println("Response Body:", string(body))
  89. contType := resp.Header.Get("Content-Type")
  90. if strings.Contains(contType, http2.ContentTypeJSON) {
  91. var codeResp respons2[CreateCloudResp]
  92. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  93. return nil, fmt.Errorf("parsing response: %w", err)
  94. }
  95. if codeResp.Code == ResponseCodeOK {
  96. return &codeResp.Data, nil
  97. }
  98. return nil, fmt.Errorf("error: %s", codeResp.Message)
  99. }
  100. return nil, fmt.Errorf("unknow response content type: %s", contType)
  101. }