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

--all option for tkn pipelinerun delete #689

Merged
merged 1 commit into from
Feb 10, 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
1 change: 1 addition & 0 deletions docs/cmd/tkn_pipelinerun_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ or
### Options

```
--all Delete all pipelineruns in a namespace (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
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 @@ -19,6 +19,10 @@ Delete pipelineruns in a namespace


.SH OPTIONS
.PP
\fB\-\-all\fP[=false]
Delete all pipelineruns in a namespace (default: false)

.PP
\fB\-\-allow\-missing\-template\-keys\fP[=true]
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.
Expand Down
43 changes: 36 additions & 7 deletions pkg/cmd/pipelinerun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

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

Expand Down Expand Up @@ -64,37 +64,54 @@ or
return err
}

return deletePipelineRuns(s, p, args, opts.ParentResourceName)
return deletePipelineRuns(s, p, args, opts)
},
}
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.Flags().BoolVarP(&opts.DeleteAllNs, "all", "", false, "Delete all pipelineruns in a namespace (default: false)")
_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_pipelinerun")
return c
}

func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, parentPipeline string) error {
func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, opts *options.DeleteOptions) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}
var d *deleter.Deleter
if parentPipeline == "" {
switch {
case opts.DeleteAllNs:
d = deleter.New("PipelineRun", func(pipelineRunName string) error {
return cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(pipelineRunName, &metav1.DeleteOptions{})
})
prs, err := allPipelineRunNames(p, cs)
if err != nil {
return err
}
d.Delete(s, prs)
case opts.ParentResourceName == "":
d = deleter.New("PipelineRun", func(pipelineRunName string) error {
return cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).Delete(pipelineRunName, &metav1.DeleteOptions{})
})
d.Delete(s, prNames)
} else {
default:
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.DeleteRelated(s, []string{opts.ParentResourceName})
}
if !opts.DeleteAllNs {
d.PrintSuccesses(s)
} else if opts.DeleteAllNs {
if d.Errors() == nil {
fmt.Fprintf(s.Out, "All PipelineRuns deleted in namespace %q\n", p.Namespace())
}
}
d.PrintSuccesses(s)
return d.Errors()
}

Expand All @@ -114,3 +131,15 @@ func pipelineRunLister(p cli.Params, cs *cli.Clients) func(string) ([]string, er
return names, nil
}
}

func allPipelineRunNames(p cli.Params, cs *cli.Clients) ([]string, error) {
pipelineRuns, err := cs.Tekton.TektonV1alpha1().PipelineRuns(p.Namespace()).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
var names []string
for _, pr := range pipelineRuns.Items {
names = append(names, pr.Name)
}
return names, nil
}
59 changes: 57 additions & 2 deletions pkg/cmd/pipelinerun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestPipelineRunDelete(t *testing.T) {
}

seeds := make([]pipelinetest.Clients, 0)
for i := 0; i < 3; i++ {
for i := 0; i < 5; i++ {
cs, _ := test.SeedTestData(t, pipelinetest.Data{
Pipelines: []*v1alpha1.Pipeline{
tb.Pipeline("pipeline", "ns",
Expand All @@ -68,7 +68,38 @@ func TestPipelineRunDelete(t *testing.T) {
cb.PipelineRunCompletionTime(clock.Now().Add(10*time.Minute)),
),
),
tb.PipelineRun("pipeline-run-2", "ns",
cb.PipelineRunCreationTimestamp(clock.Now()),
tb.PipelineRunLabel("tekton.dev/pipeline", "pipeline"),
tb.PipelineRunSpec("pipeline"),
tb.PipelineRunStatus(
tb.PipelineRunStatusCondition(apis.Condition{
Status: corev1.ConditionTrue,
Reason: resources.ReasonSucceeded,
}),
// pipeline run starts now
tb.PipelineRunStartTime(clock.Now()),
// takes 10 minutes to complete
cb.PipelineRunCompletionTime(clock.Now().Add(10*time.Minute)),
),
),
tb.PipelineRun("pipeline-run-3", "ns",
cb.PipelineRunCreationTimestamp(clock.Now()),
tb.PipelineRunLabel("tekton.dev/pipeline", "pipeline"),
tb.PipelineRunSpec("pipeline"),
tb.PipelineRunStatus(
tb.PipelineRunStatusCondition(apis.Condition{
Status: corev1.ConditionTrue,
Reason: resources.ReasonSucceeded,
}),
// pipeline run starts now
tb.PipelineRunStartTime(clock.Now()),
// takes 10 minutes to complete
cb.PipelineRunCompletionTime(clock.Now().Add(10*time.Minute)),
),
),
},

Namespaces: ns,
})
seeds = append(seeds, cs)
Expand Down Expand Up @@ -144,7 +175,31 @@ func TestPipelineRunDelete(t *testing.T) {
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): `,
want: "Are you sure you want to delete all pipelineruns related to pipeline \"pipeline\" (y/n): PipelineRuns deleted: \"pipeline-run-2\", \"pipeline-run-3\"\n",
},
{
name: "Delete all with prompt",
command: []string{"delete", "--all", "-n", "ns"},
input: seeds[3],
inputStream: strings.NewReader("y"),
wantError: false,
want: "Are you sure you want to delete all pipelineruns in namespace \"ns\" (y/n): All PipelineRuns deleted in namespace \"ns\"\n",
},
{
name: "Delete all with -f",
command: []string{"delete", "--all", "-f", "-n", "ns"},
input: seeds[4],
inputStream: nil,
wantError: false,
want: "All PipelineRuns deleted in namespace \"ns\"\n",
},
{
name: "Error from using pipelinerun name with --all",
command: []string{"delete", "pipelinerun", "--all", "-n", "ns"},
input: seeds[4],
inputStream: nil,
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/taskrun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ func TestTaskRunDelete(t *testing.T) {
name: "Delete all with -f",
command: []string{"delete", "--all", "-f", "-n", "ns"},
input: seeds[4],
inputStream: strings.NewReader("y"),
inputStream: nil,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No prompt needed for -f and error scenario

wantError: false,
want: "All TaskRuns deleted in namespace \"ns\"\n",
},
{
name: "Error from using taskrun name with --all",
command: []string{"delete", "taskrun", "--all", "-n", "ns"},
input: seeds[4],
inputStream: strings.NewReader("y"),
inputStream: nil,
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
Expand Down