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

Enable auto-select support in ClusterTask/TaskRun if only one is present #1154

Merged
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
18 changes: 18 additions & 0 deletions pkg/clustertask/clustertask.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ import (

var clustertaskGroupResource = schema.GroupVersionResource{Group: "tekton.dev", Resource: "clustertasks"}

func GetAllClusterTaskNames(p cli.Params) ([]string, error) {
cs, err := p.Clients()
if err != nil {
return nil, err
}

clustertasks, err := List(cs, metav1.ListOptions{})
if err != nil {
return nil, err
}

ret := []string{}
for _, item := range clustertasks.Items {
ret = append(ret, item.ObjectMeta.Name)
}
return ret, nil
}

func List(c *cli.Clients, opts metav1.ListOptions) (*v1beta1.ClusterTaskList, error) {
unstructuredCT, err := actions.List(clustertaskGroupResource, c, "", opts)
if err != nil {
Expand Down
84 changes: 84 additions & 0 deletions pkg/clustertask/clustertask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,90 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestClusterTask_GetAllTaskNames(t *testing.T) {
version := "v1alpha1"
clock := clockwork.NewFakeClock()
ctdata := []*v1alpha1.ClusterTask{
{
ObjectMeta: metav1.ObjectMeta{
Name: "clustertask",
// created 5 minutes back
CreationTimestamp: metav1.Time{Time: clock.Now().Add(-5 * time.Minute)},
},
},
}
cs, _ := test.SeedTestData(t, pipelinetest.Data{
ClusterTasks: ctdata,
})
cs.Pipeline.Resources = cb.APIResourceList(version, []string{"clustertask"})
tdc := testDynamic.Options{}
dc, err := tdc.Client(
cb.UnstructuredCT(ctdata[0], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
}

ctdata2 := []*v1alpha1.ClusterTask{
{
ObjectMeta: metav1.ObjectMeta{
Name: "clustertask",
// created 5 minutes back
CreationTimestamp: metav1.Time{Time: clock.Now().Add(-5 * time.Minute)},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "clustertask2",
// created 5 minutes back
CreationTimestamp: metav1.Time{Time: clock.Now().Add(-5 * time.Minute)},
},
},
}
cs2, _ := test.SeedTestData(t, pipelinetest.Data{
ClusterTasks: ctdata2,
})
cs2.Pipeline.Resources = cb.APIResourceList(version, []string{"clustertask"})
tdc2 := testDynamic.Options{}
dc2, err := tdc2.Client(
cb.UnstructuredCT(ctdata2[0], version),
cb.UnstructuredCT(ctdata2[1], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
}

p := &test.Params{Tekton: cs.Pipeline, Clock: clock, Kube: cs.Kube, Dynamic: dc}
p2 := &test.Params{Tekton: cs2.Pipeline, Clock: clock, Kube: cs2.Kube, Dynamic: dc2}

testParams := []struct {
name string
params *test.Params
want []string
}{
{
name: "Single ClusterTask",
params: p,
want: []string{"clustertask"},
},
{
name: "Multi ClusterTasks",
params: p2,
want: []string{"clustertask", "clustertask2"},
},
}

for _, tp := range testParams {
t.Run(tp.name, func(t *testing.T) {
got, err := GetAllClusterTaskNames(tp.params)
if err != nil {
t.Errorf("unexpected Error")
}
test.AssertOutput(t, tp.want, got)
})
}
}

func TestClusterTask_List(t *testing.T) {
version := "v1alpha1"
clock := clockwork.NewFakeClock()
Expand Down
14 changes: 1 addition & 13 deletions pkg/cmd/clustertask/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func deleteClusterTasks(opts *options.DeleteOptions, s *cli.Stream, p cli.Params
})
switch {
case opts.DeleteAll:
cts, err := allClusterTaskNames(cs)
cts, err := clustertask.GetAllClusterTaskNames(p)
if err != nil {
return err
}
Expand All @@ -112,18 +112,6 @@ func deleteClusterTasks(opts *options.DeleteOptions, s *cli.Stream, p cli.Params
return d.Errors()
}

func allClusterTaskNames(cs *cli.Clients) ([]string, error) {
clusterTasks, err := clustertask.List(cs, metav1.ListOptions{})
if err != nil {
return nil, err
}
var names []string
for _, ct := range clusterTasks.Items {
names = append(names, ct.Name)
}
return names, nil
}

func taskRunLister(cs *cli.Clients, p cli.Params) func(string) ([]string, error) {
return func(taskName string) ([]string, error) {
lOpts := metav1.ListOptions{
Expand Down
23 changes: 11 additions & 12 deletions pkg/cmd/clustertask/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,18 @@ or
}

if len(args) == 0 {
err = askClusterTaskName(opts, p)
clusterTaskNames, err := clustertask.GetAllClusterTaskNames(p)
if err != nil {
return err
}
if len(clusterTaskNames) == 1 {
opts.ClusterTaskName = clusterTaskNames[0]
} else {
err = askClusterTaskName(opts, clusterTaskNames)
if err != nil {
return err
}
}
} else {
opts.ClusterTaskName = args[0]
}
Expand Down Expand Up @@ -261,20 +269,11 @@ func sortResourcesByTypeAndName(tres []v1beta1.TaskResource) []v1beta1.TaskResou
return tres
}

func askClusterTaskName(opts *options.DescribeOptions, p cli.Params) error {
cs, err := p.Clients()
if err != nil {
return err
}
clusterTaskNames, err := allClusterTaskNames(cs)
if err != nil {
return err
}
func askClusterTaskName(opts *options.DescribeOptions, clusterTaskNames []string) error {
if len(clusterTaskNames) == 0 {
return fmt.Errorf("no ClusterTasks found")
}

err = opts.Ask(options.ResourceNameClusterTask, clusterTaskNames)
err := opts.Ask(options.ResourceNameClusterTask, clusterTaskNames)
if err != nil {
return err
}
Expand Down
57 changes: 57 additions & 0 deletions pkg/cmd/clustertask/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,34 @@ func Test_ClusterTaskDescribe(t *testing.T) {
}
}

func TestClusterTaskDescribe_WithoutNameIfOnlyOneClusterTaskPresent(t *testing.T) {
cstasks := []*v1alpha1.ClusterTask{
{
ObjectMeta: metav1.ObjectMeta{
Name: "task-1",
},
},
}
version := "v1alpha1"
tdc := testDynamic.Options{}
dynamic, err := tdc.Client(
cb.UnstructuredCT(cstasks[0], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
}

cs, _ := test.SeedTestData(t, pipelinetest.Data{ClusterTasks: cstasks})
cs.Pipeline.Resources = cb.APIResourceList(version, []string{"clustertask", "taskrun"})
p := &test.Params{Tekton: cs.Pipeline, Kube: cs.Kube, Dynamic: dynamic}
clusterTask := Command(p)
out, err := test.ExecuteCommand(clusterTask, "desc")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name()))
}

func TestClusterTask_custom_output(t *testing.T) {
name := "clustertask"
expected := "clustertask.tekton.dev/" + name
Expand Down Expand Up @@ -467,3 +495,32 @@ func TestClusterTaskDescribe_With_Workspaces(t *testing.T) {
}
golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name()))
}

func TestClusterTaskDescribe_WithoutNameIfOnlyOneV1beta1ClusterTaskPresent(t *testing.T) {
cttasks := []*v1beta1.ClusterTask{
{
ObjectMeta: metav1.ObjectMeta{
Name: "task-1",
},
},
}

version := "v1beta1"
tdc := testDynamic.Options{}
dynamic, err := tdc.Client(
cb.UnstructuredV1beta1CT(cttasks[0], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
}

cs, _ := test.SeedV1beta1TestData(t, pipelinev1beta1test.Data{ClusterTasks: cttasks})
cs.Pipeline.Resources = cb.APIResourceList(version, []string{"clustertask", "taskrun"})
p := &test.Params{Tekton: cs.Pipeline, Kube: cs.Kube, Dynamic: dynamic}
cttask := Command(p)
out, err := test.ExecuteCommand(cttask, "desc")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name()))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Name: task-1

Input Resources

No input resources

Output Resources

No output resources

Params

No params

Results

No results

Workspaces

No workspaces

Steps

No steps

Taskruns

No taskruns
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Name: task-1

Input Resources

No input resources

Output Resources

No output resources

Params

No params

Results

No results

Workspaces

No workspaces

Steps

No steps

Taskruns

No taskruns
20 changes: 11 additions & 9 deletions pkg/cmd/taskrun/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,21 @@ or
}

if len(args) == 0 {
lOpts := metav1.ListOptions{}
if !opts.Last {
err = askTaskRunName(opts, p)
trs, err := trlist.GetAllTaskRuns(p, lOpts, opts.Limit)
if err != nil {
return err
}
if len(trs) == 1 {
opts.TaskrunName = strings.Fields(trs[0])[0]
} else {
err = askTaskRunName(opts, trs)
if err != nil {
return err
}
}
} else {
lOpts := metav1.ListOptions{}
trs, err := trlist.GetAllTaskRuns(p, lOpts, 1)
if err != nil {
return err
Expand Down Expand Up @@ -113,18 +121,12 @@ or
return c
}

func askTaskRunName(opts *options.DescribeOptions, p cli.Params) error {
lOpts := metav1.ListOptions{}

func askTaskRunName(opts *options.DescribeOptions, trs []string) error {
err := opts.ValidateOpts()
if err != nil {
return err
}

trs, err := trlist.GetAllTaskRuns(opts.Params, lOpts, opts.Limit)
if err != nil {
return err
}
if len(trs) == 0 {
return fmt.Errorf("no TaskRuns found")
}
Expand Down
Loading