update/fix: fixed issue where parameters were not being sent with API response.

Fixed issue where cron workflows did not apply their labels to the newly created workflow executions.
This commit is contained in:
Andrey Melnikov
2020-05-06 15:39:55 -07:00
parent c64954562b
commit 872ed2d9af
4 changed files with 118 additions and 3 deletions

View File

@@ -452,6 +452,25 @@ type WorkflowExecution struct {
Labels map[string]string
}
func (we *WorkflowExecution) LoadParametersFromBytes() ([]Parameter, error) {
loadedParameters := make([]Parameter, 0)
err := json.Unmarshal(we.ParametersBytes, &loadedParameters)
if err != nil {
return we.Parameters, err
}
// It might be nil because the value "null" is stored in db if there are no parameters.
// for consistency, we return an empty array.
if loadedParameters == nil {
loadedParameters = make([]Parameter, 0)
}
we.Parameters = loadedParameters
return we.Parameters, err
}
type ListOptions = metav1.ListOptions
type PodGCStrategy = wfv1.PodGCStrategy
@@ -653,3 +672,13 @@ func getWorkspaceColumns(alias string, destination string, extraColumns ...strin
columns := []string{"id", "created_at", "modified_at", "uid", "name", "namespace", "phase", "parameters", "workspace_template_id", "workspace_template_version", "started_at", "paused_at", "terminated_at"}
return formatColumnSelect(columns, alias, destination, extraColumns...)
}
func LabelsToMapping(labels ...*Label) map[string]string {
result := make(map[string]string)
for _, label := range labels {
result[label.Key] = label.Value
}
return result
}

View File

@@ -516,6 +516,11 @@ func (c *Client) CronStartWorkflowExecutionStatisticInsert(namespace, name strin
return err
}
cronLabels, err := c.GetDbLabels(TypeCronWorkflow, cronWorkflow.ID)
if err != nil {
return err
}
tx, err := c.DB.Begin()
if err != nil {
return err
@@ -541,11 +546,27 @@ func (c *Client) CronStartWorkflowExecutionStatisticInsert(namespace, name strin
"parameters": string(parametersJson),
}
_, err = sb.Insert("workflow_executions").
SetMap(insertMap).RunWith(tx).Exec()
workflowExecutionId := uint64(0)
err = sb.Insert("workflow_executions").
SetMap(insertMap).
Suffix("RETURNING id").
RunWith(tx).
QueryRow().
Scan(&workflowExecutionId)
if err != nil {
return err
}
if len(cronLabels) > 0 {
labelsMapped := LabelsToMapping(cronLabels...)
_, err = c.InsertLabelsBuilder(TypeWorkflowExecution, workflowExecutionId, labelsMapped).
RunWith(tx).
Exec()
if err != nil {
return err
}
}
err = tx.Commit()
if err != nil {
return err
@@ -556,7 +577,7 @@ func (c *Client) CronStartWorkflowExecutionStatisticInsert(namespace, name strin
func (c *Client) GetWorkflowExecution(namespace, name string) (workflow *WorkflowExecution, err error) {
workflow = &WorkflowExecution{}
query, args, err := sb.Select("we.uid", "we.name", "we.phase", "we.started_at", "we.finished_at").
query, args, err := sb.Select(getWorkflowExecutionColumns("we", "")...).
From("workflow_executions we").
Join("workflow_template_versions wtv ON wtv.id = we.workflow_template_version_id").
Join("workflow_templates wt ON wt.id = wtv.workflow_template_id").

View File

@@ -2,6 +2,7 @@ package converter
import (
"github.com/onepanelio/core/api"
v1 "github.com/onepanelio/core/pkg"
)
func APIKeyValueToLabel(apiKeyValues []*api.KeyValue) map[string]string {
@@ -29,3 +30,58 @@ func MappingToKeyValue(mapping map[string]string) []*api.KeyValue {
return keyValues
}
func ParameterOptionToAPI(option v1.ParameterOption) *api.ParameterOption {
apiOption := &api.ParameterOption{
Name: option.Name,
Value: option.Value,
}
return apiOption
}
func ParameterOptionsToAPI(options []*v1.ParameterOption) []*api.ParameterOption {
result := make([]*api.ParameterOption, len(options))
for i := range options {
newItem := ParameterOptionToAPI(*options[i])
result[i] = newItem
}
return result
}
func ParameterToAPI(param v1.Parameter) *api.Parameter {
apiParam := &api.Parameter{
Name: param.Name,
Type: param.Type,
Required: param.Required,
}
if param.Value != nil {
apiParam.Value = *param.Value
}
if param.DisplayName != nil {
apiParam.DisplayName = *param.DisplayName
}
if param.Hint != nil {
apiParam.Hint = *param.Hint
}
if param.Options != nil {
apiParam.Options = ParameterOptionsToAPI(param.Options)
}
return apiParam
}
func ParametersToAPI(params []v1.Parameter) []*api.Parameter {
result := make([]*api.Parameter, len(params))
for i := range params {
newItem := ParameterToAPI(params[i])
result[i] = newItem
}
return result
}

View File

@@ -49,6 +49,15 @@ func apiWorkflowExecution(wf *v1.WorkflowExecution) (workflow *api.WorkflowExecu
workflow.WorkflowTemplate = apiWorkflowTemplate(wf.WorkflowTemplate)
}
if wf.ParametersBytes != nil {
parameters, err := wf.LoadParametersFromBytes()
if err != nil {
return nil
}
workflow.Parameters = converter.ParametersToAPI(parameters)
}
return
}