Skip to content

Commit

Permalink
Allow labels to be overriden by custom container_image rules (#2223)
Browse files Browse the repository at this point in the history
* Allow labels to be overriden by custom container_image rules

Follows the pattern used for other attributes of the
container_image macro to expose overriding via the method
recommended for custom rules using the _impl() method and
image attrs.

* Allow labels to be overriden by custom container_image rules

Follows the pattern used for other attributes of the
container_image macro to expose overriding via the method
recommended for custom rules using the _impl() method and
image attrs.

* Allow labels to be overriden by custom container_image rules

Follows the pattern used for other attributes of the
container_image macro to expose overriding via the method
recommended for custom rules using the _impl() method and
image attrs.
  • Loading branch information
hamsterstorm authored Mar 3, 2023
1 parent 1f81145 commit 1d7b0d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
37 changes: 27 additions & 10 deletions container/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def _add_create_image_config_args(
manifest,
config,
labels,
label_files,
entrypoint,
cmd,
null_cmd,
Expand Down Expand Up @@ -121,8 +122,8 @@ def _add_create_image_config_args(
inputs += layer_names
args.add_all(layer_names, before_each = "-layerDigestFile", format_each = "@%s")

if ctx.attr.label_files:
inputs += ctx.files.label_files
if label_files:
inputs += label_files

if base_config:
args.add("-baseConfig", base_config)
Expand Down Expand Up @@ -172,23 +173,26 @@ def _image_config(
workdir = None,
user = None,
null_entrypoint = False,
null_cmd = False):
null_cmd = False,
labels = None,
label_files = None,
label_file_strings = None):
"""Create the configuration for a new container image."""
config = ctx.actions.declare_file(name + "." + layer_name + ".config")
manifest = ctx.actions.declare_file(name + "." + layer_name + ".manifest")

label_file_dict = _string_to_label(
ctx.files.label_files,
ctx.attr.label_file_strings,
label_files,
label_file_strings,
)

labels = dict()
labels_fixed = dict()
for label in ctx.attr.labels:
fname = ctx.attr.labels[label]
if fname[0] == "@":
labels[label] = "@" + label_file_dict[fname[1:]].path
labels_fixed[label] = "@" + label_file_dict[fname[1:]].path
else:
labels[label] = fname
labels_fixed[label] = fname

args = ctx.actions.args()
inputs = []
Expand All @@ -200,7 +204,8 @@ def _image_config(
inputs,
manifest,
config,
labels,
labels_fixed,
label_files,
entrypoint,
cmd,
null_cmd,
Expand Down Expand Up @@ -289,7 +294,10 @@ def _impl(
user = None,
null_cmd = None,
null_entrypoint = None,
tag_name = None):
tag_name = None,
labels = None,
label_files = None,
label_file_strings = None):
"""Implementation for the container_image rule.
You can write a customized container_image rule by writing something like:
Expand Down Expand Up @@ -348,6 +356,9 @@ def _impl(
null_cmd: bool, overrides ctx.attr.null_cmd
null_entrypoint: bool, overrides ctx.attr.null_entrypoint
tag_name: str, overrides ctx.attr.tag_name
labels: str Dict, overrides ctx.attr.labels
label_files: File list, overrides ctx.attr.label_files
label_file_strings: str list, overrides ctx.attr.label_file_strings
"""
name = name or ctx.label.name
base = base or ctx.attr.base
Expand All @@ -370,6 +381,9 @@ def _impl(
null_cmd = null_cmd or ctx.attr.null_cmd
null_entrypoint = null_entrypoint or ctx.attr.null_entrypoint
tag_name = tag_name or ctx.attr.tag_name
labels = labels or ctx.attr.labels
label_files = label_files or ctx.files.label_files
label_file_strings = label_file_strings or ctx.attr.label_file_strings

# If this target specifies docker_run_flags, they are always used.
# Fall back to the base image's run flags if present, otherwise use the default value.
Expand Down Expand Up @@ -460,6 +474,9 @@ def _impl(
user = user or ctx.attr.user,
null_entrypoint = null_entrypoint,
null_cmd = null_cmd,
labels = labels,
label_files = label_files,
label_file_strings = label_file_strings,
)

# Construct a temporary name based on the build target. This is the name
Expand Down
5 changes: 4 additions & 1 deletion docs/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ image.implementation(<a href="#image.implementation-ctx">ctx</a>, <a href="#imag
<a href="#image.implementation-compression_options">compression_options</a>, <a href="#image.implementation-experimental_tarball_format">experimental_tarball_format</a>, <a href="#image.implementation-debs">debs</a>, <a href="#image.implementation-tars">tars</a>, <a href="#image.implementation-architecture">architecture</a>,
<a href="#image.implementation-operating_system">operating_system</a>, <a href="#image.implementation-os_version">os_version</a>, <a href="#image.implementation-output_executable">output_executable</a>, <a href="#image.implementation-output_tarball">output_tarball</a>, <a href="#image.implementation-output_config">output_config</a>,
<a href="#image.implementation-output_config_digest">output_config_digest</a>, <a href="#image.implementation-output_digest">output_digest</a>, <a href="#image.implementation-output_layer">output_layer</a>, <a href="#image.implementation-workdir">workdir</a>, <a href="#image.implementation-user">user</a>, <a href="#image.implementation-null_cmd">null_cmd</a>,
<a href="#image.implementation-null_entrypoint">null_entrypoint</a>, <a href="#image.implementation-tag_name">tag_name</a>)
<a href="#image.implementation-null_entrypoint">null_entrypoint</a>, <a href="#image.implementation-tag_name">tag_name</a>, <a href="#image.implementation-labels">labels</a>, <a href="#image.implementation-label_files">label_files</a>, <a href="#image.implementation-label_file_strings">label_file_strings</a>)
</pre>

Implementation for the container_image rule.
Expand Down Expand Up @@ -470,5 +470,8 @@ You can write a customized container_image rule by writing something like:
| <a id="image.implementation-null_cmd"></a>null_cmd | bool, overrides ctx.attr.null_cmd | <code>None</code> |
| <a id="image.implementation-null_entrypoint"></a>null_entrypoint | bool, overrides ctx.attr.null_entrypoint | <code>None</code> |
| <a id="image.implementation-tag_name"></a>tag_name | str, overrides ctx.attr.tag_name | <code>None</code> |
| <a id="image.implementation-labels"></a>labels | str Dict, overrides ctx.attr.labels | <code>None</code> |
| <a id="image.implementation-label_files"></a>label_files | File list, overrides ctx.attr.label_files | <code>None</code> |
| <a id="image.implementation-label_file_strings"></a>label_file_strings | str list, overrides ctx.attr.label_file_strings | <code>None</code> |


0 comments on commit 1d7b0d8

Please sign in to comment.