diff --git a/pkg/cfn/manager/tasks_test.go b/pkg/cfn/manager/tasks_test.go index 6907a1e44d..5a5e07d3a7 100644 --- a/pkg/cfn/manager/tasks_test.go +++ b/pkg/cfn/manager/tasks_test.go @@ -5,6 +5,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5" "github.com/weaveworks/eksctl/pkg/testutils/mockprovider" vpcfakes "github.com/weaveworks/eksctl/pkg/vpc/fakes" @@ -94,7 +95,10 @@ var _ = Describe("StackCollection Tasks", func() { // in these tests { tasks := stackManager.NewUnmanagedNodeGroupTask(makeNodeGroups("bar", "foo"), false, fakeVPCImporter) - Expect(tasks.Describe()).To(Equal(`2 parallel tasks: { create nodegroup "bar", create nodegroup "foo" }`)) + Expect(tasks.Describe()).To(Equal(` +2 parallel tasks: { create nodegroup "bar", create nodegroup "foo" +} +`)) } { tasks := stackManager.NewUnmanagedNodeGroupTask(makeNodeGroups("bar"), false, fakeVPCImporter) @@ -110,11 +114,21 @@ var _ = Describe("StackCollection Tasks", func() { } { tasks := stackManager.NewTasksToCreateClusterWithNodeGroups(makeNodeGroups("bar", "foo"), nil, true) - Expect(tasks.Describe()).To(Equal(`2 sequential tasks: { create cluster control plane "test-cluster", 2 parallel sub-tasks: { create nodegroup "bar", create nodegroup "foo" } }`)) + Expect(tasks.Describe()).To(Equal(` +2 sequential tasks: { create cluster control plane "test-cluster", + 2 parallel sub-tasks: { + create nodegroup "bar", + create nodegroup "foo", + } +} +`)) } { tasks := stackManager.NewTasksToCreateClusterWithNodeGroups(makeNodeGroups("bar"), nil, false) - Expect(tasks.Describe()).To(Equal(`2 sequential tasks: { create cluster control plane "test-cluster", create nodegroup "bar" }`)) + Expect(tasks.Describe()).To(Equal(` +2 sequential tasks: { create cluster control plane "test-cluster", create nodegroup "bar" +} +`)) } { tasks := stackManager.NewTasksToCreateClusterWithNodeGroups(nil, nil, true) @@ -122,15 +136,38 @@ var _ = Describe("StackCollection Tasks", func() { } { tasks := stackManager.NewTasksToCreateClusterWithNodeGroups(makeNodeGroups("bar", "foo"), makeManagedNodeGroups("m1", "m2"), false) - Expect(tasks.Describe()).To(Equal(`2 sequential tasks: { create cluster control plane "test-cluster", 4 parallel sub-tasks: { create nodegroup "bar", create nodegroup "foo", create managed nodegroup "m1", create managed nodegroup "m2" } }`)) + Expect(tasks.Describe()).To(Equal(` +2 sequential tasks: { create cluster control plane "test-cluster", + 4 parallel sub-tasks: { + create nodegroup "bar", + create nodegroup "foo", + create managed nodegroup "m1", + create managed nodegroup "m2", + } +} +`)) } { tasks := stackManager.NewTasksToCreateClusterWithNodeGroups(makeNodeGroups("foo"), makeManagedNodeGroups("m1"), true) - Expect(tasks.Describe()).To(Equal(`2 sequential tasks: { create cluster control plane "test-cluster", 2 parallel sub-tasks: { create nodegroup "foo", create managed nodegroup "m1" } }`)) + Expect(tasks.Describe()).To(Equal(` +2 sequential tasks: { create cluster control plane "test-cluster", + 2 parallel sub-tasks: { + create nodegroup "foo", + create managed nodegroup "m1", + } +} +`)) } { tasks := stackManager.NewTasksToCreateClusterWithNodeGroups(makeNodeGroups("bar"), nil, false, &task{id: 1}) - Expect(tasks.Describe()).To(Equal(`2 sequential tasks: { create cluster control plane "test-cluster", 2 sequential sub-tasks: { task 1, create nodegroup "bar" } }`)) + Expect(tasks.Describe()).To(Equal(` +2 sequential tasks: { create cluster control plane "test-cluster", + 2 sequential sub-tasks: { + task 1, + create nodegroup "bar", + } +} +`)) } }) }) diff --git a/pkg/utils/tasks/tasks.go b/pkg/utils/tasks/tasks.go index c60c294257..f625cdc74e 100644 --- a/pkg/utils/tasks/tasks.go +++ b/pkg/utils/tasks/tasks.go @@ -62,38 +62,73 @@ func (t *TaskTree) Len() int { return len(t.Tasks) } -// Describe the set +// Describe collects all tasks which have been added to the task tree. +// This is a lazy tree which does not track its nodes in any form. This function +// is recursively called from the rest of the task Describes and eventually +// returns a collection of all the tasks' `Info` value. func (t *TaskTree) Describe() string { - descriptions := []string{} - for _, task := range t.Tasks { - descriptions = append(descriptions, task.Describe()) + if t.Len() == 0 { + return "no tasks" } - mode := "sequential" - if t.Parallel { - mode = "parallel" + var descriptions []string + for _, task := range t.Tasks { + descriptions = append(descriptions, strings.TrimSuffix(task.Describe(), "\n")) } - count := len(descriptions) - var msg string noun := "task" if t.IsSubTask { noun = "sub-task" } - switch count { - case 0: - msg = "no tasks" - case 1: - msg = fmt.Sprintf("1 %s: { %s }", noun, descriptions[0]) + if len(descriptions) == 1 { + msg := fmt.Sprintf("1 %s: { %s }", noun, descriptions[0]) if t.IsSubTask { - msg = descriptions[0] // simple description for single sub-task + msg = descriptions[0] + } + return msg + } + count := len(descriptions) + mode := "sequential" + if t.Parallel { + mode = "parallel" + } + noun += "s" + head := fmt.Sprintf("\n%d %s %s: { ", count, mode, noun) + var tail string + if t.IsSubTask { + // Only add a linebreak at the end if we have multiple subtasks as well. Otherwise, leave it + // as single line. + head = fmt.Sprintf("\n%s%d %s %s: { ", strings.Repeat(" ", 4), count, mode, noun) + tail = "\n" + for _, d := range descriptions { + // all tasks are sub-tasks if they are inside a task. + // which means we don't have to care about sequential tasks. + if strings.Contains(d, "sub-task") { + // trim the previous leading tail new line... + d = strings.TrimPrefix(d, "\n") + split := strings.Split(d, "\n") + // indent all lines of the subtask one deepness more + var result []string + for _, s := range split { + result = append(result, strings.Repeat(" ", 4)+s) + } + // join it back up with line breaks + d = strings.Join(result, "\n") + } else { + d = strings.Repeat(" ", 8) + d + } + tail += fmt.Sprintf("%s,\n", d) } - default: - noun += "s" - msg = fmt.Sprintf("%d %s %s: { %s }", count, mode, noun, strings.Join(descriptions, ", ")) + // closing the final bracket + tail += fmt.Sprintf("%s}", strings.Repeat(" ", 4)) + } else { + // if it isn't a subtask, we just add the descriptions as is joined by new line. + // this results in line like `1 task: { t1.1 }` which are more readable this way. + tail = fmt.Sprintf("%s \n}", strings.Join(descriptions, ", ")) } + msg := head + tail if t.PlanMode { - return "(plan) " + msg + msg = "(plan) " + msg } - return msg + return msg + "\n" } // Do will run through the set in the background, it may return an error immediately, diff --git a/pkg/utils/tasks/tasks_test.go b/pkg/utils/tasks/tasks_test.go index cf6aed2dcc..8946e9a533 100644 --- a/pkg/utils/tasks/tasks_test.go +++ b/pkg/utils/tasks/tasks_test.go @@ -21,9 +21,15 @@ var _ = Describe("TaskTree", func() { tasks.IsSubTask = true tasks.PlanMode = true tasks.Append(&TaskTree{Parallel: false, IsSubTask: true}) - Expect(tasks.Describe()).To(Equal("(plan) 2 sequential sub-tasks: { no tasks, no tasks }")) + fmt.Println(tasks.Describe()) + expected := []byte(`(plan) + 2 sequential sub-tasks: { + no tasks, + no tasks, + } +`) + Expect([]byte(tasks.Describe())).To(Equal(expected)) } - { tasks := &TaskTree{Parallel: false} subTask1 := &TaskTree{Parallel: false, IsSubTask: true} @@ -48,7 +54,17 @@ var _ = Describe("TaskTree", func() { tasks.Append(subTask2) subTask1.Append(subTask3) - Expect(tasks.Describe()).To(Equal("2 sequential tasks: { 2 sequential sub-tasks: { t1.1, 2 parallel sub-tasks: { t3.1, t3.2 } }, t2.1 }")) + Expect(tasks.Describe()).To(Equal(` +2 sequential tasks: { + 2 sequential sub-tasks: { + t1.1, + 2 parallel sub-tasks: { + t3.1, + t3.2, + }, + }, t2.1 +} +`)) } }) @@ -130,7 +146,17 @@ var _ = Describe("TaskTree", func() { }) subTask1.Append(subTask3) - Expect(tasks.Describe()).To(Equal("2 sequential tasks: { 2 sequential sub-tasks: { t1.1, 2 parallel sub-tasks: { t3.1, t3.2 } }, t2.1 }")) + Expect(tasks.Describe()).To(Equal(` +2 sequential tasks: { + 2 sequential sub-tasks: { + t1.1, + 2 parallel sub-tasks: { + t3.1, + t3.2, + }, + }, t2.1 +} +`)) status.startTime = time.Now() errs := tasks.DoAllSync() @@ -240,7 +266,17 @@ var _ = Describe("TaskTree", func() { }) subTask1.Append(subTask3) - Expect(tasks.Describe()).To(Equal("2 sequential tasks: { 2 sequential sub-tasks: { t1.1, 2 parallel sub-tasks: { t3.1, t3.2 } }, t2.1 }")) + Expect(tasks.Describe()).To(Equal(` +2 sequential tasks: { + 2 sequential sub-tasks: { + t1.1, + 2 parallel sub-tasks: { + t3.1, + t3.2, + }, + }, t2.1 +} +`)) status.startTime = time.Now() errs := tasks.DoAllSync()