2025-04-08 17:21:59 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"oc-monitord/conf"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"cloud.o-forge.io/core/oc-lib/logs"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
var logger zerolog.Logger
|
2025-04-09 18:59:37 +02:00
|
|
|
var wfLogger zerolog.Logger
|
|
|
|
|
2025-04-08 17:21:59 +02:00
|
|
|
|
|
|
|
// Take the slice of string that make up one round of stderr outputs from the --watch option in argo submit
|
2025-04-10 11:10:16 +02:00
|
|
|
func NewLocalArgoWatch(inputs []string) *ArgoWatch {
|
2025-04-08 17:21:59 +02:00
|
|
|
var workflow ArgoWatch
|
|
|
|
|
|
|
|
for _, input := range inputs {
|
|
|
|
line := strings.TrimSpace(input)
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(line, "Name:"):
|
|
|
|
workflow.Name = parseValue(line)
|
|
|
|
case strings.HasPrefix(line, "Namespace:"):
|
|
|
|
workflow.Namespace = parseValue(line)
|
|
|
|
case strings.HasPrefix(line, "Status:"):
|
|
|
|
workflow.Status = parseValue(line)
|
|
|
|
case strings.HasPrefix(line, "PodRunning"):
|
|
|
|
workflow.PodRunning = parseBoolValue(line)
|
|
|
|
case strings.HasPrefix(line, "Completed"):
|
|
|
|
workflow.Completed = parseBoolValue(line)
|
|
|
|
case strings.HasPrefix(line, "Created:"):
|
|
|
|
workflow.Created = parseValue(line)
|
|
|
|
case strings.HasPrefix(line, "Started:"):
|
|
|
|
workflow.Started = parseValue(line)
|
|
|
|
case strings.HasPrefix(line, "Duration:"):
|
|
|
|
workflow.Duration = parseValue(line)
|
|
|
|
case strings.HasPrefix(line, "Progress:"):
|
|
|
|
workflow.Progress = parseValue(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &workflow
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func parseValue(line string) string {
|
|
|
|
parts := strings.SplitN(line, ":", 2)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(parts[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseBoolValue(line string) bool {
|
|
|
|
value := parseValue(line)
|
|
|
|
return value == "True"
|
|
|
|
}
|
|
|
|
|
2025-04-09 18:59:37 +02:00
|
|
|
func LogLocalWorkflow(wfName string, pipe io.ReadCloser, wg *sync.WaitGroup) {
|
2025-04-08 17:21:59 +02:00
|
|
|
logger = logs.GetLogger()
|
|
|
|
|
|
|
|
logger.Debug().Msg("created wf_logger")
|
|
|
|
fmt.Println("created wf_logger")
|
2025-04-09 18:59:37 +02:00
|
|
|
wfLogger = logger.With().Str("argo_name", wfName).Str("workflow_id", conf.GetConfig().WorkflowID).Str("workflow_execution_id", conf.GetConfig().ExecutionID).Logger()
|
2025-04-08 17:21:59 +02:00
|
|
|
|
|
|
|
var current_watch, previous_watch ArgoWatch
|
|
|
|
|
|
|
|
watch_output := make([]string, 0)
|
|
|
|
scanner := bufio.NewScanner(pipe)
|
|
|
|
for scanner.Scan() {
|
|
|
|
log := scanner.Text()
|
|
|
|
watch_output = append(watch_output, log)
|
|
|
|
|
2025-04-09 18:59:37 +02:00
|
|
|
// Log the progress of the WF
|
2025-04-08 17:21:59 +02:00
|
|
|
if strings.HasPrefix(log, "Progress:") {
|
|
|
|
|
2025-04-10 11:10:16 +02:00
|
|
|
current_watch = *NewLocalArgoWatch(watch_output)
|
2025-04-08 17:21:59 +02:00
|
|
|
workflowName := current_watch.Name
|
|
|
|
if !current_watch.Equals(&previous_watch) {
|
|
|
|
wg.Add(1)
|
|
|
|
// checkStatus(current_watch.Status, previous_watch.Status)
|
|
|
|
jsonified, err := json.Marshal(current_watch)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error().Msg("Could not create watch log for " + workflowName)
|
|
|
|
}
|
2025-04-09 18:59:37 +02:00
|
|
|
wfLogger.Info().Msg(string(jsonified))
|
2025-04-08 17:21:59 +02:00
|
|
|
previous_watch = current_watch
|
|
|
|
current_watch = ArgoWatch{}
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
}
|
2025-04-09 18:59:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-08 17:21:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug, no logs sent
|
2025-04-10 11:10:16 +02:00
|
|
|
func LogPods(wfName string, pipe io.ReadCloser, steps []string, wg *sync.WaitGroup) {
|
2025-04-09 18:59:37 +02:00
|
|
|
scanner := bufio.NewScanner(pipe)
|
|
|
|
for scanner.Scan() {
|
2025-04-10 11:10:16 +02:00
|
|
|
var podLogger zerolog.Logger
|
2025-04-09 18:59:37 +02:00
|
|
|
fmt.Println("new line")
|
|
|
|
wg.Add(1)
|
2025-04-10 11:10:16 +02:00
|
|
|
|
2025-04-09 18:59:37 +02:00
|
|
|
line := scanner.Text()
|
|
|
|
podName := strings.Split(line, ":")[0]
|
|
|
|
podLogger = wfLogger.With().Str("step_name", getStepName(podName, steps)).Logger()
|
|
|
|
log := strings.Split(line,podName+":")[1]
|
2025-04-10 11:10:16 +02:00
|
|
|
podLog := NewArgoPodLog(wfName,podName,log)
|
|
|
|
jsonifiedLog, err := json.Marshal(podLog)
|
|
|
|
if err != nil {
|
|
|
|
podLogger.Fatal().Msg(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
podLogger.Info().Msg(string(jsonifiedLog))
|
2025-04-09 18:59:37 +02:00
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStepName(podName string, steps []string) string {
|
|
|
|
|
|
|
|
for _, step := range(steps) {
|
|
|
|
if strings.Contains(podName,step){
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "error"
|
|
|
|
}
|
|
|
|
|