Skip to content

Commit cefb088

Browse files
committed
break out oci_image_layer into separate file
1 parent 628605a commit cefb088

File tree

5 files changed

+111
-79
lines changed

5 files changed

+111
-79
lines changed

docs/defs.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,6 @@ oci_image_index(<a href="#oci_image_index-name">name</a>, <a href="#oci_image_in
5050
| <a id="oci_image_index-manifests"></a>manifests | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
5151

5252

53-
<a id="oci_image_layer"></a>
54-
55-
## oci_image_layer
56-
57-
<pre>
58-
oci_image_layer(<a href="#oci_image_layer-name">name</a>, <a href="#oci_image_layer-directory">directory</a>, <a href="#oci_image_layer-file_map">file_map</a>, <a href="#oci_image_layer-files">files</a>, <a href="#oci_image_layer-symlinks">symlinks</a>)
59-
</pre>
60-
61-
Create a tarball and an OCI descriptor for it
62-
63-
**ATTRIBUTES**
64-
65-
66-
| Name | Description | Type | Mandatory | Default |
67-
| :------------- | :------------- | :------------- | :------------- | :------------- |
68-
| <a id="oci_image_layer-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
69-
| <a id="oci_image_layer-directory"></a>directory | Directory in the tarball to place the `files` | String | optional | `""` |
70-
| <a id="oci_image_layer-file_map"></a>file_map | Dictionary of file -> file location in tarball | <a href="https://bazel.build/rules/lib/dict">Dictionary: Label -> String</a> | optional | `{}` |
71-
| <a id="oci_image_layer-files"></a>files | List of files to include under `directory` | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
72-
| <a id="oci_image_layer-symlinks"></a>symlinks | Dictionary of symlink -> target entries to place in the tarball | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
73-
74-
7553
<a id="oci_image_layout"></a>
7654

7755
## oci_image_layout
@@ -122,3 +100,26 @@ Pushes a manifest or a list of manifests to an OCI registry.
122100
| <a id="oci_push-x_meta_headers"></a>x_meta_headers | (optional) A list of key/values to to be sent to the registry as headers with an X-Meta- prefix. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
123101

124102

103+
<a id="oci_image_layer"></a>
104+
105+
## oci_image_layer
106+
107+
<pre>
108+
oci_image_layer(<a href="#oci_image_layer-name">name</a>, <a href="#oci_image_layer-directory">directory</a>, <a href="#oci_image_layer-file_map">file_map</a>, <a href="#oci_image_layer-files">files</a>, <a href="#oci_image_layer-symlinks">symlinks</a>, <a href="#oci_image_layer-kwargs">kwargs</a>)
109+
</pre>
110+
111+
oci_image_layer
112+
113+
**PARAMETERS**
114+
115+
116+
| Name | Description | Default Value |
117+
| :------------- | :------------- | :------------- |
118+
| <a id="oci_image_layer-name"></a>name | A unique name for this rule | none |
119+
| <a id="oci_image_layer-directory"></a>directory | Directory in the tarball to place the `files` | `"/"` |
120+
| <a id="oci_image_layer-file_map"></a>file_map | Dictionary of file -> file location in tarball | `None` |
121+
| <a id="oci_image_layer-files"></a>files | List of files to include under `directory` | `None` |
122+
| <a id="oci_image_layer-symlinks"></a>symlinks | Dictionary of symlink -> target entries to place in the tarball | `None` |
123+
| <a id="oci_image_layer-kwargs"></a>kwargs | Additional arguments to pass to the rule, e.g. `tags` or `visibility` | none |
124+
125+

oci/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ bzl_library(
3030
"//oci:providers.bzl",
3131
"//oci/private:debug_flag.bzl",
3232
"//oci/private:image.bzl",
33+
"//oci/private:oci_image_layer.bzl",
3334
"//oci/private:oci_image_layout.bzl",
3435
"//oci/private:push.bzl",
3536
"@aspect_bazel_lib//lib:stamping",

oci/defs.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ load(
44
"//oci/private:image.bzl",
55
_oci_image = "oci_image",
66
_oci_image_index = "oci_image_index",
7-
_oci_image_layer = "oci_image_layer",
87
)
8+
load("//oci/private:oci_image_layer.bzl", _oci_image_layer = "oci_image_layer")
99
load("//oci/private:oci_image_layout.bzl", _oci_image_layout = "oci_image_layout")
1010
load("//oci/private:push.bzl", _oci_push = "oci_push")
1111

oci/private/image.bzl

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,62 +21,6 @@ def get_descriptor_file(ctx, desc):
2121

2222
return out
2323

24-
def _oci_image_layer_impl(ctx):
25-
toolchain = ctx.toolchains["//oci:toolchain"]
26-
27-
descriptor_file = ctx.actions.declare_file("{}.descriptor.json".format(ctx.label.name))
28-
29-
ctx.actions.run(
30-
executable = toolchain.sdk.ocitool,
31-
arguments = [
32-
"create-layer",
33-
"--out={}".format(ctx.outputs.layer.path),
34-
"--outd={}".format(descriptor_file.path),
35-
"--dir={}".format(ctx.attr.directory),
36-
"--bazel-label={}".format(ctx.label),
37-
] +
38-
["--file={}".format(f.path) for f in ctx.files.files] +
39-
["--symlink={}={}".format(k, v) for k, v in ctx.attr.symlinks.items()] +
40-
["--file-map={}={}".format(k.files.to_list()[0].path, v) for k, v in ctx.attr.file_map.items()],
41-
inputs = ctx.files.files + ctx.files.file_map,
42-
outputs = [
43-
descriptor_file,
44-
ctx.outputs.layer,
45-
],
46-
)
47-
48-
return [
49-
OCIDescriptor(
50-
descriptor_file = descriptor_file,
51-
file = ctx.outputs.layer,
52-
),
53-
]
54-
55-
oci_image_layer = rule(
56-
implementation = _oci_image_layer_impl,
57-
doc = "Create a tarball and an OCI descriptor for it",
58-
attrs = {
59-
"files": attr.label_list(
60-
doc = "List of files to include under `directory`",
61-
allow_files = True,
62-
),
63-
"directory": attr.string(
64-
doc = "Directory in the tarball to place the `files`",
65-
),
66-
"symlinks": attr.string_dict(
67-
doc = "Dictionary of symlink -> target entries to place in the tarball",
68-
),
69-
"file_map": attr.label_keyed_string_dict(
70-
doc = "Dictionary of file -> file location in tarball",
71-
allow_files = True,
72-
),
73-
},
74-
toolchains = ["//oci:toolchain"],
75-
outputs = {
76-
"layer": "%{name}-layer.tar.gz",
77-
},
78-
)
79-
8024
def _oci_image_index_impl(ctx):
8125
toolchain = ctx.toolchains["//oci:toolchain"]
8226

oci/private/oci_image_layer.bzl

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
""" oci_image_layer """
2+
3+
load("//oci:providers.bzl", "OCIDescriptor")
4+
5+
def oci_image_layer(
6+
*,
7+
name,
8+
directory = "/", # str
9+
file_map = None, # dict[label, str]
10+
files = None, # list[label]
11+
symlinks = None, # dict[str, str]
12+
**kwargs):
13+
# -> None
14+
""" oci_image_layer
15+
16+
Args:
17+
name: A unique name for this rule
18+
directory: Directory in the tarball to place the `files`
19+
file_map: Dictionary of file -> file location in tarball
20+
files: List of files to include under `directory`
21+
symlinks: Dictionary of symlink -> target entries to place in the tarball
22+
**kwargs: Additional arguments to pass to the rule, e.g. `tags` or `visibility`
23+
"""
24+
file_map = file_map or {}
25+
files = files or []
26+
symlinks = symlinks or {}
27+
28+
if len(files) == 0 and len(file_map) == 0:
29+
fail("At least one of `files` or `file_map` must be provided")
30+
31+
_oci_image_layer(
32+
name = name,
33+
directory = directory,
34+
file_map = file_map,
35+
files = files,
36+
symlinks = symlinks,
37+
**kwargs
38+
)
39+
40+
def _impl(ctx):
41+
toolchain = ctx.toolchains["//oci:toolchain"]
42+
43+
descriptor_file = ctx.actions.declare_file("{}.descriptor.json".format(ctx.label.name))
44+
45+
ctx.actions.run(
46+
executable = toolchain.sdk.ocitool,
47+
arguments = [
48+
"create-layer",
49+
"--out={}".format(ctx.outputs.layer.path),
50+
"--outd={}".format(descriptor_file.path),
51+
"--dir={}".format(ctx.attr.directory),
52+
"--bazel-label={}".format(ctx.label),
53+
] +
54+
["--file={}".format(f.path) for f in ctx.files.files] +
55+
["--symlink={}={}".format(k, v) for k, v in ctx.attr.symlinks.items()] +
56+
["--file-map={}={}".format(k.files.to_list()[0].path, v) for k, v in ctx.attr.file_map.items()],
57+
inputs = ctx.files.files + ctx.files.file_map,
58+
outputs = [
59+
descriptor_file,
60+
ctx.outputs.layer,
61+
],
62+
)
63+
64+
return [
65+
DefaultInfo(
66+
files = depset([ctx.outputs.layer, descriptor_file]),
67+
),
68+
OCIDescriptor(
69+
descriptor_file = descriptor_file,
70+
file = ctx.outputs.layer,
71+
),
72+
]
73+
74+
_oci_image_layer = rule(
75+
implementation = _impl,
76+
attrs = {
77+
"files": attr.label_list(allow_files = True),
78+
"directory": attr.string(),
79+
"symlinks": attr.string_dict(),
80+
"file_map": attr.label_keyed_string_dict(allow_files = True),
81+
},
82+
toolchains = ["//oci:toolchain"],
83+
outputs = {
84+
"layer": "%{name}-layer.tar.gz",
85+
},
86+
)

0 commit comments

Comments
 (0)