logs_apply_v2.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. }
  45. const (
  46. lokiLabel_PorterAppName = "porter_run_app_name"
  47. lokiLabel_PorterServiceName = "porter_run_service_name"
  48. lokiLabel_Namespace = "namespace"
  49. )
  50. // ServeHTTP gets logs for a given app, service, and deployment target
  51. func (c *AppLogsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  52. ctx, span := telemetry.NewSpan(r.Context(), "serve-app-logs")
  53. defer span.End()
  54. r = r.Clone(ctx)
  55. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  56. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  57. request := &AppLogsRequest{}
  58. if ok := c.DecodeAndValidate(w, r, request); !ok {
  59. err := telemetry.Error(ctx, span, nil, "invalid request")
  60. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  61. return
  62. }
  63. if request.AppName == "" {
  64. err := telemetry.Error(ctx, span, nil, "must provide app name")
  65. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  66. return
  67. }
  68. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: request.AppName})
  69. if request.ServiceName == "" {
  70. err := telemetry.Error(ctx, span, nil, "must provide service name")
  71. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  72. return
  73. }
  74. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "service-name", Value: request.ServiceName})
  75. if request.DeploymentTargetID == "" {
  76. err := telemetry.Error(ctx, span, nil, "must provide deployment target id")
  77. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  78. return
  79. }
  80. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID})
  81. deploymentTargetDetailsReq := connect.NewRequest(&porterv1.DeploymentTargetDetailsRequest{
  82. ProjectId: int64(project.ID),
  83. DeploymentTargetId: request.DeploymentTargetID,
  84. })
  85. deploymentTargetDetailsResp, err := c.Config().ClusterControlPlaneClient.DeploymentTargetDetails(ctx, deploymentTargetDetailsReq)
  86. if err != nil {
  87. err := telemetry.Error(ctx, span, err, "error getting deployment target details from cluster control plane client")
  88. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  89. return
  90. }
  91. if deploymentTargetDetailsResp == nil || deploymentTargetDetailsResp.Msg == nil {
  92. err := telemetry.Error(ctx, span, err, "deployment target details resp is nil")
  93. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  94. return
  95. }
  96. if deploymentTargetDetailsResp.Msg.ClusterId != int64(cluster.ID) {
  97. err := telemetry.Error(ctx, span, err, "deployment target details resp cluster id does not match cluster id")
  98. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  99. return
  100. }
  101. namespace := deploymentTargetDetailsResp.Msg.Namespace
  102. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "namespace", Value: namespace})
  103. if request.StartRange.IsZero() || request.EndRange.IsZero() {
  104. err := telemetry.Error(ctx, span, nil, "must provide start and end range")
  105. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  106. return
  107. }
  108. telemetry.WithAttributes(span,
  109. telemetry.AttributeKV{Key: "start-range", Value: request.StartRange.String()},
  110. telemetry.AttributeKV{Key: "end-range", Value: request.EndRange.String()},
  111. )
  112. k8sAgent, err := c.GetAgent(r, cluster, "")
  113. if err != nil {
  114. _ = telemetry.Error(ctx, span, err, "unable to get agent")
  115. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get agent"), http.StatusInternalServerError))
  116. return
  117. }
  118. agentSvc, err := porter_agent.GetAgentService(k8sAgent.Clientset)
  119. if err != nil {
  120. _ = telemetry.Error(ctx, span, err, "unable to get agent service")
  121. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get agent service"), http.StatusInternalServerError))
  122. return
  123. }
  124. matchLabels := map[string]string{
  125. lokiLabel_Namespace: namespace,
  126. lokiLabel_PorterAppName: request.AppName,
  127. }
  128. if request.ServiceName != "all" {
  129. matchLabels[lokiLabel_PorterServiceName] = request.ServiceName
  130. }
  131. logRequest := &types.LogRequest{
  132. Limit: request.Limit,
  133. StartRange: &request.StartRange,
  134. EndRange: &request.EndRange,
  135. MatchLabels: matchLabels,
  136. Direction: request.Direction,
  137. SearchParam: request.SearchParam,
  138. }
  139. logs, err := porter_agent.Logs(k8sAgent.Clientset, agentSvc, logRequest)
  140. if err != nil {
  141. _ = telemetry.Error(ctx, span, err, "unable to get logs")
  142. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get logs"), http.StatusInternalServerError))
  143. return
  144. }
  145. c.WriteResult(w, r, logs)
  146. }