Skip to content

Commit

Permalink
Fix tkn pr/tr delete for --keep and --keep-since flag
Browse files Browse the repository at this point in the history
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 : tektoncd#1990

Signed-off-by: Shiv Verma <[email protected]>
  • Loading branch information
pratap0007 committed May 3, 2023
1 parent 964ae54 commit 8c49052
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
28 changes: 23 additions & 5 deletions pkg/cmd/pipelinerun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
22 changes: 19 additions & 3 deletions pkg/cmd/taskrun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 8c49052

Please sign in to comment.