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

Add flag to task and pipeline to only delete related resources #608

Merged
merged 1 commit into from Jan 28, 2020
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_pipeline_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ or
### Options

```
-a, --all Whether to delete related resources (pipelineruns) (default: false)
-a, --all Whether to also delete related resources (pipelineruns) (default: false)
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
-f, --force Whether to force deletion (default: false)
-h, --help help for delete
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/tkn_pipelinerun_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ or
-f, --force Whether to force deletion (default: false)
-h, --help help for delete
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
-p, --pipeline string The name of a pipeline whose pipelineruns should be deleted (does not delete the pipeline)
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
```

Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/tkn_task_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ or
### Options

```
-a, --all Whether to delete related resources (taskruns) (default: false)
-a, --all Whether to also delete related resources (taskruns) (default: false)
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
-f, --force Whether to force deletion (default: false)
-h, --help help for delete
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/tkn_taskrun_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ or
-f, --force Whether to force deletion (default: false)
-h, --help help for delete
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
-t, --task string The name of a task whose taskruns should be deleted (does not delete the task)
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
```

Expand Down
2 changes: 1 addition & 1 deletion docs/man/man1/tkn-pipeline-delete.1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Delete pipelines in a namespace
.SH OPTIONS
.PP
\fB\-a\fP, \fB\-\-all\fP[=false]
Whether to delete related resources (pipelineruns) (default: false)
Whether to also delete related resources (pipelineruns) (default: false)

.PP
\fB\-\-allow\-missing\-template\-keys\fP[=true]
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-pipelinerun-delete.1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Delete pipelineruns in a namespace
\fB\-o\fP, \fB\-\-output\fP=""
Output format. One of: json|yaml|name|go\-template|go\-template\-file|template|templatefile|jsonpath|jsonpath\-file.

.PP
\fB\-p\fP, \fB\-\-pipeline\fP=""
The name of a pipeline whose pipelineruns should be deleted (does not delete the pipeline)

.PP
\fB\-\-template\fP=""
Template string or path to template file to use when \-o=go\-template, \-o=go\-template\-file. The template format is golang templates [
Expand Down
2 changes: 1 addition & 1 deletion docs/man/man1/tkn-task-delete.1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Delete task resources in a namespace
.SH OPTIONS
.PP
\fB\-a\fP, \fB\-\-all\fP[=false]
Whether to delete related resources (taskruns) (default: false)
Whether to also delete related resources (taskruns) (default: false)

.PP
\fB\-\-allow\-missing\-template\-keys\fP[=true]
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-taskrun-delete.1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Delete taskruns in a namespace
\fB\-o\fP, \fB\-\-output\fP=""
Output format. One of: json|yaml|name|go\-template|go\-template\-file|template|templatefile|jsonpath|jsonpath\-file.

.PP
\fB\-t\fP, \fB\-\-task\fP=""
The name of a task whose taskruns should be deleted (does not delete the task)

.PP
\fB\-\-template\fP=""
Template string or path to template file to use when \-o=go\-template, \-o=go\-template\-file. The template format is golang templates [
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/clustertask/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@ func deleteClusterTasks(s *cli.Stream, p cli.Params, tNames []string) error {
d := deleter.New("ClusterTask", func(taskName string) error {
return cs.Tekton.TektonV1alpha1().ClusterTasks().Delete(taskName, &metav1.DeleteOptions{})
})
return d.Execute(s, tNames)
d.Delete(s, tNames)
d.PrintSuccesses(s)
return d.Errors()
}
4 changes: 3 additions & 1 deletion pkg/cmd/condition/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,7 @@ func deleteConditions(s *cli.Stream, p cli.Params, condNames []string) error {
d := deleter.New("Condition", func(conditionName string) error {
return cs.Tekton.TektonV1alpha1().Conditions(p.Namespace()).Delete(conditionName, &metav1.DeleteOptions{})
})
return d.Execute(s, condNames)
d.Delete(s, condNames)
d.PrintSuccesses(s)
return d.Errors()
}
4 changes: 3 additions & 1 deletion pkg/cmd/eventlistener/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ func deleteEventListeners(s *cli.Stream, p cli.Params, elNames []string) error {
d := deleter.New("EventListener", func(listenerName string) error {
return cs.Triggers.TektonV1alpha1().EventListeners(p.Namespace()).Delete(listenerName, &metav1.DeleteOptions{})
})
return d.Execute(s, elNames)
d.Delete(s, elNames)
d.PrintSuccesses(s)
return d.Errors()
}
13 changes: 8 additions & 5 deletions pkg/cmd/pipeline/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ or
}
f.AddFlags(c)
c.Flags().BoolVarP(&opts.ForceDelete, "force", "f", false, "Whether to force deletion (default: false)")
c.Flags().BoolVarP(&opts.DeleteAll, "all", "a", false, "Whether to delete related resources (pipelineruns) (default: false)")
c.Flags().BoolVarP(&opts.DeleteAll, "all", "a", false, "Whether to also delete related resources (pipelineruns) (default: false)")

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_pipeline")
return c
Expand All @@ -82,12 +82,15 @@ func deletePipelines(opts *options.DeleteOptions, s *cli.Stream, p cli.Params, p
d := deleter.New("Pipeline", func(pipelineName string) error {
return cs.Tekton.TektonV1alpha1().Pipelines(p.Namespace()).Delete(pipelineName, &metav1.DeleteOptions{})
})
d.WithRelated("PipelineRun", pipelineRunLister(p, cs), func(pipelineRunName string) error {
return cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(pipelineRunName, &metav1.DeleteOptions{})
})
deletedPipelineNames := d.Delete(s, pNames)
if opts.DeleteAll {
d.WithRelated("PipelineRun", pipelineRunLister(p, cs), func(pipelineRunName string) error {
return cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(pipelineRunName, &metav1.DeleteOptions{})
})
d.DeleteRelated(s, deletedPipelineNames)
}
return d.Execute(s, pNames)
d.PrintSuccesses(s)
return d.Errors()
}

func pipelineRunLister(p cli.Params, cs *cli.Clients) func(string) ([]string, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pipeline/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestPipelineDelete(t *testing.T) {
clock := clockwork.NewFakeClock()

seeds := make([]pipelinetest.Clients, 0)
for i := 0; i < 5; i++ {
for i := 0; i < 7; i++ {
cs, _ := test.SeedTestData(t, pipelinetest.Data{
Pipelines: []*v1alpha1.Pipeline{
tb.Pipeline("pipeline", "ns",
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/pipelineresource/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ func deleteResources(s *cli.Stream, p cli.Params, preNames []string) error {
d := deleter.New("PipelineResource", func(resourceName string) error {
return cs.Tekton.TektonV1alpha1().PipelineResources(p.Namespace()).Delete(resourceName, &metav1.DeleteOptions{})
})
return d.Execute(s, preNames)
d.Delete(s, preNames)
d.PrintSuccesses(s)
return d.Errors()
}
48 changes: 40 additions & 8 deletions pkg/cmd/pipelinerun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package pipelinerun

import (
"errors"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -27,7 +28,7 @@ import (
)

func deleteCommand(p cli.Params) *cobra.Command {
opts := &options.DeleteOptions{Resource: "pipelinerun", ForceDelete: false}
opts := &options.DeleteOptions{Resource: "pipelinerun", ForceDelete: false, ParentResource: "pipeline"}
f := cliopts.NewPrintFlags("delete")
eg := `Delete PipelineRuns with names 'foo' and 'bar' in namespace 'quux':

Expand All @@ -43,7 +44,7 @@ or
Aliases: []string{"rm"},
Short: "Delete pipelineruns in a namespace",
Example: eg,
Args: cobra.MinimumNArgs(1),
Args: cobra.MinimumNArgs(0),
SilenceUsage: true,
Annotations: map[string]string{
"commandType": "main",
Expand All @@ -63,22 +64,53 @@ or
return err
}

return deletePipelineRuns(s, p, args)
return deletePipelineRuns(s, p, args, opts.ParentResourceName)
},
}
f.AddFlags(c)
c.Flags().BoolVarP(&opts.ForceDelete, "force", "f", false, "Whether to force deletion (default: false)")
c.Flags().StringVarP(&opts.ParentResourceName, "pipeline", "p", "", "The name of a pipeline whose pipelineruns should be deleted (does not delete the pipeline)")
_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_pipelinerun")
return c
}

func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string) error {
func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, parentPipeline string) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}
d := deleter.New("PipelineRun", func(pipelineRunName string) error {
return cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(pipelineRunName, &metav1.DeleteOptions{})
})
return d.Execute(s, prNames)
var d *deleter.Deleter
if parentPipeline == "" {
d = deleter.New("PipelineRun", func(pipelineRunName string) error {
return cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(pipelineRunName, &metav1.DeleteOptions{})
})
d.Delete(s, prNames)
} else {
d = deleter.New("Pipeline", func(_ string) error {
return errors.New("the pipeline should not be deleted")
})
d.WithRelated("PipelineRun", pipelineRunLister(p, cs), func(pipelineRunName string) error {
return cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(pipelineRunName, &metav1.DeleteOptions{})
})
d.DeleteRelated(s, []string{parentPipeline})
}
d.PrintSuccesses(s)
return d.Errors()
}

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", pipelineName),
}
pipelineRuns, err := cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).List(lOpts)
if err != nil {
return nil, err
}
var names []string
for _, pr := range pipelineRuns.Items {
names = append(names, pr.Name)
}
return names, nil
}
}
23 changes: 20 additions & 3 deletions pkg/cmd/pipelinerun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ func TestPipelineRunDelete(t *testing.T) {
wantError: true,
want: "failed to delete pipelinerun \"nonexistent\": pipelineruns.tekton.dev \"nonexistent\" not found",
},
{
name: "Attempt remove forgetting to include pipelinerun names",
command: []string{"rm", "-n", "ns"},
input: seeds[2],
inputStream: nil,
wantError: true,
want: "must provide pipelineruns to delete or --pipeline flag",
},
{
name: "Remove pipelineruns of a pipeline",
command: []string{"rm", "--pipeline", "pipeline", "-n", "ns"},
input: seeds[0],
inputStream: strings.NewReader("y"),
wantError: false,
want: `Are you sure you want to delete all pipelineruns related to pipeline "pipeline" (y/n): `,
},
}

for _, tp := range testParams {
Expand All @@ -144,12 +160,13 @@ func TestPipelineRunDelete(t *testing.T) {
out, err := test.ExecuteCommand(pipelinerun, tp.command...)
if tp.wantError {
if err == nil {
t.Errorf("Error expected here")
t.Errorf("error expected here")
} else {
test.AssertOutput(t, tp.want, err.Error())
}
test.AssertOutput(t, tp.want, err.Error())
} else {
if err != nil {
t.Errorf("Unexpected Error")
t.Errorf("unexpected Error")
}
test.AssertOutput(t, tp.want, out)
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/cmd/task/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ or
}
f.AddFlags(c)
c.Flags().BoolVarP(&opts.ForceDelete, "force", "f", false, "Whether to force deletion (default: false)")
c.Flags().BoolVarP(&opts.DeleteAll, "all", "a", false, "Whether to delete related resources (taskruns) (default: false)")
c.Flags().BoolVarP(&opts.DeleteAll, "all", "a", false, "Whether to also delete related resources (taskruns) (default: false)")

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_task")
return c
Expand All @@ -82,12 +82,15 @@ func deleteTask(opts *options.DeleteOptions, s *cli.Stream, p cli.Params, taskNa
d := deleter.New("Task", func(taskName string) error {
return cs.Tekton.TektonV1alpha1().Tasks(p.Namespace()).Delete(taskName, &metav1.DeleteOptions{})
})
d.WithRelated("TaskRun", taskRunLister(p, cs), func(taskRunName string) error {
return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{})
})
deletedTaskNames := d.Delete(s, taskNames)
if opts.DeleteAll {
d.WithRelated("TaskRun", taskRunLister(p, cs), func(taskRunName string) error {
return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{})
})
d.DeleteRelated(s, deletedTaskNames)
}
return d.Execute(s, taskNames)
d.PrintSuccesses(s)
return d.Errors()
}

func taskRunLister(p cli.Params, cs *cli.Clients) func(string) ([]string, error) {
Expand Down
48 changes: 40 additions & 8 deletions pkg/cmd/taskrun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package taskrun

import (
"errors"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -27,7 +28,7 @@ import (
)

func deleteCommand(p cli.Params) *cobra.Command {
opts := &options.DeleteOptions{Resource: "taskrun", ForceDelete: false}
opts := &options.DeleteOptions{Resource: "taskrun", ForceDelete: false, ParentResource: "task"}
f := cliopts.NewPrintFlags("delete")
eg := `Delete TaskRuns with names 'foo' and 'bar' in namespace 'quux':

Expand All @@ -43,7 +44,7 @@ or
Aliases: []string{"rm"},
Short: "Delete taskruns in a namespace",
Example: eg,
Args: cobra.MinimumNArgs(1),
Args: cobra.MinimumNArgs(0),
SilenceUsage: true,
Annotations: map[string]string{
"commandType": "main",
Expand All @@ -63,22 +64,53 @@ or
return err
}

return deleteTaskRuns(s, p, args)
return deleteTaskRuns(s, p, args, opts.ParentResourceName)
},
}
f.AddFlags(c)
c.Flags().BoolVarP(&opts.ForceDelete, "force", "f", false, "Whether to force deletion (default: false)")
c.Flags().StringVarP(&opts.ParentResourceName, "task", "t", "", "The name of a task whose taskruns should be deleted (does not delete the task)")
_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_taskrun")
return c
}

func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string) error {
func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, parentTask string) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}
d := deleter.New("TaskRun", func(taskRunName string) error {
return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{})
})
return d.Execute(s, trNames)
var d *deleter.Deleter
if parentTask == "" {
d = deleter.New("TaskRun", func(taskRunName string) error {
return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{})
})
d.Delete(s, trNames)
} else {
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{})
})
d.DeleteRelated(s, []string{parentTask})
}
d.PrintSuccesses(s)
return d.Errors()
}

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", taskName),
}
taskRuns, err := cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).List(lOpts)
if err != nil {
return nil, err
}
var names []string
for _, tr := range taskRuns.Items {
names = append(names, tr.Name)
}
return names, nil
}
}
Loading