Switch to a per-namespace configurable expiration policy for time machine, and switch the tag gc to respect it.
This commit is contained in:
parent
d81e6c7a4d
commit
872539bdbf
4 changed files with 26 additions and 17 deletions
|
@ -2,6 +2,7 @@ import bcrypt
|
|||
import logging
|
||||
import dateutil.parser
|
||||
import json
|
||||
import time
|
||||
|
||||
from datetime import datetime, timedelta, date
|
||||
|
||||
|
@ -1531,8 +1532,8 @@ def get_repository_images(namespace_name, repository_name):
|
|||
|
||||
|
||||
def _tag_alive(query):
|
||||
return query.where((RepositoryTag.lifetime_end >> None) |
|
||||
(RepositoryTag.lifetime_end > datetime.utcnow()))
|
||||
return query.where((RepositoryTag.lifetime_end_ts >> None) |
|
||||
(RepositoryTag.lifetime_end_ts > int(time.time())))
|
||||
|
||||
|
||||
def list_repository_tags(namespace_name, repository_name):
|
||||
|
@ -1547,14 +1548,18 @@ def list_repository_tags(namespace_name, repository_name):
|
|||
|
||||
|
||||
def _garbage_collect_tags(namespace_name, repository_name):
|
||||
with config.app_config['DB_TRANSACTION_FACTORY'](db):
|
||||
repo = _get_repository(namespace_name, repository_name)
|
||||
collect_time = (datetime.utcnow() -
|
||||
timedelta(seconds=config.app_config['TIME_MACHINE_DELTA_SECONDS']))
|
||||
to_delete = (RepositoryTag
|
||||
.select(RepositoryTag.id)
|
||||
.join(Repository)
|
||||
.join(Namespace, on=(Repository.namespace_user == Namespace.id))
|
||||
.where(Repository.name == repository_name, Namespace.username == namespace_name,
|
||||
~(RepositoryTag.lifetime_end_ts >> None),
|
||||
(RepositoryTag.lifetime_end_ts + Namespace.removed_tag_expiration_s) <=
|
||||
int(time.time())))
|
||||
|
||||
(RepositoryTag
|
||||
.delete()
|
||||
.where(RepositoryTag.repository == repo, RepositoryTag.lifetime_end < collect_time)
|
||||
.where(RepositoryTag.id << to_delete)
|
||||
.execute())
|
||||
|
||||
|
||||
|
@ -1745,7 +1750,7 @@ def create_or_update_tag(namespace_name, repository_name, tag_name,
|
|||
except Image.DoesNotExist:
|
||||
raise DataModelException('Invalid image with id: %s' % tag_docker_image_id)
|
||||
|
||||
now = datetime.utcnow()
|
||||
now_ts = int(time.time())
|
||||
|
||||
try:
|
||||
# When we move a tag, we really end the timeline of the old one and create a new one
|
||||
|
@ -1753,13 +1758,14 @@ def create_or_update_tag(namespace_name, repository_name, tag_name,
|
|||
.select()
|
||||
.where(RepositoryTag.repository == repo, RepositoryTag.name == tag_name))
|
||||
tag = query.get()
|
||||
tag.lifetime_end = now
|
||||
tag.lifetime_end_ts = now_ts
|
||||
tag.save()
|
||||
except RepositoryTag.DoesNotExist:
|
||||
# No tag that needs to be ended
|
||||
pass
|
||||
|
||||
tag = RepositoryTag.create(repository=repo, image=image, name=tag_name, lifetime_start=now)
|
||||
tag = RepositoryTag.create(repository=repo, image=image, name=tag_name,
|
||||
lifetime_start_ts=now_ts)
|
||||
|
||||
return tag
|
||||
|
||||
|
@ -1781,7 +1787,7 @@ def delete_tag(namespace_name, repository_name, tag_name):
|
|||
(tag_name, namespace_name, repository_name))
|
||||
raise DataModelException(msg)
|
||||
|
||||
found.lifetime_end = datetime.utcnow()
|
||||
found.lifetime_end_ts = int(time.time())
|
||||
found.save()
|
||||
|
||||
|
||||
|
|
Reference in a new issue