mirror of
https://github.com/onepanelio/onepanel.git
synced 2025-10-09 23:50:07 +08:00
feat: Updated functions that worked with workflow templates to be methods and renamed them a bit for simplicity.
This commit is contained in:
135
pkg/types.go
135
pkg/types.go
@@ -176,9 +176,7 @@ func (wt *WorkflowTemplate) FormatManifest() (string, error) {
|
||||
}
|
||||
manifestMap.PruneEmpty()
|
||||
|
||||
if wt.ArgoWorkflowTemplate != nil {
|
||||
AddWorkflowTemplateParametersFromAnnotations(manifestMap, wt.ArgoWorkflowTemplate.Annotations)
|
||||
}
|
||||
wt.AddWorkflowTemplateParametersFromAnnotations(manifestMap)
|
||||
|
||||
manifestBytes, err := manifestMap.ToYamlBytes()
|
||||
if err != nil {
|
||||
@@ -192,6 +190,79 @@ func (wt *WorkflowTemplate) FormatManifest() (string, error) {
|
||||
return string(manifestBytes), nil
|
||||
}
|
||||
|
||||
// Take the manifest from the workflow template, which is just the "spec" contents
|
||||
// and wrap it so we have
|
||||
// {
|
||||
// metadata: {},
|
||||
// spec: spec_data
|
||||
// }
|
||||
// the above wrapping is what is returned.
|
||||
func (wt *WorkflowTemplate) WrapSpec() ([]byte, error) {
|
||||
data := wt.GetManifestBytes()
|
||||
|
||||
mapping := make(map[interface{}]interface{})
|
||||
|
||||
if err := yaml.Unmarshal(data, mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
contentMap := map[interface{}]interface{}{
|
||||
"metadata": make(map[interface{}]interface{}),
|
||||
"spec": mapping,
|
||||
}
|
||||
|
||||
finalBytes, err := yaml.Marshal(contentMap)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return finalBytes, nil
|
||||
}
|
||||
|
||||
func (wt *WorkflowTemplate) AddWorkflowTemplateParametersFromAnnotations(spec mapping.Mapping) {
|
||||
if wt.ArgoWorkflowTemplate == nil {
|
||||
return
|
||||
}
|
||||
|
||||
annotations := wt.ArgoWorkflowTemplate.Annotations
|
||||
if spec == nil || len(annotations) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
arguments, err := spec.GetChildMap("arguments")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
arguments["parameters"] = make([]interface{}, 0)
|
||||
parameters := make([]interface{}, len(annotations))
|
||||
|
||||
for _, value := range annotations {
|
||||
data, err := mapping.NewFromYamlString(value)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Method": "AddWorkflowTemplateParametersFromAnnotations",
|
||||
"Step": "NewFromYamlString",
|
||||
"Error": err.Error(),
|
||||
}).Error("Error with AddWorkflowTemplateParametersFromAnnotations")
|
||||
continue
|
||||
}
|
||||
|
||||
order := 0
|
||||
orderValue, ok := data["order"]
|
||||
if ok {
|
||||
order = orderValue.(int)
|
||||
delete(data, "order")
|
||||
|
||||
if order >= 0 && order < len(parameters) {
|
||||
parameters[order] = data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
arguments["parameters"] = parameters
|
||||
}
|
||||
|
||||
const (
|
||||
WorfklowPending WorkflowExecutionPhase = "Pending"
|
||||
WorfklowRunning WorkflowExecutionPhase = "Running"
|
||||
@@ -299,61 +370,3 @@ func FilePathToExtension(path string) string {
|
||||
|
||||
return path[dotIndex+1:]
|
||||
}
|
||||
|
||||
func WrapSpecInK8s(data []byte) ([]byte, error) {
|
||||
mapping := make(map[interface{}]interface{})
|
||||
if err := yaml.Unmarshal(data, mapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
contentMap := map[interface{}]interface{}{
|
||||
"metadata": make(map[interface{}]interface{}),
|
||||
"spec": mapping,
|
||||
}
|
||||
|
||||
finalBytes, err := yaml.Marshal(contentMap)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return finalBytes, nil
|
||||
}
|
||||
|
||||
func AddWorkflowTemplateParametersFromAnnotations(spec mapping.Mapping, annotations map[string]string) {
|
||||
if spec == nil || len(annotations) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
arguments, err := spec.GetChildMap("arguments")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
arguments["parameters"] = make([]interface{}, 0)
|
||||
parameters := make([]interface{}, len(annotations))
|
||||
|
||||
for _, value := range annotations {
|
||||
data, err := mapping.NewFromYamlString(value)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Method": "AddWorkflowTemplateParametersFromAnnotations",
|
||||
"Step": "NewFromYamlString",
|
||||
"Error": err.Error(),
|
||||
}).Error("Error with AddWorkflowTemplateParametersFromAnnotations")
|
||||
continue
|
||||
}
|
||||
|
||||
order := 0
|
||||
orderValue, ok := data["order"]
|
||||
if ok {
|
||||
order = orderValue.(int)
|
||||
delete(data, "order")
|
||||
|
||||
if order >= 0 && order < len(parameters) {
|
||||
parameters[order] = data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
arguments["parameters"] = parameters
|
||||
}
|
||||
|
@@ -238,7 +238,7 @@ func (c *Client) archiveWorkflowTemplate(namespace, uid string) (bool, error) {
|
||||
|
||||
func (c *Client) CreateWorkflowTemplate(namespace string, workflowTemplate *WorkflowTemplate) (*WorkflowTemplate, error) {
|
||||
// validate workflow template
|
||||
finalBytes, err := WrapSpecInK8s(workflowTemplate.GetManifestBytes())
|
||||
finalBytes, err := workflowTemplate.WrapSpec()
|
||||
if err != nil {
|
||||
return nil, util.NewUserError(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
@@ -267,7 +267,7 @@ func (c *Client) CreateWorkflowTemplate(namespace string, workflowTemplate *Work
|
||||
|
||||
func (c *Client) CreateWorkflowTemplateVersion(namespace string, workflowTemplate *WorkflowTemplate) (*WorkflowTemplate, error) {
|
||||
// validate workflow template
|
||||
finalBytes, err := WrapSpecInK8s(workflowTemplate.GetManifestBytes())
|
||||
finalBytes, err := workflowTemplate.WrapSpec()
|
||||
if err != nil {
|
||||
return nil, util.NewUserError(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
@@ -424,7 +424,7 @@ func createArgoWorkflowTemplate(workflowTemplate *WorkflowTemplate, version stri
|
||||
var jsonOpts []argojson.JSONOpt
|
||||
jsonOpts = append(jsonOpts, argojson.DisallowUnknownFields)
|
||||
|
||||
finalBytes, err := WrapSpecInK8s(workflowTemplate.GetManifestBytes())
|
||||
finalBytes, err := workflowTemplate.WrapSpec()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user