Skip to content

Commit

Permalink
Error if image platform does not match desired
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Ben Moss committed Aug 4, 2022
1 parent ad00979 commit a5ed557
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
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"),
)
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

0 comments on commit a5ed557

Please sign in to comment.