service_status.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/api/server/authz"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/server/shared/requestutils"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/deployment_target"
  14. "github.com/porter-dev/porter/internal/models"
  15. "github.com/porter-dev/porter/internal/porter_app"
  16. "github.com/porter-dev/porter/internal/telemetry"
  17. )
  18. // ServiceStatusHandler is the handler for GET /apps/pods
  19. type ServiceStatusHandler struct {
  20. handlers.PorterHandlerReadWriter
  21. authz.KubernetesAgentGetter
  22. }
  23. // NewServiceStatusHandler returns a new ServiceStatusHandler
  24. func NewServiceStatusHandler(
  25. config *config.Config,
  26. decoderValidator shared.RequestDecoderValidator,
  27. writer shared.ResultWriter,
  28. ) *ServiceStatusHandler {
  29. return &ServiceStatusHandler{
  30. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  31. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  32. }
  33. }
  34. // ServiceStatusRequest is the expected format for a request body on GET /apps/pods
  35. type ServiceStatusRequest struct {
  36. DeploymentTargetID string `schema:"deployment_target_id"`
  37. ServiceName string `schema:"service"`
  38. }
  39. // ServiceStatusResponse is the expected format for a response body on GET /apps/pods
  40. type ServiceStatusResponse struct {
  41. Status porter_app.ServiceStatus `json:"status"`
  42. }
  43. func (c *ServiceStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  44. ctx, span := telemetry.NewSpan(r.Context(), "serve-pod-status")
  45. defer span.End()
  46. request := &ServiceStatusRequest{}
  47. if ok := c.DecodeAndValidate(w, r, request); !ok {
  48. err := telemetry.Error(ctx, span, nil, "invalid request")
  49. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  50. return
  51. }
  52. appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
  53. if reqErr != nil {
  54. err := telemetry.Error(ctx, span, reqErr, "porter app name not found in request")
  55. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  56. return
  57. }
  58. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  59. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  60. telemetry.WithAttributes(span,
  61. telemetry.AttributeKV{Key: "service-name", Value: request.ServiceName},
  62. telemetry.AttributeKV{Key: "app-name", Value: appName},
  63. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID},
  64. )
  65. deploymentTarget, err := deployment_target.DeploymentTargetDetails(ctx, deployment_target.DeploymentTargetDetailsInput{
  66. ProjectID: int64(project.ID),
  67. ClusterID: int64(cluster.ID),
  68. DeploymentTargetID: request.DeploymentTargetID,
  69. CCPClient: c.Config().ClusterControlPlaneClient,
  70. })
  71. if err != nil {
  72. err := telemetry.Error(ctx, span, err, "error getting deployment target details")
  73. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  74. return
  75. }
  76. namespace := deploymentTarget.Namespace
  77. telemetry.WithAttributes(span,
  78. telemetry.AttributeKV{Key: "namespace", Value: namespace},
  79. telemetry.AttributeKV{Key: "deployment-target-id", Value: deploymentTarget.ID},
  80. )
  81. app, err := c.Repo().PorterApp().ReadPorterAppByName(cluster.ID, appName)
  82. if err != nil {
  83. err = telemetry.Error(ctx, span, err, "error reading porter app by name")
  84. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  85. return
  86. }
  87. if app == nil || app.ID == 0 {
  88. err = telemetry.Error(ctx, span, nil, "app with name does not exist in project")
  89. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  90. return
  91. }
  92. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-id", Value: app.ID})
  93. agent, err := c.GetAgent(r, cluster, "")
  94. if err != nil {
  95. err = telemetry.Error(ctx, span, err, "unable to get agent")
  96. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  97. return
  98. }
  99. if agent == nil {
  100. err = telemetry.Error(ctx, span, nil, "agent is nil")
  101. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  102. return
  103. }
  104. listAppRevisionsReq := connect.NewRequest(&porterv1.ListAppRevisionsRequest{
  105. ProjectId: int64(project.ID),
  106. AppId: int64(app.ID),
  107. DeploymentTargetId: request.DeploymentTargetID,
  108. AppName: appName,
  109. })
  110. listAppRevisionsResp, err := c.Config().ClusterControlPlaneClient.ListAppRevisions(ctx, listAppRevisionsReq)
  111. if err != nil {
  112. err = telemetry.Error(ctx, span, err, "error listing app revisions")
  113. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  114. return
  115. }
  116. if listAppRevisionsResp == nil || listAppRevisionsResp.Msg == nil {
  117. err = telemetry.Error(ctx, span, nil, "list app revisions response is nil")
  118. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  119. return
  120. }
  121. appRevisions := listAppRevisionsResp.Msg.AppRevisions
  122. if appRevisions == nil {
  123. appRevisions = []*porterv1.AppRevision{}
  124. }
  125. var revisions []porter_app.Revision
  126. for _, revision := range appRevisions {
  127. encodedRevision, err := porter_app.EncodedRevisionFromProto(ctx, revision)
  128. if err != nil {
  129. err := telemetry.Error(ctx, span, err, "error getting encoded revision from proto")
  130. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  131. return
  132. }
  133. revisions = append(revisions, encodedRevision)
  134. }
  135. serviceStatus, err := porter_app.GetServiceStatus(ctx, porter_app.GetServiceStatusInput{
  136. DeploymentTarget: deploymentTarget,
  137. Agent: *agent,
  138. AppName: appName,
  139. ServiceName: request.ServiceName,
  140. AppRevisions: revisions,
  141. })
  142. if err != nil {
  143. err := telemetry.Error(ctx, span, err, "error getting service status")
  144. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  145. return
  146. }
  147. res := ServiceStatusResponse{
  148. Status: serviceStatus,
  149. }
  150. c.WriteResult(w, r, res)
  151. }