93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
package models
|
|
|
|
import (
|
|
"strings"
|
|
|
|
w "cloud.o-forge.io/core/oc-lib/models/workflow"
|
|
)
|
|
|
|
type WorkflowsDependancies struct {
|
|
FirstWfTasks map[string][]string
|
|
RelatedWfTasks map[string][]string
|
|
LastWfTasks map[string][]string
|
|
}
|
|
|
|
func NewWorkflowDependancies() *WorkflowsDependancies {
|
|
return &WorkflowsDependancies{
|
|
FirstWfTasks: map[string][]string{},
|
|
RelatedWfTasks: map[string][]string{},
|
|
LastWfTasks: map[string][]string{},
|
|
}
|
|
}
|
|
|
|
func (w *WorkflowsDependancies) BindFirstTasks(depsFunc func(v string) []w.Deps, dag *Dag) {
|
|
for wfID, firstTasks := range w.FirstWfTasks {
|
|
deps := depsFunc(wfID)
|
|
if task := dag.GetTask(wfID); task != nil && len(deps) > 0 {
|
|
task.Dependencies = append(task.Dependencies, firstTasks...)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (w *WorkflowsDependancies) BindRelatedTasks(dag *Dag) {
|
|
for wfID, relatedWfTasks := range w.RelatedWfTasks {
|
|
for _, dep := range relatedWfTasks {
|
|
if task := dag.GetTask(dep); task != nil {
|
|
index := -1
|
|
if i, deps := task.GetDeps(wfID); deps != "" {
|
|
index = i
|
|
}
|
|
if index != -1 {
|
|
task.Dependencies = append(task.Dependencies[:index], task.Dependencies[index+1:]...)
|
|
}
|
|
if w.LastWfTasks[wfID] != nil {
|
|
task.Dependencies = append(task.Dependencies, w.LastWfTasks[wfID]...)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
type Workflow struct {
|
|
ApiVersion string `yaml:"apiVersion"`
|
|
Kind string `yaml:"kind"`
|
|
Metadata struct {
|
|
Name string `yaml:"name"`
|
|
} `yaml:"metadata"`
|
|
Spec Spec `yaml:"spec,omitempty"`
|
|
}
|
|
|
|
func (b *Workflow) GetDag() *Dag {
|
|
for _, t := range b.Spec.Templates {
|
|
if t.Name == "dag" {
|
|
return t.Dag
|
|
}
|
|
}
|
|
b.Spec.Templates = append(b.Spec.Templates, Template{Name: "dag", Dag: &Dag{}})
|
|
return b.Spec.Templates[len(b.Spec.Templates)-1].Dag
|
|
}
|
|
|
|
type Spec struct {
|
|
ServiceAccountName string `yaml:"serviceAccountName"`
|
|
Entrypoint string `yaml:"entrypoint"`
|
|
Arguments []Parameter `yaml:"arguments,omitempty"`
|
|
Volumes []VolumeClaimTemplate `yaml:"volumeClaimTemplates,omitempty"`
|
|
Templates []Template `yaml:"templates"`
|
|
Timeout int `yaml:"activeDeadlineSeconds,omitempty"`
|
|
}
|
|
|
|
func GetArgoName(raw_name string, component_id string) (formatedName string) {
|
|
formatedName = strings.ReplaceAll(raw_name, " ", "-")
|
|
formatedName += "-" + component_id
|
|
formatedName = strings.ToLower(formatedName)
|
|
return
|
|
}
|
|
|
|
func TransformDepsToArgo(deps []w.Deps) []string {
|
|
argoDeps := []string{}
|
|
for _, dep := range deps {
|
|
argoDeps = append(argoDeps, GetArgoName(dep.Source, dep.Dest))
|
|
}
|
|
return argoDeps
|
|
}
|