Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions pkg/cfn/manager/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -110,27 +114,60 @@ 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)
Expect(tasks.Describe()).To(Equal(`1 task: { create cluster control plane "test-cluster" }`))
}
{
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",
}
}
`))
}
})
})
Expand Down
75 changes: 55 additions & 20 deletions pkg/utils/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
46 changes: 41 additions & 5 deletions pkg/utils/tasks/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
}
`))
}
})

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down