-
Notifications
You must be signed in to change notification settings - Fork 602
Implement OCI Image Layout support #261
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e1bbac5
Add pkg/v1/layout
jonjohnsonjr 9a1b0fa
Add demo tool: compass
jonjohnsonjr 9b886d0
Add LayoutOptions
jonjohnsonjr 43f1066
Start writing tests
jonjohnsonjr 1c9e063
Make Write a LayoutPath constructor function
glyn d70b16f
Turn Index into a LayoutPath constructor function named Read
glyn 69456bc
Convert functions to LayoutPath methods
glyn 7e0abea
Improve encapsulation of layout package
glyn 2b5ee23
Address review comments
glyn a66fbb0
Add Blob method to ImageIndex
jonjohnsonjr 4e5f1a3
Test dedupe blob writes
jonjohnsonjr a743b13
Remove Blob from ImageIndex interface
jonjohnsonjr 0fb28bb
Remove compass demo
jonjohnsonjr 9edc601
ImageIndex -> ImageIndexFromPath
jonjohnsonjr d186043
Fix golint issues
jonjohnsonjr 177baa5
Rename layout.Read -> layout.FromPath
jonjohnsonjr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.