Skip to content

Commit

Permalink
Transition container image target platform (bazelbuild#1963)
Browse files Browse the repository at this point in the history
  • Loading branch information
uhthomas authored Dec 17, 2021
1 parent 40cc8e8 commit 76c708f
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 30 deletions.
56 changes: 42 additions & 14 deletions container/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ def _impl(
executable = True,
outputs = _container.image.outputs,
implementation = _impl,
cfg = _container.image.cfg,
)
Args:
Expand Down Expand Up @@ -600,7 +601,7 @@ _attrs = dicts.add(_layer.attrs, {
Acceptable formats: Integer or floating point seconds since Unix Epoch, RFC 3339 date/time.
This field supports stamp variables.
If not set, defaults to {BUILD_TIMESTAMP} when stamp = True, otherwise 0""",
),
"docker_run_flags": attr.string(
Expand All @@ -612,14 +613,14 @@ _attrs = dicts.add(_layer.attrs, {
doc = """List of entrypoints to add in the image.
See https://docs.docker.com/engine/reference/builder/#entrypoint
Set `entrypoint` to `None`, `[]` or `""` will set the `Entrypoint` of the image
to be `null`.
The behavior between using `""` and `[]` may differ.
Please see [#1448](https://github.com/bazelbuild/rules_docker/issues/1448)
for more details.
This field supports stamp variables.""",
),
"experimental_tarball_format": attr.string(
Expand All @@ -642,12 +643,12 @@ _attrs = dicts.add(_layer.attrs, {
),
"labels": attr.string_dict(
doc = """Dictionary from custom metadata names to their values.
See https://docs.docker.com/engine/reference/builder/#label
You can also put a file name prefixed by '@' as a value.
Then the value is replaced with the contents of the file.
Example:
labels = {
Expand All @@ -674,7 +675,7 @@ _attrs = dicts.add(_layer.attrs, {
),
"layers": attr.label_list(
doc = """List of `container_layer` targets.
The data from each `container_layer` will be part of container image,
and the environment variable will be available in the image as well.""",
providers = [LayerInfo],
Expand Down Expand Up @@ -706,7 +707,7 @@ _attrs = dicts.add(_layer.attrs, {
# Starlark doesn't support int_list...
"ports": attr.string_list(
doc = """List of ports to expose.
See https://docs.docker.com/engine/reference/builder/#expose""",
),
"repository": attr.string(
Expand All @@ -716,7 +717,7 @@ _attrs = dicts.add(_layer.attrs, {
Images generated by `container_image` are tagged by default to
`bazel/package_name:target` for a `container_image` target at
`//package/name:target`.
Setting this attribute to `gcr.io/dummy` would set the default tag to
`gcr.io/dummy/package_name:target`.""",
),
Expand All @@ -733,19 +734,22 @@ _attrs = dicts.add(_layer.attrs, {
),
"volumes": attr.string_list(
doc = """List of volumes to mount.
See https://docs.docker.com/engine/reference/builder/#volumes""",
),
"workdir": attr.string(
doc = """Initial working directory when running the Docker image.
See https://docs.docker.com/engine/reference/builder/#workdir
Because building the image never happens inside a Docker container,
this working directory does not affect the other actions (e.g., adding files).
This field supports stamp variables.""",
),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
"_digester": attr.label(
default = "//container/go/cmd/digester",
cfg = "host",
Expand All @@ -765,19 +769,43 @@ _outputs["config_digest"] = "%{name}.json.sha256"

_outputs["build_script"] = "%{name}.executable"

def _image_transition_impl(settings, attr):
return dicts.add(settings, {
"//command_line_option:platforms": "@io_bazel_rules_docker//platforms:image_transition",
"@io_bazel_rules_docker//platforms:image_transition_cpu": "@platforms//cpu:" + {
# Architecture aliases.
"386": "x86_32",
"amd64": "x86_64",
"ppc64le": "ppc",
}.get(attr.architecture, attr.architecture),
"@io_bazel_rules_docker//platforms:image_transition_os": "@platforms//os:" + attr.operating_system,
})

_image_transition = transition(
implementation = _image_transition_impl,
inputs = [],
outputs = [
"//command_line_option:platforms",
"@io_bazel_rules_docker//platforms:image_transition_cpu",
"@io_bazel_rules_docker//platforms:image_transition_os",
],
)

image = struct(
attrs = _attrs,
outputs = _outputs,
implementation = _impl,
cfg = _image_transition,
)

container_image_ = rule(
attrs = _attrs,
attrs = image.attrs,
doc = "Called by the `container_image` macro with **kwargs, see below",
executable = True,
outputs = _outputs,
outputs = image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
implementation = _impl,
implementation = image.implementation,
cfg = image.cfg,
)

# This validates the two forms of value accepted by
Expand Down
24 changes: 12 additions & 12 deletions container/layer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ _layer_attrs = dicts.add({
"compression_options": attr.string_list(),
"data_path": attr.string(
doc = """Root path of the files.
The directory structure from the files is preserved inside the
Docker image, but a prefix path determined by `data_path`
is removed from the directory structure. This path can
Expand All @@ -310,7 +310,7 @@ _layer_attrs = dicts.add({
"debs": attr.label_list(
allow_files = deb_filetype,
doc = """Debian packages to extract.
Deprecated: A list of debian packages that will be extracted in the Docker image.
Note that this doesn't actually install the packages. Installation needs apt
or apt-get which need to be executed within a running container which
Expand All @@ -319,7 +319,7 @@ _layer_attrs = dicts.add({
"directory": attr.string(
default = "/",
doc = """Target directory.
The directory in which to expand the specified files, defaulting to '/'.
Only makes sense accompanying one of files/tars/debs.""",
),
Expand All @@ -329,16 +329,16 @@ _layer_attrs = dicts.add({
"enable_mtime_preservation": attr.bool(default = False),
"env": attr.string_dict(
doc = """Dictionary from environment variable names to their values when running the Docker image.
See https://docs.docker.com/engine/reference/builder/#env
For example,
env = {
"FOO": "bar",
...
},
},
The values of this field support make variables (e.g., `$(FOO)`)
and stamp variables; keys support make variables as well.""",
),
Expand All @@ -360,9 +360,9 @@ _layer_attrs = dicts.add({
"portable_mtime": attr.bool(default = False),
"symlinks": attr.string_dict(
doc = """Symlinks to create in the Docker image.
For example,
symlinks = {
"/path/to/link": "/path/to/target",
...
Expand All @@ -372,7 +372,7 @@ _layer_attrs = dicts.add({
"tars": attr.label_list(
allow_files = tar_filetype,
doc = """Tar file to extract in the layer.
A list of tar files whose content should be in the Docker image.""",
),
}, _hash_tools, _layer_tools, _zip_tools)
Expand All @@ -390,10 +390,10 @@ layer = struct(

container_layer_ = rule(
doc = _DOC,
attrs = _layer_attrs,
attrs = layer.attrs,
executable = False,
outputs = _layer_outputs,
implementation = _impl,
outputs = layer.outputs,
implementation = layer.implementation,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
)

Expand Down
1 change: 1 addition & 0 deletions contrib/repro_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,5 @@ container_repro_test = rule(
}),
test = True,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
cfg = _container.image.cfg,
)
8 changes: 5 additions & 3 deletions docker/package_managers/apt_key.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ key = struct(
attrs = _attrs,
outputs = _outputs,
implementation = _impl,
cfg = _container.image.cfg,
)

add_apt_key = rule(
attrs = _attrs,
outputs = _outputs,
implementation = _impl,
attrs = key.attrs,
outputs = key.outputs,
implementation = key.implementation,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
executable = True,
cfg = key.cfg,
)
2 changes: 2 additions & 0 deletions docker/toolchain_container/toolchain_container.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ language_tool_layer_ = rule(
outputs = _container.image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
implementation = _language_tool_layer_impl,
cfg = _container.image.cfg,
)

def language_tool_layer(**kwargs):
Expand Down Expand Up @@ -350,6 +351,7 @@ toolchain_container_ = rule(
outputs = _container.image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
implementation = _toolchain_container_impl,
cfg = _container.image.cfg,
)

def toolchain_container(**kwargs):
Expand Down
3 changes: 2 additions & 1 deletion docs/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ A rule that assembles data into a tarball which can be use as in layers attr in
| <a id="container_layer-empty_dirs"></a>empty_dirs | - | List of strings | optional | [] |
| <a id="container_layer-empty_files"></a>empty_files | - | List of strings | optional | [] |
| <a id="container_layer-enable_mtime_preservation"></a>enable_mtime_preservation | - | Boolean | optional | False |
| <a id="container_layer-env"></a>env | Dictionary from environment variable names to their values when running the Docker image.<br><br> See https://docs.docker.com/engine/reference/builder/#env<br><br> For example,<br><br> env = { "FOO": "bar", ... }, <br><br> The values of this field support make variables (e.g., <code>$(FOO)</code>) and stamp variables; keys support make variables as well. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="container_layer-env"></a>env | Dictionary from environment variable names to their values when running the Docker image.<br><br> See https://docs.docker.com/engine/reference/builder/#env<br><br> For example,<br><br> env = { "FOO": "bar", ... },<br><br> The values of this field support make variables (e.g., <code>$(FOO)</code>) and stamp variables; keys support make variables as well. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="container_layer-extract_config"></a>extract_config | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //container/go/cmd/extract_config:extract_config |
| <a id="container_layer-files"></a>files | File to add to the layer.<br><br> A list of files that should be included in the Docker image. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="container_layer-incremental_load_template"></a>incremental_load_template | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //container:incremental_load_template |
Expand Down Expand Up @@ -426,6 +426,7 @@ You can write a customized container_image rule by writing something like:
executable = True,
outputs = _container.image.outputs,
implementation = _impl,
cfg = _container.image.cfg,
)


Expand Down
4 changes: 4 additions & 0 deletions java/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ jar_dep_layer = rule(
outputs = lang_image.outputs,
toolchains = lang_image.toolchains,
implementation = _jar_dep_layer_impl,
cfg = lang_image.cfg,
)

def _jar_app_layer_impl(ctx):
Expand Down Expand Up @@ -255,6 +256,7 @@ jar_app_layer = rule(
outputs = _container.image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
implementation = _jar_app_layer_impl,
cfg = _container.image.cfg,
)

def java_image(
Expand Down Expand Up @@ -362,6 +364,7 @@ _war_dep_layer = rule(
outputs = _container.image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
implementation = _war_dep_layer_impl,
cfg = _container.image.cfg,
)

def _war_app_layer_impl(ctx):
Expand Down Expand Up @@ -400,6 +403,7 @@ _war_app_layer = rule(
outputs = _container.image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
implementation = _war_app_layer_impl,
cfg = _container.image.cfg,
)

def war_image(name, base = None, deps = [], layers = [], **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions lang/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ image = struct(
outputs = _container.image.outputs,
toolchains = ["@io_bazel_rules_docker//toolchains/docker:toolchain_type"],
implementation = _app_layer_impl,
cfg = _container.image.cfg,
)

_app_layer = rule(
Expand All @@ -280,6 +281,7 @@ _app_layer = rule(
outputs = image.outputs,
toolchains = image.toolchains,
implementation = image.implementation,
cfg = image.cfg,
)

# Convenience function that instantiates the _app_layer rule and returns
Expand Down
2 changes: 2 additions & 0 deletions nodejs/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ _dep_layer = rule(
outputs = lang_image.outputs,
toolchains = lang_image.toolchains,
implementation = _dep_layer_impl,
cfg = lang_image.cfg,
)

def _npm_deps_runfiles(dep):
Expand All @@ -107,6 +108,7 @@ _npm_deps_layer = rule(
outputs = lang_image.outputs,
toolchains = lang_image.toolchains,
implementation = _npm_deps_layer_impl,
cfg = lang_image.cfg,
)

def nodejs_image(
Expand Down
26 changes: 26 additions & 0 deletions platforms/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,29 @@ platform(
],
parents = ["@buildkite_config//config:platform"],
)

_IMAGE_TRANSITION_CONSTRAINTS = [
("cpu", "@platforms//cpu"),
("os", "@platforms//os"),
]

[[
# Use a constraint value which will never be valid to prevent
# accidentally leaving the associated constraint setting unset.
constraint_value(
name = "image_transition_{}_unset".format(name),
constraint_setting = constraint_setting,
),
label_setting(
name = "image_transition_{}".format(name),
build_setting_default = ":image_transition_{}_unset".format(name),
),
] for name, constraint_setting in _IMAGE_TRANSITION_CONSTRAINTS]

platform(
name = "image_transition",
constraint_values = [
":image_transition_{}".format(name)
for name, _ in _IMAGE_TRANSITION_CONSTRAINTS
],
)

0 comments on commit 76c708f

Please sign in to comment.