logs_apply_v2.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package porter_app
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  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. 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. AppID uint `schema:"app_id"`
  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_PorterAppID = "porter_run_app_id"
  49. lokiLabel_PorterServiceName = "porter_run_service_name"
  50. lokiLabel_PorterAppRevisionID = "porter_run_app_revision_id"
  51. lokiLabel_DeploymentTargetId = "porter_run_deployment_target_id"
  52. lokiLabel_Namespace = "namespace"
  53. )
  54. // ServeHTTP gets logs for a given app, service, and deployment target
  55. func (c *AppLogsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  56. ctx, span := telemetry.NewSpan(r.Context(), "serve-app-logs")
  57. defer span.End()
  58. r = r.Clone(ctx)
  59. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  60. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  61. request := &AppLogsRequest{}
  62. if ok := c.DecodeAndValidate(w, r, request); !ok {
  63. err := telemetry.Error(ctx, span, nil, "invalid request")
  64. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  65. return
  66. }
  67. appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
  68. if reqErr != nil {
  69. err := telemetry.Error(ctx, span, reqErr, "porter app name not found in request")
  70. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  71. return
  72. }
  73. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})
  74. if request.AppID == 0 {
  75. err := telemetry.Error(ctx, span, nil, "must provide app id")
  76. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  77. return
  78. }
  79. if request.ServiceName == "" {
  80. err := telemetry.Error(ctx, span, nil, "must provide service name")
  81. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  82. return
  83. }
  84. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "service-name", Value: request.ServiceName})
  85. if request.DeploymentTargetID == "" {
  86. err := telemetry.Error(ctx, span, nil, "must provide deployment target id")
  87. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  88. return
  89. }
  90. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID})
  91. deploymentTarget, err := deployment_target.DeploymentTargetDetails(ctx, deployment_target.DeploymentTargetDetailsInput{
  92. ProjectID: int64(project.ID),
  93. ClusterID: int64(cluster.ID),
  94. DeploymentTargetID: request.DeploymentTargetID,
  95. CCPClient: c.Config().ClusterControlPlaneClient,
  96. })
  97. if err != nil {
  98. err := telemetry.Error(ctx, span, err, "error getting deployment target details")
  99. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  100. return
  101. }
  102. namespace := deploymentTarget.Namespace
  103. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "namespace", Value: namespace})
  104. if request.StartRange.IsZero() || request.EndRange.IsZero() {
  105. err := telemetry.Error(ctx, span, nil, "must provide start and end range")
  106. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  107. return
  108. }
  109. telemetry.WithAttributes(span,
  110. telemetry.AttributeKV{Key: "start-range", Value: request.StartRange.String()},
  111. telemetry.AttributeKV{Key: "end-range", Value: request.EndRange.String()},
  112. )
  113. k8sAgent, err := c.GetAgent(r, cluster, "")
  114. if err != nil {
  115. _ = telemetry.Error(ctx, span, err, "unable to get agent")
  116. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get agent"), http.StatusInternalServerError))
  117. return
  118. }
  119. agentSvc, err := porter_agent.GetAgentService(k8sAgent.Clientset)
  120. if err != nil {
  121. _ = telemetry.Error(ctx, span, err, "unable to get agent service")
  122. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get agent service"), http.StatusInternalServerError))
  123. return
  124. }
  125. matchLabels := map[string]string{
  126. lokiLabel_Namespace: namespace,
  127. lokiLabel_PorterAppName: appName,
  128. lokiLabel_PorterAppID: fmt.Sprintf("%d", request.AppID),
  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. matchLabels[lokiLabel_DeploymentTargetId] = request.DeploymentTargetID
  137. logRequest := &types.LogRequest{
  138. Limit: request.Limit,
  139. StartRange: &request.StartRange,
  140. EndRange: &request.EndRange,
  141. MatchLabels: matchLabels,
  142. Direction: request.Direction,
  143. SearchParam: request.SearchParam,
  144. }
  145. logs, err := porter_agent.Logs(k8sAgent.Clientset, agentSvc, logRequest)
  146. if err != nil {
  147. _ = telemetry.Error(ctx, span, err, "unable to get logs")
  148. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get logs"), http.StatusInternalServerError))
  149. return
  150. }
  151. c.WriteResult(w, r, logs)
  152. }