Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New options for keeping images #10

Merged
merged 33 commits into from
Aug 6, 2018
Merged

Conversation

JulianSauer
Copy link
Contributor

I've added options to keep tags if they match a regular expression or if they were created after a specified date. E.g.:
./cleanreg.py -r http://192.168.56.2:5000 -n mysql -k 5 -i -re .*release.* -d 01.01.2018
will keep the 5 latest but also tags containing the word "release" as well as all those created since 01.01.2018.

Configs have to be rewritten to match the additional parameters with the option to use an underscore to ignore the parameter:
<repo> <k> <re> <d>

E.g.: mysql 5 _ _ keeps the 5 latest and does not consider regular expressions or dates.

@kekru
Copy link
Contributor

kekru commented May 31, 2018

Hi Julian,

thanks for your contribution. Here are a few thought of mine, but Halil has to decide ;)

The option to filter by a tag name is a good idea. But I would prefer to name the option --tagname instead of --regex, because it checks the tagnames.

But maybe we dont need an additional flag at all. Lets say we would allow --reponame to contain a ":", e.g --reponame postgres:9.4-alpine. The same way as you would write it in a dockerfile.

In cleanreg.py we check if there is a ":" in the reponame. If yes, we need to check the tagname.

Then we could add a --regex flag which stands for "enable interpreting of image and tag names as regex".

I think, if we add more features to the configfile, then we should better switch to a json file. The underscores are not very readable.

And maybe we need to change the testframework, when we add more and more tests. I think, it was not my best idea to use shunit :D It's a bit tricky

cleanreg.py Outdated
# check if date is valid
if args.date is not None:
try:
datetime.strptime(args.date, '%d.%m.%Y')
Copy link
Contributor

Choose a reason for hiding this comment

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

This should better be an ISO date format, maybe allow day (2018-05-31) and datetime (2018-05-31T18:03:24+00:00)

cleanreg.py Outdated
@@ -55,6 +56,8 @@ def parse_arguments():
parser.add_argument('-k', '--keepimages', help="Amount of images (not tags!) which should be kept "
"for the given repo (if -n is set) or for each repo of the "
"registry (if -cf is set).", type=int)
parser.add_argument('-re', '--regex', help="Image tags matching the regular expression will be kept", default=None)
Copy link
Contributor

Choose a reason for hiding this comment

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

see my other comment. I think this flag should only enable regex and not define the tagname. The tagname can be part of of the reponame, e.g postgres:3.6 where postgres is the image name and 3.6 the tag

cleanreg.py Outdated
@@ -55,6 +56,8 @@ def parse_arguments():
parser.add_argument('-k', '--keepimages', help="Amount of images (not tags!) which should be kept "
"for the given repo (if -n is set) or for each repo of the "
"registry (if -cf is set).", type=int)
parser.add_argument('-re', '--regex', help="Image tags matching the regular expression will be kept", default=None)
parser.add_argument('-d', '--date', help="Keep images which were created since this date. Format: dd.mm.yyyy", default=None)
Copy link
Contributor

Choose a reason for hiding this comment

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

better --after, then we could also add a --before

cleanreg.py Outdated
if date is not None and date != "_" and date != "":
parsed_date = datetime.strptime(date, '%d.%m.%Y')
for tag in deletion_tags.keys():
tag_date = datetime.strptime(deletion_tags[tag]['date'].split('T')[0], '%Y-%m-%d')
Copy link
Contributor

Choose a reason for hiding this comment

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

Better interpret the full datetime string (with the T). You can search for ISO Datetime

cleanreg.py Outdated
@@ -597,13 +632,13 @@ def get_deletiontags(verbose, tags_dates_digests, repo, repo_count):

repo_del_tags = {}
repo_del_digests = {}
for repo, count in repos_counts.iteritems():
for repo, (count, regex, date) in repos_counts.iteritems():
Copy link
Contributor

Choose a reason for hiding this comment

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

regex should better be tagname

@hcguersoy
Copy link
Owner

Sorry coming again so late (just again not get pinged by Github...).
I'll check this.

@hcguersoy
Copy link
Owner

PTAL @JulianSauer

@JulianSauer
Copy link
Contributor Author

Hey,

I've added most of the suggestions from @kekru:

  • A tagname is now an additional part of the -n option which can be added; if -re is used the tag will be parsed as a regular expression
  • Date format changed to ISO 8601
  • The time of day can additionally be specified: YYYY-MM-DDThh:mm:ss
  • I've changed the parameter name from --date to --since because it will keep all images that were created since then (and not just after)
  • Configs are now yaml files

cleanreg.py Outdated
@@ -89,8 +110,9 @@ def parse_arguments():
parser.error("[-n] and [-cf] cant be used together")

# hackish dependent arguments
if (bool(args.reponame) or args.clean_full_catalog) ^ (args.keepimages is not None):
parser.error("[-n] or [-cf] have to be used together with [-k].")
if (bool(args.reponame) or args.clean_full_catalog) ^ (args.keepimages is not 0 or args.regex is True or args.since is not None):
Copy link
Contributor

Choose a reason for hiding this comment

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

a small comment for this check and the check beneath would be nice, to better understand

cleanreg.py Outdated

deletion_tags = all_tags
if regex and tagname != "":
deletion_tags = {k: deletion_tags[k] for k in deletion_tags if re.match(tagname, k)}
Copy link
Contributor

Choose a reason for hiding this comment

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

Crazy python syntax, what do I have to search for, to find an explanation? :)

cleanreg.py Outdated
try:
parsed_date = datetime.strptime(args.since, '%Y%m%dT%H%M%S')
except ValueError:
parsed_date = datetime.strptime(args.since, '%Y-%m-%dT%H:%M:%S')
Copy link
Contributor

Choose a reason for hiding this comment

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

the date parsing could be extracted to a function (returning the parsed date or None if not parsable), because you need it two times

cleanreg.py Outdated
if ammount_tags <= keep_count:
deletion_tags.clear()
else:
deletion_tags = collections.OrderedDict(islice(deletion_tags.iteritems(), ammount_tags - keep_count))
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah comment would be great.
if amount_tags - keep_count is 7, then this will keep the first 7 in the list and remove the others. The first 7 are the oldest tags.
Right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

deletion_tags contains the tags that will be deleted. So it will remove the 7 oldest and keep the others.
(I'll add a comment)

@JulianSauer
Copy link
Contributor Author

I've changed a few more things considering the suggestions of @kekru (Simplified a for loop, moved date parsing to a function and added comments).

@hcguersoy
Copy link
Owner

Looks fine, thank you. I'll add some additional stuff into the README before I create a new release.

@hcguersoy hcguersoy merged commit 5bb6518 into hcguersoy:master Aug 6, 2018
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.

3 participants