logs_apply_v2.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package porter_app
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "connectrpc.com/connect"
  7. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  8. "github.com/porter-dev/porter/api/server/authz"
  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. porter_agent "github.com/porter-dev/porter/internal/kubernetes/porter_agent/v2"
  15. "github.com/porter-dev/porter/internal/models"
  16. "github.com/porter-dev/porter/internal/telemetry"
  17. )
  18. // AppLogsHandler handles the /apps/logs endpoint
  19. type AppLogsHandler struct {
  20. handlers.PorterHandlerReadWriter
  21. authz.KubernetesAgentGetter
  22. }
  23. // NewAppLogsHandler returns a new AppLogsHandler
  24. func NewAppLogsHandler(
  25. config *config.Config,
  26. decoderValidator shared.RequestDecoderValidator,
  27. writer shared.ResultWriter,
  28. ) *AppLogsHandler {
  29. return &AppLogsHandler{
  30. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  31. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  32. }
  33. }
  34. // AppLogsRequest represents the accepted fields on a request to the /apps/logs endpoint
  35. type AppLogsRequest struct {
  36. DeploymentTargetID string `schema:"deployment_target_id"`
  37. ServiceName string `schema:"service_name"`
  38. AppName string `schema:"app_name"`
  39. Limit uint `schema:"limit"`
  40. StartRange time.Time `schema:"start_range,omitempty"`
  41. EndRange time.Time `schema:"end_range,omitempty"`
  42. SearchParam string `schema:"search_param"`
  43. Direction string `schema:"direction"`
  44. AppRevisionID string `schema:"app_revision_id"`
  45. }
  46. const (
  47. lokiLabel_PorterAppName = "porter_run_app_name"
  48. lokiLabel_PorterServiceName = "porter_run_service_name"
  49. lokiLabel_PorterAppRevisionID = "porter_run_app_revision_id"
  50. lokiLabel_DeploymentTargetId = "porter_run_deployment_target_id"
  51. lokiLabel_Namespace = "namespace"
  52. )
  53. // ServeHTTP gets logs for a given app, service, and deployment target
  54. func (c *AppLogsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  55. ctx, span := telemetry.NewSpan(r.Context(), "serve-app-logs")
  56. defer span.End()
  57. r = r.Clone(ctx)
  58. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  59. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  60. request := &AppLogsRequest{}
  61. if ok := c.DecodeAndValidate(w, r, request); !ok {
  62. err := telemetry.Error(ctx, span, nil, "invalid request")
  63. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  64. return
  65. }
  66. if request.AppName == "" {
  67. err := telemetry.Error(ctx, span, nil, "must provide app name")
  68. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  69. return
  70. }
  71. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: request.AppName})
  72. if request.ServiceName == "" {
  73. err := telemetry.Error(ctx, span, nil, "must provide service name")
  74. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  75. return
  76. }
  77. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "service-name", Value: request.ServiceName})
  78. if request.DeploymentTargetID == "" {
  79. err := telemetry.Error(ctx, span, nil, "must provide deployment target id")
  80. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  81. return
  82. }
  83. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID})
  84. deploymentTargetDetailsReq := connect.NewRequest(&porterv1.DeploymentTargetDetailsRequest{
  85. ProjectId: int64(project.ID),
  86. DeploymentTargetId: request.DeploymentTargetID,
  87. })
  88. deploymentTargetDetailsResp, err := c.Config().ClusterControlPlaneClient.DeploymentTargetDetails(ctx, deploymentTargetDetailsReq)
  89. if err != nil {
  90. err := telemetry.Error(ctx, span, err, "error getting deployment target details from cluster control plane client")
  91. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  92. return
  93. }
  94. if deploymentTargetDetailsResp == nil || deploymentTargetDetailsResp.Msg == nil {
  95. err := telemetry.Error(ctx, span, err, "deployment target details resp is nil")
  96. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  97. return
  98. }
  99. if deploymentTargetDetailsResp.Msg.ClusterId != int64(cluster.ID) {
  100. err := telemetry.Error(ctx, span, err, "deployment target details resp cluster id does not match cluster id")
  101. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  102. return
  103. }
  104. namespace := deploymentTargetDetailsResp.Msg.Namespace
  105. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "namespace", Value: namespace})
  106. if request.StartRange.IsZero() || request.EndRange.IsZero() {
  107. err := telemetry.Error(ctx, span, nil, "must provide start and end range")
  108. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  109. return
  110. }
  111. telemetry.WithAttributes(span,
  112. telemetry.AttributeKV{Key: "start-range", Value: request.StartRange.String()},
  113. telemetry.AttributeKV{Key: "end-range", Value: request.EndRange.String()},
  114. )
  115. k8sAgent, err := c.GetAgent(r, cluster, "")
  116. if err != nil {
  117. _ = telemetry.Error(ctx, span, err, "unable to get agent")
  118. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get agent"), http.StatusInternalServerError))
  119. return
  120. }
  121. agentSvc, err := porter_agent.GetAgentService(k8sAgent.Clientset)
  122. if err != nil {
  123. _ = telemetry.Error(ctx, span, err, "unable to get agent service")
  124. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get agent service"), http.StatusInternalServerError))
  125. return
  126. }
  127. matchLabels := map[string]string{
  128. lokiLabel_Namespace: namespace,
  129. lokiLabel_PorterAppName: request.AppName,
  130. }
  131. if request.ServiceName != "all" {
  132. matchLabels[lokiLabel_PorterServiceName] = request.ServiceName
  133. }
  134. if request.AppRevisionID != "" {
  135. matchLabels[lokiLabel_PorterAppRevisionID] = request.AppRevisionID
  136. }
  137. matchLabels[lokiLabel_DeploymentTargetId] = request.DeploymentTargetID
  138. logRequest := &types.LogRequest{
  139. Limit: request.Limit,
  140. StartRange: &request.StartRange,
  141. EndRange: &request.EndRange,
  142. MatchLabels: matchLabels,
  143. Direction: request.Direction,
  144. SearchParam: request.SearchParam,
  145. }
  146. logs, err := porter_agent.Logs(k8sAgent.Clientset, agentSvc, logRequest)
  147. if err != nil {
  148. _ = telemetry.Error(ctx, span, err, "unable to get logs")
  149. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get logs"), http.StatusInternalServerError))
  150. return
  151. }
  152. c.WriteResult(w, r, logs)
  153. }