upgrade.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package release
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. semver "github.com/Masterminds/semver/v3"
  8. "github.com/porter-dev/porter/api/server/authz"
  9. "github.com/porter-dev/porter/api/server/handlers"
  10. baseReleaseHandler "github.com/porter-dev/porter/api/server/handlers/release"
  11. "github.com/porter-dev/porter/api/server/shared"
  12. "github.com/porter-dev/porter/api/server/shared/apierrors"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/api/types"
  15. "github.com/porter-dev/porter/internal/helm"
  16. "github.com/porter-dev/porter/internal/models"
  17. "github.com/porter-dev/porter/internal/notifier"
  18. "github.com/porter-dev/porter/internal/notifier/slack"
  19. "github.com/stefanmcshane/helm/pkg/release"
  20. )
  21. var createEnvSecretConstraint, _ = semver.NewConstraint(" < 0.1.0")
  22. type UpgradeReleaseHandler struct {
  23. handlers.PorterHandlerReadWriter
  24. authz.KubernetesAgentGetter
  25. }
  26. func NewUpgradeReleaseHandler(
  27. config *config.Config,
  28. decoderValidator shared.RequestDecoderValidator,
  29. writer shared.ResultWriter,
  30. ) *UpgradeReleaseHandler {
  31. return &UpgradeReleaseHandler{
  32. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  33. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  34. }
  35. }
  36. func (c *UpgradeReleaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  37. user, _ := r.Context().Value(types.UserScope).(*models.User)
  38. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  39. helmRelease, _ := r.Context().Value(types.ReleaseScope).(*release.Release)
  40. helmAgent, err := c.GetHelmAgent(r.Context(), r, cluster, "")
  41. if err != nil {
  42. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  43. return
  44. }
  45. request := &types.V1UpgradeReleaseRequest{}
  46. if ok := c.DecodeAndValidate(w, r, request); !ok {
  47. return
  48. }
  49. registries, err := c.Repo().Registry().ListRegistriesByProjectID(cluster.ProjectID)
  50. if err != nil {
  51. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  52. return
  53. }
  54. conf := &helm.UpgradeReleaseConfig{
  55. Name: helmRelease.Name,
  56. Cluster: cluster,
  57. Repo: c.Repo(),
  58. Registries: registries,
  59. }
  60. // if the chart version is set, load a chart from the repo
  61. if request.ChartVersion != "" {
  62. cache := c.Config().URLCache
  63. chartRepoURL, foundFirst := cache.GetURL(helmRelease.Chart.Metadata.Name)
  64. if !foundFirst {
  65. cache.Update()
  66. var found bool
  67. chartRepoURL, found = cache.GetURL(helmRelease.Chart.Metadata.Name)
  68. if !found {
  69. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  70. fmt.Errorf("chart not found"),
  71. http.StatusBadRequest,
  72. ))
  73. return
  74. }
  75. }
  76. chart, err := baseReleaseHandler.LoadChart(c.Config(), &baseReleaseHandler.LoadAddonChartOpts{
  77. ProjectID: cluster.ProjectID,
  78. RepoURL: chartRepoURL,
  79. TemplateName: helmRelease.Chart.Metadata.Name,
  80. TemplateVersion: request.ChartVersion,
  81. })
  82. if err != nil {
  83. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  84. return
  85. }
  86. if err != nil {
  87. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  88. fmt.Errorf("chart not found"),
  89. http.StatusBadRequest,
  90. ))
  91. return
  92. }
  93. conf.Chart = chart
  94. }
  95. conf.Values = request.Values
  96. // if LatestRevision is set, check that the revision matches the latest revision in the database
  97. if request.LatestRevision != 0 {
  98. currHelmRelease, err := helmAgent.GetRelease(context.Background(), helmRelease.Name, 0, false)
  99. if err != nil {
  100. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  101. fmt.Errorf("could not retrieve latest revision"),
  102. http.StatusBadRequest,
  103. ))
  104. return
  105. }
  106. if currHelmRelease.Version != int(request.LatestRevision) {
  107. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  108. fmt.Errorf("The provided revision is not up to date with the current revision (you may need to refresh the deployment). Provided revision is %d, latest revision is %d. If you would like to deploy from this revision, please revert first and update the configuration.", request.LatestRevision, currHelmRelease.Version),
  109. http.StatusBadRequest,
  110. ))
  111. return
  112. }
  113. }
  114. newHelmRelease, upgradeErr := helmAgent.UpgradeReleaseByValues(context.Background(), conf, c.Config().DOConf, c.Config().ServerConf.DisablePullSecretsInjection, false)
  115. if upgradeErr == nil && newHelmRelease != nil {
  116. helmRelease = newHelmRelease
  117. }
  118. slackInts, _ := c.Repo().SlackIntegration().ListSlackIntegrationsByProjectID(cluster.ProjectID)
  119. rel, releaseErr := c.Repo().Release().ReadRelease(cluster.ID, helmRelease.Name, helmRelease.Namespace)
  120. var notifConf *types.NotificationConfig
  121. notifConf = nil
  122. if rel != nil && rel.NotificationConfig != 0 {
  123. conf, err := c.Repo().NotificationConfig().ReadNotificationConfig(rel.NotificationConfig)
  124. if err != nil {
  125. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  126. return
  127. }
  128. notifConf = conf.ToNotificationConfigType()
  129. }
  130. deplNotifier := slack.NewDeploymentNotifier(notifConf, slackInts...)
  131. notifyOpts := &notifier.NotifyOpts{
  132. ProjectID: cluster.ProjectID,
  133. ClusterID: cluster.ID,
  134. ClusterName: cluster.Name,
  135. Name: helmRelease.Name,
  136. Namespace: helmRelease.Namespace,
  137. URL: fmt.Sprintf(
  138. "%s/applications/%s/%s/%s?project_id=%d",
  139. c.Config().ServerConf.ServerURL,
  140. url.PathEscape(cluster.Name),
  141. helmRelease.Namespace,
  142. helmRelease.Name,
  143. cluster.ProjectID,
  144. ),
  145. }
  146. if upgradeErr != nil {
  147. notifyOpts.Status = notifier.StatusHelmFailed
  148. notifyOpts.Info = upgradeErr.Error()
  149. if !cluster.NotificationsDisabled {
  150. deplNotifier.Notify(notifyOpts)
  151. }
  152. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  153. upgradeErr,
  154. http.StatusBadRequest,
  155. ))
  156. return
  157. }
  158. if helmRelease.Chart != nil && helmRelease.Chart.Metadata.Name != "job" {
  159. notifyOpts.Status = notifier.StatusHelmDeployed
  160. notifyOpts.Version = helmRelease.Version
  161. if !cluster.NotificationsDisabled {
  162. deplNotifier.Notify(notifyOpts)
  163. }
  164. }
  165. // update the github actions env if the release exists and is built from source
  166. if cName := helmRelease.Chart.Metadata.Name; cName == "job" || cName == "web" || cName == "worker" {
  167. if releaseErr == nil && rel != nil {
  168. err = baseReleaseHandler.UpdateReleaseRepo(c.Config(), rel, helmRelease)
  169. if err != nil {
  170. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  171. return
  172. }
  173. gitAction := rel.GitActionConfig
  174. if gitAction != nil && gitAction.ID != 0 && gitAction.GitlabIntegrationID == 0 {
  175. gaRunner, err := baseReleaseHandler.GetGARunner(
  176. r.Context(),
  177. c.Config(),
  178. user.ID,
  179. cluster.ProjectID,
  180. cluster.ID,
  181. rel.GitActionConfig,
  182. helmRelease.Name,
  183. helmRelease.Namespace,
  184. rel,
  185. helmRelease,
  186. )
  187. if err != nil {
  188. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  189. return
  190. }
  191. actionVersion, err := semver.NewVersion(gaRunner.Version)
  192. if err != nil {
  193. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  194. return
  195. }
  196. if createEnvSecretConstraint.Check(actionVersion) {
  197. if err := gaRunner.CreateEnvSecret(); err != nil {
  198. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  199. return
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }