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

Make task and clustertask start commands to work with v1 apis #1953

Merged
merged 1 commit into from
Mar 22, 2023
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
2 changes: 1 addition & 1 deletion docs/cmd/tkn_clustertask_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ For passing the workspaces via flags:
-L, --last re-run the ClusterTask using last TaskRun values
--output string format of TaskRun (yaml or json)
-o, --outputresource strings pass the output resource name and ref as name=ref
-p, --param stringArray pass the param as key=value for string type, or key=value1,value2,... for array type
-p, --param stringArray pass the param as key=value for string type, or key=value1,value2,... for array type, or key="key1:value1, key2:value2" for object type
--pod-template string local or remote file containing a PodTemplate definition
--prefix-name string specify a prefix for the TaskRun name (must be lowercase alphanumeric characters)
-s, --serviceaccount string pass the serviceaccount name
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/tkn_task_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ For passing the workspaces via flags:
-L, --last re-run the Task using last TaskRun values
--output string format of TaskRun (yaml or json)
-o, --outputresource strings pass the output resource name and ref as name=ref
-p, --param stringArray pass the param as key=value for string type, or key=value1,value2,... for array type
-p, --param stringArray pass the param as key=value for string type, or key=value1,value2,... for array type, or key="key1:value1, key2:value2" for object type
--pod-template string local or remote file containing a PodTemplate definition
--prefix-name string specify a prefix for the TaskRun name (must be lowercase alphanumeric characters)
-s, --serviceaccount string pass the serviceaccount name
Expand Down
2 changes: 1 addition & 1 deletion docs/man/man1/tkn-clustertask-start.1
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Start ClusterTasks

.PP
\fB\-p\fP, \fB\-\-param\fP=[]
pass the param as key=value for string type, or key=value1,value2,... for array type
pass the param as key=value for string type, or key=value1,value2,... for array type, or key="key1:value1, key2:value2" for object type

.PP
\fB\-\-pod\-template\fP=""
Expand Down
2 changes: 1 addition & 1 deletion docs/man/man1/tkn-task-start.1
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Start Tasks

.PP
\fB\-p\fP, \fB\-\-param\fP=[]
pass the param as key=value for string type, or key=value1,value2,... for array type
pass the param as key=value for string type, or key=value1,value2,... for array type, or key="key1:value1, key2:value2" for object type

.PP
\fB\-\-pod\-template\fP=""
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 @@ For passing the workspaces via flags:

c.Flags().StringSliceVarP(&opt.InputResources, "inputresource", "i", []string{}, "pass the input resource name and ref as name=ref")
c.Flags().StringSliceVarP(&opt.OutputResources, "outputresource", "o", []string{}, "pass the output resource name and ref as name=ref")
c.Flags().StringArrayVarP(&opt.Params, "param", "p", []string{}, "pass the param as key=value for string type, or key=value1,value2,... for array type")
c.Flags().StringArrayVarP(&opt.Params, "param", "p", []string{}, "pass the param as key=value for string type, or key=value1,value2,... for array type, or key=\"key1:value1, key2:value2\" for object type")
c.Flags().StringVarP(&opt.ServiceAccountName, "serviceaccount", "s", "", "pass the serviceaccount name")
c.Flags().BoolVarP(&opt.Last, "last", "L", false, "re-run the ClusterTask using last TaskRun values")
c.Flags().StringVarP(&opt.UseTaskRun, "use-taskrun", "", "", "specify a TaskRun name to use its values to re-run the TaskRun")
Expand Down
9 changes: 3 additions & 6 deletions pkg/cmd/clustertask/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ func Test_ClusterTask_Start(t *testing.T) {
wantError: false,
want: "TaskRun started: taskrun-1\n\nIn order to track the TaskRun progress run:\ntkn taskrun logs taskrun-1 -f -n ns\n",
},
/*TODO: this should be fixed with start command
{
name: "Start with --last option",
command: []string{"start", "clustertask-1", "--last"},
Expand All @@ -544,7 +543,7 @@ func Test_ClusterTask_Start(t *testing.T) {
inputStream: nil,
wantError: false,
want: "TaskRun started: taskrun-2\n\nIn order to track the TaskRun progress run:\ntkn taskrun logs taskrun-2 -f -n ns\n",
},*/
},
{
name: "Start with --use-taskrun option",
command: []string{"start", "clustertask-1", "--use-taskrun", "taskrun-123"},
Expand Down Expand Up @@ -673,7 +672,6 @@ func Test_ClusterTask_Start(t *testing.T) {
wantError: false,
goldenFile: true,
},
/*TODO: this should be fixed with start command
{
name: "Dry run with --last",
command: []string{"start", "clustertask-1",
Expand All @@ -689,7 +687,7 @@ func Test_ClusterTask_Start(t *testing.T) {
inputStream: nil,
wantError: false,
goldenFile: true,
},*/
},
{
name: "Dry run with --timeout v1beta1",
command: []string{"start", "clustertask-2",
Expand Down Expand Up @@ -835,7 +833,6 @@ func Test_ClusterTask_Start(t *testing.T) {
wantError: false,
goldenFile: true,
},
/*TODO: this should be fixed with start command
{
name: "Dry Run with --prefix-name and --last v1beta1",
command: []string{"start", "clustertask-1",
Expand All @@ -851,7 +848,7 @@ func Test_ClusterTask_Start(t *testing.T) {
inputStream: nil,
wantError: false,
goldenFile: true,
},*/
},
{
name: "Start clustertask with skip-optional-workspaces flag",
command: []string{"start", "clustertask-4",
Expand Down
53 changes: 47 additions & 6 deletions pkg/cmd/task/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/cmd/pipelineresource"
"github.com/tektoncd/cli/pkg/cmd/taskrun"
Expand All @@ -38,12 +39,13 @@ import (
"github.com/tektoncd/cli/pkg/options"
"github.com/tektoncd/cli/pkg/params"
"github.com/tektoncd/cli/pkg/pods"
"github.com/tektoncd/cli/pkg/task"
traction "github.com/tektoncd/cli/pkg/taskrun"
"github.com/tektoncd/cli/pkg/workspaces"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -92,7 +94,7 @@ func NameArg(args []string, p cli.Params, opt *startOptions) error {
}

name, ns := args[0], p.Namespace()
t, err := task.Get(c, name, metav1.GetOptions{}, ns)
t, err := getTaskV1beta1(taskGroupResource, c, name, ns)
if err != nil {
return fmt.Errorf(errInvalidTask, name, ns)
}
Expand Down Expand Up @@ -201,7 +203,7 @@ For passing the workspaces via flags:

c.Flags().StringSliceVarP(&opt.InputResources, "inputresource", "i", []string{}, "pass the input resource name and ref as name=ref")
c.Flags().StringSliceVarP(&opt.OutputResources, "outputresource", "o", []string{}, "pass the output resource name and ref as name=ref")
c.Flags().StringArrayVarP(&opt.Params, "param", "p", []string{}, "pass the param as key=value for string type, or key=value1,value2,... for array type")
c.Flags().StringArrayVarP(&opt.Params, "param", "p", []string{}, "pass the param as key=value for string type, or key=value1,value2,... for array type, or key=\"key1:value1, key2:value2\" for object type")
c.Flags().StringVarP(&opt.ServiceAccountName, "serviceaccount", "s", "", "pass the serviceaccount name")
_ = c.RegisterFlagCompletionFunc("serviceaccount",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -240,13 +242,25 @@ func parseTask(taskLocation string, httpClient http.Client) (*v1beta1.Task, erro
if err != nil {
return nil, err
}
task := v1beta1.Task{}

if m["apiVersion"] == "tekton.dev/v1alpha1" {
return nil, fmt.Errorf("v1alpha1 is no longer supported")
}

if err := yaml.UnmarshalStrict(b, &task); err != nil {
return nil, err
task := v1beta1.Task{}
if m["apiVersion"] == "tekton.dev/v1" {
taskV1 := v1.Task{}
if err := yaml.UnmarshalStrict(b, &taskV1); err != nil {
return nil, err
}
err = task.ConvertFrom(context.Background(), &taskV1)
if err != nil {
return nil, err
}
} else {
if err := yaml.UnmarshalStrict(b, &task); err != nil {
return nil, err
}
}

err = params.ValidateParamType(task.Spec.Params)
Expand Down Expand Up @@ -560,3 +574,30 @@ func createPipelineResource(resType v1alpha1.PipelineResourceType, askOpt survey
fmt.Fprintf(s.Out, "New %s resource \"%s\" has been created\n", newRes.Spec.Type, newRes.Name)
return newRes, nil
}

func getTaskV1beta1(gr schema.GroupVersionResource, c *cli.Clients, tName, ns string) (*v1beta1.Task, error) {
var task v1beta1.Task
gvr, err := actions.GetGroupVersionResource(gr, c.Tekton.Discovery())
if err != nil {
return nil, err
}

if gvr.Version == "v1beta1" {
err := actions.GetV1(taskGroupResource, c, tName, ns, metav1.GetOptions{}, &task)
if err != nil {
return nil, err
}
return &task, nil
}

var taskV1 v1.Task
err = actions.GetV1(taskGroupResource, c, tName, ns, metav1.GetOptions{}, &taskV1)
if err != nil {
return nil, err
}
err = task.ConvertFrom(context.Background(), &taskV1)
if err != nil {
return nil, err
}
return &task, nil
}
25 changes: 10 additions & 15 deletions pkg/cmd/task/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,6 @@ func Test_start_task(t *testing.T) {
}

func Test_start_task_last(t *testing.T) {
// TODO: this should be fixed with start command
t.Skip()
tasks := []*v1beta1.Task{
{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -703,8 +701,6 @@ func Test_start_task_last(t *testing.T) {
}

func Test_start_task_last_with_override_timeout(t *testing.T) {
// TODO: this should be fixed with start command
t.Skip()
tasks := []*v1beta1.Task{
{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -1045,8 +1041,6 @@ func Test_start_use_taskrun_cancelled_status(t *testing.T) {
}

func Test_start_task_last_generate_name(t *testing.T) {
// TODO: this should be fixed with start command
t.Skip()
tasks := []*v1beta1.Task{
{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -1211,8 +1205,6 @@ func Test_start_task_last_generate_name(t *testing.T) {
}

func Test_start_task_last_with_prefix_name(t *testing.T) {
// TODO: this should be fixed with start command
t.Skip()
tasks := []*v1beta1.Task{
{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -1376,8 +1368,6 @@ func Test_start_task_last_with_prefix_name(t *testing.T) {
}

func Test_start_task_with_prefix_name(t *testing.T) {
// TODO: this should be fixed with start command
t.Skip()
tasks := []*v1beta1.Task{
{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -1541,8 +1531,6 @@ func Test_start_task_with_prefix_name(t *testing.T) {
}

func Test_start_task_last_with_inputs(t *testing.T) {
// TODO: this should be fixed with start command
t.Skip()
tasks := []*v1beta1.Task{
{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -1732,8 +1720,6 @@ func Test_start_task_last_with_inputs(t *testing.T) {
}

func Test_start_task_last_without_taskrun(t *testing.T) {
// TODO: this should be fixed with start command
t.Skip()
tasks := []*v1beta1.Task{
{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -2383,6 +2369,10 @@ func Test_start_task_allkindparam(t *testing.T) {
Name: "printafter",
Type: v1beta1.ParamTypeArray,
},
{
Name: "printlast",
Type: v1beta1.ParamTypeObject,
},
},
Steps: []v1beta1.Step{
{
Expand Down Expand Up @@ -2421,6 +2411,7 @@ func Test_start_task_allkindparam(t *testing.T) {
"-p=myarg=value1",
"-p=print=boom,boom",
"-p=printafter=booms",
"-p=printlast=a:b, c:d",
"-l=key=value",
"-o=code-image=output-image",
"-s=svc1",
Expand All @@ -2439,7 +2430,7 @@ func Test_start_task_allkindparam(t *testing.T) {
t.Errorf("Error taskrun generated is different %+v", tr)
}

test.AssertOutput(t, 3, len(tr.Items[0].Spec.Params))
test.AssertOutput(t, 4, len(tr.Items[0].Spec.Params))

for _, v := range tr.Items[0].Spec.Params {
if v.Name == "my-arg" {
Expand All @@ -2453,6 +2444,10 @@ func Test_start_task_allkindparam(t *testing.T) {
if v.Name == "printafter" {
test.AssertOutput(t, v1.ParamValue{Type: v1.ParamTypeArray, ArrayVal: []string{"booms"}}, v.Value)
}

if v.Name == "printlast" {
test.AssertOutput(t, v1.ParamValue{Type: v1.ParamTypeObject, ObjectVal: map[string]string{"a": "b", "c": "d"}}, v.Value)
}
}

if d := cmp.Equal(tr.Items[0].ObjectMeta.Labels, map[string]string{"key": "value"}); !d {
Expand Down
Loading