diff --git a/README.md b/README.md index 829bc86e..d5f38a17 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/docs/image.md b/docs/image.md index a7cce95a..1ee4558f 100644 --- a/docs/image.md +++ b/docs/image.md @@ -7,7 +7,8 @@ Implementation details for image rule ## oci_image
-oci_image(name, architecture, base, cmd, entrypoint, env, labels, os, tars, user, variant, workdir)
+oci_image(name, annotations, architecture, base, cmd, entrypoint, env, labels, os, tars, user,
+          variant, workdir)
 
Build an OCI compatible container image. @@ -59,6 +60,7 @@ oci_image( | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | +| annotations | Annotations for the image config. See https://github.com/opencontainers/image-spec/blob/main/annotations.md. | Dictionary: String -> String | optional | {} | | architecture | 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 | String | optional | "" | | base | Label to an oci_image target to use as the base. | Label | optional | None | | cmd | Default arguments to the entrypoint of the container. These values act as defaults and may be replaced by any specified when creating a container. | List of strings | optional | [] | diff --git a/e2e/wasm/BUILD.bazel b/e2e/wasm/BUILD.bazel new file mode 100644 index 00000000..2ae5e055 --- /dev/null +++ b/e2e/wasm/BUILD.bazel @@ -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"], +) diff --git a/e2e/wasm/WORKSPACE b/e2e/wasm/WORKSPACE new file mode 100644 index 00000000..8e2b25a9 --- /dev/null +++ b/e2e/wasm/WORKSPACE @@ -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") diff --git a/e2e/wasm/main.rs b/e2e/wasm/main.rs new file mode 100644 index 00000000..3f8a1070 --- /dev/null +++ b/e2e/wasm/main.rs @@ -0,0 +1,4 @@ + +fn main() { + println!("Hello world!"); +} \ No newline at end of file diff --git a/oci/private/image.bzl b/oci/private/image.bzl index 5e786521..fbe6bbe8 100644 --- a/oci/private/image.bzl +++ b/oci/private/image.bzl @@ -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), } @@ -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")