36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
import logging
|
|
|
|
from app import app
|
|
from endpoints.v2.models_pre_oci import data_model as model
|
|
from util.timedeltastring import convert_to_timedelta
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
min_expire_sec = convert_to_timedelta(app.config.get('LABELED_EXPIRATION_MINIMUM', '1h'))
|
|
max_expire_sec = convert_to_timedelta(app.config.get('LABELED_EXPIRATION_MAXIMUM', '104w'))
|
|
|
|
def _expires_after(value, namespace_name, repo_name, digest):
|
|
""" Sets the expiration of a manifest based on the quay.expires-in label. """
|
|
try:
|
|
timedelta = convert_to_timedelta(value)
|
|
except ValueError:
|
|
logger.exception('Could not convert %s to timedeltastring for %s/%s@%s', value, namespace_name,
|
|
repo_name, digest)
|
|
return
|
|
|
|
total_seconds = min(max(timedelta.total_seconds(), min_expire_sec.total_seconds()),
|
|
max_expire_sec.total_seconds())
|
|
|
|
logger.debug('Labeling manifest %s/%s@%s with expiration of %s', namespace_name, repo_name,
|
|
digest, total_seconds)
|
|
model.set_manifest_expires_after(namespace_name, repo_name, digest, total_seconds)
|
|
|
|
|
|
_LABEL_HANDLES = {
|
|
'quay.expires-after': _expires_after,
|
|
}
|
|
|
|
def handle_label(key, value, namespace_name, repo_name, digest):
|
|
handler = _LABEL_HANDLES.get(key)
|
|
if handler is not None:
|
|
handler(value, namespace_name, repo_name, digest)
|