notify_resolved_incident.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package cluster
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/porter-dev/porter/api/server/authz"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/notifier"
  15. "github.com/porter-dev/porter/internal/notifier/sendgrid"
  16. "github.com/porter-dev/porter/internal/notifier/slack"
  17. "gorm.io/gorm"
  18. )
  19. type NotifyResolvedIncidentHandler struct {
  20. handlers.PorterHandlerReadWriter
  21. authz.KubernetesAgentGetter
  22. }
  23. func NewNotifyResolvedIncidentHandler(
  24. config *config.Config,
  25. decoderValidator shared.RequestDecoderValidator,
  26. writer shared.ResultWriter,
  27. ) *NotifyResolvedIncidentHandler {
  28. return &NotifyResolvedIncidentHandler{
  29. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  30. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  31. }
  32. }
  33. func (c *NotifyResolvedIncidentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  34. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  35. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  36. if project.GetFeatureFlag(models.ValidateApplyV2, c.Config().LaunchDarklyClient) {
  37. return
  38. }
  39. request := &types.Incident{}
  40. if ok := c.DecodeAndValidate(w, r, request); !ok {
  41. return
  42. }
  43. slackInts, _ := c.Repo().SlackIntegration().ListSlackIntegrationsByProjectID(cluster.ProjectID)
  44. rel, err := c.Repo().Release().ReadRelease(cluster.ID, request.ReleaseName, request.ReleaseNamespace)
  45. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  46. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  47. return
  48. }
  49. var notifConf *types.NotificationConfig
  50. if rel != nil && rel.NotificationConfig != 0 {
  51. conf, err := c.Repo().NotificationConfig().ReadNotificationConfig(rel.NotificationConfig)
  52. if err != nil {
  53. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  54. return
  55. }
  56. notifConf = conf.ToNotificationConfigType()
  57. }
  58. notifiers := make([]notifier.IncidentNotifier, 0)
  59. if c.Config().SlackConf != nil {
  60. notifiers = append(notifiers, slack.NewIncidentNotifier(slackInts...))
  61. }
  62. if sc := c.Config().ServerConf; sc.SendgridAPIKey != "" && sc.SendgridSenderEmail != "" && sc.SendgridIncidentAlertTemplateID != "" {
  63. users, err := getUsersByProjectID(c.Repo(), cluster.ProjectID)
  64. if err != nil {
  65. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  66. return
  67. }
  68. notifiers = append(notifiers, sendgrid.NewIncidentNotifier(&sendgrid.IncidentNotifierOpts{
  69. SharedOpts: &sendgrid.SharedOpts{
  70. APIKey: c.Config().ServerConf.SendgridAPIKey,
  71. SenderEmail: c.Config().ServerConf.SendgridSenderEmail,
  72. },
  73. IncidentResolvedTemplateID: sc.SendgridIncidentResolvedTemplateID,
  74. Users: users,
  75. }))
  76. }
  77. multi := notifier.NewMultiIncidentNotifier(
  78. notifConf,
  79. notifiers...,
  80. )
  81. if !cluster.NotificationsDisabled {
  82. url := fmt.Sprintf(
  83. "%s/applications/%s/%s/%s?project_id=%d",
  84. c.Config().ServerConf.ServerURL,
  85. cluster.Name,
  86. request.ReleaseNamespace,
  87. request.ReleaseName,
  88. cluster.ProjectID,
  89. )
  90. if strings.ToLower(string(request.InvolvedObjectKind)) == "job" {
  91. url = fmt.Sprintf(
  92. "%s/jobs/%s/%s/%s?project_id=%d&job=%s",
  93. c.Config().ServerConf.ServerURL,
  94. cluster.Name,
  95. request.ReleaseNamespace,
  96. request.ReleaseName,
  97. cluster.ProjectID,
  98. request.InvolvedObjectName,
  99. )
  100. }
  101. err := multi.NotifyResolved(request, url)
  102. if err != nil {
  103. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  104. return
  105. }
  106. }
  107. }