helm-wrapper.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package api
  2. import (
  3. "dag-jupyter/internal/types"
  4. "fmt"
  5. "github.com/parnurzeal/gorequest"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. )
  8. type Resp struct {
  9. Code int `json:"code"`
  10. Data interface{} `json:"data,omitempty"`
  11. Error string `json:"error,omitempty"`
  12. }
  13. type StatusResp struct {
  14. Code int `json:"code"`
  15. Data []*types.ReleaseStatus `json:"data,omitempty"`
  16. Error string `json:"error,omitempty"`
  17. }
  18. const url = "http://localhost:8081"
  19. // const url = "http://192.168.199.109:18080"
  20. func genParam(req *types.CreateJupyterInfo) map[string]interface{} {
  21. settings := make([]string, 0, 16)
  22. settings = append(settings, fmt.Sprintf("ingress.jupyterlab.hosts[0].host=%s", req.Host))
  23. settings = append(settings, fmt.Sprintf("ingress.jupyterlab.hosts[0].paths[0].path=%s", req.Path))
  24. settings = append(settings, fmt.Sprintf("ingress.jupyterlab.hosts[0].paths[0].pathType=%s", req.PathType))
  25. logx.Info(req.IngressClass, "------- len: ", len(req.IngressClass))
  26. if len(req.IngressClass) > 0 {
  27. // ingressClass, _ := json.Marshal(map[string]string{
  28. // "kubernetes.io/ingress.class": req.IngressClass,
  29. // })
  30. settings = append(settings, fmt.Sprintf("ingress.jupyterlab.annotations.kubernetes\\.io/ingress\\.class=%s", req.IngressClass))
  31. }
  32. settings = append(settings, fmt.Sprintf("jupyterlab.image.repository=%s", req.Image))
  33. settings = append(settings, fmt.Sprintf("jupyterlab.image.tag=%s", req.Tag))
  34. settings = append(settings, fmt.Sprintf("jupyterlab.config.baseUrl=%s", req.BaseUrl))
  35. settings = append(settings, fmt.Sprintf("jupyterlab.config.workspace=%s", req.Workspace))
  36. settings = append(settings, fmt.Sprintf("jupyterlab.config.password=%s", req.Password))
  37. m := map[string]interface{}{
  38. "dry_run": false,
  39. "create_namespace": false,
  40. "set": settings,
  41. }
  42. return m
  43. }
  44. func Install(req *types.CreateJupyterInfo) error {
  45. m := genParam(req)
  46. request := gorequest.New()
  47. var resp Resp
  48. _, _, errs := request.Post(fmt.Sprintf("%s/api/namespaces/%s/releases/%s?chart=%s", url, req.Namespace, req.ReleaseName, req.Chart)).SendMap(m).EndStruct(&resp)
  49. if len(errs) != 0 {
  50. return fmt.Errorf("helm install error: %s", errs[0])
  51. }
  52. if resp.Code != 0 {
  53. return fmt.Errorf("helm install error: %s", resp.Error)
  54. }
  55. logx.Info("helm install success~~")
  56. return nil
  57. }
  58. func Uninstall(req *types.DeleteJupyterInfo) error {
  59. request := gorequest.New()
  60. var resp Resp
  61. _, _, errs := request.Delete(fmt.Sprintf("%s/api/namespaces/%s/releases/%s", url, req.Namespace, req.ReleaseName)).EndStruct(&resp)
  62. if len(errs) != 0 {
  63. return fmt.Errorf("helm uninstall error: %s", errs[0])
  64. }
  65. if resp.Code != 0 {
  66. return fmt.Errorf("helm uninstall error: %s", resp.Error)
  67. }
  68. logx.Info("helm uninstall success~~")
  69. return nil
  70. }
  71. func Upgrade(req *types.CreateJupyterInfo) error {
  72. m := genParam(req)
  73. request := gorequest.New()
  74. var resp Resp
  75. _, _, errs := request.Put(fmt.Sprintf("%s/api/namespaces/%s/releases/%s?chart=%s", url, req.Namespace, req.ReleaseName, req.Chart)).SendMap(m).EndStruct(&resp)
  76. if len(errs) != 0 {
  77. return fmt.Errorf("helm upgrade error: %s", errs[0])
  78. }
  79. if resp.Code != 0 {
  80. return fmt.Errorf("helm upgrade error: %s", resp.Error)
  81. }
  82. logx.Info("helm upgrade success~~")
  83. return nil
  84. }
  85. func Status(req *types.ReleaseInfo) ([]*types.ReleaseStatus, error) {
  86. var resp StatusResp
  87. request := gorequest.New()
  88. m := map[string]string{
  89. "filter": req.Filter,
  90. }
  91. _, _, errs := request.Get(fmt.Sprintf("%s/api/namespaces/%s/releases", url, req.Namespace)).SendMap(m).EndStruct(&resp)
  92. if len(errs) != 0 {
  93. return nil, fmt.Errorf("helm status error: %s", errs[0])
  94. }
  95. if resp.Code != 0 {
  96. return nil, fmt.Errorf("helm status error: %s", resp.Error)
  97. }
  98. logx.Info("helm status success~~")
  99. return resp.Data, nil
  100. }