From 8211102f214d6f024797db637bdeead2e871aa28 Mon Sep 17 00:00:00 2001 From: Ben Moss Date: Thu, 4 Aug 2022 10:22:09 -0400 Subject: [PATCH] Error if image platform does not match desired 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 --- pkg/build/gobuild.go | 3 +++ pkg/build/gobuild_test.go | 5 +++++ pkg/build/gobuilds_test.go | 1 + pkg/commands/publisher_test.go | 1 + pkg/commands/resolver_test.go | 1 + 5 files changed, 11 insertions(+) diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index c140052ba4..5e34692b98 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -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 { diff --git a/pkg/build/gobuild_test.go b/pkg/build/gobuild_test.go index ccba6845cd..a23d26c116 100644 --- a/pkg/build/gobuild_test.go +++ b/pkg/build/gobuild_test.go @@ -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"), ) if err != nil { t.Fatalf("NewGo() = %v", err) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/pkg/build/gobuilds_test.go b/pkg/build/gobuilds_test.go index c601974a94..31f021b372 100644 --- a/pkg/build/gobuilds_test.go +++ b/pkg/build/gobuilds_test.go @@ -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 { diff --git a/pkg/commands/publisher_test.go b/pkg/commands/publisher_test.go index b0dd603373..7ff491f867 100644 --- a/pkg/commands/publisher_test.go +++ b/pkg/commands/publisher_test.go @@ -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 { diff --git a/pkg/commands/resolver_test.go b/pkg/commands/resolver_test.go index 49906386e0..21d2d5d610 100644 --- a/pkg/commands/resolver_test.go +++ b/pkg/commands/resolver_test.go @@ -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,