upgrade.go 7.0 KB

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