From 5165984030afcaf136f9cffd1dc3427ea2b7adc4 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Fri, 12 Apr 2019 17:04:07 +0200 Subject: [PATCH] runservice: convert RunConfigTask.Depends to a map --- internal/runconfig/runconfig.go | 15 +- internal/runconfig/runconfig_test.go | 208 ++++++------------ internal/services/gateway/api/run.go | 10 +- .../runservice/scheduler/scheduler_test.go | 32 ++- internal/services/runservice/types/types.go | 26 +-- 5 files changed, 107 insertions(+), 184 deletions(-) diff --git a/internal/runconfig/runconfig.go b/internal/runconfig/runconfig.go index ecef6bd..40760ce 100644 --- a/internal/runconfig/runconfig.go +++ b/internal/runconfig/runconfig.go @@ -197,8 +197,8 @@ func GenRunConfigTasks(uuid util.UUIDGenerator, c *config.Config, pipelineName s for _, rct := range rcts { cpe := cp.Elements[rct.Name] - depends := make([]*rstypes.RunConfigTaskDepend, len(cpe.Depends)) - for id, d := range cpe.Depends { + depends := make(map[string]*rstypes.RunConfigTaskDepend, len(cpe.Depends)) + for _, d := range cpe.Depends { conditions := make([]rstypes.RunConfigTaskDependCondition, len(d.Conditions)) // when no conditions are defined default to on_success if len(d.Conditions) == 0 { @@ -217,7 +217,7 @@ func GenRunConfigTasks(uuid util.UUIDGenerator, c *config.Config, pipelineName s } drct := getRunConfigTaskByName(rcts, d.ElementName) - depends[id] = &rstypes.RunConfigTaskDepend{ + depends[drct.ID] = &rstypes.RunConfigTaskDepend{ TaskID: drct.ID, Conditions: conditions, } @@ -331,13 +331,7 @@ func GenTasksLevels(rcts map[string]*rstypes.RunConfigTask) error { func GetParents(rcts map[string]*rstypes.RunConfigTask, task *rstypes.RunConfigTask) []*rstypes.RunConfigTask { parents := []*rstypes.RunConfigTask{} for _, t := range rcts { - isParent := false - for _, d := range task.Depends { - if d.TaskID == t.ID { - isParent = true - } - } - if isParent { + if _, ok := task.Depends[t.ID]; ok { parents = append(parents, t) } } @@ -387,5 +381,4 @@ func genValue(val config.Value, variables map[string]string) string { default: panic(fmt.Errorf("wrong value type: %q", val.Value)) } - return "" } diff --git a/internal/runconfig/runconfig_test.go b/internal/runconfig/runconfig_test.go index 7a595ab..b0a49ef 100644 --- a/internal/runconfig/runconfig_test.go +++ b/internal/runconfig/runconfig_test.go @@ -33,7 +33,7 @@ func TestGenTasksLevels(t *testing.T) { type task struct { ID string Level int - Depends []*rstypes.RunConfigTaskDepend + Depends map[string]*rstypes.RunConfigTaskDepend } tests := []struct { name string @@ -89,10 +89,8 @@ func TestGenTasksLevels(t *testing.T) { { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -104,10 +102,8 @@ func TestGenTasksLevels(t *testing.T) { { ID: "2", Level: 1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -118,19 +114,15 @@ func TestGenTasksLevels(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -142,28 +134,22 @@ func TestGenTasksLevels(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "3", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "3": &rstypes.RunConfigTaskDepend{TaskID: "3"}, }, }, { ID: "3", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -175,28 +161,22 @@ func TestGenTasksLevels(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "3", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "3": &rstypes.RunConfigTaskDepend{TaskID: "3"}, }, }, { ID: "3", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, }, @@ -243,7 +223,7 @@ func TestGetAllParents(t *testing.T) { type task struct { ID string Level int - Depends []*rstypes.RunConfigTaskDepend + Depends map[string]*rstypes.RunConfigTaskDepend } tests := []struct { name string @@ -285,10 +265,8 @@ func TestGetAllParents(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -306,10 +284,8 @@ func TestGetAllParents(t *testing.T) { { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -324,31 +300,23 @@ func TestGetAllParents(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, - &rstypes.RunConfigTaskDepend{ - TaskID: "3", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, + "3": &rstypes.RunConfigTaskDepend{TaskID: "3"}, }, }, { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "4", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "4": &rstypes.RunConfigTaskDepend{TaskID: "4"}, }, }, { ID: "3", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "5", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "5": &rstypes.RunConfigTaskDepend{TaskID: "5"}, }, }, { @@ -374,19 +342,15 @@ func TestGetAllParents(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -401,28 +365,22 @@ func TestGetAllParents(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "3", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "3": &rstypes.RunConfigTaskDepend{TaskID: "3"}, }, }, { ID: "3", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -438,28 +396,22 @@ func TestGetAllParents(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "3", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "3": &rstypes.RunConfigTaskDepend{TaskID: "3"}, }, }, { ID: "3", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, }, @@ -502,7 +454,7 @@ func TestCheckRunConfig(t *testing.T) { type task struct { ID string Level int - Depends []*rstypes.RunConfigTaskDepend + Depends map[string]*rstypes.RunConfigTaskDepend } tests := []struct { name string @@ -541,10 +493,8 @@ func TestCheckRunConfig(t *testing.T) { { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -555,19 +505,15 @@ func TestCheckRunConfig(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -584,28 +530,22 @@ func TestCheckRunConfig(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "3", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "3": &rstypes.RunConfigTaskDepend{TaskID: "3"}, }, }, { ID: "3", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "1", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "1": &rstypes.RunConfigTaskDepend{TaskID: "1"}, }, }, }, @@ -623,28 +563,22 @@ func TestCheckRunConfig(t *testing.T) { { ID: "1", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, { ID: "2", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "3", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "3": &rstypes.RunConfigTaskDepend{TaskID: "3"}, }, }, { ID: "3", Level: -1, - Depends: []*rstypes.RunConfigTaskDepend{ - &rstypes.RunConfigTaskDepend{ - TaskID: "2", - }, + Depends: map[string]*rstypes.RunConfigTaskDepend{ + "2": &rstypes.RunConfigTaskDepend{TaskID: "2"}, }, }, }, @@ -795,7 +729,7 @@ func TestGenRunConfig(t *testing.T) { out: map[string]*rstypes.RunConfigTask{ uuid.New("element01").String(): &rstypes.RunConfigTask{ ID: uuid.New("element01").String(), - Name: "element01", Depends: []*rstypes.RunConfigTaskDepend{}, + Name: "element01", Depends: map[string]*rstypes.RunConfigTaskDepend{}, Runtime: &rstypes.Runtime{Type: rstypes.RuntimeType("pod"), Containers: []*rstypes.Container{ { @@ -879,7 +813,7 @@ func TestGenRunConfig(t *testing.T) { out: map[string]*rstypes.RunConfigTask{ uuid.New("element01").String(): &rstypes.RunConfigTask{ ID: uuid.New("element01").String(), - Name: "element01", Depends: []*rstypes.RunConfigTaskDepend{}, + Name: "element01", Depends: map[string]*rstypes.RunConfigTaskDepend{}, Runtime: &rstypes.Runtime{Type: rstypes.RuntimeType("pod"), Containers: []*rstypes.Container{ { @@ -959,7 +893,7 @@ func TestGenRunConfig(t *testing.T) { out: map[string]*rstypes.RunConfigTask{ uuid.New("element01").String(): &rstypes.RunConfigTask{ ID: uuid.New("element01").String(), - Name: "element01", Depends: []*rstypes.RunConfigTaskDepend{}, + Name: "element01", Depends: map[string]*rstypes.RunConfigTaskDepend{}, Runtime: &rstypes.Runtime{Type: rstypes.RuntimeType("pod"), Containers: []*rstypes.Container{ { diff --git a/internal/services/gateway/api/run.go b/internal/services/gateway/api/run.go index eed5b23..6bcb229 100644 --- a/internal/services/gateway/api/run.go +++ b/internal/services/gateway/api/run.go @@ -67,11 +67,11 @@ type RunResponse struct { } type RunResponseTask struct { - ID string `json:"id"` - Name string `json:"name"` - Status rstypes.RunTaskStatus `json:"status"` - Level int `json:"level"` - Depends []*rstypes.RunConfigTaskDepend `json:"depends"` + ID string `json:"id"` + Name string `json:"name"` + Status rstypes.RunTaskStatus `json:"status"` + Level int `json:"level"` + Depends map[string]*rstypes.RunConfigTaskDepend `json:"depends"` WaitingApproval bool `json:"waiting_approval"` Approved bool `json:"approved"` diff --git a/internal/services/runservice/scheduler/scheduler_test.go b/internal/services/runservice/scheduler/scheduler_test.go index 8eb42f1..89100a7 100644 --- a/internal/services/runservice/scheduler/scheduler_test.go +++ b/internal/services/runservice/scheduler/scheduler_test.go @@ -30,7 +30,7 @@ func TestAdvanceRunTasks(t *testing.T) { "task01": &types.RunConfigTask{ ID: "task01", Name: "task01", - Depends: []*types.RunConfigTaskDepend{}, + Depends: map[string]*types.RunConfigTaskDepend{}, Runtime: &types.Runtime{Type: types.RuntimeType("pod"), Containers: []*types.Container{{Image: "image01"}}, }, @@ -41,10 +41,8 @@ func TestAdvanceRunTasks(t *testing.T) { "task02": &types.RunConfigTask{ ID: "task02", Name: "task02", - Depends: []*types.RunConfigTaskDepend{ - &types.RunConfigTaskDepend{ - TaskID: "task01", - }, + Depends: map[string]*types.RunConfigTaskDepend{ + "task01": &types.RunConfigTaskDepend{TaskID: "task01"}, }, Runtime: &types.Runtime{Type: types.RuntimeType("pod"), Containers: []*types.Container{{Image: "image01"}}, @@ -56,7 +54,7 @@ func TestAdvanceRunTasks(t *testing.T) { "task03": &types.RunConfigTask{ ID: "task03", Name: "task03", - Depends: []*types.RunConfigTaskDepend{}, + Depends: map[string]*types.RunConfigTaskDepend{}, Runtime: &types.Runtime{Type: types.RuntimeType("pod"), Containers: []*types.Container{{Image: "image01"}}, }, @@ -77,9 +75,9 @@ func TestAdvanceRunTasks(t *testing.T) { "task05": &types.RunConfigTask{ ID: "task05", Name: "task05", - Depends: []*types.RunConfigTaskDepend{ - &types.RunConfigTaskDepend{TaskID: "task03"}, - &types.RunConfigTaskDepend{TaskID: "task04"}, + Depends: map[string]*types.RunConfigTaskDepend{ + "task03": &types.RunConfigTaskDepend{TaskID: "task03"}, + "task04": &types.RunConfigTaskDepend{TaskID: "task04"}, }, Runtime: &types.Runtime{Type: types.RuntimeType("pod"), Containers: []*types.Container{{Image: "image01"}}, @@ -215,7 +213,7 @@ func TestGetTasksToRun(t *testing.T) { "task01": &types.RunConfigTask{ ID: "task01", Name: "task01", - Depends: []*types.RunConfigTaskDepend{}, + Depends: map[string]*types.RunConfigTaskDepend{}, Runtime: &types.Runtime{Type: types.RuntimeType("pod"), Containers: []*types.Container{{Image: "image01"}}, }, @@ -226,10 +224,8 @@ func TestGetTasksToRun(t *testing.T) { "task02": &types.RunConfigTask{ ID: "task02", Name: "task02", - Depends: []*types.RunConfigTaskDepend{ - &types.RunConfigTaskDepend{ - TaskID: "task01", - }, + Depends: map[string]*types.RunConfigTaskDepend{ + "task01": &types.RunConfigTaskDepend{TaskID: "task01"}, }, Runtime: &types.Runtime{Type: types.RuntimeType("pod"), Containers: []*types.Container{{Image: "image01"}}, @@ -241,7 +237,7 @@ func TestGetTasksToRun(t *testing.T) { "task03": &types.RunConfigTask{ ID: "task03", Name: "task03", - Depends: []*types.RunConfigTaskDepend{}, + Depends: map[string]*types.RunConfigTaskDepend{}, Runtime: &types.Runtime{Type: types.RuntimeType("pod"), Containers: []*types.Container{{Image: "image01"}}, }, @@ -262,9 +258,9 @@ func TestGetTasksToRun(t *testing.T) { "task05": &types.RunConfigTask{ ID: "task05", Name: "task05", - Depends: []*types.RunConfigTaskDepend{ - &types.RunConfigTaskDepend{TaskID: "task03"}, - &types.RunConfigTaskDepend{TaskID: "task04"}, + Depends: map[string]*types.RunConfigTaskDepend{ + "task03": &types.RunConfigTaskDepend{TaskID: "task03"}, + "task04": &types.RunConfigTaskDepend{TaskID: "task04"}, }, Runtime: &types.Runtime{Type: types.RuntimeType("pod"), Containers: []*types.Container{{Image: "image01"}}, diff --git a/internal/services/runservice/types/types.go b/internal/services/runservice/types/types.go index f8ba265..66589f5 100644 --- a/internal/services/runservice/types/types.go +++ b/internal/services/runservice/types/types.go @@ -316,19 +316,19 @@ func (rc *RunConfig) DeepCopy() *RunConfig { } type RunConfigTask struct { - Level int `json:"level,omitempty"` - ID string `json:"id,omitempty"` - Name string `json:"name,omitempty"` - Depends []*RunConfigTaskDepend `json:"depends"` - Runtime *Runtime `json:"runtime,omitempty"` - Environment map[string]string `json:"environment,omitempty"` - WorkingDir string `json:"working_dir,omitempty"` - Shell string `json:"shell,omitempty"` - User string `json:"user,omitempty"` - Steps []interface{} `json:"steps,omitempty"` - IgnoreFailure bool `json:"ignore_failure,omitempty"` - NeedsApproval bool `json:"needs_approval,omitempty"` - Skip bool `json:"skip,omitempty"` + Level int `json:"level,omitempty"` + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Depends map[string]*RunConfigTaskDepend `json:"depends"` + Runtime *Runtime `json:"runtime,omitempty"` + Environment map[string]string `json:"environment,omitempty"` + WorkingDir string `json:"working_dir,omitempty"` + Shell string `json:"shell,omitempty"` + User string `json:"user,omitempty"` + Steps []interface{} `json:"steps,omitempty"` + IgnoreFailure bool `json:"ignore_failure,omitempty"` + NeedsApproval bool `json:"needs_approval,omitempty"` + Skip bool `json:"skip,omitempty"` } type RunConfigTaskDependCondition string