Skip to content

Commit

Permalink
Merge pull request #2234 from sap-contributions/support-oci-mediatype
Browse files Browse the repository at this point in the history
Support OCI layouts with `application/vnd.oci.image.manifest.v1+json` media type
  • Loading branch information
natalieparellano authored Jul 29, 2024
2 parents c253591 + 0754b56 commit a651c5d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
8 changes: 4 additions & 4 deletions pkg/buildpack/oci_layout_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func IsOCILayoutBlob(blob blob2.Blob) (bool, error) {
}
defer readCloser.Close()

_, _, err = archive.ReadTarEntry(readCloser, "/oci-layout")
_, _, err = archive.ReadTarEntry(readCloser, v1.ImageLayoutFile)
if err != nil {
if archive.IsEntryNotExist(err) {
return false, nil
Expand Down Expand Up @@ -77,13 +77,13 @@ type ociLayoutPackage struct {
func newOCILayoutPackage(blob Blob, kind string) (*ociLayoutPackage, error) {
index := &v1.Index{}

if err := unmarshalJSONFromBlob(blob, "/index.json", index); err != nil {
if err := unmarshalJSONFromBlob(blob, v1.ImageIndexFile, index); err != nil {
return nil, err
}

var manifestDescriptor *v1.Descriptor
for _, m := range index.Manifests {
if m.MediaType == "application/vnd.docker.distribution.manifest.v2+json" {
if m.MediaType == "application/vnd.docker.distribution.manifest.v2+json" || m.MediaType == v1.MediaTypeImageManifest {
manifestDescriptor = &m // nolint:exportloopref
break
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func (o *ociLayoutPackage) GetLayer(diffID string) (io.ReadCloser, error) {
if paths.CanonicalTarPath(header.Name) == layerPath {
finalReader := blobReader

if strings.HasSuffix(layerDescriptor.MediaType, ".gzip") {
if strings.HasSuffix(layerDescriptor.MediaType, "gzip") {
finalReader, err = gzip.NewReader(tr)
if err != nil {
return nil, err
Expand Down
67 changes: 42 additions & 25 deletions pkg/buildpack/oci_layout_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/buildpacks/lifecycle/api"
"github.com/heroku/color"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

Expand All @@ -24,37 +25,53 @@ func TestOCILayoutPackage(t *testing.T) {
spec.Run(t, "Extract", testOCILayoutPackage, spec.Parallel(), spec.Report(report.Terminal{}))
}

type testCase struct {
mediatype string
file string
}

func testOCILayoutPackage(t *testing.T, when spec.G, it spec.S) {
when("#BuildpacksFromOCILayoutBlob", func() {
it("extracts buildpacks", func() {
mainBP, depBPs, err := buildpack.BuildpacksFromOCILayoutBlob(blob.NewBlob(filepath.Join("testdata", "hello-universe.cnb")))
h.AssertNil(t, err)

h.AssertEq(t, mainBP.Descriptor().Info().ID, "io.buildpacks.samples.hello-universe")
h.AssertEq(t, mainBP.Descriptor().Info().Version, "0.0.1")
h.AssertEq(t, len(depBPs), 2)
})
for _, test := range []testCase{
{
mediatype: "application/vnd.docker.distribution.manifest.v2+json",
file: "hello-universe.cnb",
},
{
mediatype: v1.MediaTypeImageManifest,
file: "hello-universe-oci.cnb",
},
} {
it(fmt.Sprintf("extracts buildpacks, media type: %s", test.mediatype), func() {
mainBP, depBPs, err := buildpack.BuildpacksFromOCILayoutBlob(blob.NewBlob(filepath.Join("testdata", test.file)))
h.AssertNil(t, err)

it("provides readable blobs", func() {
mainBP, depBPs, err := buildpack.BuildpacksFromOCILayoutBlob(blob.NewBlob(filepath.Join("testdata", "hello-universe.cnb")))
h.AssertNil(t, err)
h.AssertEq(t, mainBP.Descriptor().Info().ID, "io.buildpacks.samples.hello-universe")
h.AssertEq(t, mainBP.Descriptor().Info().Version, "0.0.1")
h.AssertEq(t, len(depBPs), 2)
})

for _, bp := range append([]buildpack.BuildModule{mainBP}, depBPs...) {
reader, err := bp.Open()
it(fmt.Sprintf("provides readable blobs, media type: %s", test.mediatype), func() {
mainBP, depBPs, err := buildpack.BuildpacksFromOCILayoutBlob(blob.NewBlob(filepath.Join("testdata", test.file)))
h.AssertNil(t, err)

_, contents, err := archive.ReadTarEntry(
reader,
fmt.Sprintf("/cnb/buildpacks/%s/%s/buildpack.toml",
bp.Descriptor().Info().ID,
bp.Descriptor().Info().Version,
),
)
h.AssertNil(t, err)
h.AssertContains(t, string(contents), bp.Descriptor().Info().ID)
h.AssertContains(t, string(contents), bp.Descriptor().Info().Version)
}
})
for _, bp := range append([]buildpack.BuildModule{mainBP}, depBPs...) {
reader, err := bp.Open()
h.AssertNil(t, err)

_, contents, err := archive.ReadTarEntry(
reader,
fmt.Sprintf("/cnb/buildpacks/%s/%s/buildpack.toml",
bp.Descriptor().Info().ID,
bp.Descriptor().Info().Version,
),
)
h.AssertNil(t, err)
h.AssertContains(t, string(contents), bp.Descriptor().Info().ID)
h.AssertContains(t, string(contents), bp.Descriptor().Info().Version)
}
})
}
})

when("#ExtensionsFromOCILayoutBlob", func() {
Expand Down
Binary file added pkg/buildpack/testdata/hello-universe-oci.cnb
Binary file not shown.

0 comments on commit a651c5d

Please sign in to comment.