k8s.go 3.8 KB

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