Skip to content

Commit

Permalink
v1beta1 support for taskrun delete
Browse files Browse the repository at this point in the history
this adds v1beta1 support for taskrun delete cammand
also puts list and delete in `actions/list` `actions/delete` so
that upcoming actions like `create`, `get` etc can be addes to
actions

may get some refactors in upcoming pr's

Signed-off-by: Pradeep Kumar [email protected]
  • Loading branch information
pradeepitm12 authored and tekton-robot committed Mar 27, 2020
1 parent 6d29e85 commit e899112
Show file tree
Hide file tree
Showing 10 changed files with 396 additions and 75 deletions.
36 changes: 36 additions & 0 deletions pkg/actions/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright © 2020 The Tekton Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package delete

import (
"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func Delete(gr schema.GroupVersionResource, clients *cli.Clients, trname, n string, op *metav1.DeleteOptions) error {
gvr, err := actions.GetGVR(gr, clients.Tekton.Discovery())
if err != nil {
return err
}

err = clients.Dynamic.Resource(*gvr).Namespace(n).Delete(trname, op)
if err != nil {
return err
}

return nil
}
36 changes: 36 additions & 0 deletions pkg/actions/gvr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright © 2020 The Tekton Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package actions

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/restmapper"
)

func GetGVR(gr schema.GroupVersionResource, discovery discovery.DiscoveryInterface) (*schema.GroupVersionResource, error) {
apiGroupRes, err := restmapper.GetAPIGroupResources(discovery)
if err != nil {
return nil, err
}

rm := restmapper.NewDiscoveryRESTMapper(apiGroupRes)
gvr, err := rm.ResourceFor(gr)
if err != nil {
return nil, err
}

return &gvr, nil
}
20 changes: 2 additions & 18 deletions pkg/list/list.go → pkg/actions/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ package list
import (
"io"

"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/printer"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/discovery"
"k8s.io/client-go/restmapper"
)

func PrintObject(groupResource schema.GroupVersionResource, w io.Writer, p cli.Params, f *cliopts.PrintFlags, ns string) error {
Expand All @@ -42,7 +41,7 @@ func PrintObject(groupResource schema.GroupVersionResource, w io.Writer, p cli.P
}

func AllObjecs(gr schema.GroupVersionResource, clients *cli.Clients, n string, op metav1.ListOptions) (*unstructured.UnstructuredList, error) {
gvr, err := getGVR(gr, clients.Tekton.Discovery())
gvr, err := actions.GetGVR(gr, clients.Tekton.Discovery())
if err != nil {
return nil, err
}
Expand All @@ -54,18 +53,3 @@ func AllObjecs(gr schema.GroupVersionResource, clients *cli.Clients, n string, o

return allRes, nil
}

func getGVR(gr schema.GroupVersionResource, discovery discovery.DiscoveryInterface) (*schema.GroupVersionResource, error) {
apiGroupRes, err := restmapper.GetAPIGroupResources(discovery)
if err != nil {
return nil, err
}

rm := restmapper.NewDiscoveryRESTMapper(apiGroupRes)
gvr, err := rm.ResourceFor(gr)
if err != nil {
return nil, err
}

return &gvr, nil
}
2 changes: 1 addition & 1 deletion pkg/cmd/clustertask/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"text/tabwriter"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions/list"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/list"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pipeline/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"text/template"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions/list"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/list"
"github.com/tektoncd/cli/pkg/pipeline"
validate "github.com/tektoncd/cli/pkg/validate"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/task/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"text/tabwriter"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions/list"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
"github.com/tektoncd/cli/pkg/list"
validate "github.com/tektoncd/cli/pkg/validate"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
17 changes: 11 additions & 6 deletions pkg/cmd/taskrun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import (
"fmt"

"github.com/spf13/cobra"
traction "github.com/tektoncd/cli/pkg/actions/delete"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/deleter"
"github.com/tektoncd/cli/pkg/options"
trlist "github.com/tektoncd/cli/pkg/taskrun/list"
validate "github.com/tektoncd/cli/pkg/validate"
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 @@ -77,6 +80,7 @@ or
}

func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options.DeleteOptions) error {
trGroupResource := schema.GroupVersionResource{Group: "tekton.dev", Resource: "taskruns"}
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
Expand All @@ -85,24 +89,24 @@ func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options
switch {
case opts.DeleteAllNs:
d = deleter.New("TaskRun", func(taskRunName string) error {
return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{})
return traction.Delete(trGroupResource, cs, taskRunName, p.Namespace(), &metav1.DeleteOptions{})
})
trs, err := allTaskRunNames(p, cs, opts.Keep)
trs, err := allTaskRunNames(cs, opts.Keep, p.Namespace())
if err != nil {
return err
}
d.Delete(s, trs)
case opts.ParentResourceName == "":
d = deleter.New("TaskRun", func(taskRunName string) error {
return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{})
return traction.Delete(trGroupResource, cs, taskRunName, p.Namespace(), &metav1.DeleteOptions{})
})
d.Delete(s, trNames)
default:
d = deleter.New("Task", func(_ string) error {
return errors.New("the task should not be deleted")
})
d.WithRelated("TaskRun", taskRunLister(p, cs), func(taskRunName string) error {
return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{})
return traction.Delete(trGroupResource, cs, taskRunName, p.Namespace(), &metav1.DeleteOptions{})
})
d.DeleteRelated(s, []string{opts.ParentResourceName})
}
Expand Down Expand Up @@ -137,8 +141,9 @@ func taskRunLister(p cli.Params, cs *cli.Clients) func(string) ([]string, error)
}
}

func allTaskRunNames(p cli.Params, cs *cli.Clients, keep int) ([]string, error) {
taskRuns, err := cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).List(metav1.ListOptions{})
func allTaskRunNames(cs *cli.Clients, keep int, ns string) ([]string, error) {

taskRuns, err := trlist.TaskRuns(cs, metav1.ListOptions{}, ns)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit e899112

Please sign in to comment.