Skip to content

Commit

Permalink
Remove v1alpha1 APIs from ClusterTask
Browse files Browse the repository at this point in the history
Since v1alpha1 APIs are removed from tektoncd/pipeline, this is part 1
of the series to remove v1alpha1 APIs usage in CLI starting with
ClusterTask.

Signed-off-by: vinamra28 <[email protected]>
  • Loading branch information
vinamra28 authored and tekton-robot committed Aug 11, 2022
1 parent 071bbb9 commit ed66d7e
Show file tree
Hide file tree
Showing 28 changed files with 176 additions and 1,940 deletions.
41 changes: 1 addition & 40 deletions pkg/clustertask/clustertask.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
package clustertask

import (
"context"
"fmt"
"os"

"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -67,30 +65,8 @@ func List(c *cli.Clients, opts metav1.ListOptions) (*v1beta1.ClusterTaskList, er
return clustertasks, nil
}

// It will fetch the resource based on the api available and return v1beta1 form
// It will fetch the ClusterTask based on ClusterTask name
func Get(c *cli.Clients, clustertaskname string, opts metav1.GetOptions) (*v1beta1.ClusterTask, error) {
gvr, err := actions.GetGroupVersionResource(clustertaskGroupResource, c.Tekton.Discovery())
if err != nil {
return nil, err
}

if gvr.Version == "v1alpha1" {
clustertask, err := getV1alpha1(c, clustertaskname, opts)
if err != nil {
return nil, err
}
var clustertaskConverted v1beta1.ClusterTask
err = clustertask.ConvertTo(context.Background(), &clustertaskConverted)
if err != nil {
return nil, err
}
return &clustertaskConverted, nil
}
return GetV1beta1(c, clustertaskname, opts)
}

// It will fetch the resource in v1beta1 struct format
func GetV1beta1(c *cli.Clients, clustertaskname string, opts metav1.GetOptions) (*v1beta1.ClusterTask, error) {
unstructuredCT, err := actions.Get(clustertaskGroupResource, c.Dynamic, c.Tekton.Discovery(), clustertaskname, "", opts)
if err != nil {
return nil, err
Expand All @@ -104,21 +80,6 @@ func GetV1beta1(c *cli.Clients, clustertaskname string, opts metav1.GetOptions)
return clustertask, nil
}

// It will fetch the resource in v1alpha1 struct format
func getV1alpha1(c *cli.Clients, clustertaskname string, opts metav1.GetOptions) (*v1alpha1.ClusterTask, error) {
unstructuredCT, err := actions.Get(clustertaskGroupResource, c.Dynamic, c.Tekton.Discovery(), clustertaskname, "", opts)
if err != nil {
return nil, err
}

var clustertask *v1alpha1.ClusterTask
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredCT.UnstructuredContent(), &clustertask); err != nil {
fmt.Fprintf(os.Stderr, "failed to get clustertask")
return nil, err
}
return clustertask, nil
}

func Create(c *cli.Clients, ct *v1beta1.ClusterTask, opts metav1.CreateOptions) (*v1beta1.ClusterTask, error) {
_, err := actions.GetGroupVersionResource(clustertaskGroupResource, c.Tekton.Discovery())
if err != nil {
Expand Down
142 changes: 0 additions & 142 deletions pkg/clustertask/clustertask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,105 +115,6 @@ func TestClusterTask_GetAllTaskNames(t *testing.T) {
}

func TestClusterTask_List(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}

c1, err := p.Clients()
if err != nil {
t.Errorf("unable to create client: %v", err)
}

c2, err := p2.Clients()
if err != nil {
t.Errorf("unable to create client: %v", err)
}

testParams := []struct {
name string
client *cli.Clients
want []string
}{
{
name: "Single clusterTask",
client: c1,
want: []string{"clustertask"},
},
{
name: "Multi clusterTasks",
client: c2,
want: []string{"clustertask", "clustertask2"},
},
}

for _, tp := range testParams {
t.Run(tp.name, func(t *testing.T) {
got, err := List(tp.client, metav1.ListOptions{})
if err != nil {
t.Errorf("unexpected Error")
}

ctnames := []string{}
for _, ct := range got.Items {
ctnames = append(ctnames, ct.Name)
}
test.AssertOutput(t, tp.want, ctnames)
})
}
}

func TestClusterTaskV1beta1_List(t *testing.T) {
version := "v1beta1"
clock := clockwork.NewFakeClock()
ctdata := []*v1beta1.ClusterTask{
Expand Down Expand Up @@ -307,49 +208,6 @@ func TestClusterTaskV1beta1_List(t *testing.T) {
}

func TestClusterTask_Get(t *testing.T) {
version := "v1alpha1"
clock := clockwork.NewFakeClock()
ctdata := []*v1alpha1.ClusterTask{
{
ObjectMeta: metav1.ObjectMeta{
Name: "clustertask",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "clustertask2",
// 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),
cb.UnstructuredCT(ctdata[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}
c, err := p.Clients()
if err != nil {
t.Errorf("unable to create client: %v", err)
}

got, err := Get(c, "clustertask", metav1.GetOptions{})
if err != nil {
t.Errorf("unexpected Error")
}
test.AssertOutput(t, "clustertask", got.Name)
}

func TestClusterTaskV1beta1_Get(t *testing.T) {
version := "v1beta1"
clock := clockwork.NewFakeClock()
ctdata := []*v1beta1.ClusterTask{
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/clustertask/clustertask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
)

const (
versionA1 = "v1alpha1"
versionB1 = "v1beta1"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/clustertask/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestCreate_InNamespace(t *testing.T) {
},
}

version := "v1alpha1"
version := "v1beta1"
tdc := testDynamic.Options{}
dynamic, err := tdc.Client(
cb.UnstructuredV1beta1T(tasks[0], version),
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestCreate_WithoutFlag(t *testing.T) {
},
}

version := "v1alpha1"
version := "v1beta1"
tdc := testDynamic.Options{}
dynamic, err := tdc.Client(
cb.UnstructuredV1beta1T(tasks[0], version),
Expand Down
Loading

0 comments on commit ed66d7e

Please sign in to comment.