Skip to content

Commit

Permalink
Refactoring resource list
Browse files Browse the repository at this point in the history
This will refactor the listing of pipeline resource
in `tkn res list` and `tkn task describe taskname`
and `tkn pipeline describe pipelinename` commands.

Now it will show the resources sorted by type and
then further sorted by name

Refactor tests accordingly
  • Loading branch information
piyush-garg committed Oct 9, 2019
1 parent aea0869 commit 5ec20b2
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 12 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/evanphx/json-patch v4.1.0+incompatible // indirect
github.com/fatih/color v1.7.0
github.com/ghodss/yaml v1.0.0 // indirect
github.com/gogo/protobuf v1.2.0 // indirect
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
github.com/google/go-cmp v0.3.1
github.com/google/go-containerregistry v0.0.0-20190320210540-8d4083db9aa0 // indirect
Expand All @@ -40,6 +42,7 @@ require (
github.com/onsi/gomega v1.7.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829 // indirect
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect
github.com/prometheus/procfs v0.0.0-20190322151404-55ae3d9d5573 // indirect
github.com/spf13/cobra v0.0.5
Expand Down
22 changes: 22 additions & 0 deletions pkg/cmd/pipeline/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pipeline
import (
"fmt"
"io"
"sort"
"text/tabwriter"
"text/template"

Expand Down Expand Up @@ -94,6 +95,10 @@ func printPipelineDescription(out io.Writer, p cli.Params, pname string) error {
return err
}

if len(pipeline.Spec.Resources) > 0 {
pipeline.Spec.Resources = sortResourcesByTypeAndName(pipeline.Spec.Resources)
}

opts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/pipeline=%s", pname),
}
Expand Down Expand Up @@ -129,3 +134,20 @@ func printPipelineDescription(out io.Writer, p cli.Params, pname string) error {

return w.Flush()
}

// this will sort the Resource by Type and then by Name
func sortResourcesByTypeAndName(pres []v1alpha1.PipelineDeclaredResource) []v1alpha1.PipelineDeclaredResource {
sort.Slice(pres, func(i, j int) bool {
if pres[j].Type < pres[i].Type {
return false
}

if pres[j].Type > pres[i].Type {
return true
}

return pres[j].Name > pres[i].Name
})

return pres
}
77 changes: 77 additions & 0 deletions pkg/cmd/pipeline/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,80 @@ func TestPipelinesDescribe_with_resource_task_run(t *testing.T) {
t.Errorf("Unexpected output mismatch: \n%s\n", d)
}
}

func TestPipelinesDescribe_with_multiple_resource_task_run(t *testing.T) {
clock := clockwork.NewFakeClock()

cs, _ := test.SeedTestData(t, pipelinetest.Data{
Pipelines: []*v1alpha1.Pipeline{
tb.Pipeline("pipeline", "ns",
// created 5 minutes back
cb.PipelineCreationTimestamp(clock.Now().Add(-5*time.Minute)),
tb.PipelineSpec(
tb.PipelineTask("task", "taskref",
tb.RunAfter("one", "two"),
),
tb.PipelineDeclaredResource("name", v1alpha1.PipelineResourceTypeGit),
tb.PipelineDeclaredResource("code", v1alpha1.PipelineResourceTypeGit),
tb.PipelineDeclaredResource("code-image", v1alpha1.PipelineResourceTypeImage),
tb.PipelineDeclaredResource("artifact-image", v1alpha1.PipelineResourceTypeImage),
tb.PipelineDeclaredResource("repo", v1alpha1.PipelineResourceTypeGit),
),
),
},
PipelineRuns: []*v1alpha1.PipelineRun{

tb.PipelineRun("pipeline-run-1", "ns",
cb.PipelineRunCreationTimestamp(clock.Now()),
tb.PipelineRunLabel("tekton.dev/pipeline", "pipeline"),
tb.PipelineRunSpec("pipeline"),
tb.PipelineRunStatus(
tb.PipelineRunStatusCondition(apis.Condition{
Status: corev1.ConditionTrue,
Reason: resources.ReasonSucceeded,
}),
// pipeline run starts now
tb.PipelineRunStartTime(clock.Now()),
// takes 10 minutes to complete
cb.PipelineRunCompletionTime(clock.Now().Add(10*time.Minute)),
),
),
},
})

p := &test.Params{Tekton: cs.Pipeline, Clock: clock}
pipeline := Command(p)

// -5 : pipeline created
// 0 : pipeline run - 1 started
// 10 : pipeline run - 1 finished
// 15 : <<< now run pipeline ls << - advance clock to this point

clock.Advance(15 * time.Minute)
got, err := test.ExecuteCommand(pipeline, "desc", "-n", "ns", "pipeline")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

expected := []string{
"Name: pipeline",
"\nResources",
"NAME TYPE",
"code git",
"name git",
"repo git",
"artifact-image image",
"code-image image\n",
"Tasks",
"NAME TASKREF RUNAFTER",
"task taskref [one two]\n",
"Pipelineruns",
"NAME STARTED DURATION STATUS",
"pipeline-run-1 15 minutes ago 10 minutes Succeeded\n",
}

text := strings.Join(expected, "\n")
if d := cmp.Diff(text, got); d != "" {
t.Errorf("Unexpected output mismatch: \n%s\n", d)
}
}
25 changes: 23 additions & 2 deletions pkg/cmd/pipelineresource/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pipelineresource
import (
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"

Expand All @@ -25,9 +26,8 @@ import (
"github.com/tektoncd/cli/pkg/printer"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
"k8s.io/apimachinery/pkg/runtime/schema"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)

Expand Down Expand Up @@ -122,6 +122,10 @@ func list(client versioned.Interface, namespace string, resourceType string) (*v
pres.Items = filterByType(pres.Items, resourceType)
}

if len(pres.Items) > 0 {
pres.Items = sortResourcesByTypeAndName(pres.Items)
}

// NOTE: this is required for -o json|yaml to work properly since
// tektoncd go client fails to set these; probably a bug
pres.GetObjectKind().SetGroupVersionKind(
Expand Down Expand Up @@ -175,3 +179,20 @@ func filterByType(resources []v1alpha1.PipelineResource, resourceType string) (r
}
return
}

// this will sort the Pipeline Resource by Type and then by Name
func sortResourcesByTypeAndName(pres []v1alpha1.PipelineResource) []v1alpha1.PipelineResource {
sort.Slice(pres, func(i, j int) bool {
if pres[j].Spec.Type < pres[i].Spec.Type {
return false
}

if pres[j].Spec.Type > pres[i].Spec.Type {
return true
}

return pres[j].Name > pres[i].Name
})

return pres
}
17 changes: 12 additions & 5 deletions pkg/cmd/pipelineresource/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import (
func TestPipelineResourceList(t *testing.T) {

pres := []*v1alpha1.PipelineResource{
tb.PipelineResource("test", "test-ns-1",
tb.PipelineResourceSpec("git",
tb.PipelineResourceSpecParam("url", "[email protected]:tektoncd/cli-new.git"),
),
),
tb.PipelineResource("test-1", "test-ns-1",
tb.PipelineResourceSpec("image",
tb.PipelineResourceSpecParam("URL", "quey.io/tekton/controller"),
Expand Down Expand Up @@ -60,8 +65,9 @@ func TestPipelineResourceList(t *testing.T) {
args: []string{"list", "-n", "test-ns-1"},
expected: []string{
"NAME TYPE DETAILS",
"test-1 image URL: quey.io/tekton/controller",
"test git url: [email protected]:tektoncd/cli-new.git",
"test-2 git url: [email protected]:tektoncd/cli.git",
"test-1 image URL: quey.io/tekton/controller",
"test-3 image ---",
"",
},
Expand All @@ -79,10 +85,10 @@ func TestPipelineResourceList(t *testing.T) {
{
name: "Single Pipeline Resource by type",
command: command(t, pres),
args: []string{"list", "-n", "test-ns-1", "-t", "git"},
args: []string{"list", "-n", "test-ns-2", "-t", "image"},
expected: []string{
"NAME TYPE DETAILS",
"test-2 git url: [email protected]:tektoncd/cli.git",
"NAME TYPE DETAILS",
"test-4 image URL: quey.io/tekton/webhook",
"",
},
},
Expand Down Expand Up @@ -111,8 +117,9 @@ func TestPipelineResourceList(t *testing.T) {
command: command(t, pres),
args: []string{"list", "-n", "test-ns-1", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"},
expected: []string{
"test-1",
"test",
"test-2",
"test-1",
"test-3",
"",
},
Expand Down
27 changes: 27 additions & 0 deletions pkg/cmd/task/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package task

import (
"fmt"
"sort"
"text/tabwriter"
"text/template"

Expand Down Expand Up @@ -138,6 +139,15 @@ func printTaskDescription(s *cli.Stream, p cli.Params, tname string) error {
fmt.Fprintf(s.Err, "failed to get task %s\n", tname)
return err
}

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

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

opts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/task=%s", tname),
}
Expand Down Expand Up @@ -172,3 +182,20 @@ func printTaskDescription(s *cli.Stream, p cli.Params, tname string) error {
}
return nil
}

// this will sort the Task Resource by Type and then by Name
func sortResourcesByTypeAndName(tres []v1alpha1.TaskResource) []v1alpha1.TaskResource {
sort.Slice(tres, func(i, j int) bool {
if tres[j].Type < tres[i].Type {
return false
}

if tres[j].Type > tres[i].Type {
return true
}

return tres[j].Name > tres[i].Name
})

return tres
}
14 changes: 9 additions & 5 deletions pkg/cmd/task/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ func TestTaskDescribe_Full(t *testing.T) {
tb.TaskInputs(
tb.InputsResource("my-repo", v1alpha1.PipelineResourceTypeGit),
tb.InputsResource("my-image", v1alpha1.PipelineResourceTypeImage),
tb.InputsResource("source-repo", v1alpha1.PipelineResourceTypeGit),
tb.InputsParamSpec("myarg", v1alpha1.ParamTypeString),
tb.InputsParamSpec("print", v1alpha1.ParamTypeString),
),
tb.TaskOutputs(
tb.OutputsResource("code-image", v1alpha1.PipelineResourceTypeImage),
tb.OutputsResource("artifact-image", v1alpha1.PipelineResourceTypeImage),
),
tb.Step("hello", "busybox"),
tb.Step("exit", "busybox"),
Expand Down Expand Up @@ -220,13 +222,15 @@ func TestTaskDescribe_Full(t *testing.T) {
Namespace: ns
Input Resources
NAME TYPE
my-repo git
my-image image
NAME TYPE
my-repo git
source-repo git
my-image image
Output Resources
NAME TYPE
code-image image
NAME TYPE
artifact-image image
code-image image
Params
NAME TYPE
Expand Down

0 comments on commit 5ec20b2

Please sign in to comment.