2
0

logs_apply_v2.go 6.6 KB

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