k8s.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. 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. req, err := http.NewRequest(
  52. "GET",
  53. fmt.Sprintf("%s/projects/%d/clusters/%d/kubeconfig", c.BaseURL, projectID, clusterID),
  54. nil,
  55. )
  56. if err != nil {
  57. return nil, err
  58. }
  59. req = req.WithContext(ctx)
  60. bodyResp := &GetKubeconfigResponse{}
  61. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  62. if httpErr != nil {
  63. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  64. }
  65. return nil, err
  66. }
  67. return bodyResp, nil
  68. }
  69. // GetReleaseLatestRevision gets the latest revision of a Helm release
  70. type GetReleaseResponse api.PorterRelease
  71. // GetK8sAllPods gets all pods for a given release
  72. func (c *Client) GetRelease(
  73. ctx context.Context,
  74. projectID, clusterID uint,
  75. namespace, name string,
  76. ) (*GetReleaseResponse, error) {
  77. cl := fmt.Sprintf("%d", clusterID)
  78. req, err := http.NewRequest(
  79. "GET",
  80. fmt.Sprintf("%s/projects/%d/releases/%s/0?"+url.Values{
  81. "cluster_id": []string{cl},
  82. "namespace": []string{namespace},
  83. "storage": []string{"secret"},
  84. }.Encode(), c.BaseURL, projectID, name),
  85. nil,
  86. )
  87. if err != nil {
  88. return nil, err
  89. }
  90. req = req.WithContext(ctx)
  91. bodyResp := &GetReleaseResponse{}
  92. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  93. if httpErr != nil {
  94. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  95. }
  96. return nil, err
  97. }
  98. return bodyResp, nil
  99. }
  100. // GetReleaseAllPodsResponse is the list of all pods for a given Helm release
  101. type GetReleaseAllPodsResponse []v1.Pod
  102. // GetK8sAllPods gets all pods for a given release
  103. func (c *Client) GetK8sAllPods(
  104. ctx context.Context,
  105. projectID, clusterID uint,
  106. namespace, name string,
  107. ) (GetReleaseAllPodsResponse, error) {
  108. cl := fmt.Sprintf("%d", clusterID)
  109. req, err := http.NewRequest(
  110. "GET",
  111. fmt.Sprintf("%s/projects/%d/releases/%s/0/pods/all?"+url.Values{
  112. "cluster_id": []string{cl},
  113. "namespace": []string{namespace},
  114. "storage": []string{"secret"},
  115. }.Encode(), c.BaseURL, projectID, name),
  116. nil,
  117. )
  118. if err != nil {
  119. return nil, err
  120. }
  121. req = req.WithContext(ctx)
  122. bodyResp := &GetReleaseAllPodsResponse{}
  123. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  124. if httpErr != nil {
  125. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  126. }
  127. return nil, err
  128. }
  129. return *bodyResp, nil
  130. }