Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[layer_tools/generate_args_for_image] Inject extra argument to the given callback #2088

Merged
merged 1 commit into from
Jun 3, 2022
Merged

[layer_tools/generate_args_for_image] Inject extra argument to the given callback #2088

merged 1 commit into from
Jun 3, 2022

Conversation

ghost
Copy link

@ghost ghost commented May 14, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe: Just improve the flexibility of a starlark function.

What is the current behavior?

generate_args_for_image(ctx, image, to_path = _file_path)

Issue Number: N/A

What is the new behavior?

generate_args_for_image(ctx, image, to_path = _file_path, **kwargs)

When using a transition we need to give a callback function to compute a path specific for this transition.
This information is not available from the context (ctx) and the given file.
We need to have a way to inject this extra information.

Example with an architecture specific transition:

load("@io_bazel_rules_docker//container:layer_tools.bzl", "generate_args_for_image")

def _to_arch_path(ctx, f, arch = None):
    """Returns the architecture specific path of the given file f."""
    return paths.join(
        paths.dirname(f.short_path),
        arch,
        paths.basename(f.short_path),
    )

def _xxx_impl(ctx):

    ...

    # Here `image` is an attribute with a 1:2+ transition
    # All of their files share the same `short_path`.
    for image_target in ctx.attr.image:
        image = ... [ImageInfo].container_parts
        arch = ...

        ...

        img_args, img_inputs = generate_args_for_image(
            ctx, image, _to_arch_path, arch = arch,
        )

        ...

        # Use symlinks in `runfiles()` to make available all files through their arch specific path.
        symlinks.update({
            _to_arch_path(ctx, i, arch = arch): i
            for i in img_inputs
        })

    ...

    runfiles = ctx.runfiles(
        files = ...
        symlinks = symlinks
    )

Does this PR introduce a breaking change?

  • Yes
  • No

…ven callback

When using a transition we need to give a callback function
to compute a path specific for this transition.
This information is not available from the context (ctx)
and the given file.
We need to have a way to inject this extra information.

Example with an architecture specific transition:

```starlark
def _to_arch_path(ctx, f, arch = None):
    """Returns the architecture specific path of the given file f."""
    return paths.join(
        paths.dirname(f.short_path),
        arch,
        paths.basename(f.short_path),
    )

def _xxx_impl(ctx):

    ...

    # image is an attribute with a 1:2+ transition
    for image_target in ctx.attr.image:
        image = ... [ImageInfo].container_parts
        arch = ...

        ...

        img_args, img_inputs = generate_args_for_image(
            ctx, image, _to_arch_path, arch = arch,
        )

        ...

        symlinks.update({
            _to_arch_path(ctx, i, arch = arch): i
            for i in img_inputs
        })

    ...

    runfiles = ctx.runfiles(
        files = ...
        symlinks = symlinks
    )
```
@ghost ghost requested review from gravypod, linzhp, pcj, smukherj1 and uhthomas as code owners May 14, 2022 16:30
Copy link
Collaborator

@gravypod gravypod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gravypod gravypod merged commit 078bdd8 into bazelbuild:master Jun 3, 2022
St0rmingBr4in pushed a commit to St0rmingBr4in/rules_docker that referenced this pull request Oct 17, 2022
…ven callback (bazelbuild#2088)

When using a transition we need to give a callback function
to compute a path specific for this transition.
This information is not available from the context (ctx)
and the given file.
We need to have a way to inject this extra information.

Example with an architecture specific transition:

```starlark
def _to_arch_path(ctx, f, arch = None):
    """Returns the architecture specific path of the given file f."""
    return paths.join(
        paths.dirname(f.short_path),
        arch,
        paths.basename(f.short_path),
    )

def _xxx_impl(ctx):

    ...

    # image is an attribute with a 1:2+ transition
    for image_target in ctx.attr.image:
        image = ... [ImageInfo].container_parts
        arch = ...

        ...

        img_args, img_inputs = generate_args_for_image(
            ctx, image, _to_arch_path, arch = arch,
        )

        ...

        symlinks.update({
            _to_arch_path(ctx, i, arch = arch): i
            for i in img_inputs
        })

    ...

    runfiles = ctx.runfiles(
        files = ...
        symlinks = symlinks
    )
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants