Skip to content

Commit

Permalink
Fixes pipelinerun and taskrun deletion with keep flag
Browse files Browse the repository at this point in the history
This patch fixes pipelinerun and taskrun deletion when keep argument
is not satisfied(to keep more than the existing pipelinerun/taskrun
for a pipeline/task) and returns exit code 0

Sigend-off-by: Shiv Verma <shverma@redhat.com>
  • Loading branch information
pratap0007 authored and tekton-robot committed Apr 15, 2022
1 parent 4f2e6bf commit 31fc00c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pkg/cmd/pipelinerun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, opts *opt
labelSelector := fmt.Sprintf("tekton.dev/pipeline=%s", opts.ParentResourceName)

// Compute the total no of PipelineRuns which we need to delete
prtodelete, _, err := allPipelineRunNames(cs, opts.Keep, opts.KeepSince, opts.IgnoreRunning, labelSelector, p.Namespace())
prtodelete, prtokeep, err := allPipelineRunNames(cs, opts.Keep, opts.KeepSince, opts.IgnoreRunning, labelSelector, p.Namespace())
if err != nil {
return err
}
Expand All @@ -179,6 +179,10 @@ func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, opts *opt
d.WithRelated("PipelineRun", pipelineRunLister(cs, opts.Keep, opts.KeepSince, p.Namespace(), opts.IgnoreRunning), func(pipelineRunName string) error {
return actions.Delete(prGroupResource, cs.Dynamic, cs.Tekton.Discovery(), pipelineRunName, p.Namespace(), metav1.DeleteOptions{})
})
if len(prtodelete) == 0 && opts.Keep > len(prtokeep) {
fmt.Fprintf(s.Out, "There is/are only %d %s(s) associated for Pipeline: %s \n", len(prtokeep), opts.Resource, opts.ParentResourceName)
return nil
}
d.DeleteRelated(s, []string{opts.ParentResourceName})
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/pipelinerun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,15 @@ func TestPipelineRunDelete_v1beta1(t *testing.T) {
wantError: false,
want: "No PipelineRuns found\n",
},
{
name: "Attempt to delete PieplineRun by keeping more than existing PipelineRun for a Pipeline",
command: []string{"delete", "-i", "-f", "--keep", "3", "--pipeline", "pipeline", "-n", "ns"},
dynamic: seeds[2].dynamicClient,
input: seeds[2].pipelineClient,
inputStream: nil,
wantError: false,
want: "There is/are only 2 PipelineRun(s) associated for Pipeline: pipeline \n",
},
}

for _, tp := range testParams {
Expand Down
6 changes: 5 additions & 1 deletion pkg/cmd/taskrun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options
labelSelector := fmt.Sprintf("tekton.dev/%s=%s", resourceType, opts.ParentResourceName)

// Compute the total no of TaskRuns which we need to delete
trToDelete, _, err := allTaskRunNames(cs, opts.Keep, opts.KeepSince, opts.IgnoreRunning, labelSelector, p.Namespace())
trToDelete, trToKeep, err := allTaskRunNames(cs, opts.Keep, opts.KeepSince, opts.IgnoreRunning, labelSelector, p.Namespace())
if err != nil {
return err
}
Expand All @@ -202,6 +202,10 @@ func deleteTaskRuns(s *cli.Stream, p cli.Params, trNames []string, opts *options
d.WithRelated("TaskRun", taskRunLister(p, opts.Keep, opts.KeepSince, opts.ParentResource, cs, opts.IgnoreRunning), func(taskRunName string) error {
return actions.Delete(trGroupResource, cs.Dynamic, cs.Tekton.Discovery(), taskRunName, p.Namespace(), metav1.DeleteOptions{})
})
if len(trToDelete) == 0 && opts.Keep > len(trToKeep) {
fmt.Fprintf(s.Out, "There is/are only %d %s(s) associated for Task: %s \n", len(trToKeep), opts.Resource, opts.ParentResourceName)
return nil
}
d.DeleteRelated(s, []string{opts.ParentResourceName})
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/taskrun/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,15 @@ func TestTaskRunDelete_v1beta1(t *testing.T) {
wantError: true,
want: "taskruns.tekton.dev \"nonexistent\" not found",
},
{
name: "Attempt to delete TaskRun by keeping more than existing TaskRun for a Task",
command: []string{"delete", "-i", "-f", "--keep", "2", "--task", "random", "-n", "ns"},
dynamic: seeds[10].dynamicClient,
input: seeds[10].pipelineClient,
inputStream: nil,
wantError: false,
want: "There is/are only 1 TaskRun(s) associated for Task: random \n",
},
}

for _, tp := range testParams {
Expand Down

0 comments on commit 31fc00c

Please sign in to comment.