current_app_revision.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/porter-dev/porter/internal/porter_app"
  9. "github.com/porter-dev/porter/internal/telemetry"
  10. "github.com/porter-dev/porter/api/server/handlers"
  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/models"
  16. )
  17. // LatestAppRevisionHandler handles requests to the /apps/{porter_app_name}/latest endpoint
  18. type LatestAppRevisionHandler struct {
  19. handlers.PorterHandlerReadWriter
  20. authz.KubernetesAgentGetter
  21. }
  22. // NewLatestAppRevisionHandler returns a new LatestAppRevisionHandler
  23. func NewLatestAppRevisionHandler(
  24. config *config.Config,
  25. decoderValidator shared.RequestDecoderValidator,
  26. writer shared.ResultWriter,
  27. ) *LatestAppRevisionHandler {
  28. return &LatestAppRevisionHandler{
  29. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  30. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  31. }
  32. }
  33. // LatestAppRevisionRequest is the request object for the /apps/{porter_app_name}/latest endpoint
  34. type LatestAppRevisionRequest struct {
  35. DeploymentTargetID string `schema:"deployment_target_id,omitempty"`
  36. DeploymentTargetName string `schema:"deployment_target_name,omitempty"`
  37. }
  38. // LatestAppRevisionResponse is the response object for the /apps/{porter_app_name}/latest endpoint
  39. type LatestAppRevisionResponse struct {
  40. // AppRevision is the latest revision for the app
  41. AppRevision porter_app.Revision `json:"app_revision"`
  42. }
  43. // ServeHTTP translates the request into a CurrentAppRevision grpc request, forwards to the cluster control plane, and returns the response.
  44. // Multi-cluster projects are not supported, as they may have multiple porter-apps with the same name in the same project.
  45. func (c *LatestAppRevisionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  46. ctx, span := telemetry.NewSpan(r.Context(), "serve-latest-app-revision")
  47. defer span.End()
  48. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  49. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  50. telemetry.WithAttributes(span,
  51. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  52. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  53. )
  54. appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
  55. if reqErr != nil {
  56. e := telemetry.Error(ctx, span, reqErr, "error parsing stack name from url")
  57. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  58. return
  59. }
  60. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})
  61. request := &LatestAppRevisionRequest{}
  62. if ok := c.DecodeAndValidate(w, r, request); !ok {
  63. err := telemetry.Error(ctx, span, nil, "error decoding request")
  64. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  65. return
  66. }
  67. deploymentTargetName := request.DeploymentTargetName
  68. if request.DeploymentTargetName == "" && request.DeploymentTargetID == "" {
  69. defaultDeploymentTarget, err := defaultDeploymentTarget(ctx, defaultDeploymentTargetInput{
  70. ProjectID: project.ID,
  71. ClusterID: cluster.ID,
  72. ClusterControlPlaneClient: c.Config().ClusterControlPlaneClient,
  73. })
  74. if err != nil {
  75. err := telemetry.Error(ctx, span, err, "error getting default deployment target")
  76. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  77. return
  78. }
  79. deploymentTargetName = defaultDeploymentTarget.Name
  80. }
  81. telemetry.WithAttributes(span,
  82. telemetry.AttributeKV{Key: "deployment-target-name", Value: deploymentTargetName},
  83. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID},
  84. )
  85. porterApps, err := c.Repo().PorterApp().ReadPorterAppsByProjectIDAndName(project.ID, appName)
  86. if err != nil {
  87. err := telemetry.Error(ctx, span, err, "error getting porter app from repo")
  88. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  89. return
  90. }
  91. if len(porterApps) == 0 {
  92. err := telemetry.Error(ctx, span, err, "no porter apps returned")
  93. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  94. return
  95. }
  96. if len(porterApps) > 1 {
  97. err := telemetry.Error(ctx, span, err, "multiple porter apps returned; unable to determine which one to use")
  98. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  99. return
  100. }
  101. appId := porterApps[0].ID
  102. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-id", Value: appId})
  103. if appId == 0 {
  104. err := telemetry.Error(ctx, span, err, "porter app id is missing")
  105. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  106. return
  107. }
  108. currentAppRevisionReq := connect.NewRequest(&porterv1.CurrentAppRevisionRequest{
  109. ProjectId: int64(project.ID),
  110. AppId: int64(appId),
  111. DeploymentTargetIdentifier: &porterv1.DeploymentTargetIdentifier{
  112. Id: request.DeploymentTargetID,
  113. Name: deploymentTargetName,
  114. },
  115. })
  116. currentAppRevisionResp, err := c.Config().ClusterControlPlaneClient.CurrentAppRevision(ctx, currentAppRevisionReq)
  117. if err != nil {
  118. err := telemetry.Error(ctx, span, err, "error getting current app revision from cluster control plane client")
  119. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  120. return
  121. }
  122. if currentAppRevisionResp == nil || currentAppRevisionResp.Msg == nil {
  123. err := telemetry.Error(ctx, span, err, "current app revision resp is nil")
  124. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  125. return
  126. }
  127. appRevision := currentAppRevisionResp.Msg.AppRevision
  128. encodedRevision, err := porter_app.EncodedRevisionFromProto(ctx, appRevision)
  129. if err != nil {
  130. err := telemetry.Error(ctx, span, err, "error encoding revision from proto")
  131. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  132. return
  133. }
  134. appRevisionId := encodedRevision.ID
  135. appInstanceId := encodedRevision.AppInstanceID
  136. telemetry.WithAttributes(span,
  137. telemetry.AttributeKV{Key: "app-revision-id", Value: appRevisionId},
  138. telemetry.AttributeKV{Key: "app-instance-id", Value: appInstanceId},
  139. )
  140. response := LatestAppRevisionResponse{
  141. AppRevision: encodedRevision,
  142. }
  143. c.WriteResult(w, r, response)
  144. }