oc-monitord/main.go

115 lines
3.6 KiB
Go
Raw Normal View History

2024-07-04 09:14:25 +02:00
package main
import (
"log"
"os"
"oc-monitor/conf"
"cloud.o-forge.io/core/oc-lib/logs"
"github.com/akamensky/argparse"
"github.com/goraz/onion"
"github.com/rs/zerolog"
2024-07-04 09:14:25 +02:00
)
var logger zerolog.Logger
var parser argparse.Parser
const defaultConfigFile = "/etc/oc/ocmonitor_conf.json"
const localConfigFile = "./conf/ocmonitor_conf.json"
2024-07-04 09:14:25 +02:00
func main() {
// 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
parser = *argparse.NewParser("oc-monitor","Launch the execution of a workflow given as a parameter and sends the produced logs to a loki database")
loadConfig(false, &parser)
} else {
// Executed in a k8s environment
loadConfig(true,nil)
2024-07-04 09:14:25 +02:00
}
logger = logs.CreateLogger("oc-monitor", conf.GetConfig().LokiURL)
logger.Debug().Msg("Loki URL : " + conf.GetConfig().LokiURL)
logger.Debug().Msg("Filename : " + conf.GetConfig().ArgoFile)
logger.Debug().Msg("Container Name : " + conf.GetConfig().ContainerName)
// Wait for the argo file to be copied to the pod
wf_found := false
for(!wf_found){
if _, err := os.Stat("./workflows/" + conf.GetConfig().ArgoFile); err == nil {
wf_found = true
}
2024-07-04 09:14:25 +02:00
}
logger.Debug().Msg("Submitting the argo workflow : " + conf.GetConfig().ArgoFile)
// // Initialize LokiLogger
// lokiLogger := NewLokiLogger("http://localhost:3100/loki/api/v1/push") // Replace with your Loki URL
// Run the Argo command
// cmd := exec.Command("argo", "submit", "your-workflow.yaml")
// output, err := cmd.CombinedOutput()
// if err != nil {
// log.Fatalf("failed to run Argo command: %v", err)
// }
// logger.Info().Msg(string(output))
// // Send logs to Loki
// if err := lokiLogger.Log(`{job="argo"}`, string(output)); err != nil {
// log.Fatalf("failed to send logs to Loki: %v", err)
// }
2024-07-04 09:14:25 +02:00
log.Println("Logs sent to Loki successfully.")
}
func loadConfig(is_k8s bool, parser *argparse.Parser){
var o *onion.Onion
logger = logs.CreateLogger("oc-monitor","")
configFile := ""
l3 := onion.NewEnvLayerPrefix("_", "OCMONITOR")
l2, err := onion.NewFileLayer(defaultConfigFile, nil)
if err == nil {
logger.Info().Msg("Config file found : " + defaultConfigFile)
configFile = defaultConfigFile
}
l1, err := onion.NewFileLayer(localConfigFile, nil)
if err == nil {
logger.Info().Msg("Local config file found " + localConfigFile + ", overriding default file")
configFile = localConfigFile
}
if configFile == "" {
logger.Info().Msg("No config file found, using env")
o = onion.New(l3)
} else if l1 == nil && l2 == nil {
o = onion.New(l1, l2, l3)
} else if l1 == nil {
o = onion.New(l2, l3)
} else if l2 == nil {
o = onion.New(l1, l3)
}
// These variables can only be retrieved in the onion
// Variables that don't depend on the environmen (from conf file), can be loaded after
if (is_k8s){
// We can't use underscore in the env variable names because it's the delimitor with OCMONITOR too
conf.GetConfig().LokiURL = o.GetStringDefault("lokiurl", "http://127.0.0.1:3100")
conf.GetConfig().ArgoFile = o.GetString("argofile")
conf.GetConfig().ContainerName = o.GetString("containername")
} else{
url := parser.String("u", "url", &argparse.Options{Required: true,Default: "http://127.0.0.1:3100"})
conf.GetConfig().LokiURL = *url
file := parser.String("f", "file", &argparse.Options{Required: true})
conf.GetConfig().ArgoFile = *file
name := parser.String("n", "name", &argparse.Options{Required: true})
conf.GetConfig().ContainerName = *name
err := parser.Parse(os.Args)
if err != nil {
logger.Fatal().Msg(parser.Usage(err))
}
}
}