Minimize code + docker + monitord naming
This commit is contained in:
@@ -5,14 +5,12 @@
|
||||
package workflow_builder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"oc-monitor/conf"
|
||||
"oc-monitor/logger"
|
||||
. "oc-monitor/models"
|
||||
. "oc-monitord/models"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
oclib "cloud.o-forge.io/core/oc-lib"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resource_model"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/workflow/graph"
|
||||
"github.com/nwtgck/go-fakelish"
|
||||
@@ -41,30 +39,29 @@ type Spec struct {
|
||||
}
|
||||
|
||||
func (b *ArgoBuilder) CreateDAG() (string, error) {
|
||||
|
||||
|
||||
b.createTemplates()
|
||||
b.createDAGstep()
|
||||
b.createVolumes()
|
||||
b.Workflow.Spec.Entrypoint = "dag"
|
||||
|
||||
b.Workflow.ApiVersion = "argoproj.io/v1alpha1"
|
||||
b.Workflow.Kind = "Workflow"
|
||||
random_name := generateWfName()
|
||||
b.Workflow.Metadata.Name = "oc-monitor-" + random_name
|
||||
|
||||
logger := oclib.GetLogger()
|
||||
yamlified, err := yaml.Marshal(b.Workflow)
|
||||
if err != nil {
|
||||
logger.Logger.Error().Msg("Could not transform object to yaml file")
|
||||
logger.Error().Msg("Could not transform object to yaml file")
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Give a unique name to each argo file with its timestamp DD:MM:YYYY_hhmmss
|
||||
current_timestamp := time.Now().Format("02_01_2006_150405")
|
||||
file_name := random_name + "_" + current_timestamp + ".yml"
|
||||
workflows_dir := conf.GetConfig().MonitorDir+"/argo_workflows/"
|
||||
workflows_dir := "./argo_workflows/"
|
||||
err = os.WriteFile(workflows_dir+file_name, []byte(yamlified), 0660)
|
||||
if err != nil {
|
||||
logger.Logger.Error().Msg("Could not write the yaml file")
|
||||
logger.Error().Msg("Could not write the yaml file")
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -72,11 +69,10 @@ func (b *ArgoBuilder) CreateDAG() (string, error) {
|
||||
}
|
||||
|
||||
func (b *ArgoBuilder) createTemplates() {
|
||||
|
||||
for _, comp := range b.getProcessings() {
|
||||
var command string
|
||||
var args string
|
||||
var env string
|
||||
var args string
|
||||
var env string
|
||||
|
||||
comp_res := comp.Processing
|
||||
|
||||
@@ -107,13 +103,9 @@ func (b *ArgoBuilder) createTemplates() {
|
||||
}
|
||||
|
||||
func (b *ArgoBuilder) createDAGstep() {
|
||||
|
||||
new_dag := Dag{}
|
||||
|
||||
for _, comp := range b.getProcessings() {
|
||||
|
||||
comp_res := comp.Processing
|
||||
|
||||
env := getStringValue(comp_res.AbstractResource, "env")
|
||||
unique_name := getArgoName(comp_res.GetName(), comp.ID)
|
||||
step := Task{Name: unique_name, Template: unique_name}
|
||||
@@ -124,12 +116,9 @@ func (b *ArgoBuilder) createDAGstep() {
|
||||
}
|
||||
|
||||
// retrieves the name (computing.name-computing.ID)
|
||||
step.Dependencies = b.getDependency(comp.ID) // Error : we use the component ID instead of the GraphItem ID -> store objects
|
||||
|
||||
step.Dependencies = b.getDependency(comp.ID) // Error : we use the component ID instead of the GraphItem ID -> store objects
|
||||
new_dag.Tasks = append(new_dag.Tasks, step)
|
||||
|
||||
}
|
||||
|
||||
b.Workflow.Spec.Templates = append(b.Workflow.Spec.Templates, Template{Name: "dag", Dag: new_dag})
|
||||
|
||||
}
|
||||
@@ -140,69 +129,36 @@ func (b *ArgoBuilder) createVolumes() {
|
||||
new_volume.Metadata.Name = "workdir"
|
||||
new_volume.Spec.AccessModes = []string{"ReadWriteOnce"}
|
||||
new_volume.Spec.Resources.Requests.Storage = "1Gi"
|
||||
|
||||
b.Workflow.Spec.Volumes = append(b.Workflow.Spec.Volumes, new_volume)
|
||||
}
|
||||
|
||||
func (b *ArgoBuilder) getDependency(current_computing_id string) (dependencies []string) {
|
||||
|
||||
for _, link := range b.graph.Links {
|
||||
source := b.graph.Items[link.Source.ID].Processing
|
||||
if !b.IsProcessing(link.Source.ID) || !b.IsProcessing(link.Destination.ID) {
|
||||
continue
|
||||
}
|
||||
source := b.graph.Items[link.Source.ID].Processing
|
||||
if current_computing_id == link.Destination.ID && source != nil {
|
||||
dependency_name := getArgoName(source.GetName(), link.Source.ID)
|
||||
dependencies = append(dependencies, dependency_name)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
// func (b *ArgoBuilder) componentInBranch(component_id string, branch []string) bool {
|
||||
// for _, link := range branch {
|
||||
// if b.graph.Links[link].Source == component_id || b.graph.Links[link].Destination == component_id {
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
|
||||
// func (b *ArgoBuilder) findPreviousComputing(computing_id string, branch []string, index int) string {
|
||||
|
||||
// for i := index; i >= 0 ; i-- {
|
||||
// previousLink := b.graph.Links[branch[i]]
|
||||
|
||||
// if previousLink.Source != computing_id && b.graph.GetComponentType(previousLink.Source) == "computing"{
|
||||
// name := getArgoName(b.graph.getComponentName(previousLink.Source),previousLink.Source)
|
||||
// return name
|
||||
// }
|
||||
// if previousLink.Destination != computing_id && b.graph.GetComponentType(previousLink.Destination) == "computing"{
|
||||
// name := getArgoName(b.graph.getComponentName(previousLink.Destination),previousLink.Destination)
|
||||
// return name
|
||||
// }
|
||||
|
||||
// }
|
||||
// return ""
|
||||
// }
|
||||
|
||||
func getComputingCommands(user_input string) (list_command []string) {
|
||||
func getComputingCommands(user_input string) []string {
|
||||
user_input = removeImageName(user_input)
|
||||
if len(user_input) == 0 {
|
||||
return
|
||||
return []string{}
|
||||
}
|
||||
|
||||
list_command = strings.Split(user_input, " ")
|
||||
for i := range list_command {
|
||||
list_command[i] = list_command[i]
|
||||
}
|
||||
return
|
||||
return strings.Split(user_input, " ")
|
||||
}
|
||||
|
||||
func getComputingArgs(user_input []string, command string) (list_args []string) {
|
||||
if len(user_input) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// quickfix that might need improvement
|
||||
if strings.Contains(command, "sh -c") {
|
||||
list_args = append(list_args, strings.Join(user_input, " "))
|
||||
@@ -210,13 +166,12 @@ func getComputingArgs(user_input []string, command string) (list_args []string)
|
||||
}
|
||||
|
||||
list_args = append(list_args, user_input...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Currently implements code to overcome problems in data structure
|
||||
func getComputingEnvironment(user_input []string) (map_env map[string]string) {
|
||||
|
||||
logger := oclib.GetLogger()
|
||||
is_empty := len(user_input) == 0
|
||||
is_empty_string := len(user_input) == 1 && user_input[0] == ""
|
||||
|
||||
@@ -234,7 +189,7 @@ func getComputingEnvironment(user_input []string) (map_env map[string]string) {
|
||||
new_pair := strings.Split(str, "=")
|
||||
|
||||
if len(new_pair) != 2 {
|
||||
logger.Logger.Error().Msg("Error extracting the environment variable from " + str)
|
||||
logger.Error().Msg("Error extracting the environment variable from " + str)
|
||||
panic(0)
|
||||
}
|
||||
|
||||
@@ -265,15 +220,6 @@ func getArgoName(raw_name string, component_id string) (formatedName string) {
|
||||
return
|
||||
}
|
||||
|
||||
func printYAML(data interface{}) {
|
||||
yamlData, err := yaml.Marshal(data)
|
||||
if err != nil {
|
||||
fmt.Printf("Error marshalling YAML: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(yamlData))
|
||||
}
|
||||
|
||||
func removeImageName(user_input string) string {
|
||||
// First command is the name of the container for now
|
||||
if len(strings.Split(user_input, " ")) == 1 {
|
||||
@@ -297,10 +243,18 @@ func (b *ArgoBuilder) getProcessings() (list_computings []graph.GraphItem) {
|
||||
return
|
||||
}
|
||||
|
||||
func getStringValue(comp resource_model.AbstractResource, key string) string {
|
||||
if res := comp.GetModelValue(key); res != nil {
|
||||
return res.(string)
|
||||
}
|
||||
return ""
|
||||
func (b *ArgoBuilder) IsProcessing(id string) bool {
|
||||
for _, item := range b.graph.Items {
|
||||
if item.Processing != nil && item.Processing.GetID() == id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getStringValue(comp resource_model.AbstractResource, key string) string {
|
||||
if res := comp.GetModelValue(key); res != nil {
|
||||
return res.(string)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user