Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for settings volumes #632

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/compare_dockerfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Let's compare them to their rules_oci counterparts:
- `SHELL` -> Use `oci_image#entrypoint` instead.
- `STOPSIGNAL` -> Not supported
- `USER` -> Not supported. Use the tar rule's mechanism for setting gid/uid
- `VOLUME` -> See: https://github.com/bazel-contrib/rules_oci/issues/406
- `VOLUME` -> Use `oci_image#volumes`
- `WORKDIR` -> Use `oci_image#workdir`


Expand Down
6 changes: 4 additions & 2 deletions docs/image.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/assert.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def assert_oci_config(
cmd_eq = None,
env_eq = None,
exposed_ports_eq = None,
volumes_eq = None,
user_eq = None,
workdir_eq = None,
architecture_eq = None,
Expand All @@ -46,6 +47,8 @@ def assert_oci_config(
config["WorkingDir"] = workdir_eq
if exposed_ports_eq:
config["ExposedPorts"] = {port: {} for port in exposed_ports_eq}
if volumes_eq:
config["Volumes"] = {volume: {} for volume in volumes_eq}
if user_eq:
config["User"] = user_eq
if labels_eq:
Expand Down
4 changes: 4 additions & 0 deletions examples/assertion/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ oci_image(
"5678/udp",
"5000",
],
volumes = ["/srv/data"],
# user & workdir
user = "root",
workdir = "/root",
Expand Down Expand Up @@ -230,6 +231,9 @@ assert_oci_config(
"5678/udp",
"5000",
],
volumes_eq = [
"/srv/data",
],
image = ":case8",
labels_eq = {
"org.opencontainers.image.version": "0.0.0",
Expand Down
14 changes: 13 additions & 1 deletion oci/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ oci_image_rule = _oci_image
oci_image_index = _oci_image_index
oci_push_rule = _oci_push

def oci_image(name, labels = None, annotations = None, env = None, cmd = None, entrypoint = None, exposed_ports = None, **kwargs):
def oci_image(name, labels = None, annotations = None, env = None, cmd = None, entrypoint = None, exposed_ports = None, volumes = None, **kwargs):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the naming of the argument (referring to the discussion here: #503 (comment)).

The reference in that comment is about mounts in running containers, not the volume directive in images.

Volumes in images are specified here (and called volumes):
https://github.com/opencontainers/image-spec/blob/main/config.md#properties

Mounts are concrete attachments to paths inside the container (so they need a counterpart path on the host or an actual volume).

Volumes are merely metadata about an image, stating that it stores data there. (docker uses this to automatically create anonymous volumes, I do not know whether this behavior is specified).

"""Macro wrapper around [oci_image_rule](#oci_image_rule).

Allows labels and annotations to be provided as a dictionary, in addition to a text file.
Expand All @@ -44,6 +44,7 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
cmd: Command & argument configured by default in the running container. See documentation above.
entrypoint: Entrypoint configured by default in the running container. See documentation above.
exposed_ports: Exposed ports in the running container. See documentation above.
volumes: Volumes for the container. See documentation above.
**kwargs: other named arguments to [oci_image_rule](#oci_image_rule) and
[common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes).
"""
Expand Down Expand Up @@ -109,6 +110,16 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
)
exposed_ports = exposed_ports_label

if types.is_list(volumes):
volumes_label = "_{}_write_volumes".format(name)
write_file(
name = volumes_label,
out = "_{}.volumes.txt".format(name),
content = [",".join(volumes)],
**forwarded_kwargs
)
volumes = volumes_label

oci_image_rule(
name = name,
annotations = annotations,
Expand All @@ -117,6 +128,7 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
cmd = cmd,
entrypoint = entrypoint,
exposed_ports = exposed_ports,
volumes = volumes,
**kwargs
)

Expand Down
5 changes: 5 additions & 0 deletions oci/private/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ If `group/gid` is not specified, the default group and supplementary groups of t
"""),
"workdir": attr.string(doc = "Sets the current working directory of the `entrypoint` process in the container. This value acts as a default and may be replaced by a working directory specified when creating a container."),
"exposed_ports": attr.label(doc = "A file containing a comma separated list of exposed ports. (e.g. 2000/tcp, 3000/udp or 4000. No protocol defaults to tcp).", allow_single_file = True),
"volumes": attr.label(doc = "A file containing a comma separated list of volumes. (e.g. /srv/data,/srv/other-data)", allow_single_file = True),
"os": attr.string(doc = "The name of the operating system which the image is built to run on. eg: `linux`, `windows`. See $GOOS documentation for possible values: https://go.dev/doc/install/source#environment"),
"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."),
Expand Down Expand Up @@ -195,6 +196,10 @@ def _oci_image_impl(ctx):
args.add(ctx.file.exposed_ports.path, format = "--exposed-ports=%s")
inputs.append(ctx.file.exposed_ports)

if ctx.attr.volumes:
args.add(ctx.file.volumes.path, format = "--volumes=%s")
inputs.append(ctx.file.volumes)

if ctx.attr.cmd:
args.add(ctx.file.cmd.path, format = "--cmd=%s")
inputs.append(ctx.file.cmd)
Expand Down
3 changes: 3 additions & 0 deletions oci/private/image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ for ARG in "$@"; do
--exposed-ports=*)
CONFIG=$(jq --rawfile ep "${ARG#--exposed-ports=}" '.config.ExposedPorts = ($ep | split(",") | map({key: ., value: {}}) | from_entries)' <<<"$CONFIG")
;;
--volumes=*)
CONFIG=$(jq --rawfile volumes "${ARG#--volumes=}" '.config.Volumes = ($volumes | split(",") | map({key: ., value: {}}) | from_entries)' <<<"$CONFIG")
;;
--user=*)
CONFIG=$(jq --arg user "${ARG#--user=}" '.config.User = $user' <<<"$CONFIG")
;;
Expand Down
Loading