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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. TaskID TaskID `json:"taskId"`
  62. }
  63. func (c *Client) CreateJob(req CreateCloudJobReq, token string) (*CreateCloudResp, error) {
  64. targetUrl, err := url.JoinPath(c.baseURL, "/cloud/container/create")
  65. if err != nil {
  66. return nil, err
  67. }
  68. resp, err := http2.PostJSON(targetUrl, http2.RequestParam{
  69. Body: req,
  70. Header: map[string]string{
  71. "Authorization": token,
  72. },
  73. })
  74. if err != nil {
  75. return nil, err
  76. }
  77. // 打印resp.Body内容
  78. //body, err := io.ReadAll(resp.Body)
  79. //if err != nil {
  80. // println("Error reading response body:", err)
  81. //}
  82. //println("Response Body:", string(body))
  83. contType := resp.Header.Get("Content-Type")
  84. if strings.Contains(contType, http2.ContentTypeJSON) {
  85. var codeResp respons2[CreateCloudResp]
  86. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  87. return nil, fmt.Errorf("parsing response: %w", err)
  88. }
  89. if codeResp.Code == ResponseCodeOK {
  90. return &codeResp.Data, nil
  91. }
  92. return nil, fmt.Errorf("error: %s", codeResp.Message)
  93. }
  94. return nil, fmt.Errorf("unknow response content type: %s", contType)
  95. }