Workin' Argo struct

This commit is contained in:
mr
2025-02-05 08:36:26 +01:00
parent a1d28f2563
commit a0b1117075
24 changed files with 459 additions and 119 deletions

34
main.go
View File

@@ -21,6 +21,7 @@ import (
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/akamensky/argparse"
"github.com/google/uuid"
@@ -38,7 +39,6 @@ import (
var logger zerolog.Logger
var wf_logger zerolog.Logger
var parser argparse.Parser
var monitorLocal bool
var workflowName string
const defaultConfigFile = "/etc/oc/ocmonitord_conf.json"
@@ -48,7 +48,6 @@ func main() {
os.Setenv("test_service", "true") // Only for service demo, delete before merging on main
monitorLocal = false
// Test if monitor is launched outside (with parameters) or in a k8s environment (env variables sets)
if os.Getenv("KUBERNETES_SERVICE_HOST") == "" {
// Not in a k8s environment, get conf from parameters
@@ -58,7 +57,6 @@ func main() {
} else {
// Executed in a k8s environment
fmt.Println("Executes inside a k8s")
monitorLocal = true
loadConfig(true, nil)
}
oclib.InitDaemon("oc-monitord")
@@ -89,7 +87,7 @@ func main() {
// // create argo
new_wf := workflow_builder.WorflowDB{}
err := new_wf.LoadFrom(conf.GetConfig().WorkflowID)
err := new_wf.LoadFrom(conf.GetConfig().WorkflowID, conf.GetConfig().PeerID)
if err != nil {
logger.Error().Msg("Could not retrieve workflow " + conf.GetConfig().WorkflowID + " from oc-catalog API")
}
@@ -112,7 +110,7 @@ func main() {
// Return the Workflow ID associated to a workflow execution object
func getWorkflowId(exec_id string) string {
res := oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), "", "", []string{}, nil).LoadOne(exec_id)
res := oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), "", conf.GetConfig().PeerID, []string{}, nil).LoadOne(exec_id)
if res.Code != 200 {
logger.Error().Msg("Could not retrieve workflow ID from execution ID " + exec_id)
return ""
@@ -145,7 +143,6 @@ func executeWorkflow(argo_file_path string, stepMax int) {
wf_logger.Error().Msg(err.Error() + bufio.NewScanner(stderr).Text())
updateStatus("fatal")
}
wg.Wait()
}
@@ -174,6 +171,10 @@ func logWorkflow(argo_file_path string, stepMax int, pipe io.ReadCloser, wg *syn
if err != nil {
logger.Error().Msg("Could not create watch log")
}
if strings.Contains(strings.ToLower(strings.Join(current_watch.Logs, " ")), "error") || strings.Contains(strings.ToLower(strings.ToLower(strings.Join(current_watch.Logs, " "))), "err") {
current_watch.Status = "Failed"
}
if current_watch.Status == "Failed" {
wf_logger.Error().Msg(string(jsonified))
} else {
@@ -188,13 +189,8 @@ func logWorkflow(argo_file_path string, stepMax int, pipe io.ReadCloser, wg *syn
}
func loadConfig(is_k8s bool, parser *argparse.Parser) {
var o *onion.Onion
o = initOnion(o)
// These variables can only be retrieved in the onion
// Variables that don't depend on the environment (from conf file), can be loaded after
// We can't use underscore in the env variable names because it's the delimitor with OCMONITOR too
setConf(is_k8s, o, parser)
if !IsValidUUID(conf.GetConfig().ExecutionID) {
@@ -212,6 +208,7 @@ func setConf(is_k8s bool, o *onion.Onion, parser *argparse.Parser) {
logger.Error().Msg("Could not parse timeout, using default value")
}
conf.GetConfig().ExecutionID = o.GetString("workflow")
conf.GetConfig().PeerID = o.GetString("peer")
mongo := o.GetStringDefault("mongourl", "mongodb://127.0.0.1:27017")
db := o.GetStringDefault("database", "DC_myDC")
@@ -220,6 +217,7 @@ func setConf(is_k8s bool, o *onion.Onion, parser *argparse.Parser) {
} else {
url := parser.String("u", "url", &argparse.Options{Required: true, Default: "http://127.0.0.1:3100", Help: "Url to the Loki database logs will be sent to"})
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"})
mongo := parser.String("m", "mongo", &argparse.Options{Required: true, Default: "mongodb://127.0.0.1:27017", Help: "URL to reach the MongoDB"})
db := parser.String("d", "database", &argparse.Options{Required: true, Default: "DC_myDC", Help: "Name of the database to query in MongoDB"})
timeout := parser.Int("t", "timeout", &argparse.Options{Required: false, Default: -1, Help: "Timeout for the execution of the workflow"})
@@ -228,14 +226,13 @@ func setConf(is_k8s bool, o *onion.Onion, parser *argparse.Parser) {
fmt.Println(parser.Usage(err))
os.Exit(1)
}
conf.GetConfig().Logs = "debug"
conf.GetConfig().LokiURL = *url
conf.GetConfig().MongoURL = *mongo
conf.GetConfig().Database = *db
conf.GetConfig().Timeout = *timeout
conf.GetConfig().ExecutionID = *execution
conf.GetConfig().PeerID = *peer
}
}
@@ -293,11 +290,10 @@ func updateStatus(status string) {
wf_exec := &workflow_execution.WorkflowExecutions{AbstractObject: utils.AbstractObject{UUID: conf.GetConfig().ExecutionID}}
wf_exec.ArgoStatusToState(status)
res := oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), "", "", []string{}, nil).UpdateOne(
wf_exec.Serialize(wf_exec), exec_id)
if res.Code != 200 {
logger.Error().Msg("Could not update status for workflow execution " + exec_id)
_, _, err := workflow_execution.NewAccessor(&tools.APIRequest{
PeerID: conf.GetConfig().PeerID,
}).UpdateOne(wf_exec, exec_id)
if err != nil {
logger.Error().Msg("Could not update status for workflow execution " + exec_id + err.Error())
}
}