11"""Custom resource definition with proper names from the autogenerated code."""
22
3- from datetime import datetime
3+ from datetime import UTC , datetime
44from typing import Any , cast
55from urllib .parse import urljoin , urlparse , urlunparse
66
7- from kubernetes .utils import parse_quantity
7+ from kubernetes .utils import parse_duration , parse_quantity
88from pydantic import BaseModel , Field , field_validator
99from sanic .log import logger
1010from ulid import ULID
2424 SecretRef ,
2525 Session ,
2626 State ,
27+ Status ,
2728 Storage ,
2829 TlsSecret ,
2930)
@@ -190,6 +191,30 @@ def as_apispec(self) -> apispec.SessionResponse:
190191 else :
191192 state = apispec .State3 .starting
192193
194+ will_hibernate_at : datetime | None = None
195+ will_delete_at : datetime | None = None
196+ match self .status , self .spec .culling :
197+ case (
198+ Status (idle = True , idleSince = idle_since ),
199+ Culling (maxIdleDuration = max_idle ),
200+ ) if idle_since and max_idle :
201+ will_hibernate_at = idle_since + parse_duration (max_idle )
202+ case (
203+ Status (state = State .Failed , failingSince = failing_since ),
204+ Culling (maxFailedDuration = max_failed ),
205+ ) if failing_since and max_failed :
206+ will_hibernate_at = failing_since + parse_duration (max_failed )
207+ case (
208+ Status (state = State .NotReady ),
209+ Culling (maxAge = max_age ),
210+ ) if max_age and self .metadata .creationTimestamp :
211+ will_hibernate_at = self .metadata .creationTimestamp + parse_duration (max_age )
212+ case (
213+ Status (state = State .Hibernated , hibernatedSince = hibernated_since ),
214+ Culling (maxHibernatedDuration = max_hibernated ),
215+ ) if hibernated_since and max_hibernated :
216+ will_delete_at = hibernated_since + parse_duration (max_hibernated )
217+
193218 return apispec .SessionResponse (
194219 image = self .spec .session .image ,
195220 name = self .metadata .name ,
@@ -205,6 +230,8 @@ def as_apispec(self) -> apispec.SessionResponse:
205230 state = state ,
206231 ready_containers = ready_containers ,
207232 total_containers = total_containers ,
233+ will_hibernate_at = will_hibernate_at ,
234+ will_delete_at = will_delete_at ,
208235 ),
209236 url = url ,
210237 project_id = str (self .project_id ),
0 commit comments