Skip to content

Commit

Permalink
Add WorkflowContext methods to Workflow CRD
Browse files Browse the repository at this point in the history
* Add WorkflowContext conversion func
* Moved FrozenTime to root of module

Signed-off-by: Micah Hausler <[email protected]>
  • Loading branch information
micahhausler committed Mar 22, 2022
1 parent e1e2af6 commit 87ffc62
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 3 deletions.
File renamed without changes.
80 changes: 80 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,83 @@ func (w *Workflow) GetStartTime() *metav1.Time {
}
return nil
}

type taskInfo struct {
CurrentWorker string
CurrentTask string
CurrentTaskIndex int
CurrentAction string
CurrentActionIndex int
CurrentActionState string
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 {
for ai, action := range task.Actions {
actionCount++
// Find either the first action in a non-terminal state or the first action in an unsuccessful terminal state
if (action.Status == "STATE_PENDING" || action.Status == "STATE_RUNNING" ||
action.Status == "STATE_FAILED" || action.Status == "STATE_TIMEOUT") && !found {
taskIndex = ti
actionTaskIndex = ai
found = true
}
if !found {
actionIndex++
}
}
}

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() string {
return w.getTaskActionInfo().CurrentActionState
}

func (w *Workflow) GetTotalNumberOfActions() int {
return w.getTaskActionInfo().TotalNumberOfActions
}
173 changes: 172 additions & 1 deletion 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 @@ -180,3 +180,174 @@ 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: "STATE_RUNNING",
GlobalTimeout: 600,
Tasks: []Task{
{
Name: "empty task",
// WorkerAddr: "", // intentionally not set
Actions: []Action{
{
Name: "empty action",
Status: "STATE_FAILED",
},
},
},
{
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: "STATE_SUCCESS",
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: "STATE_RUNNING",
StartedAt: TestNow.MetaV1AfterSec(21),
},
},
},
},
},
},
taskInfo{
TotalNumberOfActions: 3,
CurrentTaskIndex: 0,
CurrentTask: "empty task",
CurrentWorker: "",
CurrentAction: "empty action",
CurrentActionState: "STATE_FAILED",
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: "STATE_RUNNING",
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: "STATE_SUCCESS",
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: "STATE_SUCCESS",
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: "STATE_RUNNING",
StartedAt: TestNow.MetaV1AfterSec(21),
},
},
},
},
},
},
taskInfo{
TotalNumberOfActions: 3,
CurrentTaskIndex: 1,
CurrentTask: "os-installation",
CurrentWorker: "3c:ec:ef:4c:4f:54",
CurrentAction: "write-file",
CurrentActionState: "STATE_RUNNING",
CurrentActionIndex: 2,
},
},
}
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)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/controllers/workflow/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/tinkerbell/tink/internal/tests"
"github.com/tinkerbell/tink/pkg/apis/core/v1alpha1"
"github.com/tinkerbell/tink/pkg/internal/tests"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down
2 changes: 1 addition & 1 deletion pkg/convert/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/tinkerbell/tink/internal/tests"
"github.com/tinkerbell/tink/pkg/apis/core/v1alpha1"
"github.com/tinkerbell/tink/pkg/internal/tests"
prototemplate "github.com/tinkerbell/tink/protos/template"
"google.golang.org/protobuf/testing/protocmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
15 changes: 15 additions & 0 deletions pkg/convert/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func WorkflowToWorkflowContext(wf *v1alpha1.Workflow) *protoworkflow.WorkflowContext {
if wf == nil {
return nil
}
return &protoworkflow.WorkflowContext{
WorkflowId: wf.GetName(),
CurrentWorker: wf.GetCurrentWorker(),
CurrentTask: wf.GetCurrentTask(),
CurrentAction: wf.GetCurrentAction(),
CurrentActionIndex: int64(wf.GetCurrentActionIndex()),
CurrentActionState: protoworkflow.State(protoworkflow.State_value[wf.GetCurrentActionState()]),
TotalNumberOfActions: int64(wf.GetTotalNumberOfActions()),
}
}

func WorkflowYAMLToStatus(wf *workflow.Workflow) *v1alpha1.WorkflowStatus {
if wf == nil {
return nil
Expand Down
Loading

0 comments on commit 87ffc62

Please sign in to comment.