Skip to content

Commit

Permalink
Fix bazel-contrib#220: Add exposed_ports to oci_image
Browse files Browse the repository at this point in the history
  • Loading branch information
gzm0 committed Oct 25, 2023
1 parent 50bb6b8 commit dc5b894
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
8 changes: 5 additions & 3 deletions docs/image.md

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

5 changes: 5 additions & 0 deletions e2e/crane_as_registry/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ oci_image(
env = {
"ENV": "/test",
},
exposed_ports = [
"1234/tcp",
"5678/udp",
"5000",
],
os = "linux",
)

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, **kwargs):
def oci_image(name, labels = None, annotations = None, env = None, cmd = None, entrypoint = None, exposed_ports = None, **kwargs):
"""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 @@ -43,6 +43,7 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
env: Environment variables provisioned by default to the running container. See documentation above.
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.
**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 @@ -108,13 +109,24 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
)
entrypoint = entrypoint_label

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

oci_image_rule(
name = name,
annotations = annotations,
labels = labels,
env = env,
cmd = cmd,
entrypoint = entrypoint,
exposed_ports = exposed_ports,
**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 @@ -72,6 +72,7 @@ For Linux based systems, all of the following are valid: `user`, `uid`, `user:gr
If `group/gid` is not specified, the default group and supplementary groups of the given `user/uid` in `/etc/passwd` from the container are applied.
"""),
"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),
"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 @@ -149,6 +150,10 @@ def _oci_image_impl(ctx):
args.add(ctx.file.entrypoint.path, format = "--entrypoint-file=%s")
inputs_depsets.append(depset([ctx.file.entrypoint]))

if ctx.attr.exposed_ports:
args.add(ctx.file.exposed_ports.path, format = "--exposed-ports-file=%s")
inputs_depsets.append(depset([ctx.file.exposed_ports]))

if ctx.attr.cmd:
args.add(ctx.file.cmd.path, format = "--cmd-file=%s")
inputs_depsets.append(depset([ctx.file.cmd]))
Expand Down
5 changes: 5 additions & 0 deletions oci/private/image.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ for ARG in "$@"; do
FIXED_ARGS+=("--entrypoint=$in")
done <"${ARG#--entrypoint-file=}"
;;
(--exposed-ports-file=*)
while IFS= read -r in || [ -n "$in" ]; do
FIXED_ARGS+=("--exposed-ports=$in")
done <"${ARG#--exposed-ports-file=}"
;;
(*) FIXED_ARGS+=( "${ARG}" )
esac
done
Expand Down

0 comments on commit dc5b894

Please sign in to comment.