diff --git a/argo_builder.go b/argo_builder.go index ba32291..31e0935 100644 --- a/argo_builder.go +++ b/argo_builder.go @@ -11,6 +11,7 @@ import ( "strings" "github.com/beego/beego/v2/core/logs" + "github.com/nwtgck/go-fakelish" "gopkg.in/yaml.v3" ) @@ -21,16 +22,33 @@ type ArgoBuilder struct { } type Workflow struct { - Templates []Template `yaml:"templates"` + ApiVersion string `yaml:"apiVersion"` + Kind string `yaml:"kind"` + Metadata struct { + GenerateName string `yaml:"generateName"` + } `yaml:"metadata"` + Spec Spec `yaml:"spec,omitempty"` } - +type Spec struct { + Entrypoint string `yaml:"entrypoint"` + Arguments []Parameter `yaml:"arguments,omitempty"` + Volumes []VolumeClaimTemplate `yaml:"volumeClaimTemplates,omitempty"` + Templates []Template `yaml:"templates"` +} func (b *ArgoBuilder) CreateDAG() bool { fmt.Println("list of branches : ", b.branches) b.createTemplates() b.createDAGstep() + b.createVolumes() + b.Workflow.Spec.Entrypoint = "dag" + + b.Workflow.ApiVersion = "argoproj.io/v1alpha1" + b.Workflow.Kind = "Workflow" + b.Workflow.Metadata.GenerateName = "oc-test-" + generateName() + yamlified, err := yaml.Marshal(b.Workflow) if err != nil { @@ -39,7 +57,7 @@ func (b *ArgoBuilder) CreateDAG() bool { } fmt.Println(string(yamlified)) err = os.WriteFile("argo.yml", []byte(yamlified), 0660) - if err != nil { + if err != nil { logs.Error("Could not write the yaml file") return false } @@ -61,7 +79,8 @@ func (b *ArgoBuilder) createTemplates() { new_temp := Template{Name: comp.Name + "_" + comp.ID, Container: temp_container} new_temp.Inputs.Parameters = inputs_container - b.Workflow.Templates = append(b.Workflow.Templates, new_temp) + new_temp.Container.VolumeMounts = append(new_temp.Container.VolumeMounts, VolumeMount{Name: "workdir",MountPath: "/mnt/vol"}) // TODO : replace this with a search of the storage / data source name + b.Workflow.Spec.Templates = append(b.Workflow.Spec.Templates, new_temp) } @@ -94,11 +113,21 @@ func (b *ArgoBuilder) createDAGstep() { } - b.Workflow.Templates = append (b.Workflow.Templates, Template{Name: "dag", Dag: new_dag}) + b.Workflow.Spec.Templates = append (b.Workflow.Spec.Templates, Template{Name: "dag", Dag: new_dag}) } -func (b *ArgoBuilder) getDependency (current_computing_id string, branch []string) string { +func (b *ArgoBuilder) createVolumes() { + // For testing purposes we only declare one volume, mounted in each computing + new_volume := VolumeClaimTemplate{} + 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, branch []string) string { for i := len(branch)-1; i >= 0 ; i-- { current_link := b.graph.Links[branch[i]] @@ -196,4 +225,37 @@ func getComputingEnvironmentName(user_input []string) (list_names []string){ } return +} + +func generateName() (Name string){ + Name = fakelish.GenerateFakeWord(5, 8) + "-" + fakelish.GenerateFakeWord(5, 8) + return +} + + +func testDoubleIndent(w Workflow) { + for _, temp := range w.Spec.Templates{ + if temp.Name == "dag" { + tasks := temp.Dag.Tasks + fmt.Println("name") + printYAML(tasks[0].Name) + fmt.Println("template") + printYAML(tasks[0].Template) + fmt.Println("dependencies") + printYAML(tasks[0].Dependencies) + fmt.Println("arguments") + printYAML(tasks[0].Arguments) + + + } + } +} + +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)) } \ No newline at end of file diff --git a/go.mod b/go.mod index ffcb3b4..96a72ba 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ require ( cloud.o-forge.io/core/oc-catalog v0.0.0-20240416165405-9bd5be775813 github.com/beego/beego/v2 v2.2.0 github.com/goraz/onion v0.1.3 + github.com/nwtgck/go-fakelish v0.1.3 github.com/rs/zerolog v1.32.0 - github.com/sbabiv/xml2map v1.2.1 github.com/tidwall/gjson v1.17.1 gopkg.in/yaml.v3 v3.0.1 ) @@ -35,6 +35,7 @@ require ( github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect + github.com/ugorji/go/codec v1.1.7 // indirect github.com/vk496/cron v1.2.0 // indirect github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc // indirect diff --git a/template_models.go b/template_models.go index 2829717..03a2d7d 100644 --- a/template_models.go +++ b/template_models.go @@ -1,7 +1,7 @@ package main type Parameter struct { - Name string `yaml:"name"` + Name string `yaml:"name,omitempty"` Value string `yaml:"value,omitempty"` } diff --git a/volume_models.go b/volume_models.go new file mode 100644 index 0000000..8396ada --- /dev/null +++ b/volume_models.go @@ -0,0 +1,19 @@ +package main + + + +type VolumeClaimTemplate struct { + Metadata struct { + Name string `yaml:"name"` + } `yaml:"metadata"` + Spec VolumeSpec `yaml:"spec"` +} + +type VolumeSpec struct { + AccessModes []string `yaml:"accessModes,flow"` + Resources struct { + Requests struct { + Storage string `yaml:"storage"` + } `yaml:"requests"` + } `yaml:"resources"` +} \ No newline at end of file