Skip to content

Commit

Permalink
Make changes in clustertask describe
Browse files Browse the repository at this point in the history
This will move the clustertask cmd to new funcs and also
add new param type support in desc

Part of tektoncd#1804
  • Loading branch information
piyush-garg committed Mar 22, 2023
1 parent 7dc2e87 commit 9c6a012
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 31 deletions.
1 change: 1 addition & 0 deletions pkg/clustertask/clustertask.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func GetAllClusterTaskNames(gr schema.GroupVersionResource, c *cli.Clients) ([]s
return ret, nil
}

// TODO: remove as all the function uses are moved to new func
// It will fetch the ClusterTask based on ClusterTask name
func Get(c *cli.Clients, clustertaskname string, opts metav1.GetOptions) (*v1beta1.ClusterTask, error) {
unstructuredCT, err := actions.Get(clustertaskGroupResource, c.Dynamic, c.Tekton.Discovery(), clustertaskname, "", opts)
Expand Down
5 changes: 3 additions & 2 deletions pkg/clustertask/clustertask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,12 @@ func TestClusterTask_Get(t *testing.T) {
t.Errorf("unable to create client: %v", err)
}

got, err := Get(c, "clustertask", metav1.GetOptions{})
var clustertask *v1beta1.ClusterTask
err = actions.GetV1(clustertaskGroupResource, c, "clustertask", "", metav1.GetOptions{}, &clustertask)
if err != nil {
t.Errorf("unexpected Error")
}
test.AssertOutput(t, "clustertask", got.Name)
test.AssertOutput(t, "clustertask", clustertask.Name)
}

func TestClusterTask_Create(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions pkg/cmd/clustertask/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/clustertask"
clustertaskpkg "github.com/tektoncd/cli/pkg/clustertask"
"github.com/tektoncd/cli/pkg/deleter"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/options"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"go.uber.org/multierr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -41,7 +42,8 @@ func ctExists(args []string, p cli.Params) ([]string, error) {
}
var errorList error
for _, name := range args {
_, err := clustertask.Get(c, name, metav1.GetOptions{})
var clustertask *v1beta1.ClusterTask
err := actions.GetV1(clustertaskGroupResource, c, name, "", metav1.GetOptions{}, &clustertask)
if err != nil {
errorList = multierr.Append(errorList, err)
continue
Expand Down Expand Up @@ -115,7 +117,7 @@ func deleteClusterTasks(opts *options.DeleteOptions, s *cli.Stream, p cli.Params
})
switch {
case opts.DeleteAll:
cts, err := clustertask.GetAllClusterTaskNames(clustertaskGroupResource, cs)
cts, err := clustertaskpkg.GetAllClusterTaskNames(clustertaskGroupResource, cs)
if err != nil {
return err
}
Expand Down
42 changes: 19 additions & 23 deletions pkg/cmd/clustertask/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import (
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/clustertask"
clustertaskpkg "github.com/tektoncd/cli/pkg/clustertask"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/options"
trsort "github.com/tektoncd/cli/pkg/taskrun/sort"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)

Expand Down Expand Up @@ -78,8 +77,10 @@ const describeTemplate = `{{decorate "bold" "Name"}}: {{ .ClusterTask.Name }}
{{- else }}
{{- if eq $p.Type "string" }}
{{decorate "bullet" $p.Name }} {{ $p.Type }} {{ formatDesc $p.Description }} {{ $p.Default.StringVal }}
{{- else }}
{{- else if eq $p.Type "array" }}
{{decorate "bullet" $p.Name }} {{ $p.Type }} {{ formatDesc $p.Description }} {{ $p.Default.ArrayVal }}
{{- else }}
{{decorate "bullet" $p.Name }} {{ $p.Type }} {{ formatDesc $p.Description }} {{ $p.Default.ObjectVal }}
{{- end }}
{{- end }}
{{- end }}
Expand Down Expand Up @@ -160,7 +161,7 @@ or
}

if len(args) == 0 {
clusterTaskNames, err := clustertask.GetAllClusterTaskNames(clustertaskGroupResource, cs)
clusterTaskNames, err := clustertaskpkg.GetAllClusterTaskNames(clustertaskGroupResource, cs)
if err != nil {
return err
}
Expand All @@ -177,41 +178,36 @@ or
}

if output != "" {
clustertaskGroupResource := schema.GroupVersionResource{Group: "tekton.dev", Resource: "clustertasks"}
return actions.PrintObject(clustertaskGroupResource, opts.ClusterTaskName, cmd.OutOrStdout(), cs.Dynamic, cs.Tekton.Discovery(), f, "")
return actions.PrintObjectV1(clustertaskGroupResource, opts.ClusterTaskName, cmd.OutOrStdout(), cs, f, "")
}

return printClusterTaskDescription(s, p, opts.ClusterTaskName)
return printClusterTaskDescription(s, cs, opts.ClusterTaskName, p.Namespace(), p.Time())
},
}

f.AddFlags(c)
return c
}

func printClusterTaskDescription(s *cli.Stream, p cli.Params, tname string) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

ct, err := clustertask.Get(cs, tname, metav1.GetOptions{})
func printClusterTaskDescription(s *cli.Stream, cs *cli.Clients, ctname, namespace string, time clockwork.Clock) error {
var clustertask *v1beta1.ClusterTask
err := actions.GetV1(clustertaskGroupResource, cs, ctname, "", metav1.GetOptions{}, &clustertask)
if err != nil {
return fmt.Errorf("failed to get ClusterTask %s", tname)
return fmt.Errorf("failed to get ClusterTask %s", ctname)
}

if ct.Spec.Resources != nil {
ct.Spec.Resources.Inputs = sortResourcesByTypeAndName(ct.Spec.Resources.Inputs)
ct.Spec.Resources.Outputs = sortResourcesByTypeAndName(ct.Spec.Resources.Outputs)
if clustertask.Spec.Resources != nil {
clustertask.Spec.Resources.Inputs = sortResourcesByTypeAndName(clustertask.Spec.Resources.Inputs)
clustertask.Spec.Resources.Outputs = sortResourcesByTypeAndName(clustertask.Spec.Resources.Outputs)
}

opts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/clusterTask=%s", tname),
LabelSelector: fmt.Sprintf("tekton.dev/clusterTask=%s", ctname),
}

var taskRuns *v1.TaskRunList
if err := actions.ListV1(taskrunGroupResource, cs, opts, p.Namespace(), &taskRuns); err != nil {
return fmt.Errorf("failed to get TaskRuns for ClusterTask %s", tname)
if err := actions.ListV1(taskrunGroupResource, cs, opts, namespace, &taskRuns); err != nil {
return fmt.Errorf("failed to get TaskRuns for ClusterTask %s", ctname)
}

trsort.SortByStartTime(taskRuns.Items)
Expand All @@ -221,9 +217,9 @@ func printClusterTaskDescription(s *cli.Stream, p cli.Params, tname string) erro
TaskRuns *v1.TaskRunList
Time clockwork.Clock
}{
ClusterTask: ct,
ClusterTask: clustertask,
TaskRuns: taskRuns,
Time: p.Time(),
Time: time,
}

funcMap := template.FuncMap{
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/clustertask/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/clustertask"
"github.com/tektoncd/cli/pkg/cmd/taskrun"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/tektoncd/cli/pkg/options"
"github.com/tektoncd/cli/pkg/task"
taskrunpkg "github.com/tektoncd/cli/pkg/taskrun"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -37,7 +39,8 @@ func nameArg(args []string, p cli.Params) error {
return err
}
name := args[0]
if _, err = clustertask.Get(c, name, metav1.GetOptions{}); err != nil {
var clustertask *v1beta1.ClusterTask
if err = actions.GetV1(clustertaskGroupResource, c, name, "", metav1.GetOptions{}, &clustertask); err != nil {
return err
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/clustertask/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
ctactions "github.com/tektoncd/cli/pkg/clustertask"
"github.com/tektoncd/cli/pkg/cmd/pipelineresource"
"github.com/tektoncd/cli/pkg/cmd/taskrun"
"github.com/tektoncd/cli/pkg/file"
Expand Down Expand Up @@ -90,7 +90,8 @@ func NameArg(args []string, p cli.Params, opt *startOptions) error {
}

name := args[0]
ct, err := ctactions.Get(c, name, metav1.GetOptions{})
var ct *v1beta1.ClusterTask
err = actions.GetV1(clustertaskGroupResource, c, name, "", metav1.GetOptions{}, &ct)
if err != nil {
return fmt.Errorf(errInvalidClusterTask, name)
}
Expand Down

0 comments on commit 9c6a012

Please sign in to comment.