clean: capitalization of names and added WorkflowTemplate uid generator method to encapsulate the behavior

This commit is contained in:
Andrey Melnikov
2020-06-26 16:14:07 -07:00
parent 0b54bd1c43
commit b8b9ff2543
3 changed files with 42 additions and 41 deletions

View File

@@ -224,11 +224,10 @@ func (c *Client) ArchiveWorkflowExecution(namespace, uid string) error {
return nil
}
/*
Name is == to UID, no user friendly name.
Workflow execution name == uid, example: name = my-friendly-wf-name-8skjz, uid = my-friendly-wf-name-8skjz
*/
func (c *Client) createWorkflow(namespace string, workflowTemplateId uint64, workflowTemplateVersionId uint64, wf *wfv1.Workflow, opts *WorkflowExecutionOptions) (newDbId uint64, createdWorkflow *wfv1.Workflow, err error) {
// createWorkflow creates the workflow in the database and argo.
// Name is == to UID, no user friendly name.
// Workflow execution name == uid, example: name = my-friendly-wf-name-8skjz, uid = my-friendly-wf-name-8skjz
func (c *Client) createWorkflow(namespace string, workflowTemplateID uint64, workflowTemplateVersionID uint64, wf *wfv1.Workflow, opts *WorkflowExecutionOptions) (newDBID uint64, createdWorkflow *wfv1.Workflow, err error) {
if opts == nil {
opts = &WorkflowExecutionOptions{}
}
@@ -274,7 +273,7 @@ func (c *Client) createWorkflow(namespace string, workflowTemplateId uint64, wor
return 0, nil, err
}
err = injectExitHandlerWorkflowExecutionStatistic(wf, &workflowTemplateId)
err = injectExitHandlerWorkflowExecutionStatistic(wf, &workflowTemplateID)
if err != nil {
return 0, nil, err
}
@@ -294,7 +293,7 @@ func (c *Client) createWorkflow(namespace string, workflowTemplateId uint64, wor
}
//Create an entry for workflow_executions statistic
//CURL code will hit the API endpoint that will update the db row
newDbId, err = c.insertPreWorkflowExecutionStatistic(namespace, createdWorkflow.Name, workflowTemplateVersionId, createdWorkflow.CreationTimestamp.UTC(), uid, opts.Parameters)
newDBID, err = c.insertPreWorkflowExecutionStatistic(namespace, createdWorkflow.Name, workflowTemplateVersionID, createdWorkflow.CreationTimestamp.UTC(), uid, opts.Parameters)
if err != nil {
return 0, nil, err
}
@@ -372,6 +371,7 @@ func (c *Client) CreateWorkflowExecution(namespace string, workflow *WorkflowExe
return nil, err
}
// TODO make a method to UnmarshalWorkflows from a workflow template - seems silly we do it in two steps?
workflows, err := UnmarshalWorkflows(manifest, true)
if err != nil {
log.WithFields(log.Fields{
@@ -436,7 +436,7 @@ func (c *Client) CloneWorkflowExecution(namespace, uid string) (*WorkflowExecuti
return c.CreateWorkflowExecution(namespace, workflowExecution, workflowTemplate)
}
func (c *Client) insertPreWorkflowExecutionStatistic(namespace, name string, workflowTemplateVersionId uint64, createdAt time.Time, uid string, parameters []Parameter) (newId uint64, err error) {
func (c *Client) insertPreWorkflowExecutionStatistic(namespace, name string, workflowTemplateVersionID uint64, createdAt time.Time, UID string, parameters []Parameter) (newID uint64, err error) {
tx, err := c.DB.Begin()
if err != nil {
return 0, err
@@ -448,24 +448,21 @@ func (c *Client) insertPreWorkflowExecutionStatistic(namespace, name string, wor
return 0, err
}
insertMap := sq.Eq{
"uid": uid,
"workflow_template_version_id": workflowTemplateVersionId,
"name": name,
"namespace": namespace,
"created_at": createdAt.UTC(),
"phase": wfv1.NodePending,
"parameters": string(parametersJSON),
"is_archived": false,
}
err = sb.Insert("workflow_executions").
SetMap(insertMap).
SetMap(sq.Eq{
"UID": UID,
"workflow_template_version_id": workflowTemplateVersionID,
"name": name,
"namespace": namespace,
"created_at": createdAt.UTC(),
"phase": wfv1.NodePending,
"parameters": string(parametersJSON),
"is_archived": false,
}).
Suffix("RETURNING id").
RunWith(tx).
QueryRow().
Scan(&newId)
Scan(&newID)
if err != nil {
return 0, err
}
@@ -473,7 +470,7 @@ func (c *Client) insertPreWorkflowExecutionStatistic(namespace, name string, wor
if err != nil {
return 0, err
}
return newId, err
return newID, err
}
func (c *Client) FinishWorkflowExecutionStatisticViaExitHandler(namespace, name string, workflowTemplateID int64, phase wfv1.NodePhase, startedAt time.Time) (err error) {
@@ -1547,7 +1544,7 @@ func workflowExecutionsSelectBuilder(namespace, workflowTemplateUID, workflowTem
}
func (c *Client) getWorkflowExecutionAndTemplate(namespace string, uid string) (workflow *WorkflowExecution, err error) {
query, args, err := sb.Select(getWorkflowExecutionColumns("we", "")...).
sb := sb.Select(getWorkflowExecutionColumns("we", "")...).
Columns(getWorkflowTemplateColumns("wt", "workflow_template")...).
Columns(`wtv.manifest "workflow_template.manifest"`, `wtv.version "workflow_template.version"`).
From("workflow_executions we").
@@ -1557,16 +1554,10 @@ func (c *Client) getWorkflowExecutionAndTemplate(namespace string, uid string) (
"wt.namespace": namespace,
"we.name": uid,
"we.is_archived": false,
}).
ToSql()
if err != nil {
return nil, err
}
// TODO DB call
})
workflow = &WorkflowExecution{}
if err = c.DB.Get(workflow, query, args...); err != nil {
if err = c.DB.Getx(workflow, sb); err != nil {
return nil, err
}

View File

@@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"github.com/onepanelio/core/pkg/util/pagination"
uid2 "github.com/onepanelio/core/pkg/util/uid"
"strconv"
"strings"
"time"
@@ -24,11 +23,10 @@ import (
// createWorkflowTemplate creates a WorkflowTemplate and all of the DB/Argo/K8s related resources
// The returned WorkflowTemplate has the ArgoWorkflowTemplate set to the newly created one.
func (c *Client) createWorkflowTemplate(namespace string, workflowTemplate *WorkflowTemplate) (*WorkflowTemplate, *WorkflowTemplateVersion, error) {
uid, err := uid2.GenerateUID(workflowTemplate.Name, 30)
if err != nil {
if err := workflowTemplate.GenerateUID(workflowTemplate.Name); err != nil {
return nil, nil, err
}
workflowTemplate.UID = uid
tx, err := c.DB.Begin()
if err != nil {
return nil, nil, err
@@ -39,7 +37,7 @@ func (c *Client) createWorkflowTemplate(namespace string, workflowTemplate *Work
err = sb.Insert("workflow_templates").
SetMap(sq.Eq{
"uid": uid,
"uid": workflowTemplate.UID,
"name": workflowTemplate.Name,
"namespace": namespace,
"is_system": workflowTemplate.IsSystem,
@@ -743,15 +741,14 @@ func createArgoWorkflowTemplate(workflowTemplate *WorkflowTemplate, version int6
return nil, err
}
worfklowTemplateName, err := uid2.GenerateUID(workflowTemplate.Name, 30)
if err != nil {
if err := workflowTemplate.GenerateUID(workflowTemplate.Name); err != nil {
return nil, err
}
argoWft.Name = fmt.Sprintf("%v-v%v", worfklowTemplateName, version)
argoWft.Name = fmt.Sprintf("%v-v%v", workflowTemplate.UID, version)
labels := map[string]string{
label.WorkflowTemplate: worfklowTemplateName,
label.WorkflowTemplate: workflowTemplate.UID,
label.WorkflowTemplateUid: workflowTemplate.UID,
label.Version: fmt.Sprintf("%v", version),
label.VersionLatest: "true",

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/onepanelio/core/pkg/util/mapping"
uid2 "github.com/onepanelio/core/pkg/util/uid"
"github.com/onepanelio/core/util/sql"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
@@ -38,6 +39,18 @@ type WorkflowTemplate struct {
ResourceUID *string // see Resource field
}
// GenerateUID generates a uid from the input name and sets it on the workflow template
func (wt *WorkflowTemplate) GenerateUID(name string) error {
result, err := uid2.GenerateUID(name, 30)
if err != nil {
return err
}
wt.UID = result
return nil
}
// GetManifestBytes returns the manifest as []byte
func (wt *WorkflowTemplate) GetManifestBytes() []byte {
return []byte(wt.Manifest)