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

Error if image platform does not match desired #785

Merged
merged 1 commit into from
Aug 19, 2022

Conversation

benmoss
Copy link
Contributor

@benmoss benmoss commented Aug 4, 2022

Fixes #699

If the user specifies an image manifest and its platform does not match the platforms specified or defaulted, error out.

With a .ko.yaml specifying the linux/amd64 image manifest instead of the multi-platform index:

baseImageOverrides:
  github.com/google/ko: golang@sha256:a4081692fa3015104d84b125cbf623c566a0c9e2a39b9a29f6d939225d5687a4

Prior behavior

❯ go run main.go build --platform=linux/arm64 .
2022/08/04 10:36:41 No matching credentials were found, falling back on anonymous
2022/08/04 10:36:42 Using base golang@sha256:a4081692fa3015104d84b125cbf623c566a0c9e2a39b9a29f6d939225d5687a4 for github.com/google/ko
2022/08/04 10:36:42 Building github.com/google/ko for linux/amd64
...

New behavior

❯ go run main.go build --platform=linux/arm64 .
2022/08/04 10:43:52 No matching credentials were found, falling back on anonymous
2022/08/04 10:43:53 Using base golang@sha256:a4081692fa3015104d84b125cbf623c566a0c9e2a39b9a29f6d939225d5687a4 for github.com/google/ko
Error: failed to publish images: error building "ko://github.com/google/ko": base image platform "linux/amd64" does not match desired platforms [linux/arm64]
2022/08/04 10:43:53 error during command execution:failed to publish images: error building "ko://github.com/google/ko": base image platform "linux/amd64" does not match desired platforms [linux/arm64]
exit status 1

As mentioned in #699 --platform=all is a lil ambiguous so we're keeping that behavior:

❯ go run main.go build --platform=all .
2022/08/04 10:45:40 No matching credentials were found, falling back on anonymous
2022/08/04 10:45:41 Using base golang@sha256:a4081692fa3015104d84b125cbf623c566a0c9e2a39b9a29f6d939225d5687a4 for github.com/google/ko
2022/08/04 10:45:41 Building github.com/google/ko for linux/amd64
...

If the user specifies an image manifest and its platform does not match
the platforms specified or defaulted, error out.

Signed-off-by: Ben Moss <[email protected]>
@benmoss benmoss force-pushed the error-on-platform-mismatch branch from a5ed557 to 8211102 Compare August 4, 2022 15:06
Copy link
Member

@imjasonh imjasonh left a comment

Choose a reason for hiding this comment

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

This mostly looks good, thanks for the contribution!

If there was an additional test in the e2e tests that checked that ko build --platform=linux/fake resulted in an error, I'd award extra bonus points 😁🏅

@@ -437,6 +437,7 @@ func TestGoBuildNoKoData(t *testing.T) {
WithBaseImages(func(context.Context, string) (name.Reference, Result, error) { return baseRef, base, nil }),
withBuilder(writeTempFile),
withSBOMber(fauxSBOM),
WithPlatforms("all"),
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused a bit why this addition is needed for tests. I would have expected random.Image to produce a linux/amd64 image by default, and for NewGo to assume that platform if none is provided, so that platform matcher check you added wouldn't be necessary.

Does this change require callers of the Go API to explicitly pass WithPlatforms?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

random.Image has no platform (or any config at all), so you get this somewhat odd error

gobuild_test.go:448: Build() = base image platform "" does not match desired platforms []

It looks like from empty.Image it just gets an empty Platform struct:

*v1.Platform{Architecture: "", OS: "", OSVersion: "", OSFeatures: []string len: 0, cap: 0, nil, Variant: "", Features: []string len: 0, cap: 0, nil}

The CLI calls gobuildOptions which handles the defaulting of desired platforms.

My understanding from reading over the code is that the Go API (NewGo) doesn't do any defaulting, so if the user didn't set any desired platforms the current behavior would be:

The tests were relying on that latter behavior, so the resulting image would also have a blank platform.

@benmoss
Copy link
Contributor Author

benmoss commented Aug 19, 2022

If there was an additional test in the e2e tests that checked that ko build --platform=linux/fake resulted in an error, I'd award extra bonus points 😁🏅

We'd have to set KO_DEFAULTBASEIMAGE as well, since it defaults to an image index (go:1.18), and that already handles erroring for unsupported platforms. This is behavior that only triggers when you use image manifests as your base image.

@imjasonh imjasonh merged commit cbe2784 into ko-build:main Aug 19, 2022
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.

--platform can be ignored by what the base image supports
2 participants