@@ -189,6 +189,10 @@ func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, opts *opt
189
189
fmt .Fprintf (s .Out , "There is/are only %d %s(s) associated for Pipeline: %s \n " , len (prtokeep ), opts .Resource , opts .ParentResourceName )
190
190
return nil
191
191
}
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
+ }
192
196
d .DeleteRelated ([]string {opts .ParentResourceName })
193
197
}
194
198
@@ -292,7 +296,7 @@ func allPipelineRunNames(cs *cli.Clients, keep, since int, ignoreRunning bool, l
292
296
return todelete , tokeep , nil
293
297
}
294
298
295
- func keepPipelineRunsByAge (pipelineRuns * v1.PipelineRunList , keep int , ignoreRunning bool ) ([]string , []string ) {
299
+ func keepPipelineRunsByAge (pipelineRuns * v1.PipelineRunList , since int , ignoreRunning bool ) ([]string , []string ) {
296
300
var todelete , tokeep []string
297
301
for _ , run := range pipelineRuns .Items {
298
302
if run .Status .Conditions == nil {
@@ -302,7 +306,7 @@ func keepPipelineRunsByAge(pipelineRuns *v1.PipelineRunList, keep int, ignoreRun
302
306
// for PipelineRuns in running status
303
307
case ! ignoreRunning && run .Status .CompletionTime == nil :
304
308
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 :
306
310
todelete = append (todelete , run .Name )
307
311
default :
308
312
tokeep = append (tokeep , run .Name )
@@ -334,10 +338,26 @@ func keepPipelineRunsByNumber(pipelineRuns *v1.PipelineRunList, keep int) ([]str
334
338
func keepPipelineRunsByAgeAndNumber (pipelineRuns * v1.PipelineRunList , since int , keep int , ignoreRunning bool ) ([]string , []string ) {
335
339
var todelete , tokeep []string
336
340
337
- todelete , tokeep = keepPipelineRunsByAge (pipelineRuns , since , ignoreRunning )
341
+ // Sort PipelineRuns by time
342
+ prsort .SortByStartTime (pipelineRuns .Items )
338
343
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
+ }
341
361
}
342
362
return todelete , tokeep
343
363
}
0 commit comments