apply.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package porter_app
  2. import (
  3. "encoding/base64"
  4. "net/http"
  5. "connectrpc.com/connect"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  7. "github.com/porter-dev/api-contracts/generated/go/helpers"
  8. "github.com/porter-dev/porter/internal/telemetry"
  9. "github.com/porter-dev/porter/api/server/handlers"
  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/models"
  15. )
  16. // ApplyPorterAppHandler is the handler for the /apps/parse endpoint
  17. type ApplyPorterAppHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. // NewApplyPorterAppHandler handles POST requests to the endpoint /apps/apply
  21. func NewApplyPorterAppHandler(
  22. config *config.Config,
  23. decoderValidator shared.RequestDecoderValidator,
  24. writer shared.ResultWriter,
  25. ) *ApplyPorterAppHandler {
  26. return &ApplyPorterAppHandler{
  27. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  28. }
  29. }
  30. // ApplyPorterAppRequest is the request object for the /apps/apply endpoint
  31. type ApplyPorterAppRequest struct {
  32. Base64AppProto string `json:"b64_app_proto"`
  33. DeploymentTargetId string `json:"deployment_target_id"`
  34. AppRevisionID string `json:"app_revision_id"`
  35. }
  36. // ApplyPorterAppResponse is the response object for the /apps/apply endpoint
  37. type ApplyPorterAppResponse struct {
  38. AppRevisionId string `json:"app_revision_id"`
  39. CLIAction porterv1.EnumCLIAction `json:"cli_action"`
  40. }
  41. // ServeHTTP translates the request into a ApplyPorterApp request, forwards to the cluster control plane, and returns the response
  42. func (c *ApplyPorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  43. ctx, span := telemetry.NewSpan(r.Context(), "serve-apply-porter-app")
  44. defer span.End()
  45. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  46. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  47. telemetry.WithAttributes(span,
  48. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  49. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  50. )
  51. if !project.ValidateApplyV2 {
  52. err := telemetry.Error(ctx, span, nil, "project does not have validate apply v2 enabled")
  53. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  54. return
  55. }
  56. request := &ApplyPorterAppRequest{}
  57. if ok := c.DecodeAndValidate(w, r, request); !ok {
  58. err := telemetry.Error(ctx, span, nil, "error decoding request")
  59. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  60. return
  61. }
  62. var appRevisionID string
  63. var appProto *porterv1.PorterApp
  64. var deploymentTargetID string
  65. if request.AppRevisionID != "" {
  66. appRevisionID = request.AppRevisionID
  67. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-revision-id", Value: request.AppRevisionID})
  68. } else {
  69. if request.Base64AppProto == "" {
  70. err := telemetry.Error(ctx, span, nil, "b64 yaml is empty")
  71. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  72. return
  73. }
  74. decoded, err := base64.StdEncoding.DecodeString(request.Base64AppProto)
  75. if err != nil {
  76. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  77. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  78. return
  79. }
  80. appProto = &porterv1.PorterApp{}
  81. err = helpers.UnmarshalContractObject(decoded, appProto)
  82. if err != nil {
  83. err := telemetry.Error(ctx, span, err, "error unmarshalling app proto")
  84. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  85. return
  86. }
  87. if request.DeploymentTargetId == "" {
  88. err := telemetry.Error(ctx, span, err, "deployment target id is empty")
  89. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  90. return
  91. }
  92. deploymentTargetID = request.DeploymentTargetId
  93. telemetry.WithAttributes(span,
  94. telemetry.AttributeKV{Key: "app-name", Value: appProto.Name},
  95. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetId},
  96. )
  97. }
  98. applyReq := connect.NewRequest(&porterv1.ApplyPorterAppRequest{
  99. ProjectId: int64(project.ID),
  100. DeploymentTargetId: deploymentTargetID,
  101. App: appProto,
  102. PorterAppRevisionId: appRevisionID,
  103. })
  104. ccpResp, err := c.Config().ClusterControlPlaneClient.ApplyPorterApp(ctx, applyReq)
  105. if err != nil {
  106. err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
  107. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  108. return
  109. }
  110. if ccpResp == nil {
  111. err := telemetry.Error(ctx, span, err, "ccp resp is nil")
  112. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  113. return
  114. }
  115. if ccpResp.Msg == nil {
  116. err := telemetry.Error(ctx, span, err, "ccp resp msg is nil")
  117. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  118. return
  119. }
  120. if ccpResp.Msg.PorterAppRevisionId == "" {
  121. err := telemetry.Error(ctx, span, err, "ccp resp app revision id is nil")
  122. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  123. return
  124. }
  125. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "resp-app-revision-id", Value: ccpResp.Msg.PorterAppRevisionId})
  126. if ccpResp.Msg.CliAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_UNSPECIFIED {
  127. err := telemetry.Error(ctx, span, err, "ccp resp cli action is nil")
  128. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  129. return
  130. }
  131. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "cli-action", Value: ccpResp.Msg.CliAction.String()})
  132. response := &ApplyPorterAppResponse{
  133. AppRevisionId: ccpResp.Msg.PorterAppRevisionId,
  134. CLIAction: ccpResp.Msg.CliAction,
  135. }
  136. c.WriteResult(w, r, response)
  137. }