current_app_revision.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package porter_app
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/authz"
  5. "github.com/porter-dev/porter/api/server/shared/requestutils"
  6. "connectrpc.com/connect"
  7. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  8. "github.com/google/uuid"
  9. "github.com/porter-dev/porter/internal/porter_app"
  10. "github.com/porter-dev/porter/internal/porter_app/notifications"
  11. "github.com/porter-dev/porter/internal/telemetry"
  12. "github.com/porter-dev/porter/api/server/handlers"
  13. "github.com/porter-dev/porter/api/server/shared"
  14. "github.com/porter-dev/porter/api/server/shared/apierrors"
  15. "github.com/porter-dev/porter/api/server/shared/config"
  16. "github.com/porter-dev/porter/api/types"
  17. "github.com/porter-dev/porter/internal/models"
  18. )
  19. // LatestAppRevisionHandler handles requests to the /apps/{porter_app_name}/latest endpoint
  20. type LatestAppRevisionHandler struct {
  21. handlers.PorterHandlerReadWriter
  22. authz.KubernetesAgentGetter
  23. }
  24. // NewLatestAppRevisionHandler returns a new LatestAppRevisionHandler
  25. func NewLatestAppRevisionHandler(
  26. config *config.Config,
  27. decoderValidator shared.RequestDecoderValidator,
  28. writer shared.ResultWriter,
  29. ) *LatestAppRevisionHandler {
  30. return &LatestAppRevisionHandler{
  31. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  32. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  33. }
  34. }
  35. // LatestAppRevisionRequest is the request object for the /apps/{porter_app_name}/latest endpoint
  36. type LatestAppRevisionRequest struct {
  37. DeploymentTargetID string `schema:"deployment_target_id"`
  38. }
  39. // LatestAppRevisionResponse is the response object for the /apps/{porter_app_name}/latest endpoint
  40. type LatestAppRevisionResponse struct {
  41. // AppRevision is the latest revision for the app
  42. AppRevision porter_app.Revision `json:"app_revision"`
  43. // Notifications are the notifications associated with the app revision
  44. Notifications []notifications.Notification `json:"notifications"`
  45. }
  46. // ServeHTTP translates the request into a CurrentAppRevision grpc request, forwards to the cluster control plane, and returns the response.
  47. // Multi-cluster projects are not supported, as they may have multiple porter-apps with the same name in the same project.
  48. func (c *LatestAppRevisionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  49. ctx, span := telemetry.NewSpan(r.Context(), "serve-latest-app-revision")
  50. defer span.End()
  51. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  52. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  53. telemetry.WithAttributes(span,
  54. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  55. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  56. )
  57. appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
  58. if reqErr != nil {
  59. e := telemetry.Error(ctx, span, reqErr, "error parsing stack name from url")
  60. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  61. return
  62. }
  63. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})
  64. request := &LatestAppRevisionRequest{}
  65. if ok := c.DecodeAndValidate(w, r, request); !ok {
  66. err := telemetry.Error(ctx, span, nil, "error decoding request")
  67. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  68. return
  69. }
  70. _, err := uuid.Parse(request.DeploymentTargetID)
  71. if err != nil {
  72. err := telemetry.Error(ctx, span, err, "error parsing deployment target id")
  73. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  74. return
  75. }
  76. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID})
  77. porterApps, err := c.Repo().PorterApp().ReadPorterAppsByProjectIDAndName(project.ID, appName)
  78. if err != nil {
  79. err := telemetry.Error(ctx, span, err, "error getting porter app from repo")
  80. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  81. return
  82. }
  83. if len(porterApps) == 0 {
  84. err := telemetry.Error(ctx, span, err, "no porter apps returned")
  85. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  86. return
  87. }
  88. if len(porterApps) > 1 {
  89. err := telemetry.Error(ctx, span, err, "multiple porter apps returned; unable to determine which one to use")
  90. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  91. return
  92. }
  93. appId := porterApps[0].ID
  94. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-id", Value: appId})
  95. if appId == 0 {
  96. err := telemetry.Error(ctx, span, err, "porter app id is missing")
  97. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  98. return
  99. }
  100. currentAppRevisionReq := connect.NewRequest(&porterv1.CurrentAppRevisionRequest{
  101. ProjectId: int64(project.ID),
  102. AppId: int64(appId),
  103. DeploymentTargetId: request.DeploymentTargetID,
  104. })
  105. currentAppRevisionResp, err := c.Config().ClusterControlPlaneClient.CurrentAppRevision(ctx, currentAppRevisionReq)
  106. if err != nil {
  107. err := telemetry.Error(ctx, span, err, "error getting current app revision from cluster control plane client")
  108. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  109. return
  110. }
  111. if currentAppRevisionResp == nil || currentAppRevisionResp.Msg == nil {
  112. err := telemetry.Error(ctx, span, err, "current app revision resp is nil")
  113. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  114. return
  115. }
  116. appRevision := currentAppRevisionResp.Msg.AppRevision
  117. encodedRevision, err := porter_app.EncodedRevisionFromProto(ctx, appRevision)
  118. if err != nil {
  119. err := telemetry.Error(ctx, span, err, "error encoding revision from proto")
  120. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  121. return
  122. }
  123. appRevisionId := encodedRevision.ID
  124. appInstanceId := encodedRevision.AppInstanceID
  125. telemetry.WithAttributes(span,
  126. telemetry.AttributeKV{Key: "app-revision-id", Value: appRevisionId},
  127. telemetry.AttributeKV{Key: "app-instance-id", Value: appInstanceId},
  128. )
  129. notificationEvents, err := c.Repo().PorterAppEvent().ReadNotificationsByAppRevisionID(ctx, appInstanceId, appRevisionId)
  130. if err != nil {
  131. err := telemetry.Error(ctx, span, err, "error getting notifications from repo")
  132. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  133. return
  134. }
  135. latestNotifications := make([]notifications.Notification, 0)
  136. for _, event := range notificationEvents {
  137. notification, err := notifications.NotificationFromPorterAppEvent(event)
  138. if err != nil {
  139. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "notification-conversion-error", Value: err.Error()})
  140. continue
  141. }
  142. if notification == nil {
  143. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "notification-conversion-error", Value: "notification is nil"})
  144. continue
  145. }
  146. // TODO: remove this check once this attribute is not found in the span for >30 days
  147. if notification.Scope == "" {
  148. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "notification-conversion-error", Value: "old-notification-format"})
  149. continue
  150. }
  151. latestNotifications = append(latestNotifications, *notification)
  152. }
  153. response := LatestAppRevisionResponse{
  154. AppRevision: encodedRevision,
  155. Notifications: latestNotifications,
  156. }
  157. c.WriteResult(w, r, response)
  158. }