Monitord Acces Change

This commit is contained in:
mr
2026-05-27 16:09:45 +02:00
parent a9284314ef
commit 7c91a8b032
19 changed files with 2496 additions and 332 deletions
+28 -28
View File
@@ -6,6 +6,7 @@ import (
"os"
"regexp"
"strings"
"time"
"oc-monitord/conf"
l "oc-monitord/logger"
@@ -42,19 +43,14 @@ var parser argparse.Parser
var workflowName string
func main() {
o := config.GetConfLoader("oc-monitord")
parser = *argparse.NewParser("oc-monitord", "Launch the execution of a workflow given as a parameter and sends the produced logs to a loki database")
loadConfig(&parser)
fmt.Println("sqdqs", o.GetStringDefault("MONGO_URL", "mongodb://mongo:27017"))
oclib.InitDaemon("oc-monitord")
// Lance l'abonné NATS centralisé pour les confirmations PB_CONSIDERS.
workflow_builder.StartConsidersListener()
fmt.Println(conf.GetConfig())
logger = u.GetLogger()
logger.Debug().Msg("Loki URL : " + config.GetConfig().LokiUrl)
@@ -62,9 +58,7 @@ func main() {
exec := u.GetExecution(conf.GetConfig().ExecutionID)
if exec == nil {
logger.Fatal().Msg("Could not retrieve workflow ID from execution ID " + conf.GetConfig().ExecutionID + " on peer " + conf.GetConfig().PeerID)
oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), nil).UpdateOne(map[string]interface{}{
"state": enum.FAILURE.EnumIndex(),
}, conf.GetConfig().ExecutionID)
u.EmitExecStateUpdate(conf.GetConfig().ExecutionID, enum.FAILURE)
return
}
conf.GetConfig().WorkflowID = exec.WorkflowID
@@ -83,35 +77,42 @@ func main() {
if err != nil {
logger.Error().Msg("Could not retrieve workflow " + conf.GetConfig().WorkflowID + " from oc-catalog API")
}
fmt.Println("ExportToArgo")
builder, _, err := new_wf.ExportToArgo(exec, conf.GetConfig().Timeout) // Removed stepMax so far, I don't know if we need it anymore
fmt.Println("ExportToArgo", err)
if err != nil {
logger.Error().Msg("Could not create the Argo file for " + conf.GetConfig().WorkflowID)
logger.Error().Msg(err.Error())
oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), nil).UpdateOne(map[string]interface{}{
"state": enum.FAILURE.EnumIndex(),
}, exec.GetID())
u.EmitExecStateUpdate(exec.GetID(), enum.FAILURE)
return
}
fmt.Println("CompleteBuild")
argoFilePath, err := builder.CompleteBuild(exec.ExecutionsID)
fmt.Println("CompleteBuild", err)
if err != nil {
logger.Error().Msg("Error when completing the build of the workflow: " + err.Error())
oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), nil).UpdateOne(map[string]interface{}{
"state": enum.FAILURE.EnumIndex(),
}, exec.GetID())
u.EmitExecStateUpdate(exec.GetID(), enum.FAILURE)
return
}
workflowName = getContainerName(argoFilePath)
fmt.Println("getContainerName", workflowName, conf.GetConfig().KubeHost)
if conf.GetConfig().KubeHost == "" {
// Not in a k8s environment, get conf from parameters
panic("can't exec with no kube for argo deployment")
} else {
// Executed in a k8s environment
logger.Info().Msg("Executes inside a k8s")
// executeInside(exec.GetID(), "argo", argo_file_path, stepMax) // commenting to use conf.ExecutionID instead of exec.GetID()
// Wait until the scheduled start time if prep finished early.
if st := conf.GetConfig().ScheduledTime; !st.IsZero() && time.Now().Before(st) {
wait := time.Until(st)
logger.Info().Msgf("Prep done early, waiting %s until scheduled start %s", wait.Round(time.Second), st.Format(time.RFC3339))
u.EmitExecStateUpdate(exec.GetID(), enum.IN_PREPARATION)
time.Sleep(wait)
} else if st := conf.GetConfig().ScheduledTime; !st.IsZero() && time.Now().After(st) {
logger.Warn().Msgf("Prep finished %s late vs scheduled start %s", time.Since(st).Round(time.Second), st.Format(time.RFC3339))
}
fmt.Println("EXEC")
executeInside(exec.ExecutionsID, exec.GetID(), argoFilePath)
}
}
@@ -133,15 +134,7 @@ func executeInside(ns string, execID string, argo_file_path string) {
logger.Info().Msg(fmt.Sprint("Data :" + conf.GetConfig().KubeData))
return
} else {
watcher, err := t.GetArgoWatch(ns, workflowName)
if err != nil {
logger.Error().Msg("Could not retrieve Watcher : " + err.Error())
oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), nil).UpdateOne(map[string]interface{}{
"state": enum.FAILURE.EnumIndex(),
}, execID)
}
l.LogKubernetesArgo(name, execID, ns, ns, watcher)
l.LogKubernetesArgo(name, execID, ns, ns, t, workflowName)
logger.Info().Msg("Finished, exiting...")
}
@@ -149,9 +142,11 @@ func executeInside(ns string, execID string, argo_file_path string) {
func loadConfig(parser *argparse.Parser) {
mode := parser.String("M", "mode", &argparse.Options{Required: false, Default: "", Help: "Mode of the execution"})
ocNamespace := parser.String("n", "namespace", &argparse.Options{Required: false, Default: "opencloud", Help: "Kubernetes namespace where OpenCloud components (NATS) run"})
execution := parser.String("e", "execution", &argparse.Options{Required: true, Help: "Execution ID of the workflow to request from oc-catalog API"})
peer := parser.String("p", "peer", &argparse.Options{Required: false, Default: "", Help: "Peer ID of the workflow to request from oc-catalog API"})
timeout := parser.Int("t", "timeout", &argparse.Options{Required: false, Default: -1, Help: "Timeout for the execution of the workflow"})
scheduledUnix := parser.Int("s", "scheduled-time", &argparse.Options{Required: false, Default: 0, Help: "Unix timestamp of the scheduled start; oc-monitord will wait until this time before submitting the Argo workflow"})
ca := parser.String("c", "ca", &argparse.Options{Required: false, Default: "", Help: "CA file for the Kubernetes cluster"})
cert := parser.String("C", "cert", &argparse.Options{Required: false, Default: "", Help: "Cert file for the Kubernetes cluster"})
@@ -160,6 +155,7 @@ func loadConfig(parser *argparse.Parser) {
host := parser.String("H", "host", &argparse.Options{Required: false, Default: "", Help: "Host for the Kubernetes cluster"})
port := parser.String("P", "port", &argparse.Options{Required: false, Default: "6443", Help: "Port for the Kubernetes cluster"})
natsUrl := parser.String("N", "nats", &argparse.Options{Required: false, Default: "", Help: "Nats URL"})
// argoHost := parser.String("h", "argoHost", &argparse.Options{Required: false, Default: "", Help: "Host where Argo is running from"}) // can't use -h because its reserved to help
err := parser.Parse(os.Args)
@@ -172,7 +168,11 @@ func loadConfig(parser *argparse.Parser) {
conf.GetConfig().Mode = *mode
conf.GetConfig().ExecutionID = *execution
conf.GetConfig().PeerID = *peer
conf.GetConfig().OCNamespace = *ocNamespace
if *scheduledUnix > 0 {
conf.GetConfig().ScheduledTime = time.Unix(int64(*scheduledUnix), 0)
}
conf.GetConfig().NatsUrl = *natsUrl
conf.GetConfig().KubeHost = *host
conf.GetConfig().KubePort = *port