Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tkn pr/tr delete for --keep and --keep-since flag #2017

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions pkg/cmd/pipelinerun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ func deletePipelineRuns(s *cli.Stream, p cli.Params, prNames []string, opts *opt
fmt.Fprintf(s.Out, "There is/are only %d %s(s) associated for Pipeline: %s \n", len(prtokeep), opts.Resource, opts.ParentResourceName)
return nil
}
if len(prtodelete) == 0 && opts.KeepSince > 0 {
fmt.Fprintf(s.Out, "There is no PipelineRun older than %d minutes \n", opts.KeepSince)
return nil
}
d.DeleteRelated([]string{opts.ParentResourceName})
}

Expand Down Expand Up @@ -292,7 +296,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 @@ -302,7 +306,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 @@ -334,10 +338,26 @@ 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:
todelete = append(todelete, run.Name)
case keep > 0:
tokeep = append(tokeep, run.Name)
keep--
default:
todelete = append(todelete, run.Name)
}
}
return todelete, tokeep
}
Loading
Loading