--- title: "ADR 0071: Cleanup deletes the git-auth secret itself after deleting an old PipelineRun" --- * Status: accepted * Code: [`pkg/kubeinteraction/cleanups.go`](https://github.com/tektoncd/pipelines-as-code/blob/main/pkg/kubeinteraction/cleanups.go), [`pkg/kubeinteraction/secrets.go`](https://github.com/tektoncd/pipelines-as-code/blob/main/pkg/kubeinteraction/secrets.go) ## Context `CleanupPipelines` removes completed PipelineRuns beyond `maxKeep` for a Repository. The basic-auth secret created for git-clone is recorded on the PipelineRun in the `keys.GitAuthSecret` annotation. `secrets.go` provides `UpdateSecretWithOwnerRef`, which is meant to make the secret owned by the PipelineRun so Kubernetes garbage-collects it with the run. ## Decision * Runs are selected by labels: the repository name, the original PipelineRun name (not the generated name) and the completed state. A PipelineRun without the `keys.OriginalPRName` annotation is rejected with an error. * Runs are sorted by completion time with `PipelineRunSortByCompletionTime`; the first `maxKeep` are kept. Runs whose Succeeded reason is Running or Pending are skipped. * Past `maxKeep`, an optional `KeepFunc` may still keep a run and returns the reason that is logged. * After deleting a run, cleanup also deletes the secret named in its `keys.GitAuthSecret` annotation. The earlier source stated the reason: the ownerRef should normally delete the secret, but on conflicts the ownerRef may not be set and the secret would stay. The code does not show how often that happens. ## Consequences * A failure to delete the secret is ignored: nothing is logged and the loop continues; only a success is logged. * An error from deleting a PipelineRun, or from listing, aborts the cleanup. * A test comment referred to "my comment in code" for why only one run is kept; no such comment exists in the code, so the reason for `kept: 1` in `cleanups_test.go` is not documented.