chart_handler.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "github.com/go-chi/chi"
  8. "github.com/porter-dev/porter/internal/forms"
  9. "github.com/porter-dev/porter/internal/helm"
  10. )
  11. // Enumeration of chart API error codes, represented as int64
  12. const (
  13. ErrChartDecode ErrorCode = iota + 600
  14. ErrChartValidateFields
  15. ErrChartReadData
  16. )
  17. // HandleListReleases retrieves a list of releases for a cluster
  18. // with various filter options
  19. func (app *App) HandleListReleases(w http.ResponseWriter, r *http.Request) {
  20. form := &forms.ListChartForm{
  21. ChartForm: &forms.ChartForm{
  22. Form: &helm.Form{},
  23. },
  24. ListFilter: &helm.ListFilter{},
  25. }
  26. agent, err := app.getAgentFromQueryParams(
  27. w,
  28. r,
  29. form.ChartForm,
  30. form.ChartForm.PopulateHelmOptionsFromQueryParams,
  31. form.PopulateListFromQueryParams,
  32. )
  33. // errors are handled in app.getAgentFromQueryParams
  34. if err != nil {
  35. return
  36. }
  37. releases, err := agent.ListReleases(form.Namespace, form.ListFilter)
  38. if err != nil {
  39. app.handleErrorRead(err, ErrChartReadData, w)
  40. return
  41. }
  42. if err := json.NewEncoder(w).Encode(releases); err != nil {
  43. app.handleErrorFormDecoding(err, ErrChartDecode, w)
  44. return
  45. }
  46. }
  47. // HandleGetChart retrieves a single chart based on a name and revision
  48. func (app *App) HandleGetChart(w http.ResponseWriter, r *http.Request) {
  49. name := chi.URLParam(r, "name")
  50. revision, err := strconv.ParseUint(chi.URLParam(r, "revision"), 0, 64)
  51. form := &forms.GetChartForm{
  52. ChartForm: &forms.ChartForm{
  53. Form: &helm.Form{},
  54. },
  55. Name: name,
  56. Revision: int(revision),
  57. }
  58. agent, err := app.getAgentFromQueryParams(
  59. w,
  60. r,
  61. form.ChartForm,
  62. form.ChartForm.PopulateHelmOptionsFromQueryParams,
  63. )
  64. // errors are handled in app.getAgentFromQueryParams
  65. if err != nil {
  66. return
  67. }
  68. release, err := agent.GetRelease(form.Name, form.Revision)
  69. if err != nil {
  70. app.handleErrorRead(err, ErrChartReadData, w)
  71. return
  72. }
  73. if err := json.NewEncoder(w).Encode(release); err != nil {
  74. app.handleErrorFormDecoding(err, ErrChartDecode, w)
  75. return
  76. }
  77. }
  78. // HandleListChartHistory retrieves a history of charts based on a chart name
  79. func (app *App) HandleListChartHistory(w http.ResponseWriter, r *http.Request) {
  80. name := chi.URLParam(r, "name")
  81. form := &forms.ListChartHistoryForm{
  82. ChartForm: &forms.ChartForm{
  83. Form: &helm.Form{},
  84. },
  85. Name: name,
  86. }
  87. agent, err := app.getAgentFromQueryParams(
  88. w,
  89. r,
  90. form.ChartForm,
  91. form.ChartForm.PopulateHelmOptionsFromQueryParams,
  92. )
  93. // errors are handled in app.getAgentFromQueryParams
  94. if err != nil {
  95. return
  96. }
  97. release, err := agent.GetReleaseHistory(form.Name)
  98. if err != nil {
  99. app.handleErrorFormValidation(err, ErrChartValidateFields, w)
  100. return
  101. }
  102. if err := json.NewEncoder(w).Encode(release); err != nil {
  103. app.handleErrorFormDecoding(err, ErrChartDecode, w)
  104. return
  105. }
  106. }
  107. // HandleUpgradeChart upgrades a chart with new values.yaml
  108. func (app *App) HandleUpgradeChart(w http.ResponseWriter, r *http.Request) {
  109. name := chi.URLParam(r, "name")
  110. form := &forms.UpgradeChartForm{
  111. ChartForm: &forms.ChartForm{
  112. Form: &helm.Form{},
  113. },
  114. Name: name,
  115. }
  116. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  117. app.handleErrorFormDecoding(err, ErrUserDecode, w)
  118. return
  119. }
  120. agent, err := app.getAgentFromChartForm(
  121. w,
  122. r,
  123. form.ChartForm,
  124. )
  125. // errors are handled in app.getAgentFromBodyParams
  126. if err != nil {
  127. return
  128. }
  129. _, err = agent.UpgradeChart(form.Name, form.Values)
  130. if err != nil {
  131. app.handleErrorInternal(err, w)
  132. return
  133. }
  134. w.WriteHeader(http.StatusOK)
  135. }
  136. // HandleRollbackChart rolls a release back to a specified revision
  137. func (app *App) HandleRollbackChart(w http.ResponseWriter, r *http.Request) {
  138. name := chi.URLParam(r, "name")
  139. revision, err := strconv.ParseUint(chi.URLParam(r, "revision"), 0, 64)
  140. if err != nil {
  141. app.handleErrorFormDecoding(err, ErrChartDecode, w)
  142. return
  143. }
  144. form := &forms.RollbackChartForm{
  145. ChartForm: &forms.ChartForm{
  146. Form: &helm.Form{},
  147. },
  148. Name: name,
  149. Revision: int(revision),
  150. }
  151. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  152. app.handleErrorFormDecoding(err, ErrUserDecode, w)
  153. return
  154. }
  155. agent, err := app.getAgentFromChartForm(
  156. w,
  157. r,
  158. form.ChartForm,
  159. )
  160. // errors are handled in app.getAgentFromBodyParams
  161. if err != nil {
  162. return
  163. }
  164. err = agent.RollbackRelease(form.Name, form.Revision)
  165. if err != nil {
  166. app.handleErrorInternal(err, w)
  167. return
  168. }
  169. w.WriteHeader(http.StatusOK)
  170. }
  171. // ------------------------ Release handler helper functions ------------------------ //
  172. // getAgentFromQueryParams uses the query params to populate a form, and then
  173. // passes that form to the underlying app.getAgentFromChartForm to create a new
  174. // Helm agent.
  175. func (app *App) getAgentFromQueryParams(
  176. w http.ResponseWriter,
  177. r *http.Request,
  178. form *forms.ChartForm,
  179. // populate uses the query params to populate a form
  180. populate ...func(vals url.Values),
  181. ) (*helm.Agent, error) {
  182. vals, err := url.ParseQuery(r.URL.RawQuery)
  183. if err != nil {
  184. app.handleErrorFormDecoding(err, ErrChartDecode, w)
  185. return nil, err
  186. }
  187. for _, f := range populate {
  188. f(vals)
  189. }
  190. return app.getAgentFromChartForm(w, r, form)
  191. }
  192. // getAgentFromChartForm uses a non-validated form to construct a new Helm agent based on
  193. // the userID found in the session and the options required by the Helm agent.
  194. func (app *App) getAgentFromChartForm(
  195. w http.ResponseWriter,
  196. r *http.Request,
  197. form *forms.ChartForm,
  198. ) (*helm.Agent, error) {
  199. // read the session in order to generate the Helm agent
  200. session, err := app.store.Get(r, app.cookieName)
  201. // since we have already authenticated the user, throw a data read error if the session
  202. // cannot be found
  203. if err != nil {
  204. app.handleErrorDataRead(err, w)
  205. return nil, err
  206. }
  207. if userID, ok := session.Values["user_id"].(uint); ok {
  208. form.PopulateHelmOptionsFromUserID(userID, app.repo.User)
  209. }
  210. // validate the form
  211. if err := app.validator.Struct(form); err != nil {
  212. app.handleErrorFormValidation(err, ErrChartValidateFields, w)
  213. return nil, err
  214. }
  215. // create a new agent
  216. var agent *helm.Agent
  217. if app.testing {
  218. agent = app.TestAgents.HelmAgent
  219. } else {
  220. agent, err = helm.GetAgentOutOfClusterConfig(form.Form, app.logger)
  221. }
  222. return agent, err
  223. }