forked from jupyterhub/zero-to-jupyterhub-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-chart-yaml-annotations.py
executable file
·68 lines (50 loc) · 1.85 KB
/
set-chart-yaml-annotations.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
"""
This script inspects the images used by the chart and updates Chart.yaml's
annotations. Specifically, an annotation read by artificathub.io. For more
information, see this issue describing why we want this:
https://github.com/jupyterhub/zero-to-jupyterhub-k8s/issues/2044
FIXME: This implementation is done quick and dirty by appending to Chart.yaml
instead of loading it, updating it, and writing content back.
"""
import os
import textwrap
from collections.abc import MutableMapping
import yaml
here_dir = os.path.abspath(os.path.dirname(__file__))
values_yaml = os.path.join(here_dir, os.pardir, "jupyterhub", "values.yaml")
chart_yaml = os.path.join(here_dir, os.pardir, "jupyterhub", "Chart.yaml")
def find_images(values, images=None):
"""
Searches through values.yaml for images and their tags and returns a list of
"<image>:<tag>" strings.
"""
if images == None:
images = []
for k, v in values.items():
if isinstance(v, MutableMapping):
if k == "image":
if "name" in v and "tag" in v:
images.append(f"{v['name']}:{v['tag']}")
else:
find_images(v, images)
return sorted(images)
def run():
with open(values_yaml) as f:
values = yaml.safe_load(f)
images = find_images(values)
images_artifacthub_format = textwrap.indent(
yaml.dump(
[{"name": i.split(":")[0].split("/")[-1], "image": i} for i in images]
),
" ",
)
chart_yaml_appendix = (
"annotations:\n" + ' "artifacthub.io/images": |\n' + images_artifacthub_format
)
print("Appending the following to Chart.yaml:\n")
print(chart_yaml_appendix)
with open(chart_yaml, "a") as f:
f.write(chart_yaml_appendix)
print("jupyterhub/Chart.yaml annotations updated")
run()