k8s.go 3.5 KB

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