refactor for better readability

This commit is contained in:
pb 2024-09-03 15:57:14 +02:00
parent bbedc53133
commit ea44ca33b2

View File

@ -8,7 +8,6 @@ import (
. "oc-monitord/models"
"os"
"slices"
"strconv"
"strings"
"time"
@ -18,8 +17,6 @@ import (
w "cloud.o-forge.io/core/oc-lib/models/workflow"
"github.com/nwtgck/go-fakelish"
"github.com/rs/zerolog"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"gopkg.in/yaml.v3"
)
@ -322,158 +319,6 @@ func (b *ArgoBuilder) isService(id string) bool{
return is_exposed
}
func (b *ArgoBuilder) CreateService(processing graph.GraphItem) Service{
// model {
// Type : "dict",
// Value : {
// "80" : {
// "reverse" : "",
// "PAT" : "34000"
// },
// "344" : {
// "reverse" : "",
// "PAT" : "34400"
// }
// }
// }
new_service := Service{APIVersion: "v1",
Kind: "Service",
Metadata: Metadata{
Name: "workflow-service" ,
},
Spec: ServiceSpec{
Selector: map[string]string{"app": "oc-service"},
Ports: []ServicePort{
},
Type: "NodePort",
},
}
completeServicePorts(&new_service, processing)
yamlified, _ := yaml.Marshal(new_service)
x := string(yamlified)
_ = x
return new_service
}
func completeServicePorts(service *Service, processing graph.GraphItem) {
contract := getExposeContract(processing.Processing.ResourceModel.Model["expose"])
for str_port,translation_dict := range contract{
port, err := strconv.ParseInt(str_port, 10, 64)
if err != nil {
logger.Error().Msg("Could not convert " + str_port + "to an int")
return
}
if _, ok := translation_dict["PAT"]; ok{
port_translation, err := strconv.ParseInt(translation_dict["PAT"], 10, 64)
if err != nil {
logger.Error().Msg("Could not convert " + translation_dict["PAT"] + "to an int")
return
}
new_port_translation := ServicePort{
Name: strings.ToLower(processing.Processing.Name) + processing.ID,
Port: port_translation-30000,
TargetPort: port,
NodePort: port_translation,
Protocol: "TCP",
}
service.Spec.Ports = append(service.Spec.Ports, new_port_translation)
}
}
return
}
// TODO : refactor this method or the deserialization process in oc-lib to get rid of the mongo code
func getExposeContract(expose resource_model.Model) map[string]map[string]string {
contract := make(map[string]map[string]string,0)
mapped_info := bson.M{}
// var contract PortTranslation
_ , byt, _ := bson.MarshalValue(expose.Value)
bson.Unmarshal(byt,&mapped_info)
for _,v := range mapped_info {
port := v.(primitive.M)["Key"].(string)
// exposed_port := map[string]interface{}{data["Key"] : ""}
port_translation := v.(primitive.M)["Value"]
contract[port] = map[string]string{}
for _,v2 := range port_translation.(primitive.A) {
if v2.(primitive.M)["Key"] == "reverse" {
contract[port]["reverse"] = v2.(primitive.M)["Value"].(string)
}
if v2.(primitive.M)["Key"] == "PAT" {
contract[port]["PAT"] = v2.(primitive.M)["Value"].(string)
}
}
}
return contract
}
// func getPortsFromModel(model map[string]resource_model.Model) (data []int) {
// defer func() { // recover the panic
// if r := recover(); r != nil {
// for _, v := range model["expose"].Value.(map[string]interface{}) {
// subMap := v.(map[string]interface{})
// for k2, v2 := range subMap {
// if k2 == "PAT" {
// data = append(data, v2.(int))
// }
// }
// }
// }
// }()
// expose := model["expose"].Value
// // sub := expose.([]primitive.A)
// for _, item := range expose.(primitive.A) {
// if doc, ok := item.(primitive.D); ok {
// for v,k := range doc{
// key := k.Key
// valueMap := make(map[string]interface{})
// if nestedArray, ok := elem.Value.(primitive.A); ok {
// for _, nestedItem := range nestedArray {
// if nestedDoc, ok := nestedItem.(primitive.D); ok {
// for _, nestedElem := range nestedDoc {
// valueMap[nestedElem.Key] = nestedElem.Value
// }
// }
// }
// }
// }
// }
// }
// return
// }
func (b *ArgoBuilder) createService(service Service, processing_name string, processing_id string) {
if b.Services != nil{
b.Services.Spec.Ports = append(b.Services.Spec.Ports, service.Spec.Ports...)
}else {
b.Services = &service
}
b.addLabel(processing_name,processing_id)
}
func (b *ArgoBuilder) addLabel(name string, id string) {
argo_name := getArgoName(name,id)
@ -485,23 +330,3 @@ func (b *ArgoBuilder) addLabel(name string, id string) {
}
}
func (b *ArgoBuilder) addServiceToArgo() error {
service_manifest, err := yaml.Marshal(b.Services)
if err != nil {
logger.Error().Msg("Could not marshal service manifest")
return err
}
service_template := Template{Name: "workflow-service-pod",
Resource: ServiceResource{
Action: "create",
SuccessCondition: "status.succeeded > 0",
FailureCondition: "status.failed > 3",
SetOwnerReference: true,
Manifest: string(service_manifest),
},
}
b.Workflow.Spec.Templates = append(b.Workflow.Spec.Templates, service_template)
return nil
}