Skip to content

Commit

Permalink
fix: forward well known attributes to macro expanded targets (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
reltuk authored Oct 16, 2023
1 parent 4e125bf commit f4031fb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "aspect_bazel_lib", version = "1.32.0")
bazel_dep(name = "aspect_bazel_lib", version = "1.35.0")
bazel_dep(name = "bazel_skylib", version = "1.4.1")
bazel_dep(name = "platforms", version = "0.0.5")

Expand Down
5 changes: 2 additions & 3 deletions docs/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ oci_image(
## oci_image

<pre>
oci_image(<a href="#oci_image-name">name</a>, <a href="#oci_image-labels">labels</a>, <a href="#oci_image-annotations">annotations</a>, <a href="#oci_image-env">env</a>, <a href="#oci_image-cmd">cmd</a>, <a href="#oci_image-entrypoint">entrypoint</a>, <a href="#oci_image-tags">tags</a>, <a href="#oci_image-kwargs">kwargs</a>)
oci_image(<a href="#oci_image-name">name</a>, <a href="#oci_image-labels">labels</a>, <a href="#oci_image-annotations">annotations</a>, <a href="#oci_image-env">env</a>, <a href="#oci_image-cmd">cmd</a>, <a href="#oci_image-entrypoint">entrypoint</a>, <a href="#oci_image-kwargs">kwargs</a>)
</pre>

Macro wrapper around [oci_image_rule](#oci_image_rule).
Expand Down Expand Up @@ -118,7 +118,6 @@ This is similar to the same-named target created by rules_docker's `container_im
| <a id="oci_image-env"></a>env | Environment variables provisioned by default to the running container. See documentation above. | <code>None</code> |
| <a id="oci_image-cmd"></a>cmd | Command & argument configured by default in the running container. See documentation above. | <code>None</code> |
| <a id="oci_image-entrypoint"></a>entrypoint | Entrypoint configured by default in the running container. See documentation above. | <code>None</code> |
| <a id="oci_image-tags"></a>tags | Tags to propagate to targets declared by this macro. | <code>[]</code> |
| <a id="oci_image-kwargs"></a>kwargs | other named arguments to [oci_image_rule](#oci_image_rule) | none |
| <a id="oci_image-kwargs"></a>kwargs | other named arguments to [oci_image_rule](#oci_image_rule) and [common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes). | none |


5 changes: 2 additions & 3 deletions docs/push.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ oci_push(
## oci_push

<pre>
oci_push(<a href="#oci_push-name">name</a>, <a href="#oci_push-remote_tags">remote_tags</a>, <a href="#oci_push-tags">tags</a>, <a href="#oci_push-kwargs">kwargs</a>)
oci_push(<a href="#oci_push-name">name</a>, <a href="#oci_push-remote_tags">remote_tags</a>, <a href="#oci_push-kwargs">kwargs</a>)
</pre>

Macro wrapper around [oci_push_rule](#oci_push_rule).
Expand All @@ -149,7 +149,6 @@ Allows the remote_tags attribute to be a list of strings in addition to a text f
| :------------- | :------------- | :------------- |
| <a id="oci_push-name"></a>name | name of resulting oci_push_rule | none |
| <a id="oci_push-remote_tags"></a>remote_tags | a list of tags to apply to the image after pushing, or a label of a file containing tags one-per-line. See [stamped_tags](https://github.com/bazel-contrib/rules_oci/blob/main/examples/push/stamp_tags.bzl) as one example of a way to produce such a file. | <code>None</code> |
| <a id="oci_push-tags"></a>tags | Tags to propagate to targets declared by this macro. | <code>[]</code> |
| <a id="oci_push-kwargs"></a>kwargs | other named arguments to [oci_push_rule](#oci_push_rule). | none |
| <a id="oci_push-kwargs"></a>kwargs | other named arguments to [oci_push_rule](#oci_push_rule) and [common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes). | none |


50 changes: 27 additions & 23 deletions oci/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load("@rules_oci//oci:defs.bzl", ...)
load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
load("@aspect_bazel_lib//lib:directory_path.bzl", "directory_path")
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
load("@aspect_bazel_lib//lib:utils.bzl", "propagate_common_rule_attributes")
load("@bazel_skylib//lib:types.bzl", "types")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//oci/private:image.bzl", _oci_image = "oci_image")
Expand All @@ -21,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, tags = [], **kwargs):
def oci_image(name, labels = None, annotations = None, env = None, cmd = None, entrypoint = 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 @@ -42,16 +43,18 @@ 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.
tags: Tags to propagate to targets declared by this macro.
**kwargs: other named arguments to [oci_image_rule](#oci_image_rule)
**kwargs: other named arguments to [oci_image_rule](#oci_image_rule) and
[common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes).
"""
forwarded_kwargs = propagate_common_rule_attributes(kwargs)

if types.is_dict(annotations):
annotations_label = "_{}_write_annotations".format(name)
write_file(
name = annotations_label,
out = "_{}.annotations.txt".format(name),
content = ["{}={}".format(key, value) for (key, value) in annotations.items()],
tags = tags,
**forwarded_kwargs,
)
annotations = annotations_label

Expand All @@ -61,7 +64,7 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
name = labels_label,
out = "_{}.labels.txt".format(name),
content = ["{}={}".format(key, value) for (key, value) in labels.items()],
tags = tags,
**forwarded_kwargs,
)
labels = labels_label

Expand All @@ -71,7 +74,7 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
name = env_label,
out = "_{}.env.txt".format(name),
content = ["{}={}".format(key, value) for (key, value) in env.items()],
tags = tags,
**forwarded_kwargs,
)
env = env_label

Expand All @@ -81,7 +84,7 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
name = env_label,
out = "_{}.env.txt".format(name),
content = ["{}={}".format(key, value) for (key, value) in env.items()],
tags = tags,
**forwarded_kwargs,
)
env = env_label

Expand All @@ -91,7 +94,7 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
name = cmd_label,
out = "_{}.cmd.txt".format(name),
content = [",".join(cmd)],
tags = tags,
**forwarded_kwargs,
)
cmd = cmd_label

Expand All @@ -101,7 +104,7 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
name = entrypoint_label,
out = "_{}.entrypoint.txt".format(name),
content = [",".join(entrypoint)],
tags = tags,
**forwarded_kwargs,
)
entrypoint = entrypoint_label

Expand All @@ -112,7 +115,6 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
env = env,
cmd = cmd,
entrypoint = entrypoint,
tags = tags,
**kwargs
)

Expand All @@ -122,14 +124,14 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
name = "_{}_index_json".format(name),
directory = name,
path = "index.json",
tags = tags,
**forwarded_kwargs,
)

copy_file(
name = "_{}_index_json_cp".format(name),
src = "_{}_index_json".format(name),
out = "_{}_index.json".format(name),
tags = tags,
**forwarded_kwargs,
)

# Matches the [name].digest target produced by rules_docker container_image
Expand All @@ -139,10 +141,10 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
srcs = ["_{}_index.json".format(name)],
filter = """.manifests[0].digest""",
out = name + ".json.sha256", # path chosen to match rules_docker for easy migration
tags = tags,
**forwarded_kwargs,
)

def oci_push(name, remote_tags = None, tags = [], **kwargs):
def oci_push(name, remote_tags = None, **kwargs):
"""Macro wrapper around [oci_push_rule](#oci_push_rule).
Allows the remote_tags attribute to be a list of strings in addition to a text file.
Expand All @@ -153,27 +155,28 @@ def oci_push(name, remote_tags = None, tags = [], **kwargs):
or a label of a file containing tags one-per-line.
See [stamped_tags](https://github.com/bazel-contrib/rules_oci/blob/main/examples/push/stamp_tags.bzl)
as one example of a way to produce such a file.
tags: Tags to propagate to targets declared by this macro.
**kwargs: other named arguments to [oci_push_rule](#oci_push_rule).
**kwargs: other named arguments to [oci_push_rule](#oci_push_rule) and
[common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes).
"""
forwarded_kwargs = propagate_common_rule_attributes(kwargs)

if types.is_list(remote_tags):
tags_label = "_{}_write_tags".format(name)
write_file(
name = tags_label,
out = "_{}.tags.txt".format(name),
content = remote_tags,
tags = tags,
**forwarded_kwargs,
)
remote_tags = tags_label

oci_push_rule(
name = name,
remote_tags = remote_tags,
tags = tags,
**kwargs
)

def oci_tarball(name, repo_tags = None, tags = [], **kwargs):
def oci_tarball(name, repo_tags = None, **kwargs):
"""Macro wrapper around [oci_tarball_rule](#oci_tarball_rule).
Allows the repo_tags attribute to be a list of strings in addition to a text file.
Expand All @@ -184,22 +187,23 @@ def oci_tarball(name, repo_tags = None, tags = [], **kwargs):
or a label of a file containing tags one-per-line.
See [stamped_tags](https://github.com/bazel-contrib/rules_oci/blob/main/examples/push/stamp_tags.bzl)
as one example of a way to produce such a file.
tags: Tags to propagate to targets declared by this macro.
**kwargs: other named arguments to [oci_tarball_rule](#oci_tarball_rule).
**kwargs: other named arguments to [oci_tarball_rule](#oci_tarball_rule) and
[common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes).
"""
forwarded_kwargs = propagate_common_rule_attributes(kwargs)

if types.is_list(repo_tags):
tags_label = "_{}_write_tags".format(name)
write_file(
name = tags_label,
out = "_{}.tags.txt".format(name),
content = repo_tags,
tags = tags,
**forwarded_kwargs,
)
repo_tags = tags_label

oci_tarball_rule(
name = name,
repo_tags = repo_tags,
tags = tags,
**kwargs
)
6 changes: 3 additions & 3 deletions oci/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def rules_oci_dependencies():

http_archive(
name = "aspect_bazel_lib",
sha256 = "f1c181b910f821072f38ee45bb87db6b56275458c7f832c54c54ba6334119eca",
strip_prefix = "bazel-lib-1.32.0",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v1.32.0/bazel-lib-v1.32.0.tar.gz",
sha256 = "e9505bd956da64b576c433e4e41da76540fd8b889bbd17617fe480a646b1bfb9",
strip_prefix = "bazel-lib-1.35.0",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v1.35.0/bazel-lib-v1.35.0.tar.gz",
)

0 comments on commit f4031fb

Please sign in to comment.