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

Add docker image output field to support publish to repository when using BuildKit #20154

Merged
merged 6 commits into from
Nov 13, 2023

Conversation

riisi
Copy link
Contributor

@riisi riisi commented Nov 7, 2023

Currently, the publish goal doesn't work with docker images when buildkit is enabled, as by default buildkit doesn't save the build output locally, and publish expects that the images were saved.

This PR adds support for setting the output type, and defaults it todocker, which is the legacy docker build behavior, i.e. saves to the local image store.

However, we only want to set that when buildkit is enabled. I thought it better to add an explicit option for that at the subsystem level; this allows for validation of buildkit-only options.

This eliminates the need to set DOCKER_BUILDKIT=1 in env vars - I need to update the docs on that actually.

I have validated that with this change, docker images can be published to a registry.

@riisi riisi added the backend: Docker Docker backend-related issues label Nov 7, 2023
@riisi riisi requested a review from kaos November 7, 2023 19:04
Copy link
Member

@kaos kaos left a comment

Choose a reason for hiding this comment

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

Very nice. This will make it much easier to get into build kit mode :)

src/python/pants/backend/docker/goals/package_image.py Outdated Show resolved Hide resolved
src/python/pants/backend/docker/target_types.py Outdated Show resolved Hide resolved
src/python/pants/backend/docker/target_types.py Outdated Show resolved Hide resolved
Copy link
Member

@kaos kaos left a comment

Choose a reason for hiding this comment

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

We're getting closer :)

error_cls=DockerImageOptionValueError,
)
yield from target[field_type].options(format)
elif issubclass(field_type, DockerBuildKitOptionField):
Copy link
Member

Choose a reason for hiding this comment

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

sorry, but I have an itch to scratch here. I think I may have an idea how to further improve this logic slightly.

There are two things going on here now, one is validating option use and the other is to produce the option values themselves.

Looking at this elif chain, I fear it'll be too easy to add a new option that inherits from both DockerBuildOptionFieldMixin and DockerBuildKitOptionField and then you don't get the behaviour we want.

How about splitting this into two elif chains. The first for option validation (such as the use buildx toggle for buildkit options) and the other for producing the option values. That would also get rid of the duplication we have now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems a lot cleaner now - let me know

@@ -347,7 +350,7 @@ def get_build_options(
source=source,
error_cls=DockerImageOptionValueError,
)
yield from target[field_type].options(format)
yield from target[field_type].options(format, global_build_hosts_options=global_build_hosts_options) # type: ignore[attr-defined]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I assume this is a mypy bug but couldn't find any workaround. It seems to occur when there are more than 2 subclasses added to issubclass- e.g. splitting into separate if statements for each subclass doesn't trigger it.

Copy link
Member

Choose a reason for hiding this comment

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

interesting. we can go with this for now.. I'll have a look on my end, if I'll find anything I can follow up in a new PR in that case.

@@ -347,7 +350,7 @@ def get_build_options(
source=source,
error_cls=DockerImageOptionValueError,
)
yield from target[field_type].options(format)
yield from target[field_type].options(format, global_build_hosts_options=global_build_hosts_options) # type: ignore[attr-defined]
Copy link
Member

Choose a reason for hiding this comment

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

interesting. we can go with this for now.. I'll have a look on my end, if I'll find anything I can follow up in a new PR in that case.

@kaos kaos changed the title Add docker image output field to support publish to repository Add docker image output field to support publish to repository when using BuildKit Nov 13, 2023
@kaos kaos merged commit c621a71 into pantsbuild:main Nov 13, 2023
@huonw huonw added this to the 2.19.x milestone Nov 15, 2023
WorkerPants pushed a commit that referenced this pull request Nov 15, 2023
…sing BuildKit (#20154)

Currently, the `publish` goal doesn't work with docker images when
buildkit is enabled, as by [default buildkit doesn't save the build
output locally](docker/buildx#166), and
`publish` expects that the images were saved.

This PR adds support for setting the output type, and defaults it
to`docker`, which is the legacy docker build behavior, i.e. saves to the
local image store.

However, we only want to set that when buildkit is enabled. I thought it
better to add an explicit option for that at the subsystem level; this
allows for validation of buildkit-only options.

This eliminates the need to set `DOCKER_BUILDKIT=1` in env vars - I need
to update the docs on that actually.

I have validated that with this change, docker images can be published
to a registry.

---------

Co-authored-by: Rhys Madigan <rhys.madigan@accenture.com>
@WorkerPants
Copy link
Member

I tried to automatically cherry-pick this change back to each relevant milestone, so that it is available in those older releases of Pants.

✔️ 2.19.x

Successfully opened #20185.


Thanks again for your contributions!

🤖 Beep Boop here's my run link

@WorkerPants
Copy link
Member

I tried to automatically cherry-pick this change back to each relevant milestone, so that it is available in those older releases of Pants.

❌ 2.18.x

I was unable to cherry-pick this PR to 2.18.x, likely due to merge-conflicts.

Steps to Cherry-Pick locally

To resolve:

  1. (Ensure your git working directory is clean)
  2. Run the following script to reproduce the merge-conflicts:
    git fetch https://github.com/pantsbuild/pants main \
      && git fetch https://github.com/pantsbuild/pants 2.18.x \
      && git checkout -b cherry-pick-20154-to-2.18.x FETCH_HEAD \
      && git cherry-pick c621a7145ad88543ab61dc3336b91a05f5ec86cf
  3. Fix the merge conflicts and commit the changes
  4. Run build-support/cherry_pick/make_pr.sh "20154" "2.18.x"

Please note that I cannot re-run CI if a job fails. Please work with your PR approver(s) to re-run CI if necessary.

✔️ 2.19.x

Successfully opened #20185.


When you're done manually cherry-picking, please remove the needs-cherrypick label on this PR.

Thanks again for your contributions!

🤖 Beep Boop here's my run link

@WorkerPants WorkerPants added the auto-cherry-picking-failed Auto Cherry-Picking Failed label Nov 15, 2023
kaos pushed a commit that referenced this pull request Nov 15, 2023
…sing BuildKit (Cherry-pick of #20154) (#20185)

Currently, the `publish` goal doesn't work with docker images when
buildkit is enabled, as by [default buildkit doesn't save the build
output locally](docker/buildx#166), and
`publish` expects that the images were saved.

This PR adds support for setting the output type, and defaults it
to`docker`, which is the legacy docker build behavior, i.e. saves to the
local image store.

However, we only want to set that when buildkit is enabled. I thought it
better to add an explicit option for that at the subsystem level; this
allows for validation of buildkit-only options.

This eliminates the need to set `DOCKER_BUILDKIT=1` in env vars - I need
to update the docs on that actually.

I have validated that with this change, docker images can be published
to a registry.

Co-authored-by: riisi <rhysmadigan@gmail.com>
Co-authored-by: Rhys Madigan <rhys.madigan@accenture.com>
@kaos kaos modified the milestones: 2.18.x, 2.19.x Nov 15, 2023
@kaos kaos removed the auto-cherry-picking-failed Auto Cherry-Picking Failed label Nov 15, 2023
@kaos
Copy link
Member

kaos commented Nov 15, 2023

@huonw apologies, my bad I got this feature mixed up with something else, so we only need it picked to 2.19.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend: Docker Docker backend-related issues category:new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants