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

Feature Ruest: Keep last N docker images by laste-download date #61

Closed
tahajahangir opened this issue Aug 27, 2022 · 14 comments
Closed
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed

Comments

@tahajahangir
Copy link

It's reasonable to keep N last downloaded (or N last created) tags per image.

@donhui
Copy link
Contributor

donhui commented Aug 27, 2022

The rule KeepLatestNFilesInFolder(count=N) may be useful for you.

@allburov allburov added the question Further information is requested label Aug 29, 2022
@allburov
Copy link
Member

@tahajahangir did KeepLatestNFilesInFolder help you?

@tahajahangir
Copy link
Author

No, the file-based rules don't work with docker images (which have several layers and files). That's why DeleteDockerImagesOlderThan rule exists in addition to DeleteOlderThan.

@donhui
Copy link
Contributor

donhui commented Aug 30, 2022

I have defined the following rules, which can meet my needs. You can refer to them.

CleanupPolicy(
    'docker-demo-cleanup',
    rules.repo('docker-demo'),
    rules.exclude_docker_images(['*:latest', '*:release*']),
    rules.delete_docker_images_older_than(days=30),
    rules.exclude_path("base-tools*"),
    rules.keep_latest_n_file_in_folder(count=3),
)

@allburov
Copy link
Member

allburov commented Sep 4, 2022

@tahajahangir do you have any luck with it?
If it works - we can add as a reference to FAQ section

@tahajahangir
Copy link
Author

Yes, it works

@allburov allburov added help wanted Extra attention is needed documentation Improvements or additions to documentation and removed question Further information is requested labels Sep 5, 2022
@afolgado
Copy link

afolgado commented Sep 7, 2022

As I can see, it won't work. It won't keep N tags per image, but N files per tag. In Docker images, every tag is a folder and every file in that folder is a layer of the image (plus the manifes.json file). This will delete some of the layers of every non-excluded tag, making them useless.

@seth-stvns
Copy link
Contributor

seth-stvns commented Sep 8, 2022

I am seeing the same behavior as @afolgado (sorry didn't mean to send the tag). It is deleting a few layers from every image. Based on artifactory's layout of docker images and other resources (maven, gradle, npm, etc) this filter should be checking the folder directly above the files.

Will dig more into the code to see if I can find the issue when I have some time.

@donhui
Copy link
Contributor

donhui commented Sep 9, 2022

I have defined the following rules, which can meet my needs. You can refer to them.

CleanupPolicy(
    'docker-demo-cleanup',
    rules.repo('docker-demo'),
    rules.exclude_docker_images(['*:latest', '*:release*']),
    rules.delete_docker_images_older_than(days=30),
    rules.exclude_path("base-tools*"),
    rules.keep_latest_n_file_in_folder(count=3),
)

In this case, the rule rules.delete_docker_images_older_than(days=30) is required.
It can filter some docker images to delete.
Then the rule rules.keep_latest_n_file_in_folder(count=3) will filter on this basis.

@zbialik
Copy link

zbialik commented Apr 2, 2024

FWIW this feature request specifically asks for a method to keep latest by download time, not creation time. Because of this, I don't believe KeepLatestNFilesInFolder achieves the desired request.

I'm now trying to write a custom rule that's basically the same as KeepLatestNFilesInFolder except that it's based on the download time, rather than the creation time. Not sure if anyone else has implemented something similar.

@allburov
Copy link
Member

allburov commented Apr 3, 2024

I see, so like KeepLatestNFiles but for docker?
I'm not sure, but may be KeepLatestNFiles could work with docker rules as well, could one try it out?

@allburov allburov reopened this Apr 3, 2024
@allburov allburov closed this as completed Apr 3, 2024
@allburov
Copy link
Member

allburov commented Apr 3, 2024

Sorry, I see the solution for that was accepted
#61 (comment)

@allburov
Copy link
Member

allburov commented Apr 3, 2024

@zbialik please share with us the solution when it's ready, I'll be happy to review and accept a PR! 🙏

@zbialik
Copy link

zbialik commented Apr 3, 2024

@allburov I actually created my own custom rule that's literally a copy of KeepLatestNFiles class definition, except I modified the sort operation.

Specifically, I replace this line:

    artifacts.sort(key=lambda x: x["created"])

With this:

    artifacts.sort(key=lambda x: (
        x["stats"].get("downloaded", "1900-01-01T00:00:00.000Z"),
        x["created"]
    ))

For artifacts without a stats.downloaded field, I default this to a really old date. Then, for artifacts with identical download times (which can happen when there's several with 0 downloads and are defaulted to the old download date), these are sorted by the original created date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed
Development

No branches or pull requests

6 participants