-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (40 loc) · 1.74 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import os
import logging
import requests
import sys
from requests.auth import HTTPBasicAuth
log = logging.getLogger(__name__)
registry_url = os.getenv("REGISTRY_URL")
nof_tags_to_keep = int(os.getenv("NOF_TAGS_TO_KEEP", 3))
username = os.getenv("DOCKER_USERNAME")
password = os.getenv("DOCKER_PASSWORD")
if not registry_url:
log.error("Registry URL not found. Please set REGISTRY_URL env variable.")
sys.exit()
auth = HTTPBasicAuth(username, password) if (username and password) else None
result = requests.get(f"{registry_url}/v2/_catalog", auth=auth)
if result.status_code != 200:
log.error("Could not fetch registry catalog. Please check parameters")
sys.exit()
catalog = result.json()["repositories"]
stats = {image: 0 for image in catalog}
for image in catalog:
result = requests.get(f"{registry_url}/v2/{image}/tags/list", auth=auth)
tags = result.json()["tags"]
tags_to_delete = tags[:-nof_tags_to_keep]
for tag in tags_to_delete:
manifests_url = f"{registry_url}/v2/{image}/manifests"
headers = {"Accept": "application/vnd.docker.distribution.manifest.v2+json"}
result = requests.get(f"{manifests_url}/{tag}", auth=auth, headers=headers)
if "Docker-Content-Digest" in result.headers:
sha = result.headers["Docker-Content-Digest"]
delete_result = requests.delete(f"{manifests_url}/{sha}", auth=auth)
if delete_result.status_code == 202:
stats[image] += 1
else:
log.error(f"Could not delete tag {tag} for image {image}")
else:
log.warning(f"Could not find digest sha for image tag {image}:{tag}")
print("Image cleanup completed. Number of tags deleted:")
print(json.dumps(stats, indent=4))