oc-monitord/models/template.go

112 lines
3.1 KiB
Go
Raw Normal View History

package models
2024-10-11 13:44:16 +02:00
import (
"strings"
"cloud.o-forge.io/core/oc-lib/models/resources/processing"
"cloud.o-forge.io/core/oc-lib/models/resources/storage"
)
type Parameter struct {
Name string `yaml:"name,omitempty"`
Value string `yaml:"value,omitempty"`
}
type Container struct {
Image string `yaml:"image"`
Command []string `yaml:"command,omitempty,flow"`
Args []string `yaml:"args,omitempty,flow"`
VolumeMounts []VolumeMount `yaml:"volumeMounts,omitempty"`
}
2024-10-11 13:44:16 +02:00
func (c *Container) AddVolumeMount(volumeMount VolumeMount, volumes []VolumeMount) []VolumeMount {
for _, vm := range c.VolumeMounts {
if vm.Name == volumeMount.Name {
return volumes
}
}
c.VolumeMounts = append(c.VolumeMounts, volumeMount)
for _, vm := range c.VolumeMounts {
for _, v := range volumes {
if vm.Name == v.Name {
return volumes
}
}
}
volumes = append(volumes, volumeMount)
return volumes
}
type VolumeMount struct {
2024-10-11 13:44:16 +02:00
Name string `yaml:"name"`
MountPath string `yaml:"mountPath"`
Storage *storage.StorageResource `yaml:"-"`
}
type Task struct {
Name string `yaml:"name"`
Template string `yaml:"template"`
Dependencies []string `yaml:"dependencies,omitempty"`
Arguments struct {
Parameters []Parameter `yaml:"parameters,omitempty"`
} `yaml:"arguments,omitempty"`
}
type Dag struct {
Tasks []Task `yaml:"tasks,omitempty"`
}
type TemplateMetadata struct {
Labels map[string]string `yaml:"labels,omitempty"`
}
type Template struct {
Name string `yaml:"name"`
Inputs struct {
Parameters []Parameter `yaml:"parameters"`
} `yaml:"inputs,omitempty"`
2024-10-11 13:44:16 +02:00
Container Container `yaml:"container,omitempty"`
Dag *Dag `yaml:"dag,omitempty"`
Metadata TemplateMetadata `yaml:"metadata,omitempty"`
2024-10-11 13:44:16 +02:00
Resource ServiceResource `yaml:"resource,omitempty"`
}
func (template *Template) CreateContainer(processing *processing.ProcessingResource, dag *Dag) {
container := Container{Image: processing.Container.Image}
if container.Image == "" {
return
}
container.Command = []string{"sh", "-c"} // all is bash
for name := range processing.Container.Env {
template.Inputs.Parameters = append(template.Inputs.Parameters, Parameter{Name: name})
}
for _, a := range strings.Split(processing.Container.Args, " ") {
container.Args = append(container.Args, template.replacePerEnv(a, processing.Container.Env, dag))
}
cmd := strings.ReplaceAll(processing.Container.Command, container.Image, "")
container.Args = []string{cmd + " " + strings.Join(container.Args, " ")}
template.Container = container
}
func (template *Template) replacePerEnv(arg string, envs map[string]string, dag *Dag) string {
for k, v := range envs {
if strings.Contains(arg, k) {
value := v
for _, task := range dag.Tasks {
if task.Name == template.Name {
for _, p := range task.Arguments.Parameters {
if p.Name == k {
value = p.Value
break
}
}
}
}
arg = strings.ReplaceAll(arg, "$"+k, value)
arg = strings.ReplaceAll(arg, "${"+k+"}", value)
arg = strings.ReplaceAll(arg, k, value)
}
}
return arg
}