notify_resolved_incident.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  35. request := &types.Incident{}
  36. if ok := c.DecodeAndValidate(w, r, request); !ok {
  37. return
  38. }
  39. slackInts, _ := c.Repo().SlackIntegration().ListSlackIntegrationsByProjectID(cluster.ProjectID)
  40. rel, err := c.Repo().Release().ReadRelease(cluster.ID, request.ReleaseName, request.ReleaseNamespace)
  41. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  42. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  43. return
  44. }
  45. var notifConf *types.NotificationConfig
  46. if rel != nil && rel.NotificationConfig != 0 {
  47. conf, err := c.Repo().NotificationConfig().ReadNotificationConfig(rel.NotificationConfig)
  48. if err != nil {
  49. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  50. return
  51. }
  52. notifConf = conf.ToNotificationConfigType()
  53. }
  54. notifiers := make([]notifier.IncidentNotifier, 0)
  55. if c.Config().SlackConf != nil {
  56. notifiers = append(notifiers, slack.NewIncidentNotifier(slackInts...))
  57. }
  58. if sc := c.Config().ServerConf; sc.SendgridAPIKey != "" && sc.SendgridSenderEmail != "" && sc.SendgridIncidentAlertTemplateID != "" {
  59. users, err := getUsersByProjectID(c.Repo(), cluster.ProjectID)
  60. if err != nil {
  61. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  62. return
  63. }
  64. notifiers = append(notifiers, sendgrid.NewIncidentNotifier(&sendgrid.IncidentNotifierOpts{
  65. SharedOpts: &sendgrid.SharedOpts{
  66. APIKey: c.Config().ServerConf.SendgridAPIKey,
  67. SenderEmail: c.Config().ServerConf.SendgridSenderEmail,
  68. },
  69. IncidentResolvedTemplateID: sc.SendgridIncidentResolvedTemplateID,
  70. Users: users,
  71. }))
  72. }
  73. multi := notifier.NewMultiIncidentNotifier(
  74. notifConf,
  75. notifiers...,
  76. )
  77. if !cluster.NotificationsDisabled {
  78. url := fmt.Sprintf(
  79. "%s/applications/%s/%s/%s?project_id=%d",
  80. c.Config().ServerConf.ServerURL,
  81. cluster.Name,
  82. request.ReleaseNamespace,
  83. request.ReleaseName,
  84. cluster.ProjectID,
  85. )
  86. if strings.ToLower(string(request.InvolvedObjectKind)) == "job" {
  87. url = fmt.Sprintf(
  88. "%s/jobs/%s/%s/%s?project_id=%d&job=%s",
  89. c.Config().ServerConf.ServerURL,
  90. cluster.Name,
  91. request.ReleaseNamespace,
  92. request.ReleaseName,
  93. cluster.ProjectID,
  94. request.InvolvedObjectName,
  95. )
  96. }
  97. err := multi.NotifyResolved(request, url)
  98. if err != nil {
  99. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  100. return
  101. }
  102. }
  103. }