Skip to content

feat: add spec.ttl for automatic job cleanup after completion - #118

Open
Harshith-umesh wants to merge 5 commits into
openshift-psap:mainfrom
Harshith-umesh:feat/ttl-gc
Open

feat: add spec.ttl for automatic job cleanup after completion#118
Harshith-umesh wants to merge 5 commits into
openshift-psap:mainfrom
Harshith-umesh:feat/ttl-gc

Conversation

@Harshith-umesh

@Harshith-umesh Harshith-umesh commented Aug 25, 2026

Copy link
Copy Markdown
Member

Summary

  • Adds a spec.ttl field to the FournosJob CRD that defines the delay after job termination (Succeeded/Failed/Stopped) before the CR is automatically deleted
  • Uses Go duration format (e.g. 12h, 30m, 7d)
  • When not set, the job is never auto-pruned (preserving existing behavior)
  • The operator's existing GC loop handles TTL expiry checks alongside stale resource cleanup

Changes

  • manifests/crd.yaml — new spec.ttl field in OpenAPI schema
  • fournos/core/duration.py — Go-style duration parser ("12h" -> timedelta)
  • fournos/operator.py_gc_expired_jobs() added to GC loop, deletes terminal jobs past their TTL
  • tests/test_ttl.py — unit tests for duration parsing and TTL GC logic

Test plan

  • Unit tests pass (pytest tests/test_ttl.py)
  • Deploy CRD update, verify spec.ttl accepted on new jobs
  • Create a job with ttl: 1m, wait for completion, confirm auto-deletion after ~5 min (GC interval)
  • Confirm jobs without ttl are not affected

Made with Cursor

fixes: #48
fixes: #93

@openshift-ci

openshift-ci Bot commented Aug 25, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign mml-coder for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai

coderabbitai Bot commented Aug 25, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2017ffda-b184-47b9-b742-351db6f71b01


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Harshith-umesh
Harshith-umesh force-pushed the feat/ttl-gc branch 2 times, most recently from c2dc107 to 70f4078 Compare August 25, 2026 21:42
Add a TTL field to FournosJob that defines the delay after job
termination before the CR is automatically deleted. When not set,
jobs are never auto-pruned (preserving current behavior).

- Add spec.ttl to CRD schema (Go duration format: "12h", "7d", etc.)
- Add duration parser utility (fournos/core/duration.py)
- Add _gc_expired_jobs() to the operator GC loop
- Add unit tests for TTL logic

Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread fournos/operator.py Outdated
Comment thread fournos/operator.py Outdated

@kpouget kpouget left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @Harshith-umesh , looks good mostly,
just that I think it would be nicer to store the completion timestamp in the status,
in a similar way Pods (containers) do, instead of searching the transition timestamps

Comment thread fournos/operator.py Outdated
Comment thread fournos/operator.py
@kpouget

kpouget commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

can you also validate that this works even if added later with an oc patch command?

Harshith-umesh and others added 4 commits August 26, 2026 09:30
Co-authored-by: Kevin Pouget <kpouget@redhat.com>
- Add status.completionTime to CRD schema, set atomically on all
  terminal transitions via new set_terminal_phase() helper
- Validate spec.ttl in on_create; fail immediately if unparseable
- Refactor _get_completion_time to read status.completionTime with
  fallback to metadata.creationTimestamp for creation-time failures
- Downgrade GC log for invalid/missing TTL from warning to debug
- Fix indentation from accepted early-continue suggestion
- Update tests to match completionTime-based logic

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@Harshith-umesh
Harshith-umesh requested a review from kpouget August 26, 2026 13:54
Comment thread fournos/operator.py
Comment on lines +181 to +186
def _get_completion_time(job: dict) -> datetime | None:
"""Return the time the job entered its terminal phase.

Prefers status.completionTime (set by the operator on terminal transitions).
Falls back to metadata.creationTimestamp for jobs that failed at creation.
"""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we want to fallback on the creation time in the get_completion_time method (it's counter-intuitive to get a completion time if the job is still running)

Falls back to metadata.creationTimestamp for jobs that failed at creation.

jobs shouldn't fail at creation time AFAIU

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this method should check that the fjob is in a terminal state?

patch.status["conditions"] = result


def set_terminal_phase(patch, phase: str, message: str) -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for consistency, this method should check that phase is in the TERMINAL_PHASES

@kpouget kpouget left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after addressing the reviews, please deploy the controller in the WIP namespace with this:

/test deploy-fournos-wip

and test it manually by creating fjobs in psap-automation-wip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FEATURE: after completion TTL FEATURE: Add a periodic cleanup for shutdown jobs

2 participants