فهرست منبع

use agent-cli correctly

Mohammed Nafees 3 سال پیش
والد
کامیت
4a69a3e4bf
3فایلهای تغییر یافته به همراه52 افزوده شده و 11 حذف شده
  1. 24 1
      api/server/handlers/cluster/get_incident_event_logs.go
  2. 3 1
      api/types/cluster.go
  3. 25 9
      internal/kubernetes/agent.go

+ 24 - 1
api/server/handlers/cluster/get_incident_event_logs.go

@@ -1,7 +1,10 @@
 package cluster
 
 import (
+	"fmt"
 	"net/http"
+	"strings"
+	"time"
 
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
@@ -46,7 +49,27 @@ func (c *GetIncidentEventLogsHandler) ServeHTTP(w http.ResponseWriter, r *http.R
 		return
 	}
 
-	err = k8sAgent.StreamPorterAgentLokiLog(request.LogID, safeRW)
+	if len(request.Labels) == 0 {
+		return
+	}
+
+	// validate that the labels are valid
+	for _, label := range request.Labels {
+		if key, val, found := strings.Cut(label, "="); !found || key == "" || val == "" {
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("invalid label: %s", label),
+				http.StatusBadRequest))
+			return
+		}
+	}
+
+	// validate start time
+	if _, err := time.Parse(time.RFC3339, request.StartTime); err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("invalid start time: %s", request.StartTime),
+			http.StatusBadRequest))
+		return
+	}
+
+	err = k8sAgent.StreamPorterAgentLokiLog(request.Labels, request.StartTime, request.Limit, safeRW)
 
 	if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))

+ 3 - 1
api/types/cluster.go

@@ -280,7 +280,9 @@ type GetIncidentsRequest struct {
 }
 
 type GetIncidentEventLogsRequest struct {
-	LogID string `schema:"log_id" form:"required"`
+	Labels    []string `schema:"labels" form:"required"`
+	StartTime string   `schema:"start_time" form:"required"`
+	Limit     uint32   `schema:"limit"`
 }
 
 type IncidentNotifyRequest struct {

+ 25 - 9
internal/kubernetes/agent.go

@@ -1755,7 +1755,12 @@ func (a *Agent) StreamHelmReleases(namespace string, chartList []string, selecto
 	return a.RunWebsocketTask(run)
 }
 
-func (a *Agent) StreamPorterAgentLokiLog(logID string, rw *websocket.WebsocketSafeReadWriter) error {
+func (a *Agent) StreamPorterAgentLokiLog(
+	labels []string,
+	startTime string,
+	limit uint32,
+	rw *websocket.WebsocketSafeReadWriter,
+) error {
 	run := func() error {
 		errorchan := make(chan error)
 
@@ -1830,15 +1835,26 @@ func (a *Agent) StreamPorterAgentLokiLog(logID string, rw *websocket.WebsocketSa
 				Namespace(pod.Namespace).
 				SubResource("exec")
 
+			cmd := []string{
+				"sh",
+				"-c",
+				"/porter/agent-cli",
+				"--start",
+				startTime,
+			}
+
+			for _, label := range labels {
+				cmd = append(cmd, "--label", label)
+			}
+
+			if limit > 0 {
+				cmd = append(cmd, "--limit", fmt.Sprintf("%d", limit))
+			}
+
 			opts := &v1.PodExecOptions{
-				Command: []string{
-					"sh",
-					"-c",
-					"/porter/agent-cli",
-					logID,
-				},
-				Stdout: true,
-				Stderr: true,
+				Command: cmd,
+				Stdout:  true,
+				Stderr:  true,
 			}
 
 			req.VersionedParams(