Add keep_by_hours option
To be more adaptive to user needs, keep_by_hours option works as the inverse of the "delete_by_hours" option. "keep_by_hours" will keep any tag newer than the specified number of hours.
This commit is contained in:
39
registry.py
39
registry.py
@@ -501,7 +501,14 @@ for more detail on garbage collection read here:
|
||||
|
||||
parser.add_argument(
|
||||
'--delete-by-hours',
|
||||
help=('Will delete all tags that older than specified hours. Be careful!'),
|
||||
help=('Will delete all tags that are older than specified hours. Be careful!'),
|
||||
default=False,
|
||||
nargs='?',
|
||||
metavar='Hours')
|
||||
|
||||
parser.add_argument(
|
||||
'--keep-by-hours',
|
||||
help=('Will keep all tags that are newer than specified hours.'),
|
||||
default=False,
|
||||
nargs='?',
|
||||
metavar='Hours')
|
||||
@@ -605,6 +612,30 @@ def delete_tags_by_age(registry, image_name, dry_run, hours, tags_to_keep):
|
||||
delete_tags(registry, image_name, dry_run, tags_to_delete, tags_to_keep)
|
||||
|
||||
|
||||
def get_newer_tags(registry, image_name, hours, tags_list):
|
||||
newer_tags = []
|
||||
print('---------------------------------')
|
||||
for tag in tags_list:
|
||||
image_config = registry.get_tag_config(image_name, tag)
|
||||
|
||||
if image_config == []:
|
||||
print("tag not found")
|
||||
continue
|
||||
|
||||
image_age = registry.get_image_age(image_name, image_config)
|
||||
|
||||
if image_age == []:
|
||||
print("timestamp not found")
|
||||
continue
|
||||
|
||||
if dt.strptime(image_age[:-4], "%Y-%m-%dT%H:%M:%S.%f") >= dt.now() - timedelta(hours=int(hours)):
|
||||
print("Keeping tag: {0} timestamp: {1}".format(
|
||||
tag, image_age))
|
||||
newer_tags.append(tag)
|
||||
|
||||
return newer_tags
|
||||
|
||||
|
||||
def main_loop(args):
|
||||
global DEBUG
|
||||
|
||||
@@ -682,10 +713,14 @@ def main_loop(args):
|
||||
layer['blobSum']))
|
||||
|
||||
# add tags to "tags_to_keep" list, if we have regexp "tags_to_keep"
|
||||
# entries:
|
||||
# entries or a number of hours for "keep_by_hours":
|
||||
keep_tags = []
|
||||
if args.keep_tags_like:
|
||||
keep_tags.extend(get_tags_like(args.keep_tags_like, tags_list))
|
||||
if args.keep_by_hours:
|
||||
keep_tags.extend(get_newer_tags(registry, image_name,
|
||||
args.keep_by_hours, tags_list))
|
||||
keep_tags = list(set(keep_tags)) # Eliminate duplicates
|
||||
|
||||
# delete tags if told so
|
||||
if args.delete or args.delete_all:
|
||||
|
||||
Reference in New Issue
Block a user