|
1 | | -""" image """ |
| 1 | +""" oci_image """ |
2 | 2 |
|
3 | 3 | load("//oci:providers.bzl", "OCIDescriptor", "OCILayout") |
| 4 | +load(":common.bzl", "get_descriptor_file") |
4 | 5 |
|
5 | | -# buildifier: disable=function-docstring |
6 | | -def get_descriptor_file(ctx, desc): |
7 | | - if hasattr(desc, "descriptor_file"): |
8 | | - return desc.descriptor_file |
9 | | - |
10 | | - out = ctx.actions.declare_file(desc.digest) |
11 | | - ctx.actions.write( |
12 | | - output = out, |
13 | | - content = json.encode({ |
14 | | - "mediaType": desc.media_type, |
15 | | - "size": desc.size, |
16 | | - "digest": desc.digest, |
17 | | - "urls": desc.urls, |
18 | | - "annotations": desc.annotations, |
19 | | - }), |
20 | | - ) |
21 | | - |
22 | | - return out |
23 | | - |
24 | | -def _oci_image_index_impl(ctx): |
25 | | - toolchain = ctx.toolchains["//oci:toolchain"] |
26 | | - |
27 | | - layout_files = depset(None, transitive = [m[OCILayout].files for m in ctx.attr.manifests]) |
28 | | - |
29 | | - index_desc_file = ctx.actions.declare_file("{}.index.descriptor.json".format(ctx.label.name)) |
30 | | - index_file = ctx.actions.declare_file("{}.index.json".format(ctx.label.name)) |
31 | | - layout_file = ctx.actions.declare_file("{}.index.layout.json".format(ctx.label.name)) |
32 | | - |
33 | | - desc_files = [] |
34 | | - for manifest in ctx.attr.manifests: |
35 | | - desc_files.append(get_descriptor_file(ctx, manifest[OCIDescriptor])) |
36 | | - |
37 | | - outputs = [ |
38 | | - index_file, |
39 | | - index_desc_file, |
40 | | - layout_file, |
41 | | - ] |
42 | | - |
43 | | - ctx.actions.run( |
44 | | - executable = toolchain.sdk.ocitool, |
45 | | - arguments = ["--layout={}".format(m[OCILayout].blob_index.path) for m in ctx.attr.manifests] + |
46 | | - [ |
47 | | - "create-index", |
48 | | - "--out-index={}".format(index_file.path), |
49 | | - "--out-layout={}".format(layout_file.path), |
50 | | - "--outd={}".format(index_desc_file.path), |
51 | | - ] + |
52 | | - ["--desc={}".format(d.path) for d in desc_files] + |
53 | | - ["--annotations={}={}".format(k, v) for k, v in ctx.attr.annotations.items()], |
54 | | - inputs = desc_files + layout_files.to_list(), |
55 | | - outputs = outputs, |
56 | | - ) |
57 | | - |
58 | | - return [ |
59 | | - OCIDescriptor( |
60 | | - descriptor_file = index_desc_file, |
61 | | - ), |
62 | | - OCILayout( |
63 | | - blob_index = layout_file, |
64 | | - files = depset(direct = [index_file, layout_file], transitive = [layout_files]), |
65 | | - ), |
66 | | - DefaultInfo( |
67 | | - files = depset(outputs), |
68 | | - ), |
69 | | - ] |
70 | | - |
71 | | -oci_image_index = rule( |
72 | | - implementation = _oci_image_index_impl, |
73 | | - doc = """ |
74 | | - """, |
75 | | - attrs = { |
76 | | - "manifests": attr.label_list( |
77 | | - doc = """ |
78 | | - """, |
79 | | - ), |
80 | | - "annotations": attr.string_dict( |
81 | | - doc = """ |
82 | | - """, |
83 | | - ), |
84 | | - }, |
85 | | - toolchains = ["//oci:toolchain"], |
86 | | -) |
87 | | - |
88 | | -def _oci_image_impl(ctx): |
| 6 | +def _impl(ctx): |
89 | 7 | toolchain = ctx.toolchains["//oci:toolchain"] |
90 | 8 |
|
91 | 9 | base_desc = get_descriptor_file(ctx, ctx.attr.base[OCIDescriptor]) |
@@ -179,7 +97,7 @@ def _oci_image_impl(ctx): |
179 | 97 | ] |
180 | 98 |
|
181 | 99 | oci_image = rule( |
182 | | - implementation = _oci_image_impl, |
| 100 | + implementation = _impl, |
183 | 101 | doc = """Creates a new image manifest and config by appending the `layers` to an existing image |
184 | 102 | manifest and config defined by `base`. If `base` is an image index, then `os` and `arch` will |
185 | 103 | be used to extract the image manifest.""", |
|
0 commit comments