Skip to content

Commit 5ada0b9

Browse files
committed
Fix tkn pr/tr delete for --keep and --keep-since flag
This patch fix the tkn pr delete command for --keep and --keep-since now both flags are working together and --keep-since has the higher priority and also does unit test refactor for pipelinerun delete command fixes : #1990 Signed-off-by: Shiv Verma <[email protected]>
1 parent 2477813 commit 5ada0b9

File tree

3 files changed

+96
-86
lines changed

3 files changed

+96
-86
lines changed

pkg/cmd/pipelinerun/delete.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, opts *opt
189189
fmt.Fprintf(s.Out, "There is/are only %d %s(s) associated for Pipeline: %s \n", len(prtokeep), opts.Resource, opts.ParentResourceName)
190190
return nil
191191
}
192+
if len(prtodelete) == 0 && opts.KeepSince > 0 {
193+
fmt.Fprintf(s.Out, "There is no PipelineRun older than %d minutes \n", opts.KeepSince)
194+
return nil
195+
}
192196
d.DeleteRelated([]string{opts.ParentResourceName})
193197
}
194198

@@ -292,7 +296,7 @@ func allPipelineRunNames(cs *cli.Clients, keep, since int, ignoreRunning bool, l
292296
return todelete, tokeep, nil
293297
}
294298

295-
func keepPipelineRunsByAge(pipelineRuns *v1.PipelineRunList, keep int, ignoreRunning bool) ([]string, []string) {
299+
func keepPipelineRunsByAge(pipelineRuns *v1.PipelineRunList, since int, ignoreRunning bool) ([]string, []string) {
296300
var todelete, tokeep []string
297301
for _, run := range pipelineRuns.Items {
298302
if run.Status.Conditions == nil {
@@ -302,7 +306,7 @@ func keepPipelineRunsByAge(pipelineRuns *v1.PipelineRunList, keep int, ignoreRun
302306
// for PipelineRuns in running status
303307
case !ignoreRunning && run.Status.CompletionTime == nil:
304308
todelete = append(todelete, run.Name)
305-
case time.Since(run.Status.CompletionTime.Time) > time.Duration(keep)*time.Minute:
309+
case time.Since(run.Status.CompletionTime.Time) > time.Duration(since)*time.Minute:
306310
todelete = append(todelete, run.Name)
307311
default:
308312
tokeep = append(tokeep, run.Name)
@@ -334,10 +338,26 @@ func keepPipelineRunsByNumber(pipelineRuns *v1.PipelineRunList, keep int) ([]str
334338
func keepPipelineRunsByAgeAndNumber(pipelineRuns *v1.PipelineRunList, since int, keep int, ignoreRunning bool) ([]string, []string) {
335339
var todelete, tokeep []string
336340

337-
todelete, tokeep = keepPipelineRunsByAge(pipelineRuns, since, ignoreRunning)
341+
// Sort PipelineRuns by time
342+
prsort.SortByStartTime(pipelineRuns.Items)
338343

339-
if len(tokeep) != keep {
340-
todelete, tokeep = keepPipelineRunsByNumber(pipelineRuns, keep)
344+
for _, run := range pipelineRuns.Items {
345+
if run.Status.Conditions == nil {
346+
continue
347+
}
348+
switch {
349+
// for PipelineRuns in running status
350+
case time.Since(run.Status.CompletionTime.Time) > time.Duration(since)*time.Minute &&
351+
!ignoreRunning && run.Status.CompletionTime == nil:
352+
todelete = append(todelete, run.Name)
353+
case time.Since(run.Status.CompletionTime.Time) > time.Duration(since)*time.Minute:
354+
todelete = append(todelete, run.Name)
355+
case keep > 0:
356+
tokeep = append(tokeep, run.Name)
357+
keep--
358+
default:
359+
todelete = append(todelete, run.Name)
360+
}
341361
}
342362
return todelete, tokeep
343363
}

0 commit comments

Comments
 (0)