k8s.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. v1 "k8s.io/api/core/v1"
  8. )
  9. // GetK8sNamespacesResponse is the list of namespaces returned when a
  10. // user has successfully authenticated
  11. type GetK8sNamespacesResponse v1.NamespaceList
  12. // GetK8sNamespaces gets a namespaces list in a k8s cluster
  13. func (c *Client) GetK8sNamespaces(
  14. ctx context.Context,
  15. projectID uint,
  16. clusterID uint,
  17. ) (*GetK8sNamespacesResponse, error) {
  18. cl := fmt.Sprintf("%d", clusterID)
  19. req, err := http.NewRequest(
  20. "GET",
  21. fmt.Sprintf("%s/projects/%d/k8s/namespaces?"+url.Values{
  22. "cluster_id": []string{cl},
  23. }.Encode(), c.BaseURL, projectID),
  24. nil,
  25. )
  26. if err != nil {
  27. return nil, err
  28. }
  29. req = req.WithContext(ctx)
  30. bodyResp := &GetK8sNamespacesResponse{}
  31. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  32. if httpErr != nil {
  33. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  34. }
  35. return nil, err
  36. }
  37. return bodyResp, nil
  38. }
  39. // GetKubeconfigResponse is the list of namespaces returned when a
  40. // user has successfully authenticated
  41. type GetKubeconfigResponse struct {
  42. Kubeconfig []byte `json:"kubeconfig"`
  43. }
  44. // GetK8sNamespaces gets a namespaces list in a k8s cluster
  45. func (c *Client) GetKubeconfig(
  46. ctx context.Context,
  47. projectID uint,
  48. clusterID uint,
  49. ) (*GetKubeconfigResponse, error) {
  50. cl := fmt.Sprintf("%d", clusterID)
  51. req, err := http.NewRequest(
  52. "GET",
  53. fmt.Sprintf("%s/projects/%d/k8s/kubeconfig?"+url.Values{
  54. "cluster_id": []string{cl},
  55. }.Encode(), c.BaseURL, projectID),
  56. nil,
  57. )
  58. if err != nil {
  59. return nil, err
  60. }
  61. req = req.WithContext(ctx)
  62. bodyResp := &GetKubeconfigResponse{}
  63. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  64. if httpErr != nil {
  65. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  66. }
  67. return nil, err
  68. }
  69. return bodyResp, nil
  70. }
  71. // GetReleaseAllPodsResponse is the list of all pods for a given Helm release
  72. type GetReleaseAllPodsResponse []v1.Pod
  73. // GetK8sAllPods gets all pods for a given release
  74. func (c *Client) GetK8sAllPods(
  75. ctx context.Context,
  76. projectID, clusterID uint,
  77. namespace, name string,
  78. ) (GetReleaseAllPodsResponse, error) {
  79. cl := fmt.Sprintf("%d", clusterID)
  80. req, err := http.NewRequest(
  81. "GET",
  82. fmt.Sprintf("%s/projects/%d/releases/%s/0/pods/all?"+url.Values{
  83. "cluster_id": []string{cl},
  84. "namespace": []string{namespace},
  85. "storage": []string{"secret"},
  86. }.Encode(), c.BaseURL, projectID, name),
  87. nil,
  88. )
  89. if err != nil {
  90. return nil, err
  91. }
  92. req = req.WithContext(ctx)
  93. bodyResp := &GetReleaseAllPodsResponse{}
  94. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  95. if httpErr != nil {
  96. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  97. }
  98. return nil, err
  99. }
  100. return *bodyResp, nil
  101. }