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

Fix --keep with TaskRun and PipelineRun Delete when Using --task or --pipeline Flags #990

Merged
merged 1 commit into from
Jun 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
29 changes: 19 additions & 10 deletions pkg/cmd/pipelinerun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
pr "github.com/tektoncd/cli/pkg/pipelinerun"
prsort "github.com/tektoncd/cli/pkg/pipelinerun/sort"
validate "github.com/tektoncd/cli/pkg/validate"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
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 @@ -68,7 +69,7 @@ or
return fmt.Errorf("keep option should not be lower than 0")
}

if opts.Keep > 0 {
if opts.Keep > 0 && opts.ParentResourceName == "" {
opts.DeleteAllNs = true
}

Expand Down Expand Up @@ -115,13 +116,21 @@ func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, opts *opt
d = deleter.New("Pipeline", func(_ string) error {
return errors.New("the pipeline should not be deleted")
})
d.WithRelated("PipelineRun", pipelineRunLister(cs, p.Namespace()), func(pipelineRunName string) error {
d.WithRelated("PipelineRun", pipelineRunLister(cs, opts.Keep, p.Namespace()), func(pipelineRunName string) error {
return actions.Delete(prGroupResource, cs, pipelineRunName, p.Namespace(), &metav1.DeleteOptions{})
})
d.DeleteRelated(s, []string{opts.ParentResourceName})
}
if !opts.DeleteAllNs {
d.PrintSuccesses(s)
switch {
case opts.Keep > 0:
// Should only occur in case of --pipeline and --keep being used together
fmt.Fprintf(s.Out, "All but %d PipelineRuns associated with Pipeline %q deleted in namespace %q\n", opts.Keep, opts.ParentResourceName, p.Namespace())
case opts.ParentResourceName != "":
fmt.Fprintf(s.Out, "All PipelineRuns associated with Pipeline %q deleted in namespace %q\n", opts.ParentResourceName, p.Namespace())
default:
d.PrintSuccesses(s)
}
} else if opts.DeleteAllNs {
if d.Errors() == nil {
if opts.Keep > 0 {
Expand All @@ -134,7 +143,7 @@ func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, opts *opt
return d.Errors()
}

func pipelineRunLister(cs *cli.Clients, ns string) func(string) ([]string, error) {
func pipelineRunLister(cs *cli.Clients, keep int, ns string) func(string) ([]string, error) {
return func(pipelineName string) ([]string, error) {
lOpts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/pipeline=%s", pipelineName),
Expand All @@ -143,11 +152,7 @@ func pipelineRunLister(cs *cli.Clients, ns string) func(string) ([]string, error
if err != nil {
return nil, err
}
var names []string
for _, run := range pipelineRuns.Items {
names = append(names, run.Name)
}
return names, nil
return keepPipelineRuns(pipelineRuns, keep), nil
}
}

Expand All @@ -156,6 +161,10 @@ func allPipelineRunNames(cs *cli.Clients, keep int, ns string) ([]string, error)
if err != nil {
return nil, err
}
return keepPipelineRuns(pipelineRuns, keep), nil
}

func keepPipelineRuns(pipelineRuns *v1beta1.PipelineRunList, keep int) []string {
var names []string
var counter = 0
prsort.SortByStartTime(pipelineRuns.Items)
Expand All @@ -166,5 +175,5 @@ func allPipelineRunNames(cs *cli.Clients, keep int, ns string) ([]string, error)
}
names = append(names, run.Name)
}
return names, nil
return names
}
44 changes: 40 additions & 4 deletions pkg/cmd/pipelinerun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestPipelineRunDelete(t *testing.T) {
}

seeds := make([]clients, 0)
for i := 0; i < 5; i++ {
for i := 0; i < 6; i++ {
cs, _ := test.SeedTestData(t, pipelinetest.Data{
Pipelines: pdata,
PipelineRuns: prdata,
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestPipelineRunDelete(t *testing.T) {
input: seeds[0].pipelineClient,
inputStream: strings.NewReader("y"),
wantError: false,
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",
want: "Are you sure you want to delete all pipelineruns related to pipeline \"pipeline\" (y/n): All PipelineRuns associated with Pipeline \"pipeline\" deleted in namespace \"ns\"\n",
},
{
name: "Delete all with prompt",
Expand Down Expand Up @@ -274,6 +274,24 @@ func TestPipelineRunDelete(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Remove pipelineruns of a pipeline using --keep",
command: []string{"rm", "--pipeline", "pipeline", "-n", "ns", "--keep", "2"},
dynamic: seeds[5].dynamicClient,
input: seeds[5].pipelineClient,
inputStream: nil,
wantError: false,
want: "Are you sure you want to delete all pipelineruns related to pipeline \"pipeline\" keeping 2 pipelineruns (y/n): All but 2 PipelineRuns associated with Pipeline \"pipeline\" deleted in namespace \"ns\"\n",
},
{
name: "Error from using argument with --keep",
command: []string{"rm", "pipelinerun", "--keep", "2"},
dynamic: seeds[5].dynamicClient,
input: seeds[5].pipelineClient,
inputStream: nil,
wantError: true,
want: "--keep flag should not have any arguments specified with it",
},
}

for _, tp := range testParams {
Expand Down Expand Up @@ -379,7 +397,7 @@ func TestPipelineRunDelete_v1beta1(t *testing.T) {
}

seeds := make([]clients, 0)
for i := 0; i < 5; i++ {
for i := 0; i < 6; i++ {
cs, _ := test.SeedTestData(t, pipelinetest.Data{
Pipelines: pdata,
PipelineRuns: prdata,
Expand Down Expand Up @@ -486,7 +504,7 @@ func TestPipelineRunDelete_v1beta1(t *testing.T) {
input: seeds[0].pipelineClient,
inputStream: strings.NewReader("y"),
wantError: false,
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",
want: "Are you sure you want to delete all pipelineruns related to pipeline \"pipeline\" (y/n): All PipelineRuns associated with Pipeline \"pipeline\" deleted in namespace \"ns\"\n",
},
{
name: "Delete all with prompt",
Expand Down Expand Up @@ -533,6 +551,24 @@ func TestPipelineRunDelete_v1beta1(t *testing.T) {
wantError: true,
want: "--all flag should not have any arguments or flags specified with it",
},
{
name: "Remove pipelineruns of a pipeline using --keep",
command: []string{"rm", "--pipeline", "pipeline", "-n", "ns", "--keep", "2"},
dynamic: seeds[5].dynamicClient,
input: seeds[5].pipelineClient,
inputStream: strings.NewReader("y"),
wantError: false,
want: "Are you sure you want to delete all pipelineruns related to pipeline \"pipeline\" keeping 2 pipelineruns (y/n): All but 2 PipelineRuns associated with Pipeline \"pipeline\" deleted in namespace \"ns\"\n",
},
{
name: "Error from using argument with --keep",
command: []string{"rm", "pipelinerun", "--keep", "2"},
dynamic: seeds[5].dynamicClient,
input: seeds[5].pipelineClient,
inputStream: strings.NewReader("y"),
wantError: true,
want: "--keep flag should not have any arguments specified with it",
},
}

for _, tp := range testParams {
Expand Down
33 changes: 21 additions & 12 deletions pkg/cmd/taskrun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"

trsort "github.com/tektoncd/cli/pkg/taskrun/sort"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions"
Expand Down Expand Up @@ -69,7 +70,7 @@ or
return fmt.Errorf("keep option should not be lower than 0")
}

if opts.Keep > 0 {
if opts.Keep > 0 && opts.ParentResourceName == "" {
opts.DeleteAllNs = true
}

Expand Down Expand Up @@ -115,13 +116,22 @@ func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options
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 {
d.WithRelated("TaskRun", taskRunLister(p, opts.Keep, cs), func(taskRunName string) error {
return actions.Delete(trGroupResource, cs, taskRunName, p.Namespace(), &metav1.DeleteOptions{})
})
d.DeleteRelated(s, []string{opts.ParentResourceName})
}

if !opts.DeleteAllNs {
d.PrintSuccesses(s)
switch {
case opts.Keep > 0:
// Should only occur in case of --task flag and --keep being used together
fmt.Fprintf(s.Out, "All but %d TaskRuns associated with Task %q deleted in namespace %q\n", opts.Keep, opts.ParentResourceName, p.Namespace())
case opts.ParentResourceName != "":
fmt.Fprintf(s.Out, "All TaskRuns associated with Task %q deleted in namespace %q\n", opts.ParentResourceName, p.Namespace())
default:
d.PrintSuccesses(s)
}
} else if opts.DeleteAllNs {
if d.Errors() == nil {
if opts.Keep > 0 {
Expand All @@ -134,7 +144,7 @@ func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options
return d.Errors()
}

func taskRunLister(p cli.Params, cs *cli.Clients) func(string) ([]string, error) {
func taskRunLister(p cli.Params, keep int, cs *cli.Clients) func(string) ([]string, error) {
return func(taskName string) ([]string, error) {
lOpts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("tekton.dev/task=%s", taskName),
Expand All @@ -143,29 +153,28 @@ func taskRunLister(p cli.Params, cs *cli.Clients) func(string) ([]string, error)
if err != nil {
return nil, err
}
var names []string
for _, tr := range taskRuns.Items {
names = append(names, tr.Name)
}
return names, nil
return keepTaskRuns(taskRuns, keep), nil
}
}

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
}
trsort.SortByStartTime(taskRuns.Items)
return keepTaskRuns(taskRuns, keep), nil
}

func keepTaskRuns(taskRuns *v1beta1.TaskRunList, keep int) []string {
var names []string
var counter = 0
trsort.SortByStartTime(taskRuns.Items)
for _, tr := range taskRuns.Items {
if keep > 0 && counter != keep {
counter++
continue
}
names = append(names, tr.Name)
}
return names, nil
return names
}
Loading