diff --git a/pkg/cmd/pipelinerun/delete.go b/pkg/cmd/pipelinerun/delete.go index 5ebb1d36ca..4d2543514c 100644 --- a/pkg/cmd/pipelinerun/delete.go +++ b/pkg/cmd/pipelinerun/delete.go @@ -287,7 +287,7 @@ func allPipelineRunNames(cs *cli.Clients, keep, since int, ignoreRunning bool, l return todelete, tokeep, nil } -func keepPipelineRunsByAge(pipelineRuns *v1.PipelineRunList, keep int, ignoreRunning bool) ([]string, []string) { +func keepPipelineRunsByAge(pipelineRuns *v1.PipelineRunList, since int, ignoreRunning bool) ([]string, []string) { var todelete, tokeep []string for _, run := range pipelineRuns.Items { if run.Status.Conditions == nil { @@ -297,7 +297,7 @@ func keepPipelineRunsByAge(pipelineRuns *v1.PipelineRunList, keep int, ignoreRun // for PipelineRuns in running status case !ignoreRunning && run.Status.CompletionTime == nil: todelete = append(todelete, run.Name) - case time.Since(run.Status.CompletionTime.Time) > time.Duration(keep)*time.Minute: + case time.Since(run.Status.CompletionTime.Time) > time.Duration(since)*time.Minute: todelete = append(todelete, run.Name) default: tokeep = append(tokeep, run.Name) @@ -329,10 +329,27 @@ func keepPipelineRunsByNumber(pipelineRuns *v1.PipelineRunList, keep int) ([]str func keepPipelineRunsByAgeAndNumber(pipelineRuns *v1.PipelineRunList, since int, keep int, ignoreRunning bool) ([]string, []string) { var todelete, tokeep []string - todelete, tokeep = keepPipelineRunsByAge(pipelineRuns, since, ignoreRunning) + // Sort PipelineRuns by time + prsort.SortByStartTime(pipelineRuns.Items) - if len(tokeep) != keep { - todelete, tokeep = keepPipelineRunsByNumber(pipelineRuns, keep) + for _, run := range pipelineRuns.Items { + if run.Status.Conditions == nil { + continue + } + switch { + // for PipelineRuns in running status + case time.Since(run.Status.CompletionTime.Time) > time.Duration(since)*time.Minute && + !ignoreRunning && run.Status.CompletionTime == nil: + todelete = append(todelete, run.Name) + case time.Since(run.Status.CompletionTime.Time) > time.Duration(since)*time.Minute: + fmt.Println("ct", time.Since(run.Status.CompletionTime.Time)) + todelete = append(todelete, run.Name) + case keep > 0: + tokeep = append(tokeep, run.Name) + keep = keep - 1 + default: + todelete = append(todelete, run.Name) + } } return todelete, tokeep } diff --git a/pkg/cmd/taskrun/delete.go b/pkg/cmd/taskrun/delete.go index 0847bf83c0..7f0d106c94 100644 --- a/pkg/cmd/taskrun/delete.go +++ b/pkg/cmd/taskrun/delete.go @@ -378,10 +378,26 @@ func keepTaskRunsByNumber(taskRuns *v1.TaskRunList, keep int) ([]string, []strin func keepTaskRunsByAgeAndNumber(taskRuns *v1.TaskRunList, since int, keep int, ignoreRunning bool) ([]string, []string) { var todelete, tokeep []string - todelete, tokeep = keepTaskRunsByAge(taskRuns, since, ignoreRunning) + // Sort the taskrun by time + trsort.SortByStartTime(taskRuns.Items) - if len(tokeep) != keep { - todelete, tokeep = keepTaskRunsByNumber(taskRuns, keep) + for _, run := range taskRuns.Items { + if run.Status.Conditions == nil { + continue + } + switch { + // for PipelineRuns in running status + case time.Since(run.Status.CompletionTime.Time) > time.Duration(since)*time.Minute && + !ignoreRunning && run.Status.CompletionTime == nil: + todelete = append(todelete, run.Name) + case time.Since(run.Status.CompletionTime.Time) > time.Duration(since)*time.Minute: + todelete = append(todelete, run.Name) + case keep > 0: + tokeep = append(tokeep, run.Name) + keep = keep - 1 + default: + todelete = append(todelete, run.Name) + } } return todelete, tokeep }