Skip to content

CICD-461 · Ephemeral resource cleanup ​

Encyclopedia · FinOps and resource optimization · All snippets

Previous · CICD-460 · Next · CICD-462

1. Where it fits ​

  • Placement cleanup candidate step.
  • Prerequisites authorized read reports, explicit pricing period and reviewed ownership for cleanup.
  • Platforms local-linux, github-actions, gitea-actions.

2. Copy the snippet ​

bash
kubectl get namespace -l managed-by=preview -o json | jq -r --arg now "$(date -u +%FT%TZ)" '.items[]|select(.metadata.annotations["preview.example.com/expires"] != null and .metadata.annotations["preview.example.com/expires"] < $now)|.metadata.name'

3. How it works ​

The query reads observed cluster state. Its selector and namespace determine which resources can satisfy the check. The expression filters or transforms the input JSON. Where jq -e is used, a false or empty result can fail the gate.

4. Verify the result ​

Review protected and active environments before a separate deletion step.

Validation syntax-validated (Bash parse). Status definitions.