Skip to content
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
38 changes: 38 additions & 0 deletions pkg/v1/layout/blob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2018 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package layout

import (
"io"
"io/ioutil"
"os"

"github.com/google/go-containerregistry/pkg/v1"
)

// Blob returns a blob with the given hash from the Path.
func (l Path) Blob(h v1.Hash) (io.ReadCloser, error) {
return os.Open(l.blobPath(h))
}

// Bytes is a convenience function to return a blob from the Path as
// a byte slice.
func (l Path) Bytes(h v1.Hash) ([]byte, error) {
return ioutil.ReadFile(l.blobPath(h))
}

func (l Path) blobPath(h v1.Hash) string {
return l.path("blobs", h.Algorithm, h.Hex)
}
19 changes: 19 additions & 0 deletions pkg/v1/layout/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package layout provides facilities for reading/writing artifacts from/to
// an OCI image layout on disk, see:
//
// https://github.com/opencontainers/image-spec/blob/master/image-layout.md
package layout
127 changes: 127 additions & 0 deletions pkg/v1/layout/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2018 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package layout

import (
"fmt"
"io"
"sync"

"github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/partial"
"github.com/google/go-containerregistry/pkg/v1/types"
)

type layoutImage struct {
path Path
desc v1.Descriptor
manifestLock sync.Mutex // Protects rawManifest
rawManifest []byte
}

var _ partial.CompressedImageCore = (*layoutImage)(nil)

// Image reads a v1.Image with digest h from the Path.
func (l Path) Image(h v1.Hash) (v1.Image, error) {
ii, err := l.ImageIndex()
if err != nil {
return nil, err
}

return ii.Image(h)
}

func (li *layoutImage) MediaType() (types.MediaType, error) {
return li.desc.MediaType, nil
}

// Implements WithManifest for partial.Blobset.
func (li *layoutImage) Manifest() (*v1.Manifest, error) {
return partial.Manifest(li)
}

func (li *layoutImage) RawManifest() ([]byte, error) {
li.manifestLock.Lock()
defer li.manifestLock.Unlock()
if li.rawManifest != nil {
return li.rawManifest, nil
}

b, err := li.path.Bytes(li.desc.Digest)
if err != nil {
return nil, err
}

li.rawManifest = b
return li.rawManifest, nil
}

func (li *layoutImage) RawConfigFile() ([]byte, error) {
manifest, err := li.Manifest()
if err != nil {
return nil, err
}

return li.path.Bytes(manifest.Config.Digest)
}

func (li *layoutImage) LayerByDigest(h v1.Hash) (partial.CompressedLayer, error) {
manifest, err := li.Manifest()
if err != nil {
return nil, err
}

if h == manifest.Config.Digest {
return partial.CompressedLayer(&compressedBlob{
path: li.path,
desc: manifest.Config,
}), nil
}

for _, desc := range manifest.Layers {
if h == desc.Digest {
switch desc.MediaType {
case types.OCILayer, types.DockerLayer:
return partial.CompressedToLayer(&compressedBlob{
path: li.path,
desc: desc,
})
default:
// TODO: We assume everything is a compressed blob, but that might not be true.
// TODO: Handle foreign layers.
return nil, fmt.Errorf("unexpected media type: %v for layer: %v", desc.MediaType, desc.Digest)
}
}
}

return nil, fmt.Errorf("could not find layer in image: %s", h)
}

type compressedBlob struct {
path Path
desc v1.Descriptor
}

func (b *compressedBlob) Digest() (v1.Hash, error) {
return b.desc.Digest, nil
}

func (b *compressedBlob) Compressed() (io.ReadCloser, error) {
return b.path.Blob(b.desc.Digest)
}

func (b *compressedBlob) Size() (int64, error) {
return b.desc.Size, nil
}
108 changes: 108 additions & 0 deletions pkg/v1/layout/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2018 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package layout

import (
"testing"

"github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/google/go-containerregistry/pkg/v1/validate"
)

var (
indexDigest = v1.Hash{
Algorithm: "sha256",
Hex: "05f95b26ed10668b7183c1e2da98610e91372fa9f510046d4ce5812addad86b5",
}
manifestDigest = v1.Hash{
Algorithm: "sha256",
Hex: "eebff607b1628d67459b0596643fc07de70d702eccf030f0bc7bb6fc2b278650",
}
configDigest = v1.Hash{
Algorithm: "sha256",
Hex: "6e0b05049ed9c17d02e1a55e80d6599dbfcce7f4f4b022e3c673e685789c470e",
}
bogusDigest = v1.Hash{
Algorithm: "sha256",
Hex: "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
}
bogusPath = "testdata/does_not_exist"
testPath = "testdata/test_index"
)

func TestImage(t *testing.T) {
lp, err := FromPath(testPath)
if err != nil {
t.Fatalf("FromPath() = %v", err)
}
img, err := lp.Image(manifestDigest)
if err != nil {
t.Fatalf("Image() = %v", err)
}

if err := validate.Image(img); err != nil {
t.Errorf("validate.Image() = %v", err)
}

mt, err := img.MediaType()
if err != nil {
t.Errorf("MediaType() = %v", err)
} else if got, want := mt, types.OCIManifestSchema1; got != want {
t.Errorf("MediaType(); want: %v got: %v", want, got)
}

cfg, err := img.LayerByDigest(configDigest)
if err != nil {
t.Fatalf("LayerByDigest(%s) = %v", configDigest, err)
}

cfgName, err := img.ConfigName()
if err != nil {
t.Fatalf("ConfigName() = %v", err)
}

cfgDigest, err := cfg.Digest()
if err != nil {
t.Fatalf("cfg.Digest() = %v", err)
}

if got, want := cfgDigest, cfgName; got != want {
t.Errorf("ConfigName(); want: %v got: %v", want, got)
}
}

func TestImageErrors(t *testing.T) {
lp, err := FromPath(testPath)
if err != nil {
t.Fatalf("FromPath() = %v", err)
}
img, err := lp.Image(manifestDigest)
if err != nil {
t.Fatalf("Image() = %v", err)
}

if _, err := img.LayerByDigest(bogusDigest); err == nil {
t.Errorf("LayerByDigest(%s) = nil, expected err", bogusDigest)
}

if _, err := lp.Image(bogusDigest); err == nil {
t.Errorf("Image(%s) = nil, expected err", bogusDigest)
}

if _, err := lp.Image(bogusDigest); err == nil {
t.Errorf("Image(%s, %s) = nil, expected err", bogusPath, bogusDigest)
}
}
Loading