oc-schedulerd/argo_builder.go

121 lines
3.1 KiB
Go
Raw Normal View History

2024-05-02 09:52:28 +02:00
// A class that translates the informations held in the graph object
// via its lists of components into an argo file, using the a list of
// link ID to build the dag
package main
import (
"fmt"
2024-05-03 10:58:06 +02:00
"os"
2024-05-02 09:52:28 +02:00
"slices"
"github.com/beego/beego/v2/core/logs"
"gopkg.in/yaml.v3"
)
type ArgoBuilder struct {
2024-05-03 10:58:06 +02:00
graph Graph
2024-05-02 09:52:28 +02:00
branches [][]string
2024-05-03 10:58:06 +02:00
Workflow Workflow
2024-05-02 09:52:28 +02:00
}
2024-05-03 10:58:06 +02:00
type Workflow struct {
Templates []Template `yaml:"templates"`
2024-05-02 09:52:28 +02:00
}
func (b *ArgoBuilder) CreateDAG() bool {
fmt.Println("list of branches : ", b.branches)
b.createDAGstep()
2024-05-03 10:58:06 +02:00
yamlified, err := yaml.Marshal(b.Workflow)
2024-05-02 09:52:28 +02:00
if err != nil {
2024-05-03 10:58:06 +02:00
logs.Error("Could not transform object to yaml file")
2024-05-02 09:52:28 +02:00
}
fmt.Println(string(yamlified))
2024-05-03 10:58:06 +02:00
err = os.WriteFile("argo.yml", []byte(yamlified), 0660)
if err != nil {
logs.Error("Could not write the yaml file")
}
2024-05-02 09:52:28 +02:00
return true
}
func (b *ArgoBuilder) createDAGstep() {
2024-05-03 10:58:06 +02:00
new_dag := Dag{}
2024-05-02 09:52:28 +02:00
for _, comp := range b.graph.Computings{
unique_name := comp.Name + "_" + comp.ID
2024-05-03 10:58:06 +02:00
var comp_params []Parameter
comp_params = append(comp_params, Parameter{"toto","titi"})
step := Task{Name: unique_name, Template: unique_name}
step.Arguments.Parameters = append(step.Arguments.Parameters, comp_params...)
2024-05-02 09:52:28 +02:00
// For each branch, check if the computing has a dependency
for _, branch := range b.branches {
if b.componentInBranch(comp.ID,branch) {
// retrieves the name (computing.name_computing.ID)
dependency := b.getDependency(comp.ID,branch)
if dependency != "" && !slices.Contains(step.Dependencies,dependency) {
step.Dependencies = append(step.Dependencies,dependency)
}
}
}
2024-05-03 10:58:06 +02:00
new_dag.Tasks = append(new_dag.Tasks, step)
2024-05-02 09:52:28 +02:00
}
2024-05-03 10:58:06 +02:00
b.Workflow.Templates = append (b.Workflow.Templates, Template{Name: "dag", Dag: new_dag})
2024-05-02 09:52:28 +02:00
}
func (b *ArgoBuilder) getDependency (current_computing_id string, branch []string) string {
for i := len(branch)-1; i >= 0 ; i-- {
current_link := b.graph.Links[branch[i]]
current_computing_found := false
if current_link.Source == current_computing_id || current_link.Destination == current_computing_id {
current_computing_found = true
}
if current_computing_found {
return b.findPreviousComputing(current_computing_id,branch,i-1)
}
}
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 := b.graph.getComponentName(previousLink.Source) + "_" + previousLink.Source
return name
}
if previousLink.Destination != computing_id && b.graph.getComponentType(previousLink.Destination) == "computing"{
name := b.graph.getComponentName(previousLink.Destination) + "_" + previousLink.Destination
return name
}
}
return ""
}