predeploy_status.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package porter_app
  2. import (
  3. "net/http"
  4. "connectrpc.com/connect"
  5. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  6. "github.com/porter-dev/porter/internal/telemetry"
  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/server/shared/requestutils"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/porter-dev/porter/internal/models"
  15. )
  16. // PredeployStatusHandler handles requests to the /apps/{porter_app_name}/{app_revision_id}/predeploy-status endpoint
  17. type PredeployStatusHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. authz.KubernetesAgentGetter
  20. }
  21. // NewPredeployStatusHandler returns a new PredeployStatusHandler
  22. func NewPredeployStatusHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *PredeployStatusHandler {
  27. return &PredeployStatusHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  30. }
  31. }
  32. // PredeployStatus is the status of the predeploy
  33. type PredeployStatus string
  34. const (
  35. // PredeployStatus_InProgress signifies the predeploy is still running
  36. PredeployStatus_InProgress PredeployStatus = "in-progress"
  37. // PredeployStatus_Failed signifies the predeploy has failed
  38. PredeployStatus_Failed PredeployStatus = "failed"
  39. // PredeployStatus_Successful signifies the predeploy was successful
  40. PredeployStatus_Successful PredeployStatus = "successful"
  41. )
  42. // PredeployStatusResponse is the response object for the /apps/{porter_app_name}/{app_revision_id}/predeploy-status endpoint
  43. type PredeployStatusResponse struct {
  44. // Status is the status of the predeploy
  45. Status PredeployStatus `json:"status"`
  46. }
  47. // ServeHTTP forwards the predeploy status request to the cluster control plane and returns the response
  48. func (c *PredeployStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  49. ctx, span := telemetry.NewSpan(r.Context(), "serve-predeploy-status")
  50. defer span.End()
  51. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  52. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  53. name, _ := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
  54. appRevisionId, _ := requestutils.GetURLParamString(r, types.URLParamAppRevisionID)
  55. if appRevisionId == "" {
  56. err := telemetry.Error(ctx, span, nil, "app revision id is empty")
  57. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  58. return
  59. }
  60. telemetry.WithAttributes(span,
  61. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  62. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  63. telemetry.AttributeKV{Key: "app-name", Value: name},
  64. telemetry.AttributeKV{Key: "app-revision-id", Value: appRevisionId},
  65. )
  66. predeployStatusReq := connect.NewRequest(&porterv1.PredeployStatusRequest{
  67. ProjectId: int64(project.ID),
  68. AppRevisionId: appRevisionId,
  69. })
  70. ccpResp, err := c.Config().ClusterControlPlaneClient.PredeployStatus(ctx, predeployStatusReq)
  71. if err != nil {
  72. err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
  73. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  74. return
  75. }
  76. if ccpResp == nil {
  77. err := telemetry.Error(ctx, span, err, "ccp resp is nil")
  78. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  79. return
  80. }
  81. if ccpResp.Msg == nil {
  82. err := telemetry.Error(ctx, span, err, "ccp resp msg is nil")
  83. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  84. return
  85. }
  86. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "predeploy-status", Value: ccpResp.Msg.PredeployStatus})
  87. var status PredeployStatus
  88. switch ccpResp.Msg.PredeployStatus {
  89. case porterv1.EnumPredeployStatus_ENUM_PREDEPLOY_STATUS_IN_PROGRESS:
  90. status = PredeployStatus_InProgress
  91. case porterv1.EnumPredeployStatus_ENUM_PREDEPLOY_STATUS_FAILED:
  92. status = PredeployStatus_Failed
  93. case porterv1.EnumPredeployStatus_ENUM_PREDEPLOY_STATUS_SUCCESSFUL:
  94. status = PredeployStatus_Successful
  95. default:
  96. err := telemetry.Error(ctx, span, nil, "ccp resp predeploy status is invalid")
  97. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  98. return
  99. }
  100. response := &PredeployStatusResponse{
  101. Status: status,
  102. }
  103. c.WriteResult(w, r, response)
  104. }