blob: 53363a37edca3e8cee4ece3d1385d1a13584f6da [file] [log] [blame]
Mohammed Naser8613c862023-04-24 17:26:51 -04001#!/usr/bin/env python3
2
3import argparse
4import functools
5
6from docker_image import reference
7from oslo_config import cfg
8from oslo_log import log as logging
9from ruyaml import YAML
10import requests
11
12LOG = logging.getLogger(__name__)
13CONF = cfg.CONF
14
Oleksandr Kd4e5b232024-01-18 17:20:16 +010015SKIP_IMAGE_LIST = ["secretgen_controller"]
Mohammed Naser8613c862023-04-24 17:26:51 -040016
Mohammed Naser891a2902024-01-23 09:47:47 -050017
Mohammed Naser21066a12024-01-02 11:14:53 -050018def get_digest(image_ref, token=None):
19 headers = {}
20 if token:
21 headers["Authorization"] = f"Bearer {token}"
22
23 try:
24 headers["Accept"] = "application/vnd.docker.distribution.manifest.v2+json"
25
26 r = requests.get(
27 f"https://{image_ref.domain()}/v2/{image_ref.path()}/manifests/{image_ref['tag']}",
28 timeout=5,
29 headers=headers,
30 )
31 r.raise_for_status()
32 return r.headers["Docker-Content-Digest"]
33 except requests.exceptions.HTTPError:
34 headers["Accept"] = "application/vnd.oci.image.index.v1+json"
35
36 r = requests.get(
37 f"https://{image_ref.domain()}/v2/{image_ref.path()}/manifests/{image_ref['tag']}",
38 timeout=5,
39 headers=headers,
40 )
41 r.raise_for_status()
42 return r.headers["Docker-Content-Digest"]
43
44
Mohammed Naser8613c862023-04-24 17:26:51 -040045@functools.cache
46def get_pinned_image(image_src):
47 image_ref = reference.Reference.parse(image_src)
48
Mohammed Naser21066a12024-01-02 11:14:53 -050049 if image_ref.domain() in ("registry.k8s.io", "us-docker.pkg.dev"):
50 digest = get_digest(image_ref)
51
Mohammed Naser8613c862023-04-24 17:26:51 -040052 if image_ref.domain() == "quay.io":
53 r = requests.get(
54 f"https://quay.io/api/v1/repository/{image_ref.path()}/tag/",
Mohammed Naser21066a12024-01-02 11:14:53 -050055 timeout=5,
Mohammed Naser8613c862023-04-24 17:26:51 -040056 params={"specificTag": image_ref["tag"]},
57 )
58 r.raise_for_status()
59 digest = r.json()["tags"][0]["manifest_digest"]
60
Mohammed Naser49e66372023-07-10 14:57:00 -040061 if image_ref.domain() == "docker.io":
62 # Get token for docker.io
63 r = requests.get(
64 "https://auth.docker.io/token",
Mohammed Naser21066a12024-01-02 11:14:53 -050065 timeout=5,
Mohammed Naser16baaab2023-07-10 15:07:11 -040066 params={
67 "service": "registry.docker.io",
68 "scope": f"repository:{image_ref.path()}:pull",
69 },
Mohammed Naser49e66372023-07-10 14:57:00 -040070 )
71 r.raise_for_status()
72 token = r.json()["token"]
73
74 r = requests.get(
75 f"https://registry-1.docker.io/v2/{image_ref.path()}/manifests/{image_ref['tag']}",
Mohammed Naser21066a12024-01-02 11:14:53 -050076 timeout=5,
Mohammed Naser16baaab2023-07-10 15:07:11 -040077 headers={
78 "Accept": "application/vnd.docker.distribution.manifest.v2+json",
79 "Authorization": f"Bearer {token}",
80 },
Mohammed Naser49e66372023-07-10 14:57:00 -040081 )
82 r.raise_for_status()
83 digest = r.headers["Docker-Content-Digest"]
84
Mohammed Naser21066a12024-01-02 11:14:53 -050085 if image_ref.domain() == "ghcr.io":
86 # Get token for docker.io
87 r = requests.get(
88 "https://ghcr.io/token",
89 timeout=5,
90 params={
91 "service": "ghcr.io",
92 "scope": f"repository:{image_ref.path()}:pull",
93 },
94 )
95 r.raise_for_status()
96 token = r.json()["token"]
97
98 digest = get_digest(image_ref, token=token)
99
100 return f"{image_ref.domain()}/{image_ref.path()}:{image_ref['tag']}@{digest}"
Mohammed Naser8613c862023-04-24 17:26:51 -0400101
102
103def main():
104 logging.register_options(CONF)
105 logging.setup(CONF, "atmosphere-bump-images")
106
107 parser = argparse.ArgumentParser("bump-images")
108 parser.add_argument(
109 "src", help="Path for default values file", type=argparse.FileType("r")
110 )
111 parser.add_argument("dst", help="Path for output file", type=argparse.FileType("w"))
112
113 args = parser.parse_args()
114
115 yaml = YAML(typ="rt")
116 data = yaml.load(args.src)
117
Mohammed Naser21066a12024-01-02 11:14:53 -0500118 for image in data["_atmosphere_images"]:
Oleksandr Kd4e5b232024-01-18 17:20:16 +0100119 if image in SKIP_IMAGE_LIST:
120 continue
Mohammed Naser21066a12024-01-02 11:14:53 -0500121 image_src = data["_atmosphere_images"][image]
Mohammed Naser8613c862023-04-24 17:26:51 -0400122 pinned_image = get_pinned_image(image_src)
123
124 LOG.info("Pinning image %s from %s to %s", image, image_src, pinned_image)
ricolinb8ab0172023-06-01 15:41:02 +0800125 data["_atmosphere_images"][image] = pinned_image
Mohammed Naser8613c862023-04-24 17:26:51 -0400126
127 yaml.dump(data, args.dst)
128
129
130if __name__ == "__main__":
131 main()