Skip to content

Commit

Permalink
docs: add example for wasm containers (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn authored Feb 27, 2023
1 parent 0da3922 commit 25f3a9a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ copy the WORKSPACE snippet into your `WORKSPACE` file.

See the API documentation in the [docs](docs/) folder and the example usage in the [examples](examples/) folder.
Note that the example relies on the setup code in the `/WORKSPACE` file in the root of this repo.

Also, check out the [wasm containers](e2e/wasm/) end to end testing workspace for how to build wasm images.
4 changes: 3 additions & 1 deletion docs/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Implementation details for image rule
## oci_image

<pre>
oci_image(<a href="#oci_image-name">name</a>, <a href="#oci_image-architecture">architecture</a>, <a href="#oci_image-base">base</a>, <a href="#oci_image-cmd">cmd</a>, <a href="#oci_image-entrypoint">entrypoint</a>, <a href="#oci_image-env">env</a>, <a href="#oci_image-labels">labels</a>, <a href="#oci_image-os">os</a>, <a href="#oci_image-tars">tars</a>, <a href="#oci_image-user">user</a>, <a href="#oci_image-variant">variant</a>, <a href="#oci_image-workdir">workdir</a>)
oci_image(<a href="#oci_image-name">name</a>, <a href="#oci_image-annotations">annotations</a>, <a href="#oci_image-architecture">architecture</a>, <a href="#oci_image-base">base</a>, <a href="#oci_image-cmd">cmd</a>, <a href="#oci_image-entrypoint">entrypoint</a>, <a href="#oci_image-env">env</a>, <a href="#oci_image-labels">labels</a>, <a href="#oci_image-os">os</a>, <a href="#oci_image-tars">tars</a>, <a href="#oci_image-user">user</a>,
<a href="#oci_image-variant">variant</a>, <a href="#oci_image-workdir">workdir</a>)
</pre>

Build an OCI compatible container image.
Expand Down Expand Up @@ -59,6 +60,7 @@ oci_image(
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="oci_image-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
| <a id="oci_image-annotations"></a>annotations | Annotations for the image config. See https://github.com/opencontainers/image-spec/blob/main/annotations.md. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="oci_image-architecture"></a>architecture | The CPU architecture which the binaries in this image are built to run on. eg: <code>arm64</code>, <code>arm</code>, <code>amd64</code>, <code>s390x</code>. See $GOARCH documentation for possible values: https://go.dev/doc/install/source#environment | String | optional | "" |
| <a id="oci_image-base"></a>base | Label to an oci_image target to use as the base. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="oci_image-cmd"></a>cmd | Default arguments to the <code>entrypoint</code> of the container. These values act as defaults and may be replaced by any specified when creating a container. | List of strings | optional | [] |
Expand Down
48 changes: 48 additions & 0 deletions e2e/wasm/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
load("@rules_rust//rust:defs.bzl", "rust_binary")
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
load("@contrib_rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
load("@rules_pkg//:pkg.bzl", "pkg_tar")

package(default_visibility = ["//visibility:public"])

rust_binary(
name = "binary",
srcs = ["main.rs"],
edition = "2018",
)

platform_transition_filegroup(
name = "wasi_binary",
srcs = ["binary"],
target_platform = "@rules_rust//rust/platform:wasi",
)

pkg_tar(
name = "wasi_layer",
srcs = ["wasi_binary"],
)

oci_image(
name = "image",
annotations = {
"module.wasm.image/variant": "compat",
"run.oci.handler": "wasm",
},
architecture = "wasm32",
cmd = ["/binary.wasm"],
os = "wasi",
tars = [
":wasi_layer",
],
)

# In order to run the image you need to follow instructions at https://docs.docker.com/desktop/wasm/ first.
# then run the following;
# `bazel build :tarball`
# `docker load -i bazel-bin/tarball/tarball.tar`
# `docker run --runtime=io.containerd.wasmedge.v1 --platform=wasi/wasm32 --pull=never gcr.io/wasm:latest`
oci_tarball(
name = "tarball",
image = ":image",
repotags = ["gcr.io/wasm:latest"],
)
48 changes: 48 additions & 0 deletions e2e/wasm/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
workspace(name = "custom_registry_example")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

local_repository(
name = "contrib_rules_oci",
path = "../../",
)

load("@contrib_rules_oci//oci:dependencies.bzl", "rules_oci_dependencies")

# Fetch our "runtime" dependencies which users need as well
rules_oci_dependencies()

load("@contrib_rules_oci//oci:repositories.bzl", "LATEST_CRANE_VERSION", "LATEST_ZOT_VERSION", "oci_register_toolchains")

oci_register_toolchains(
name = "oci",
crane_version = LATEST_CRANE_VERSION,
zot_version = LATEST_ZOT_VERSION,
)

# rules_pkg
http_archive(
name = "rules_pkg",
sha256 = "451e08a4d78988c06fa3f9306ec813b836b1d076d0f055595444ba4ff22b867f",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.7.1/rules_pkg-0.7.1.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/0.7.1/rules_pkg-0.7.1.tar.gz",
],
)

load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")

rules_pkg_dependencies()

# rules_rust
http_archive(
name = "rules_rust",
sha256 = "2466e5b2514772e84f9009010797b9cd4b51c1e6445bbd5b5e24848d90e6fb2e",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.18.0/rules_rust-v0.18.0.tar.gz"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains(edition = "2022")
4 changes: 4 additions & 0 deletions e2e/wasm/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

fn main() {
println!("Hello world!");
}
4 changes: 4 additions & 0 deletions oci/private/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ If `group/gid` is not specified, the default group and supplementary groups of t
"architecture": attr.string(doc = "The CPU architecture which the binaries in this image are built to run on. eg: `arm64`, `arm`, `amd64`, `s390x`. See $GOARCH documentation for possible values: https://go.dev/doc/install/source#environment"),
"variant": attr.string(doc = "The variant of the specified CPU architecture. eg: `v6`, `v7`, `v8`. See: https://github.com/opencontainers/image-spec/blob/main/image-index.md#platform-variants for more."),
"labels": attr.string_dict(doc = "Labels for the image config. See https://github.com/opencontainers/image-spec/blob/main/annotations.md."),
"annotations": attr.string_dict(doc = "Annotations for the image config. See https://github.com/opencontainers/image-spec/blob/main/annotations.md."),
"_image_sh_tpl": attr.label(default = "image.sh.tpl", allow_single_file = True),
}

Expand Down Expand Up @@ -143,6 +144,9 @@ def _oci_image_impl(ctx):
# TODO: Support stamping the values
args.add_all(ctx.attr.labels.items(), map_each = _format_string_to_string_tuple, format_each = "--label=%s")

if ctx.attr.annotations:
args.add_all(ctx.attr.annotations.items(), map_each = _format_string_to_string_tuple, format_each = "--annotation=%s")

output = ctx.actions.declare_directory(ctx.label.name)
args.add(output.path, format = "--output=%s")

Expand Down

0 comments on commit 25f3a9a

Please sign in to comment.