Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/api/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type Config struct {
LOGGING_PLUGIN_MULTIPART_REGEX string `mapstructure:"LOGGING_PLUGIN_MULTIPART_REGEX"`
LOGGING_PLUGIN_JSON_MAP string `mapstructure:"LOGGING_PLUGIN_JSON_MAP"`
LOGGING_PLUGIN_LINE_FORMAT string `mapstructure:"LOGGING_PLUGIN_LINE_FORMAT"`
LOGGING_PLUGIN_PIPELINERUN_UID_KEY string `mapstructure:"LOGGING_PLUGIN_PIPELINERUN_UID_KEY"`
LOGGING_PLUGIN_TASKRUN_UID_KEY string `mapstructure:"LOGGING_PLUGIN_TASKRUN_UID_KEY"`
}

func Get() *Config {
Expand Down
10 changes: 8 additions & 2 deletions pkg/api/server/v1alpha2/plugin/plugin_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ func (s *LogServer) GetLog(req *pb3.GetLogRequest, srv pb3.Logs_GetLogServer) er
func (s *LogServer) getLogRequestParams(rec *db.Record) (startTime, endTime, uidKey string, err error) {
switch rec.Type {
case typePipelineRun:
uidKey = pipelineRunUIDKey
uidKey = s.config.LOGGING_PLUGIN_PIPELINERUN_UID_KEY
if uidKey == "" {
uidKey = pipelineRunUIDKey
}
data := &pipelinev1.PipelineRun{}
err := json.Unmarshal(rec.Data, data)
if err != nil {
Expand All @@ -140,7 +143,10 @@ func (s *LogServer) getLogRequestParams(rec *db.Record) (startTime, endTime, uid
endTime = strconv.FormatInt(data.Status.CompletionTime.Add(s.forwarderDelayDuration).UTC().Unix(), 10)

case typeTaskRun:
uidKey = taskRunUIDKey
uidKey = s.config.LOGGING_PLUGIN_TASKRUN_UID_KEY
if uidKey == "" {
uidKey = taskRunUIDKey
}
data := &pipelinev1.TaskRun{}
err := json.Unmarshal(rec.Data, data)
if err != nil {
Expand Down
69 changes: 69 additions & 0 deletions pkg/api/server/v1alpha2/plugin/plugin_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,75 @@ func TestLogPluginServer_GetLog(t *testing.T) {

}

func TestLogPluginServer_GetLog_WithConfigurableUIDKey(t *testing.T) {
// Create a mock Loki server
mockLoki := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify that the custom UID key is used in the query
if !strings.Contains(r.URL.String(), "custom_pipelinerun_uid_key") {
t.Errorf("expected query to contain custom_pipelinerun_uid_key, got %s", r.URL.String())
}

response := map[string]interface{}{
"status": "success",
"data": map[string]interface{}{
"result": []map[string]interface{}{
{
"stream": map[string]string{},
"values": [][]string{
{"1625081600000000000", "Log Message 0"},
},
},
},
},
}
json.NewEncoder(w).Encode(response)
}))
defer mockLoki.Close()

tokenDir := t.TempDir()
tokenPath := filepath.Join(tokenDir, "token")
os.WriteFile(tokenPath, []byte("dummytoken"), 0600)

srv, _ := server.New(&config.Config{
LOGS_API: true,
LOGS_TYPE: "Loki",
DB_ENABLE_AUTO_MIGRATION: true,
LOGGING_PLUGIN_TOKEN_PATH: tokenPath,
LOGGING_PLUGIN_API_URL: mockLoki.URL,
LOGGING_PLUGIN_TLS_VERIFICATION_DISABLE: true,
LOGGING_PLUGIN_PIPELINERUN_UID_KEY: "custom_pipelinerun_uid_key",
}, logger.Get("info"), test.NewDB(t))

ctx := context.Background()
mockServer := &mockGetLogServer{ctx: ctx}

res, _ := srv.CreateResult(ctx, &pb.CreateResultRequest{
Parent: "foo",
Result: &pb.Result{Name: "foo/results/bar"},
})

srv.CreateRecord(ctx, &pb.CreateRecordRequest{
Parent: res.GetName(),
Record: &pb.Record{
Name: record.FormatName(res.GetName(), "baz"),
Data: &pb.Any{
Type: "tekton.dev/v1.PipelineRun",
Value: jsonutil.AnyBytes(t, pipelinev1.PipelineRun{
Status: pipelinev1.PipelineRunStatus{
PipelineRunStatusFields: pipelinev1.PipelineRunStatusFields{
StartTime: &metav1.Time{Time: time.Now()},
CompletionTime: &metav1.Time{Time: time.Now()},
},
},
}),
},
},
})

req := &pb3.GetLogRequest{Name: log.FormatName(res.GetName(), "baz")}
srv.LogPluginServer.GetLog(req, mockServer)
}

func TestMergeLogParts(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading