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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,9 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
}
}

if !g.platformMatcher.matches(platform) {
return nil, fmt.Errorf("base image platform %q does not match desired platforms %v", platform, g.platformMatcher.platforms)
}
// Do the build into a temporary file.
file, err := g.build(ctx, ref.Path(), g.dir, *platform, g.configForImportPath(ref.Path()))
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

)
if err != nil {
t.Fatalf("NewGo() = %v", err)
Expand Down Expand Up @@ -719,6 +720,7 @@ func TestGoBuild(t *testing.T) {
withSBOMber(fauxSBOM),
WithLabel("foo", "bar"),
WithLabel("hello", "world"),
WithPlatforms("all"),
)
if err != nil {
t.Fatalf("NewGo() = %v", err)
Expand Down Expand Up @@ -794,6 +796,7 @@ func TestGoBuildWithKOCACHE(t *testing.T) {
"",
WithCreationTime(creationTime),
WithBaseImages(func(context.Context, string) (name.Reference, Result, error) { return baseRef, base, nil }),
WithPlatforms("all"),
)
if err != nil {
t.Fatalf("NewGo() = %v", err)
Expand Down Expand Up @@ -831,6 +834,7 @@ func TestGoBuildWithoutSBOM(t *testing.T) {
WithLabel("foo", "bar"),
WithLabel("hello", "world"),
WithDisabledSBOM(),
WithPlatforms("all"),
)
if err != nil {
t.Fatalf("NewGo() = %v", err)
Expand Down Expand Up @@ -1183,6 +1187,7 @@ func TestGoBuildConsistentMediaTypes(t *testing.T) {
WithBaseImages(func(context.Context, string) (name.Reference, Result, error) { return baseRef, base, nil }),
withBuilder(writeTempFile),
withSBOMber(fauxSBOM),
WithPlatforms("all"),
)
if err != nil {
t.Fatalf("NewGo() = %v", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/build/gobuilds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Test_gobuilds(t *testing.T) {
opts := []Option{
WithBaseImages(func(context.Context, string) (name.Reference, Result, error) { return baseRef, base, nil }),
withBuilder(writeTempFile),
WithPlatforms("all"),
}

tests := []struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestPublishImages(t *testing.T) {
bo := &options.BuildOptions{
BaseImage: baseImage,
ConcurrentBuilds: 1,
Platforms: []string{"all"},
}
builder, err := NewBuilder(ctx, bo)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func TestNewBuilder(t *testing.T) {
bo: &options.BuildOptions{
BaseImage: baseImage,
ConcurrentBuilds: 1,
Platforms: []string{"all"},
},
wantQualifiedImportpath: "ko://github.com/google/ko/test",
shouldBuildError: false,
Expand Down