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

Always produce OCI images and indexes #449

Merged
merged 2 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 13 additions & 15 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,9 @@ func (g *gobuild) configForImportPath(ip string) Config {
func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, platform *v1.Platform) (v1.Image, error) {
ref := newRef(refStr)

// Always produce OCI images, even if the base image isn't an OCI image.
base = mutate.MediaType(base, types.OCIManifestSchema1)

cf, err := base.ConfigFile()
if err != nil {
return nil, err
Expand Down Expand Up @@ -876,16 +879,15 @@ func (g *gobuild) Build(ctx context.Context, s string) (Result, error) {
}

// Annotate the image or index with base image information.
// (Docker manifest lists don't support annotations)
if mt != types.DockerManifestList {
anns := map[string]string{
specsv1.AnnotationBaseImageDigest: baseDigest.String(),
}
if _, ok := baseRef.(name.Tag); ok {
anns[specsv1.AnnotationBaseImageName] = baseRef.Name()
}
res = mutate.Annotations(res, anns).(Result)
// If the result is an Index, it should be an OCI index that supports
// annotations.
anns := map[string]string{
specsv1.AnnotationBaseImageDigest: baseDigest.String(),
}
if _, ok := baseRef.(name.Tag); ok {
anns[specsv1.AnnotationBaseImageName] = baseRef.Name()
}
res = mutate.Annotations(res, anns).(Result)

return res, nil
}
Expand Down Expand Up @@ -921,18 +923,14 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseIndex v1.ImageIn
Add: img,
Descriptor: v1.Descriptor{
URLs: desc.URLs,
MediaType: desc.MediaType,
MediaType: types.OCIManifestSchema1,
imjasonh marked this conversation as resolved.
Show resolved Hide resolved
Annotations: desc.Annotations,
Platform: desc.Platform,
},
})
}

baseType, err := baseIndex.MediaType()
if err != nil {
return nil, err
}
idx := mutate.IndexMediaType(mutate.AppendManifests(empty.Index, adds...), baseType)
idx := mutate.IndexMediaType(mutate.AppendManifests(empty.Index, adds...), types.OCIImageIndex)

return idx, nil
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/random"
"github.com/google/go-containerregistry/pkg/v1/types"
specsv1 "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand Down Expand Up @@ -418,6 +419,17 @@ func TestGoBuildNoKoData(t *testing.T) {
t.Errorf("created = %v, want %v", actual, creationTime)
}
})

t.Run("check OCI media type", func(t *testing.T) {
mt, err := img.MediaType()
if err != nil {
t.Errorf("MediaType() = %v", err)
}

if got, want := mt, types.OCIManifestSchema1; got != want {
t.Errorf("mediaType = %v, want %v", got, want)
}
})
}

func validateImage(t *testing.T, img v1.Image, baseLayers int64, creationTime v1.Time, checkAnnotations bool) {
Expand Down Expand Up @@ -729,6 +741,17 @@ func TestGoBuildIndex(t *testing.T) {
t.Errorf("Digest mismatch: %s != %s", d1, d2)
}
})

t.Run("check OCI media type", func(t *testing.T) {
mt, err := idx.MediaType()
if err != nil {
t.Errorf("MediaType() = %v", err)
}

if got, want := mt, types.OCIImageIndex; got != want {
t.Errorf("mediaType = %v, want %v", got, want)
}
})
}

func TestNestedIndex(t *testing.T) {
Expand Down