Search code examples
kubernetespipelinecicdtekton

Tekton: How delete successful pipelineruns?


My aspired tekton usecase is simple:

  • successful pipelineruns should be removed after x days
  • failed pipelineruns shouldn't be removed automatically.

I plan to do the cleanup in an initial cleanup-task. That seems better to me than annotation- or cronjob-approaches. As long as nothing new is built, nothing has to be deleted.

Direct approaches:

  • Failed: tkn delete doesn't seem very helpful because it doesn't discriminate between successful or not.
  • Failed: oc delete --field-selector ... doesn't contain the well hidden but highly expressive field status.conditions[0].type==Succeeded

Indirect approaches (first filtering a list of podnames and then delete them - not elegant at all):

  • Failed: Filtering output with -o=jsonpath... seems costly and the condition-array seems to break the statement, so that (why ever?!) everything is returned... not viable
  • My last attempt is tkn pipelineruns list --show-managed-fields and parse this with sed/awk... which is gross... but at least it does what I want it to do... and quite efficiently at that. But it might result as brittle when the design of the output is going to change in future releases...

Do you have any better more elegant approaches? Thanks a lot!


Solution

  • For completeness, I paste here the kubectl/oc native command for those of us who do not have the tkn cli. Replace target-namespace as needed.

    Delete failed pipelineruns:

    kubectl -n target-namespace delete pipelinerun $(kubectl -n target-namespace get pipelinerun -o jsonpath='{range .items[?(@.status.conditions[*].status=="False")]}{.metadata.name}{"\n"}{end}')
    

    Delete successful pipelineruns:

    kubectl -n target-namespace delete pipelinerun $(kubectl -n target-namespace get pipelinerun -o jsonpath='{range .items[?(@.status.conditions[*].status=="True")]}{.metadata.name}{"\n"}{end}')