-
Notifications
You must be signed in to change notification settings - Fork 543
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
feat: mutate on oci-layout #1869
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package local | ||
Check failure on line 1 in pkg/crane/local/options.go GitHub Actions / Go headers[Go headers] pkg/crane/local/options.go#L1
Raw output
|
||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/google/go-containerregistry/pkg/v1/layout" | ||
) | ||
|
||
// Option is a functional option for remote operations. | ||
type Option func(*options) error | ||
|
||
type options struct { | ||
path *layout.Path | ||
} | ||
|
||
func makeOptions(opts ...Option) (*options, error) { | ||
o := &options{ | ||
path: nil, | ||
} | ||
|
||
for _, option := range opts { | ||
if err := option(o); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if o.path == nil { | ||
return nil, errors.New("provide an option for local storage") | ||
} | ||
|
||
return o, nil | ||
} | ||
|
||
func WithPath(p string) Option { | ||
return func(o *options) error { | ||
path, err := layout.FromPath(p) | ||
if err != nil { | ||
return err | ||
} | ||
o.path = &path | ||
return nil | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package local | ||
Check failure on line 1 in pkg/crane/local/read.go GitHub Actions / Go headers[Go headers] pkg/crane/local/read.go#L1
Raw output
|
||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
specsv1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
func refEqDescriptor(ref name.Reference, descriptor v1.Descriptor) bool { | ||
if _, ok := descriptor.Annotations[specsv1.AnnotationRefName]; ok { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func Image(ref name.Reference, options ...Option) (v1.Image, error) { | ||
o, err := makeOptions(options...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
desc, err := Head(ref, options...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return o.path.Image(desc.Digest) | ||
} | ||
|
||
// Pull returns a v1.Image of the remote image src. | ||
func Pull(src string, options ...Option) (v1.Image, error) { | ||
ref, err := name.ParseReference(src) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing reference %q: %w", src, err) | ||
} | ||
return Image(ref, options...) | ||
} | ||
|
||
// Head returns a v1.Descriptor for the given reference | ||
func Head(ref name.Reference, options ...Option) (*v1.Descriptor, error) { | ||
o, err := makeOptions(options...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
idx, err := o.path.ImageIndex() | ||
if err != nil { | ||
return nil, err | ||
} | ||
im, err := idx.IndexManifest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, manifest := range im.Manifests { | ||
if refEqDescriptor(ref, manifest) { | ||
return &manifest, nil | ||
} | ||
} | ||
return nil, errors.New("could not find the image in oci-layout") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package local | ||
Check failure on line 1 in pkg/crane/local/write.go GitHub Actions / Go headers[Go headers] pkg/crane/local/write.go#L1
Raw output
|
||
|
||
import ( | ||
"bytes" | ||
"io" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/layout" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
specsv1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
func Write(ref name.Reference, img v1.Image, options ...Option) (rerr error) { | ||
o, err := makeOptions(options...) | ||
if err != nil { | ||
return err | ||
} | ||
return o.path.AppendImage(img, layout.WithAnnotations(map[string]string{ | ||
specsv1.AnnotationRefName: ref.String(), | ||
})) | ||
} | ||
|
||
func WriteLayer(layer v1.Layer, options ...Option) (rerr error) { | ||
o, err := makeOptions(options...) | ||
if err != nil { | ||
return err | ||
} | ||
digest, err := layer.Digest() | ||
if err != nil { | ||
return err | ||
} | ||
rc, err := layer.Compressed() | ||
if err != nil { | ||
return err | ||
} | ||
return o.path.WriteBlob(digest, rc) | ||
} | ||
|
||
func Put(ref name.Reference, t remote.Taggable, options ...Option) error { | ||
o, err := makeOptions(options...) | ||
if err != nil { | ||
return err | ||
} | ||
rmf, err := t.RawManifest() | ||
if err != nil { | ||
return err | ||
} | ||
digest, _, err := v1.SHA256(bytes.NewReader(rmf)) | ||
if err != nil { | ||
return err | ||
} | ||
err = o.path.WriteBlob(digest, io.NopCloser(bytes.NewReader(rmf))) | ||
if err != nil { | ||
return err | ||
} | ||
mf, err := v1.ParseManifest(bytes.NewReader(rmf)) | ||
if err != nil { | ||
return err | ||
} | ||
return o.path.AppendDescriptor(v1.Descriptor{ | ||
Digest: digest, | ||
MediaType: mf.MediaType, | ||
Size: int64(len(rmf)), | ||
Annotations: map[string]string{ | ||
specsv1.AnnotationRefName: ref.String(), | ||
}, | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a big change, since it affects all commands. Would it be worth having some e2e tests that set up a local layout and
crane pull --local=<that>
,crane mutate
, etc.?