Skip to content

Commit

Permalink
Merge 24e36a9 into e1e2af6
Browse files Browse the repository at this point in the history
  • Loading branch information
micahhausler authored Mar 30, 2022
2 parents e1e2af6 + 24e36a9 commit 08991a2
Show file tree
Hide file tree
Showing 9 changed files with 436 additions and 21 deletions.
File renamed without changes.
85 changes: 85 additions & 0 deletions pkg/apis/core/v1alpha1/workflow_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,88 @@ func (w *Workflow) GetStartTime() *metav1.Time {
}
return nil
}

type taskInfo struct {
CurrentWorker string
CurrentTask string
CurrentTaskIndex int
CurrentAction string
CurrentActionIndex int
CurrentActionState WorkflowState
TotalNumberOfActions int
}

// helper function for task info.
func (w *Workflow) getTaskActionInfo() taskInfo {
var (
found bool
taskIndex = -1
actionIndex int
actionTaskIndex int
actionCount int
)
for ti, task := range w.Status.Tasks {
actionCount += len(task.Actions)
if found {
continue
}
INNER:
for ai, action := range task.Actions {
// Find the first non-successful action
switch action.Status {
case WorkflowStateSuccess:
actionIndex++
continue
case WorkflowStatePending, WorkflowStateRunning, WorkflowStateFailed, WorkflowStateTimeout:
taskIndex = ti
actionTaskIndex = ai
found = true
break INNER
}
}
}

ti := taskInfo{
TotalNumberOfActions: actionCount,
CurrentActionIndex: actionIndex,
}
if taskIndex >= 0 {
ti.CurrentWorker = w.Status.Tasks[taskIndex].WorkerAddr
ti.CurrentTask = w.Status.Tasks[taskIndex].Name
ti.CurrentTaskIndex = taskIndex
}
if taskIndex >= 0 && actionIndex >= 0 {
ti.CurrentAction = w.Status.Tasks[taskIndex].Actions[actionTaskIndex].Name
ti.CurrentActionState = w.Status.Tasks[taskIndex].Actions[actionTaskIndex].Status
}

return ti
}

func (w *Workflow) GetCurrentWorker() string {
return w.getTaskActionInfo().CurrentWorker
}

func (w *Workflow) GetCurrentTask() string {
return w.getTaskActionInfo().CurrentTask
}

func (w *Workflow) GetCurrentTaskIndex() int {
return w.getTaskActionInfo().CurrentTaskIndex
}

func (w *Workflow) GetCurrentAction() string {
return w.getTaskActionInfo().CurrentAction
}

func (w *Workflow) GetCurrentActionIndex() int {
return w.getTaskActionInfo().CurrentActionIndex
}

func (w *Workflow) GetCurrentActionState() WorkflowState {
return w.getTaskActionInfo().CurrentActionState
}

func (w *Workflow) GetTotalNumberOfActions() int {
return w.getTaskActionInfo().TotalNumberOfActions
}
240 changes: 234 additions & 6 deletions pkg/apis/core/v1alpha1/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
"time"

"github.com/tinkerbell/tink/pkg/internal/tests"
"github.com/tinkerbell/tink/internal/tests"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -89,7 +89,7 @@ func TestGetStartTime(t *testing.T) {
},
Spec: WorkflowSpec{},
Status: WorkflowStatus{
State: "STATE_RUNNING",
State: WorkflowStateRunning,
GlobalTimeout: 600,
Tasks: []Task{
{
Expand All @@ -105,7 +105,7 @@ func TestGetStartTime(t *testing.T) {
"DEST_DISK": "/dev/nvme0n1",
"IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz",
},
Status: "STATE_SUCCESS",
Status: WorkflowStateSuccess,
StartedAt: TestNow.MetaV1Now(),
Seconds: 20,
},
Expand All @@ -118,7 +118,7 @@ func TestGetStartTime(t *testing.T) {
"DEST_DISK": "/dev/nvme0n1",
"IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz",
},
Status: "STATE_RUNNING",
Status: WorkflowStateRunning,
StartedAt: TestNow.MetaV1AfterSec(21),
},
},
Expand All @@ -141,7 +141,7 @@ func TestGetStartTime(t *testing.T) {
},
Spec: WorkflowSpec{},
Status: WorkflowStatus{
State: "STATE_PENDING",
State: WorkflowStatePending,
GlobalTimeout: 600,
Tasks: []Task{
{
Expand All @@ -157,7 +157,7 @@ func TestGetStartTime(t *testing.T) {
"DEST_DISK": "/dev/nvme0n1",
"IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz",
},
Status: "STATE_PENDING",
Status: WorkflowStatePending,
StartedAt: nil,
},
},
Expand All @@ -180,3 +180,231 @@ func TestGetStartTime(t *testing.T) {
})
}
}

func TestWorkflowMethods(t *testing.T) {
cases := []struct {
name string
wf *Workflow
want taskInfo
}{
{
"Empty wflow",
&Workflow{
ObjectMeta: metav1.ObjectMeta{
Name: "debian",
Namespace: "default",
},
},
taskInfo{},
},
{
"invalid workflow",
&Workflow{
TypeMeta: metav1.TypeMeta{
Kind: "Workflow",
APIVersion: "tinkerbell.org/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "debian",
Namespace: "default",
},
Spec: WorkflowSpec{},
Status: WorkflowStatus{
State: WorkflowStateRunning,
GlobalTimeout: 600,
Tasks: []Task{
{
Name: "empty task",
// WorkerAddr: "", // intentionally not set
Actions: []Action{
{
Name: "empty action",
Status: WorkflowStateFailed,
},
},
},
{
Name: "os-installation",
WorkerAddr: "3c:ec:ef:4c:4f:54",
Actions: []Action{
{
Name: "stream-debian-image",
Image: "quay.io/tinkerbell-actions/image2disk:v1.0.0",
Timeout: 60,
Environment: map[string]string{
"COMPRESSED": "true",
"DEST_DISK": "/dev/nvme0n1",
"IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz",
},
Status: WorkflowStateSuccess,
StartedAt: TestNow.MetaV1Now(),
Seconds: 20,
},
{
Name: "stream-debian-image",
Image: "quay.io/tinkerbell-actions/image2disk:v1.0.0",
Timeout: 60,
Environment: map[string]string{
"COMPRESSED": "true",
"DEST_DISK": "/dev/nvme0n1",
"IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz",
},
Status: WorkflowStateRunning,
StartedAt: TestNow.MetaV1AfterSec(21),
},
},
},
},
},
},
taskInfo{
TotalNumberOfActions: 3,
CurrentTaskIndex: 0,
CurrentTask: "empty task",
CurrentWorker: "",
CurrentAction: "empty action",
CurrentActionState: WorkflowStateFailed,
CurrentActionIndex: 0,
},
},
{
"Running workflow",
&Workflow{
TypeMeta: metav1.TypeMeta{
Kind: "Workflow",
APIVersion: "tinkerbell.org/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "debian",
Namespace: "default",
},
Spec: WorkflowSpec{},
Status: WorkflowStatus{
State: WorkflowStateRunning,
GlobalTimeout: 600,
Tasks: []Task{
{
Name: "bmc-manage",
WorkerAddr: "pbnj",
Actions: []Action{
{
Name: "configure-pxe",
Image: "quay.io/tinkerbell-actions/pbnj:v1.0.0",
Timeout: 20,
Status: WorkflowStateSuccess,
StartedAt: TestNow.MetaV1BeforeSec(15),
Seconds: 15,
},
},
},
{
Name: "os-installation",
WorkerAddr: "3c:ec:ef:4c:4f:54",
Actions: []Action{
{
Name: "stream-debian-image",
Image: "quay.io/tinkerbell-actions/image2disk:v1.0.0",
Timeout: 60,
Environment: map[string]string{
"COMPRESSED": "true",
"DEST_DISK": "/dev/nvme0n1",
"IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz",
},
Status: WorkflowStateSuccess,
StartedAt: TestNow.MetaV1Now(),
Seconds: 20,
},
{
Name: "write-file",
Image: "quay.io/tinkerbell-actions/writefile:v1.0.0",
Timeout: 60,
Environment: map[string]string{
"COMPRESSED": "true",
"DEST_DISK": "/dev/nvme0n1",
"IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz",
},
Status: WorkflowStateRunning,
StartedAt: TestNow.MetaV1AfterSec(21),
},
},
},
},
},
},
taskInfo{
TotalNumberOfActions: 3,
CurrentTaskIndex: 1,
CurrentTask: "os-installation",
CurrentWorker: "3c:ec:ef:4c:4f:54",
CurrentAction: "write-file",
CurrentActionState: WorkflowStateRunning,
CurrentActionIndex: 2,
},
},
{
"Pending workflow",
&Workflow{
TypeMeta: metav1.TypeMeta{
Kind: "Workflow",
APIVersion: "tinkerbell.org/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "debian",
Namespace: "default",
},
Spec: WorkflowSpec{},
Status: WorkflowStatus{
State: WorkflowStatePending,
GlobalTimeout: 600,
Tasks: []Task{
{
Name: "os-installation",
WorkerAddr: "3c:ec:ef:4c:4f:54",
Actions: []Action{
{
Name: "stream-debian-image",
Image: "quay.io/tinkerbell-actions/image2disk:v1.0.0",
Timeout: 60,
Environment: map[string]string{
"COMPRESSED": "true",
"DEST_DISK": "/dev/nvme0n1",
"IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz",
},
Status: WorkflowStatePending,
},
{
Name: "write-file",
Image: "quay.io/tinkerbell-actions/writefile:v1.0.0",
Timeout: 60,
Environment: map[string]string{
"COMPRESSED": "true",
"DEST_DISK": "/dev/nvme0n1",
"IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz",
},
Status: WorkflowStatePending,
},
},
},
},
},
},
taskInfo{
TotalNumberOfActions: 2,
CurrentTaskIndex: 0,
CurrentTask: "os-installation",
CurrentWorker: "3c:ec:ef:4c:4f:54",
CurrentAction: "stream-debian-image",
CurrentActionState: WorkflowStatePending,
CurrentActionIndex: 0,
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := tc.wf.getTaskActionInfo()
if got != tc.want {
t.Errorf("Got \n\t%#v\nwanted:\n\t%#v", got, tc.want)
}
})
}
}
Loading

0 comments on commit 08991a2

Please sign in to comment.