Skip to content

Commit 1296c6b

Browse files
authored
feat: add an owner arg to py_image_layer (#545)
I needed to be able to change the owner of some files in the final oci image. `mtree_mutate` seemed the right way to do it, but I couldn't control the mtree while using `py_image_layer` , so this adds an `owner` option to handle the `mtree_mutate` ### Changes are visible to end-users: yes - Searched for relevant documentation and updated as needed: yes - Breaking change (forces users to change their own code or config): no - Suggested release notes appear below: no ### Test plan <!-- Delete any which do not apply --> I tested by adding an `owner` attribute to an image I was generating. I did not update any tests, but if this change is approved, I can go back and add if we think its necessary.
1 parent 55fd502 commit 1296c6b

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

docs/py_image_layer.md

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

py/private/py_image_layer.bzl

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ oci_image(
3030
```
3131
"""
3232

33-
load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar")
33+
load("@aspect_bazel_lib//lib:tar.bzl", "mtree_mutate", "mtree_spec", "tar")
3434
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
3535

3636
default_layer_groups = {
@@ -90,7 +90,17 @@ awk < $< 'BEGIN {
9090
**kwargs
9191
)
9292

93-
def py_image_layer(name, binary, root = "/", layer_groups = {}, compress = "gzip", tar_args = [], compute_unused_inputs = 1, platform = None, **kwargs):
93+
def py_image_layer(
94+
name,
95+
binary,
96+
root = "/",
97+
layer_groups = {},
98+
compress = "gzip",
99+
tar_args = [],
100+
compute_unused_inputs = 1,
101+
platform = None,
102+
owner = None,
103+
**kwargs):
94104
"""Produce a separate tar output for each layer of a python app
95105
96106
> Requires `awk` to be installed on the host machine/rbe runner.
@@ -119,6 +129,7 @@ def py_image_layer(name, binary, root = "/", layer_groups = {}, compress = "gzip
119129
compress: Compression algorithm to use. Default is gzip. See: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/tar.md#tar_rule-compress
120130
compute_unused_inputs: Whether to compute unused inputs. Default is 1. See: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/tar.md#tar_rule-compute_unused_inputs
121131
platform: The platform to use for the transition. Default is None. See: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/transitions.md#platform_transition_binary-target_platform
132+
owner: An owner uid for the uncompressed files. See mtree_mutate: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/tar.md#mutating-the-tar-contents
122133
tar_args: Additional arguments to pass to the tar rule. Default is `[]`. See: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/tar.md#tar_rule-args
123134
**kwargs: attribute that apply to all targets expanded by the macro
124135
@@ -130,11 +141,24 @@ def py_image_layer(name, binary, root = "/", layer_groups = {}, compress = "gzip
130141

131142
# Produce the manifest for a tar file of our py_binary, but don't tar it up yet, so we can split
132143
# into fine-grained layers for better pull, push and remote cache performance.
133-
mtree_spec(
134-
name = name + ".manifest",
135-
srcs = [binary],
136-
**kwargs
137-
)
144+
manifest_name = name + ".manifest"
145+
if owner:
146+
mtree_spec(
147+
name = manifest_name + ".preprocessed",
148+
srcs = [binary],
149+
**kwargs
150+
)
151+
mtree_mutate(
152+
name = manifest_name,
153+
mtree = name + ".manifest.preprocessed",
154+
owner = owner,
155+
)
156+
else:
157+
mtree_spec(
158+
name = manifest_name,
159+
srcs = [binary],
160+
**kwargs
161+
)
138162

139163
groups = dict(**layer_groups)
140164
groups = dict(groups, **default_layer_groups)

0 commit comments

Comments
 (0)