Skip to content

Commit

Permalink
Add --keep to --all, to keep the last N taskruns
Browse files Browse the repository at this point in the history
Closes tektoncd#718

Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
  • Loading branch information
vdemeester authored and tekton-robot committed Feb 27, 2020
1 parent 4f8424f commit 5dcb626
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
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
--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
--keep int Keep n least recent number of pipelineruns when deleting alls
-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
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\-h\fP, \fB\-\-help\fP[=false]
help for delete

.PP
\fB\-\-keep\fP=0
Keep n least recent number of pipelineruns when deleting alls

.PP
\fB\-o\fP, \fB\-\-output\fP=""
Output format. One of: json|yaml|name|go\-template|go\-template\-file|template|templatefile|jsonpath|jsonpath\-file.
Expand Down
16 changes: 13 additions & 3 deletions pkg/cmd/taskrun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ or
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.Flags().BoolVarP(&opts.DeleteAllNs, "all", "", false, "Delete all taskruns in a namespace (default: false)")
c.Flags().IntVarP(&opts.Keep, "keep", "", 0, "Keep n least recent number of pipelineruns when deleting alls")
_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_taskrun")
return c
}
Expand All @@ -86,7 +87,7 @@ func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options
d = deleter.New("TaskRun", func(taskRunName string) error {
return cs.Tekton.TektonV1alpha1().TaskRuns(p.Namespace()).Delete(taskRunName, &metav1.DeleteOptions{})
})
trs, err := allTaskRunNames(p, cs)
trs, err := allTaskRunNames(p, cs, opts.Keep)
if err != nil {
return err
}
Expand All @@ -109,7 +110,11 @@ func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options
d.PrintSuccesses(s)
} else if opts.DeleteAllNs {
if d.Errors() == nil {
fmt.Fprintf(s.Out, "All TaskRuns deleted in namespace %q\n", p.Namespace())
if opts.Keep > 0 {
fmt.Fprintf(s.Out, "All but %d TaskRuns deleted in namespace %q\n", opts.Keep, p.Namespace())
} else {
fmt.Fprintf(s.Out, "All TaskRuns deleted in namespace %q\n", p.Namespace())
}
}
}
return d.Errors()
Expand All @@ -132,13 +137,18 @@ func taskRunLister(p cli.Params, cs *cli.Clients) func(string) ([]string, error)
}
}

func allTaskRunNames(p cli.Params, cs *cli.Clients) ([]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{})
if err != nil {
return nil, err
}
var names []string
var counter = 0
for _, tr := range taskRuns.Items {
if keep > 0 && counter != keep {
counter++
continue
}
names = append(names, tr.Name)
}
return names, nil
Expand Down
16 changes: 16 additions & 0 deletions pkg/cmd/taskrun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ func TestTaskRunDelete(t *testing.T) {
wantError: false,
want: "All TaskRuns deleted in namespace \"ns\"\n",
},
{
name: "Delete all keeping 2",
command: []string{"delete", "--all", "-f", "--keep", "2", "-n", "ns"},
input: seeds[4],
inputStream: nil,
wantError: false,
want: "All but 2 TaskRuns deleted in namespace \"ns\"\n",
},
{
name: "Keep -1 is a no go",
command: []string{"delete", "--all", "-f", "--keep", "-1", "-n", "ns"},
input: seeds[4],
inputStream: nil,
wantError: true,
want: "keep option should not be lower than 0",
},
{
name: "Error from using taskrun name with --all",
command: []string{"delete", "taskrun", "--all", "-n", "ns"},
Expand Down

0 comments on commit 5dcb626

Please sign in to comment.