Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix listing of taskrun #902

Merged
merged 1 commit into from
Apr 16, 2020
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
5 changes: 5 additions & 0 deletions pkg/cmd/clustertask/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/clustertask"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/task"
"github.com/tektoncd/cli/pkg/taskrun/list"
trsort "github.com/tektoncd/cli/pkg/taskrun/sort"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
Expand Down Expand Up @@ -183,6 +184,10 @@ func printClusterTaskDescription(s *cli.Stream, p cli.Params, tname string) erro
fmt.Fprintf(s.Err, "failed to get taskruns for clustertask %s \n", tname)
return err
}

// this is required as the same label is getting added for both task and ClusterTask
taskRuns.Items = task.FilterByRef(taskRuns.Items, "ClusterTask")

trsort.SortByStartTime(taskRuns.Items)

var data = struct {
Expand Down
25 changes: 22 additions & 3 deletions pkg/cmd/clustertask/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Test_ClusterTaskDescribe(t *testing.T) {
tb.TaskRun("taskrun-1", "ns",
tb.TaskRunLabel("tekton.dev/task", "clustertask-full"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("clustertask-1", tb.TaskRefKind(v1alpha1.ClusterTaskKind)),
tb.TaskRunTaskRef("clustertask-full", tb.TaskRefKind(v1alpha1.ClusterTaskKind)),
tb.TaskRunServiceAccountName("svc"),
tb.TaskRunParam("myarg", "value"),
tb.TaskRunParam("print", "booms", "booms", "booms"),
Expand All @@ -100,7 +100,7 @@ func Test_ClusterTaskDescribe(t *testing.T) {
tb.TaskRun("taskrun-2", "ns",
tb.TaskRunLabel("tekton.dev/task", "clustertask-one-everything"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("clustertask-1", tb.TaskRefKind(v1alpha1.ClusterTaskKind)),
tb.TaskRunTaskRef("clustertask-one-everything", tb.TaskRefKind(v1alpha1.ClusterTaskKind)),
tb.TaskRunServiceAccountName("svc"),
tb.TaskRunParam("myarg", "value"),
tb.TaskRunResources(tb.TaskRunResourcesInput("my-repo", tb.TaskResourceBindingRef("git"))),
Expand All @@ -117,7 +117,25 @@ func Test_ClusterTaskDescribe(t *testing.T) {
tb.TaskRun("taskrun-3", "ns",
tb.TaskRunLabel("tekton.dev/task", "clustertask-full"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("clustertask-1", tb.TaskRefKind(v1alpha1.ClusterTaskKind)),
tb.TaskRunTaskRef("clustertask-full", tb.TaskRefKind(v1alpha1.ClusterTaskKind)),
tb.TaskRunServiceAccountName("svc"),
tb.TaskRunParam("myarg", "value"),
tb.TaskRunParam("print", "booms", "booms", "booms"),
tb.TaskRunResources(tb.TaskRunResourcesInput("my-repo", tb.TaskResourceBindingRef("git"))),
tb.TaskRunResources(tb.TaskRunResourcesOutput("my-image", tb.TaskResourceBindingRef("image"))),
),
tb.TaskRunStatus(
tb.TaskRunStartTime(clock.Now().Add(-12*time.Minute)),
tb.StatusCondition(apis.Condition{
Status: corev1.ConditionUnknown,
Reason: resources.ReasonRunning,
}),
),
),
tb.TaskRun("taskrun-4", "ns",
tb.TaskRunLabel("tekton.dev/task", "clustertask-full"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("clustertask-full", tb.TaskRefKind(v1alpha1.NamespacedTaskKind)),
tb.TaskRunServiceAccountName("svc"),
tb.TaskRunParam("myarg", "value"),
tb.TaskRunParam("print", "booms", "booms", "booms"),
Expand Down Expand Up @@ -151,6 +169,7 @@ func Test_ClusterTaskDescribe(t *testing.T) {
cb.UnstructuredTR(taskruns[0], version),
cb.UnstructuredTR(taskruns[1], version),
cb.UnstructuredTR(taskruns[2], version),
cb.UnstructuredTR(taskruns[3], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/clustertask/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func startClusterTask(opt startOptions, args []string) error {

//TaskRuns are namespaced so using same LastRun method as Task
if opt.Last {
trLast, err := task.LastRun(cs.Tekton, ctname, opt.cliparams.Namespace(), "clustertask")
trLast, err := task.LastRun(cs.Tekton, ctname, opt.cliparams.Namespace(), "ClusterTask")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/clustertask/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func Test_ClusterTask_Start(t *testing.T) {
input: seeds[2],
inputStream: nil,
wantError: true,
want: "no taskruns related to clustertask clustertask-1 found in namespace ns",
want: "no taskruns related to ClusterTask clustertask-1 found in namespace ns",
},
{
name: "Start clustertask",
Expand Down
18 changes: 11 additions & 7 deletions pkg/cmd/task/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ func printTaskDescription(s *cli.Stream, p cli.Params, tname string) error {
return fmt.Errorf("failed to create tekton client")
}

task, err := task.Get(cs, tname, metav1.GetOptions{}, p.Namespace())
t, err := task.Get(cs, tname, metav1.GetOptions{}, p.Namespace())
if err != nil {
fmt.Fprintf(s.Err, "failed to get task %s\n", tname)
return err
}

if task.Spec.Resources != nil {
task.Spec.Resources.Inputs = sortResourcesByTypeAndName(task.Spec.Resources.Inputs)
task.Spec.Resources.Outputs = sortResourcesByTypeAndName(task.Spec.Resources.Outputs)
if t.Spec.Resources != nil {
t.Spec.Resources.Inputs = sortResourcesByTypeAndName(t.Spec.Resources.Inputs)
t.Spec.Resources.Outputs = sortResourcesByTypeAndName(t.Spec.Resources.Outputs)
}

opts := metav1.ListOptions{
Expand All @@ -190,14 +190,18 @@ func printTaskDescription(s *cli.Stream, p cli.Params, tname string) error {
fmt.Fprintf(s.Err, "failed to get taskruns for task %s \n", tname)
return err
}

// this is required as the same label is getting added for both task and ClusterTask
taskRuns.Items = task.FilterByRef(taskRuns.Items, "Task")

trsort.SortByStartTime(taskRuns.Items)

var data = struct {
Task *v1beta1.Task
TaskRuns *v1beta1.TaskRunList
Time clockwork.Clock
}{
Task: task,
Task: t,
TaskRuns: taskRuns,
Time: p.Time(),
}
Expand All @@ -211,8 +215,8 @@ func printTaskDescription(s *cli.Stream, p cli.Params, tname string) error {
}

w := tabwriter.NewWriter(s.Out, 0, 5, 3, ' ', tabwriter.TabIndent)
t := template.Must(template.New("Describe Task").Funcs(funcMap).Parse(describeTemplate))
err = t.Execute(w, data)
tparsed := template.Must(template.New("Describe Task").Funcs(funcMap).Parse(describeTemplate))
err = tparsed.Execute(w, data)
if err != nil {
fmt.Fprintf(s.Err, "Failed to execute template \n")
return err
Expand Down
47 changes: 45 additions & 2 deletions pkg/cmd/task/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestTaskDescribe_Full(t *testing.T) {
taskRuns := []*v1alpha1.TaskRun{
tb.TaskRun("tr-1", "ns",
tb.TaskRunLabel("tekton.dev/task", "task-1"),
tb.TaskRunSpec(tb.TaskRunTaskRef("task-1")),
tb.TaskRunSpec(tb.TaskRunTaskRef("task-1", tb.TaskRefKind(v1alpha1.NamespacedTaskKind))),
tb.TaskRunStatus(
tb.StatusCondition(apis.Condition{
Status: corev1.ConditionFalse,
Expand All @@ -247,7 +247,19 @@ func TestTaskDescribe_Full(t *testing.T) {
),
tb.TaskRun("tr-2", "ns",
tb.TaskRunLabel("tekton.dev/task", "task-1"),
tb.TaskRunSpec(tb.TaskRunTaskRef("task-1")),
tb.TaskRunSpec(tb.TaskRunTaskRef("task-1", tb.TaskRefKind(v1alpha1.NamespacedTaskKind))),
tb.TaskRunStatus(
tb.StatusCondition(apis.Condition{
Status: corev1.ConditionTrue,
Reason: resources.ReasonSucceeded,
}),
tb.TaskRunStartTime(clock.Now().Add(10*time.Minute)),
cb.TaskRunCompletionTime(clock.Now().Add(17*time.Minute)),
),
),
tb.TaskRun("tr-3", "ns",
tb.TaskRunLabel("tekton.dev/task", "task-1"),
tb.TaskRunSpec(tb.TaskRunTaskRef("task-1", tb.TaskRefKind(v1alpha1.ClusterTaskKind))),
tb.TaskRunStatus(
tb.StatusCondition(apis.Condition{
Status: corev1.ConditionTrue,
Expand All @@ -272,6 +284,7 @@ func TestTaskDescribe_Full(t *testing.T) {
cb.UnstructuredT(tasks[0], version),
cb.UnstructuredTR(taskRuns[0], version),
cb.UnstructuredTR(taskRuns[1], version),
cb.UnstructuredTR(taskRuns[2], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
Expand Down Expand Up @@ -476,6 +489,7 @@ func TestTaskV1beta1Describe_Full(t *testing.T) {
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{
Name: "task-1",
Kind: v1beta1.NamespacedTaskKind,
},
},
Status: v1beta1.TaskRunStatus{
Expand All @@ -502,6 +516,7 @@ func TestTaskV1beta1Describe_Full(t *testing.T) {
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{
Name: "task-1",
Kind: v1beta1.NamespacedTaskKind,
},
},
Status: v1beta1.TaskRunStatus{
Expand All @@ -519,6 +534,33 @@ func TestTaskV1beta1Describe_Full(t *testing.T) {
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "tr-3",
Labels: map[string]string{"tekton.dev/task": "task-1"},
},
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{
Name: "task-1",
Kind: v1beta1.ClusterTaskKind,
},
},
Status: v1beta1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: duckv1beta1.Conditions{
{
Status: corev1.ConditionFalse,
Reason: resources.ReasonFailed,
},
},
},
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
StartTime: &metav1.Time{Time: clock.Now()},
CompletionTime: &metav1.Time{Time: clock.Now().Add(5 * time.Minute)},
},
},
},
}

namespaces := []*corev1.Namespace{
Expand All @@ -535,6 +577,7 @@ func TestTaskV1beta1Describe_Full(t *testing.T) {
cb.UnstructuredV1beta1T(tasks[0], version),
cb.UnstructuredV1beta1TR(taskRuns[0], version),
cb.UnstructuredV1beta1TR(taskRuns[1], version),
cb.UnstructuredV1beta1TR(taskRuns[2], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/task/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func useTaskRunFrom(opt startOptions, tr *v1alpha1.TaskRun, cs *cli.Clients, tna
err error
)
if opt.Last {
trUsed, err = task.LastRun(cs.Tekton, tname, opt.cliparams.Namespace(), "task")
trUsed, err = task.LastRun(cs.Tekton, tname, opt.cliparams.Namespace(), "Task")
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/cmd/task/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func Test_start_task_last(t *testing.T) {
tb.TaskRun("taskrun-123", "ns",
tb.TaskRunLabel("tekton.dev/task", "task"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("task"),
tb.TaskRunTaskRef("task", tb.TaskRefKind(v1alpha1.NamespacedTaskKind)),
tb.TaskRunServiceAccountName("svc"),
tb.TaskRunInputs(tb.TaskRunInputsParam("myarg", "value")),
tb.TaskRunInputs(tb.TaskRunInputsParam("print", "booms", "booms", "booms")),
Expand Down Expand Up @@ -403,13 +403,13 @@ func Test_start_use_taskrun(t *testing.T) {
tb.TaskRun("happy", "ns",
tb.TaskRunLabel("tekton.dev/task", "task"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("task"),
tb.TaskRunTaskRef("task", tb.TaskRefKind(v1alpha1.NamespacedTaskKind)),
),
),
tb.TaskRun("camper", "ns",
tb.TaskRunLabel("tekton.dev/task", "task"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("task"),
tb.TaskRunTaskRef("task", tb.TaskRefKind(v1alpha1.NamespacedTaskKind)),
tb.TaskRunServiceAccountName("camper"),
tb.TaskRunTimeout(timeoutDuration),
),
Expand Down Expand Up @@ -480,7 +480,7 @@ func Test_start_task_last_generate_name(t *testing.T) {
tb.TaskRun("taskrun-123", "ns",
tb.TaskRunLabel("tekton.dev/task", "task"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("task"),
tb.TaskRunTaskRef("task", tb.TaskRefKind(v1alpha1.NamespacedTaskKind)),
tb.TaskRunServiceAccountName("svc"),
tb.TaskRunInputs(tb.TaskRunInputsParam("myarg", "value")),
tb.TaskRunInputs(tb.TaskRunInputsParam("print", "booms", "booms", "booms")),
Expand Down Expand Up @@ -556,7 +556,7 @@ func Test_start_task_last_with_prefix_name(t *testing.T) {
tb.TaskRun("taskrun-123", "ns",
tb.TaskRunLabel("tekton.dev/task", "task"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("task"),
tb.TaskRunTaskRef("task", tb.TaskRefKind(v1alpha1.NamespacedTaskKind)),
tb.TaskRunServiceAccountName("svc"),
tb.TaskRunInputs(tb.TaskRunInputsParam("myarg", "value")),
tb.TaskRunInputs(tb.TaskRunInputsParam("print", "booms", "booms", "booms")),
Expand Down Expand Up @@ -631,7 +631,7 @@ func Test_start_task_with_prefix_name(t *testing.T) {
tb.TaskRun("taskrun-123", "ns",
tb.TaskRunLabel("tekton.dev/task", "task"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("task"),
tb.TaskRunTaskRef("task", tb.TaskRefKind(v1alpha1.NamespacedTaskKind)),
tb.TaskRunServiceAccountName("svc"),
tb.TaskRunInputs(tb.TaskRunInputsParam("myarg", "value")),
tb.TaskRunInputs(tb.TaskRunInputsParam("print", "booms", "booms", "booms")),
Expand Down Expand Up @@ -704,7 +704,7 @@ func Test_start_task_last_with_inputs(t *testing.T) {
tb.TaskRun("taskrun-123", "ns",
tb.TaskRunLabel("tekton.dev/task", "task"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("task"),
tb.TaskRunTaskRef("task", tb.TaskRefKind(v1alpha1.NamespacedTaskKind)),
tb.TaskRunServiceAccountName("svc"),
tb.TaskRunInputs(tb.TaskRunInputsParam("myarg", "value")),
tb.TaskRunInputs(tb.TaskRunInputsParam("print", "booms", "booms", "booms")),
Expand Down Expand Up @@ -815,7 +815,7 @@ func Test_start_task_last_without_pipelinerun(t *testing.T) {

task := Command(p)
got, _ := test.ExecuteCommand(task, "start", "task-1", "--last", "-n", "ns")
expected := "Error: no taskruns related to task task-1 found in namespace ns\n"
expected := "Error: no taskruns related to Task task-1 found in namespace ns\n"
test.AssertOutput(t, expected, got)
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/cmd/taskrun/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/printer"
taskpkg "github.com/tektoncd/cli/pkg/task"
trlist "github.com/tektoncd/cli/pkg/taskrun/list"
trsort "github.com/tektoncd/cli/pkg/taskrun/sort"
"github.com/tektoncd/cli/pkg/validate"
Expand Down Expand Up @@ -198,6 +199,11 @@ func list(p cli.Params, task string, limit int, labelselector string, allnamespa
return nil, err
}

// this is required as the same label is getting added for both task and ClusterTask
if task != "" {
trs.Items = taskpkg.FilterByRef(trs.Items, "Task")
}

trslen := len(trs.Items)

if trslen != 0 {
Expand Down
Loading