Skip to content

Commit

Permalink
Refactor delete command code to make a bit more DRY
Browse files Browse the repository at this point in the history
While implementing multiple delete I noticed that each delete
command was implementing its own error handling and messaging
even though each was doing exactly the same thing. This made
working on the code cumbersome - I had to double check that I'd
added each of my changes correactly across every repition of
the delete command.

So this commit introduces a helper type, Deleter, that does most
of the repetitive work and is reused across all the delete commands.
  • Loading branch information
Scott authored and tekton-robot committed Jan 10, 2020
1 parent 32662c8 commit 0beadbb
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 240 deletions.
26 changes: 5 additions & 21 deletions pkg/cmd/clustertask/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import (

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/helper/names"
"github.com/tektoncd/cli/pkg/helper/deleter"
"github.com/tektoncd/cli/pkg/helper/options"
"go.uber.org/multierr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -64,7 +63,6 @@ or
}
f.AddFlags(c)
c.Flags().BoolVarP(&opts.ForceDelete, "force", "f", false, "Whether to force deletion (default: false)")

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_clustertasks")
return c
}
Expand All @@ -74,22 +72,8 @@ func deleteClusterTasks(s *cli.Stream, p cli.Params, tNames []string) error {
if err != nil {
return fmt.Errorf("Failed to create tekton client")
}

var errs []error
var success []string
for _, tName := range tNames {
if err := cs.Tekton.TektonV1alpha1().ClusterTasks().Delete(tName, &metav1.DeleteOptions{}); err != nil {
err = fmt.Errorf("Failed to delete clustertask %q: %s", tName, err)
errs = append(errs, err)
fmt.Fprintf(s.Err, "%s\n", err)
continue
}
success = append(success, tName)
}

if len(success) > 0 {
fmt.Fprintf(s.Out, "ClusterTasks deleted: %s\n", names.QuotedList(success))
}

return multierr.Combine(errs...)
d := deleter.New("ClusterTask", func(taskName string) error {
return cs.Tekton.TektonV1alpha1().ClusterTasks().Delete(taskName, &metav1.DeleteOptions{})
})
return d.Execute(s, tNames)
}
2 changes: 1 addition & 1 deletion pkg/cmd/clustertask/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestClusterTaskDelete(t *testing.T) {
input: seeds[2],
inputStream: strings.NewReader("y"),
wantError: true,
want: "Failed to delete clustertask \"nonexistent\": clustertasks.tekton.dev \"nonexistent\" not found",
want: "failed to delete clustertask \"nonexistent\": clustertasks.tekton.dev \"nonexistent\" not found",
},
}

Expand Down
24 changes: 5 additions & 19 deletions pkg/cmd/condition/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import (

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/helper/names"
"github.com/tektoncd/cli/pkg/helper/deleter"
"github.com/tektoncd/cli/pkg/helper/options"
validate "github.com/tektoncd/cli/pkg/helper/validate"
"go.uber.org/multierr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -78,21 +77,8 @@ func deleteConditions(s *cli.Stream, p cli.Params, condNames []string) error {
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

var errs []error
var success []string
for _, condName := range condNames {
if err := cs.Tekton.TektonV1alpha1().Conditions(p.Namespace()).Delete(condName, &metav1.DeleteOptions{}); err != nil {
err = fmt.Errorf("failed to delete condition %q: %s", condName, err)
errs = append(errs, err)
fmt.Fprintf(s.Err, "%s\n", err)
continue
}
success = append(success, condName)
}
if len(success) > 0 {
fmt.Fprintf(s.Out, "Conditions deleted: %s\n", names.QuotedList(success))
}

return multierr.Combine(errs...)
d := deleter.New("Condition", func(conditionName string) error {
return cs.Tekton.TektonV1alpha1().Conditions(p.Namespace()).Delete(conditionName, &metav1.DeleteOptions{})
})
return d.Execute(s, condNames)
}
25 changes: 5 additions & 20 deletions pkg/cmd/eventlistener/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import (

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/helper/names"
"github.com/tektoncd/cli/pkg/helper/deleter"
"github.com/tektoncd/cli/pkg/helper/options"
"github.com/tektoncd/cli/pkg/helper/validate"
"go.uber.org/multierr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -79,22 +78,8 @@ func deleteEventListeners(s *cli.Stream, p cli.Params, elNames []string) error {
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

var errs []error
var success []string
for _, elName := range elNames {
if err := cs.Triggers.TektonV1alpha1().EventListeners(p.Namespace()).Delete(elName, &metav1.DeleteOptions{}); err != nil {
err = fmt.Errorf("failed to delete eventlistener %q: %s", elName, err)
errs = append(errs, err)
fmt.Fprintf(s.Err, "%s\n", err)
continue
}
success = append(success, elName)
}

if len(success) > 0 {
fmt.Fprintf(s.Out, "EventListeners deleted: %s\n", names.QuotedList(success))
}

return multierr.Combine(errs...)
d := deleter.New("EventListener", func(listenerName string) error {
return cs.Triggers.TektonV1alpha1().EventListeners(p.Namespace()).Delete(listenerName, &metav1.DeleteOptions{})
})
return d.Execute(s, elNames)
}
58 changes: 17 additions & 41 deletions pkg/cmd/pipeline/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import (

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/helper/names"
"github.com/tektoncd/cli/pkg/helper/deleter"
"github.com/tektoncd/cli/pkg/helper/options"
validate "github.com/tektoncd/cli/pkg/helper/validate"
multierr "go.uber.org/multierr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -80,53 +79,30 @@ func deletePipelines(opts *options.DeleteOptions, s *cli.Stream, p cli.Params, p
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

var errs []error
addPrintErr := func(err error) {
errs = append(errs, err)
fmt.Fprintf(s.Err, "%s\n", err)
d := deleter.New("Pipeline", func(pipelineName string) error {
return cs.Tekton.TektonV1alpha1().Pipelines(p.Namespace()).Delete(pipelineName, &metav1.DeleteOptions{})
})
if opts.DeleteAll {
d.WithRelated("PipelineRun", pipelineRunLister(p, cs), func(pipelineRunName string) error {
return cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(pipelineRunName, &metav1.DeleteOptions{})
})
}
return d.Execute(s, pNames)
}

var successfulPipelines []string
var successfulPipelineRuns []string

for _, pName := range pNames {
if err := cs.Tekton.TektonV1alpha1().Pipelines(p.Namespace()).Delete(pName, &metav1.DeleteOptions{}); err != nil {
addPrintErr(fmt.Errorf("failed to delete pipeline %q: %s", pName, err))
continue
}
successfulPipelines = append(successfulPipelines, pName)

if !opts.DeleteAll {
continue
}

func pipelineRunLister(p cli.Params, cs *cli.Clients) func(string) ([]string, error) {
return func(pipelineName string) ([]string, error) {
lOpts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/pipeline=%s", pName),
LabelSelector: fmt.Sprintf("tekton.dev/pipeline=%s", pipelineName),
}

pipelineRuns, err := cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).List(lOpts)
if err != nil {
addPrintErr(err)
continue
return nil, err
}

var names []string
for _, pr := range pipelineRuns.Items {
if err := cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(pr.Name, &metav1.DeleteOptions{}); err != nil {
addPrintErr(fmt.Errorf("failed to delete pipelinerun %q: %s", pr.Name, err))
continue
}

successfulPipelineRuns = append(successfulPipelineRuns, pr.Name)
names = append(names, pr.Name)
}
return names, nil
}

if len(successfulPipelineRuns) > 0 {
fmt.Fprintf(s.Out, "PipelineRuns deleted: %s\n", names.QuotedList(successfulPipelineRuns))
}
if len(successfulPipelines) > 0 {
fmt.Fprintf(s.Out, "Pipelines deleted: %s\n", names.QuotedList(successfulPipelines))
}

return multierr.Combine(errs...)
}
23 changes: 5 additions & 18 deletions pkg/cmd/pipelineresource/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import (

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/helper/names"
"github.com/tektoncd/cli/pkg/helper/deleter"
"github.com/tektoncd/cli/pkg/helper/options"
validateinput "github.com/tektoncd/cli/pkg/helper/validate"
"go.uber.org/multierr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -80,20 +79,8 @@ func deleteResources(s *cli.Stream, p cli.Params, preNames []string) error {
return fmt.Errorf("failed to create tekton client")
}

var errs []error
var success []string
for _, preName := range preNames {
if err := cs.Tekton.TektonV1alpha1().PipelineResources(p.Namespace()).Delete(preName, &metav1.DeleteOptions{}); err != nil {
err = fmt.Errorf("failed to delete pipelineresource %q: %s", preName, err)
errs = append(errs, err)
fmt.Fprintf(s.Err, "%s\n", err)
continue
}
success = append(success, preName)
}
if len(success) > 0 {
fmt.Fprintf(s.Out, "PipelineResources deleted: %s\n", names.QuotedList(success))
}

return multierr.Combine(errs...)
d := deleter.New("PipelineResource", func(resourceName string) error {
return cs.Tekton.TektonV1alpha1().PipelineResources(p.Namespace()).Delete(resourceName, &metav1.DeleteOptions{})
})
return d.Execute(s, preNames)
}
25 changes: 5 additions & 20 deletions pkg/cmd/pipelinerun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import (

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/helper/names"
"github.com/tektoncd/cli/pkg/helper/deleter"
"github.com/tektoncd/cli/pkg/helper/options"
validate "github.com/tektoncd/cli/pkg/helper/validate"
"go.uber.org/multierr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -78,22 +77,8 @@ func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string) error {
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

var errs []error
var success []string
for _, prName := range prNames {
if err := cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(prName, &metav1.DeleteOptions{}); err != nil {
err = fmt.Errorf("failed to delete pipelinerun %q: %s", prName, err)
errs = append(errs, err)
fmt.Fprintf(s.Err, "%s\n", err)
continue
}
success = append(success, prName)
}

if len(success) > 0 {
fmt.Fprintf(s.Out, "PipelineRuns deleted: %s\n", names.QuotedList(success))
}

return multierr.Combine(errs...)
d := deleter.New("PipelineRun", func(pipelineRunName string) error {
return cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(pipelineRunName, &metav1.DeleteOptions{})
})
return d.Execute(s, prNames)
}
59 changes: 18 additions & 41 deletions pkg/cmd/task/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import (

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/helper/names"
"github.com/tektoncd/cli/pkg/helper/deleter"
"github.com/tektoncd/cli/pkg/helper/options"
validate "github.com/tektoncd/cli/pkg/helper/validate"
"go.uber.org/multierr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -75,57 +74,35 @@ or
return c
}

func deleteTask(opts *options.DeleteOptions, s *cli.Stream, p cli.Params, tNames []string) error {
func deleteTask(opts *options.DeleteOptions, s *cli.Stream, p cli.Params, taskNames []string) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

var errs []error
addPrintErr := func(err error) {
errs = append(errs, err)
fmt.Fprintf(s.Err, "%s\n", err)
d := deleter.New("Task", func(taskName string) error {
return cs.Tekton.TektonV1alpha1().Tasks(p.Namespace()).Delete(taskName, &metav1.DeleteOptions{})
})
if opts.DeleteAll {
d.WithRelated("TaskRun", taskRunLister(p, cs), func(taskRunName string) error {
return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{})
})
}
return d.Execute(s, taskNames)
}

var successfulTasks []string
var successfulTaskRuns []string

for _, tName := range tNames {
if err := cs.Tekton.TektonV1alpha1().Tasks(p.Namespace()).Delete(tName, &metav1.DeleteOptions{}); err != nil {
addPrintErr(fmt.Errorf("failed to delete task %q: %s", tName, err))
continue
}
successfulTasks = append(successfulTasks, tName)

if !opts.DeleteAll {
continue
}

func taskRunLister(p cli.Params, cs *cli.Clients) func(string) ([]string, error) {
return func(taskName string) ([]string, error) {
lOpts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/task=%s", tName),
LabelSelector: fmt.Sprintf("tekton.dev/task=%s", taskName),
}

taskRuns, err := cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).List(lOpts)
if err != nil {
addPrintErr(err)
continue
return nil, err
}

var names []string
for _, tr := range taskRuns.Items {
if err := cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(tr.Name, &metav1.DeleteOptions{}); err != nil {
addPrintErr(fmt.Errorf("failed to delete taskrun %q: %s", tr.Name, err))
continue
}
successfulTaskRuns = append(successfulTaskRuns, tr.Name)
names = append(names, tr.Name)
}
return names, nil
}

if len(successfulTaskRuns) > 0 {
fmt.Fprintf(s.Out, "TaskRuns deleted: %s\n", names.QuotedList(successfulTaskRuns))
}
if len(successfulTasks) > 0 {
fmt.Fprintf(s.Out, "Tasks deleted: %s\n", names.QuotedList(successfulTasks))
}

return multierr.Combine(errs...)
}
Loading

0 comments on commit 0beadbb

Please sign in to comment.