apply.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. }
  35. // ApplyPorterAppResponse is the response object for the /apps/apply endpoint
  36. type ApplyPorterAppResponse struct {
  37. AppRevisionId string `json:"app_revision_id"`
  38. CLIAction porterv1.EnumCLIAction `json:"cli_action"`
  39. }
  40. // ServeHTTP translates the request into a ApplyPorterApp request, forwards to the cluster control plane, and returns the response
  41. func (c *ApplyPorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  42. ctx, span := telemetry.NewSpan(r.Context(), "serve-apply-porter-app")
  43. defer span.End()
  44. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  45. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  46. telemetry.WithAttributes(span,
  47. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  48. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  49. )
  50. if !project.ValidateApplyV2 {
  51. err := telemetry.Error(ctx, span, nil, "project does not have validate apply v2 enabled")
  52. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  53. return
  54. }
  55. request := &ApplyPorterAppRequest{}
  56. if ok := c.DecodeAndValidate(w, r, request); !ok {
  57. err := telemetry.Error(ctx, span, nil, "error decoding request")
  58. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  59. return
  60. }
  61. if request.Base64AppProto == "" {
  62. err := telemetry.Error(ctx, span, nil, "b64 yaml is empty")
  63. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  64. return
  65. }
  66. decoded, err := base64.StdEncoding.DecodeString(request.Base64AppProto)
  67. if err != nil {
  68. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  69. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  70. return
  71. }
  72. appProto := &porterv1.PorterApp{}
  73. err = helpers.UnmarshalContractObject(decoded, appProto)
  74. if err != nil {
  75. err := telemetry.Error(ctx, span, err, "error unmarshalling app proto")
  76. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  77. return
  78. }
  79. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appProto.Name})
  80. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetId})
  81. validateReq := connect.NewRequest(&porterv1.ApplyPorterAppRequest{
  82. ProjectId: int64(project.ID),
  83. DeploymentTargetId: request.DeploymentTargetId,
  84. App: appProto,
  85. })
  86. ccpResp, err := c.Config().ClusterControlPlaneClient.ApplyPorterApp(ctx, validateReq)
  87. if err != nil {
  88. err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
  89. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  90. return
  91. }
  92. if ccpResp == nil {
  93. err := telemetry.Error(ctx, span, err, "ccp resp is nil")
  94. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  95. return
  96. }
  97. if ccpResp.Msg == nil {
  98. err := telemetry.Error(ctx, span, err, "ccp resp msg is nil")
  99. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  100. return
  101. }
  102. if ccpResp.Msg.PorterAppRevisionId == "" {
  103. err := telemetry.Error(ctx, span, err, "ccp resp app revision id is nil")
  104. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  105. return
  106. }
  107. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "resp-app-revision-id", Value: ccpResp.Msg.PorterAppRevisionId})
  108. if ccpResp.Msg.CliAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_UNSPECIFIED {
  109. err := telemetry.Error(ctx, span, err, "ccp resp cli action is nil")
  110. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  111. return
  112. }
  113. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "cli-action", Value: ccpResp.Msg.CliAction.String()})
  114. response := &ApplyPorterAppResponse{
  115. AppRevisionId: ccpResp.Msg.PorterAppRevisionId,
  116. CLIAction: ccpResp.Msg.CliAction,
  117. }
  118. c.WriteResult(w, r, response)
  119. }