Skip to content

Conversation

@flouthoc
Copy link
Contributor

@flouthoc flouthoc commented Jan 31, 2022

Allows imagebuilder to read --platform if specified in FROM and
relay it to implementations using stage.Builder.Platform so implementation can
actually use platform while pulling the image.

Example Demonstration/Implementation here: containers/buildah#3757
Reference: https://docs.docker.com/engine/reference/builder/#from

@flouthoc
Copy link
Contributor Author

flouthoc commented Jan 31, 2022

@nalind PTAL. This is the approach which i am thinking about. Following approach delegates whole Platform string to implementation since they are responsible for deciding about how are they going to use it.

Also platform string is unqiue for stages so each stage can have its own platform example

FROM --platform=linux/amd64 alpine
RUN uname -m

FROM --platform=linux/arm64 alpine
RUN uname -m

@TomSweeneyRedHat
Copy link
Contributor

@flouthoc, does Docker allow for different platforms per FROM statement? The documentation you provided wasn't clear about that.

@TomSweeneyRedHat
Copy link
Contributor

Would be good to have some tests added. I think, but I'm not sure that this handles an "AS" clause. Have you confirmed?

@flouthoc
Copy link
Contributor Author

@flouthoc, does Docker allow for different platforms per FROM statement? The documentation you provided wasn't clear about that.

@TomSweeneyRedHat Yes I tried it last time with buildkit it supports that but only makes lot of sense when user has a qemu-user-static and bindfmt_misc setup otherwise it would not make lot of sense to user.

But irrespective of the even legacy docker build supports --platform with FROM statements.

Would be good to have some tests added. I think, but I'm not sure that this handles an "AS" clause. Have you confirmed?

Yes AS clause is still handled it is exclusive of --platform and part of generic FROM. I'll add a test and implementation to docker client once everybody agrees on this approach.

@nalind
Copy link
Member

nalind commented Jan 31, 2022

How complete is this intended to be? This needs to update the various TARGET... built-in args introduced in #142 and sort out how we want to deal with "ARG TARGETPLATFORM ...", which overlaps with it. The dockerclient package is using a version of go-dockerclient that can accept a platform value when it's pulling an image, so this PR may want to wire that up as well.

@flouthoc
Copy link
Contributor Author

@nalind hmm i'm still thinking if we should override the variables I'll check with buildkit once if they are doing it then we can do the same ? But go-dockerclient for sure I'll wire it up. Just wanted to make sure if approach seemed right.

@flouthoc
Copy link
Contributor Author

@nalind I think --platform does not changes or alters any of the TARGETPLATFORM or BUILDPLATFORM. I tried this using buildkit

FROM --platform=linux/arm64 alpine
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM"

still shows I am running on linux/amd64, building for linux/amd64 even though my --platform was linux/arm64 but actual image pulled was for linux/arm64.

Command sudo docker buildx build -t test .

--platform can use TARGETPLATFORM or BUILDPLATFORM but cannot alter them.

@nalind
Copy link
Member

nalind commented Jan 31, 2022

@nalind I think --platform does not changes or alters any of the TARGETPLATFORM or BUILDPLATFORM. I tried this using buildkit

FROM --platform=linux/arm64 alpine
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM"

still shows I am running on linux/amd64, building for linux/amd64 even though my --platform was linux/arm64 but actual image pulled was for linux/arm64.

Command sudo docker buildx build -t test .

--platform can use TARGETPLATFORM or BUILDPLATFORM but cannot alter them.

Apparently docker build doesn't set the TARGETPLATFORM args based on FROM --platform, but does set it to match a --platform flag passed on the command line, and there's no correspondence enforced between the two. The output image is marked as being for TARGETPLATFORM. That suggests that TARGETPLATFORM is more about supporting cross-compiling, and FROM --platform isn't really tied to it at all. So I guess we don't have to worry about setting TARGETPLATFORM based on FROM after all.

@flouthoc
Copy link
Contributor Author

@nalind I think --platform does not changes or alters any of the TARGETPLATFORM or BUILDPLATFORM. I tried this using buildkit

FROM --platform=linux/arm64 alpine
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM"

still shows I am running on linux/amd64, building for linux/amd64 even though my --platform was linux/arm64 but actual image pulled was for linux/arm64.
Command sudo docker buildx build -t test .
--platform can use TARGETPLATFORM or BUILDPLATFORM but cannot alter them.

Apparently docker build doesn't set the TARGETPLATFORM args based on FROM --platform, but does set it to match a --platform flag passed on the command line, and there's no correspondence enforced between the two. The output image is marked as being for TARGETPLATFORM. That suggests that TARGETPLATFORM is more about supporting cross-compiling, and FROM --platform isn't really tied to it at all. So I guess we don't have to worry about setting TARGETPLATFORM based on FROM after all.

@nalind Cool then i guess just instrumenting docker-client is enough right, i'll also add a test.

@flouthoc
Copy link
Contributor Author

flouthoc commented Feb 1, 2022

/assign @nalind

Copy link
Member

@nalind nalind left a comment

Choose a reason for hiding this comment

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

A couple of formatting nits, otherwise LGTM.

builder.go Outdated

Warnings []string
// Raw platform string specified with `FROM --platform` of the stage
// Its upto the implementation or client to parse and use this field
Copy link
Member

Choose a reason for hiding this comment

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

Nit: typo "upto"

if mybuilder.Platform != expectedPlatform {
t.Errorf("Expected %v, to match %v\n", expectedPlatform, mybuilder.Platform)
}

Copy link
Member

Choose a reason for hiding this comment

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

Nit: extra blank line

…builder

Allows imagebuilder to read and `--platform` if specified in `FROM` and
relay it to implementations using `stage.Builder.Platform`

Signed-off-by: Aditya R <[email protected]>
Add support to dockerclient to actually use platform while pulling image
if specified in `FROM --platform=<>` before this commit platform was
`no-op` for image pulls.

Signed-off-by: Aditya R <[email protected]>
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Feb 2, 2022

@flouthoc: all tests passed!

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@flouthoc
Copy link
Contributor Author

flouthoc commented Feb 2, 2022

@rhatdan @nalind Fixed the nits. Does it look better now.

@rhatdan
Copy link
Contributor

rhatdan commented Feb 2, 2022

/approve
/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Feb 2, 2022
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Feb 2, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: flouthoc, rhatdan

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 2, 2022
@openshift-merge-robot openshift-merge-robot merged commit 28a05a4 into openshift:master Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants