From 8c490524022b7dc770ca57be40a9be921fb53342 Mon Sep 17 00:00:00 2001 From: pratap0007 Date: Wed, 3 May 2023 17:59:57 +0530 Subject: [PATCH] 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 fixes : #1990 Signed-off-by: Shiv Verma --- pkg/cmd/pipelinerun/delete.go | 28 +++++++++++++++++++++++----- pkg/cmd/taskrun/delete.go | 22 +++++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/pkg/cmd/pipelinerun/delete.go b/pkg/cmd/pipelinerun/delete.go index 5ebb1d36ca..c0d68eafd2 100644 --- a/pkg/cmd/pipelinerun/delete.go +++ b/pkg/cmd/pipelinerun/delete.go @@ -278,6 +278,7 @@ func allPipelineRunNames(cs *cli.Clients, keep, since int, ignoreRunning bool, l } switch { case since > 0 && keep > 0: + fmt.Println("--- keep pipelinrun by age and number", since, keep) todelete, tokeep = keepPipelineRunsByAgeAndNumber(pipelineRuns, since, keep, ignoreRunning) case since > 0: todelete, tokeep = keepPipelineRunsByAge(pipelineRuns, since, ignoreRunning) @@ -287,7 +288,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 +298,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 +330,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 }