Skip to content

Proposal: Optional PVC Auto-Cleanup for Workspaces Mode #9284

@vdemeester

Description

@vdemeester

Proposal: Optional PVC Auto-Cleanup for Workspaces Mode

Problem

Users running PipelineRuns with volumeClaimTemplate in stable workspaces mode accumulate PVCs after completion, causing increased storage costs and manual cleanup burden. Alpha modes (pipelineruns, isolate-pipelinerun) auto-delete PVCs, but there's no stable/production-ready option.

Related: #5776 | Code: pkg/reconciler/pipelinerun/affinity_assistant.go:214 (TODO comment)

Proposed Solution

Add opt-in annotation tekton.dev/auto-cleanup-pvc: "true" to enable PVC cleanup on PipelineRun completion.

Example:

apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: build-app
  annotations:
    tekton.dev/auto-cleanup-pvc: "true"
spec:
  workspaces:
    - name: source
      volumeClaimTemplate:
        spec:
          accessModes: [ReadWriteOnce]
          resources:
            requests:
              storage: 1Gi

Behavior:

  • With annotation: PVC deleted on completion
  • Without annotation: Current behavior (PVC persists until PipelineRun deleted)
  • Fully backward compatible

Implementation

Key insight: Cleanup logic already exists in PurgeFinalizerAndDeletePVCForWorkspace() (used by alpha modes). Just need to call it conditionally.

Changes in pkg/reconciler/pipelinerun/affinity_assistant.go:

// Add constant
const AutoCleanupPVCAnnotation = "tekton.dev/auto-cleanup-pvc"

// Modify cleanup (lines 213-222)
case aa.AffinityAssistantPerWorkspace:
    for _, w := range pr.Spec.Workspaces {
        // ... existing StatefulSet deletion ...

        // NEW: Check annotation
        autoCleanup := pr.Annotations != nil && pr.Annotations[AutoCleanupPVCAnnotation] == "true"
        if w.VolumeClaimTemplate != nil && autoCleanup {
            pvcName := volumeclaim.GeneratePVCNameFromWorkspaceBinding(...)
            c.pvcHandler.PurgeFinalizerAndDeletePVCForWorkspace(ctx, pvcName, pr.Namespace)
        }
    }

Complexity: ~20 lines code + ~150 lines tests + docs

Why Annotation?

  • ✅ Backward compatible (opt-in)
  • ✅ Per-PipelineRun control
  • ✅ Reuses existing cleanup logic
  • ✅ Standard Kubernetes pattern

Alternative (cluster-wide flag) could be added later if needed.

Questions

  1. Is tekton.dev/auto-cleanup-pvc an acceptable annotation name?
  2. Should this also work for persistentVolumeClaim workspaces, or only volumeClaimTemplate?
  3. Should we include a cluster-wide feature flag in this PR or defer?

References


Happy to implement if approach is approved.

cc @tektoncd/maintainers

Metadata

Metadata

Assignees

Labels

area/apiIndicates an issue or PR that deals with the API.kind/featureCategorizes issue or PR as related to a new feature.

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions