Skip to content

Commit

Permalink
break out oci_image_layer into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
bcmyers committed Jan 2, 2025
1 parent e12c2e9 commit a303427
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 79 deletions.
45 changes: 23 additions & 22 deletions docs/defs.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,6 @@ oci_image_index(<a href="#oci_image_index-name">name</a>, <a href="#oci_image_in
| <a id="oci_image_index-manifests"></a>manifests | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |


<a id="oci_image_layer"></a>

## oci_image_layer

<pre>
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>)
</pre>

Create a tarball and an OCI descriptor for it

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <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 | |
| <a id="oci_image_layer-directory"></a>directory | Directory in the tarball to place the `files` | String | optional | `""` |
| <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 | `{}` |
| <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 | `[]` |
| <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 | `{}` |


<a id="oci_image_layout"></a>

## oci_image_layout
Expand Down Expand Up @@ -122,3 +100,26 @@ Pushes a manifest or a list of manifests to an OCI registry.
| <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 | `{}` |


<a id="oci_image_layer"></a>

## oci_image_layer

<pre>
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>)
</pre>

oci_image_layer

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="oci_image_layer-name"></a>name | A unique name for this rule | none |
| <a id="oci_image_layer-directory"></a>directory | Directory in the tarball to place the `files` | `"/"` |
| <a id="oci_image_layer-file_map"></a>file_map | Dictionary of file -> file location in tarball | `None` |
| <a id="oci_image_layer-files"></a>files | List of files to include under `directory` | `None` |
| <a id="oci_image_layer-symlinks"></a>symlinks | Dictionary of symlink -> target entries to place in the tarball | `None` |
| <a id="oci_image_layer-kwargs"></a>kwargs | Additional arguments to pass to the rule, e.g. `tags` or `visibility` | none |


1 change: 1 addition & 0 deletions oci/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ bzl_library(
"//oci:providers.bzl",
"//oci/private:debug_flag.bzl",
"//oci/private:image.bzl",
"//oci/private:oci_image_layer.bzl",
"//oci/private:oci_image_layout.bzl",
"//oci/private:push.bzl",
"@aspect_bazel_lib//lib:stamping",
Expand Down
2 changes: 1 addition & 1 deletion oci/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ load(
"//oci/private:image.bzl",
_oci_image = "oci_image",
_oci_image_index = "oci_image_index",
_oci_image_layer = "oci_image_layer",
)
load("//oci/private:oci_image_layer.bzl", _oci_image_layer = "oci_image_layer")
load("//oci/private:oci_image_layout.bzl", _oci_image_layout = "oci_image_layout")
load("//oci/private:push.bzl", _oci_push = "oci_push")

Expand Down
56 changes: 0 additions & 56 deletions oci/private/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,62 +21,6 @@ def get_descriptor_file(ctx, desc):

return out

def _oci_image_layer_impl(ctx):
toolchain = ctx.toolchains["//oci:toolchain"]

descriptor_file = ctx.actions.declare_file("{}.descriptor.json".format(ctx.label.name))

ctx.actions.run(
executable = toolchain.sdk.ocitool,
arguments = [
"create-layer",
"--out={}".format(ctx.outputs.layer.path),
"--outd={}".format(descriptor_file.path),
"--dir={}".format(ctx.attr.directory),
"--bazel-label={}".format(ctx.label),
] +
["--file={}".format(f.path) for f in ctx.files.files] +
["--symlink={}={}".format(k, v) for k, v in ctx.attr.symlinks.items()] +
["--file-map={}={}".format(k.files.to_list()[0].path, v) for k, v in ctx.attr.file_map.items()],
inputs = ctx.files.files + ctx.files.file_map,
outputs = [
descriptor_file,
ctx.outputs.layer,
],
)

return [
OCIDescriptor(
descriptor_file = descriptor_file,
file = ctx.outputs.layer,
),
]

oci_image_layer = rule(
implementation = _oci_image_layer_impl,
doc = "Create a tarball and an OCI descriptor for it",
attrs = {
"files": attr.label_list(
doc = "List of files to include under `directory`",
allow_files = True,
),
"directory": attr.string(
doc = "Directory in the tarball to place the `files`",
),
"symlinks": attr.string_dict(
doc = "Dictionary of symlink -> target entries to place in the tarball",
),
"file_map": attr.label_keyed_string_dict(
doc = "Dictionary of file -> file location in tarball",
allow_files = True,
),
},
toolchains = ["//oci:toolchain"],
outputs = {
"layer": "%{name}-layer.tar.gz",
},
)

def _oci_image_index_impl(ctx):
toolchain = ctx.toolchains["//oci:toolchain"]

Expand Down
86 changes: 86 additions & 0 deletions oci/private/oci_image_layer.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
""" oci_image_layer """

load("//oci:providers.bzl", "OCIDescriptor")

def oci_image_layer(
*,
name,
directory = "/", # str
file_map = None, # dict[label, str]
files = None, # list[label]
symlinks = None, # dict[str, str]
**kwargs):
# -> None
""" oci_image_layer
Args:
name: A unique name for this rule
directory: Directory in the tarball to place the `files`
file_map: Dictionary of file -> file location in tarball
files: List of files to include under `directory`
symlinks: Dictionary of symlink -> target entries to place in the tarball
**kwargs: Additional arguments to pass to the rule, e.g. `tags` or `visibility`
"""
file_map = file_map or {}
files = files or []
symlinks = symlinks or {}

if len(files) == 0 and len(file_map) == 0:
fail("At least one of `files` or `file_map` must be provided")

_oci_image_layer(
name = name,
directory = directory,
file_map = file_map,
files = files,
symlinks = symlinks,
**kwargs
)

def _impl(ctx):
toolchain = ctx.toolchains["//oci:toolchain"]

descriptor_file = ctx.actions.declare_file("{}.descriptor.json".format(ctx.label.name))

ctx.actions.run(
executable = toolchain.sdk.ocitool,
arguments = [
"create-layer",
"--out={}".format(ctx.outputs.layer.path),
"--outd={}".format(descriptor_file.path),
"--dir={}".format(ctx.attr.directory),
"--bazel-label={}".format(ctx.label),
] +
["--file={}".format(f.path) for f in ctx.files.files] +
["--symlink={}={}".format(k, v) for k, v in ctx.attr.symlinks.items()] +
["--file-map={}={}".format(k.files.to_list()[0].path, v) for k, v in ctx.attr.file_map.items()],
inputs = ctx.files.files + ctx.files.file_map,
outputs = [
descriptor_file,
ctx.outputs.layer,
],
)

return [
DefaultInfo(
files = depset([ctx.outputs.layer, descriptor_file]),
),
OCIDescriptor(
descriptor_file = descriptor_file,
file = ctx.outputs.layer,
),
]

_oci_image_layer = rule(
implementation = _impl,
attrs = {
"files": attr.label_list(allow_files = True),
"directory": attr.string(),
"symlinks": attr.string_dict(),
"file_map": attr.label_keyed_string_dict(allow_files = True),
},
toolchains = ["//oci:toolchain"],
outputs = {
"layer": "%{name}-layer.tar.gz",
},
)

0 comments on commit a303427

Please sign in to comment.